1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2002 Josh Coalson
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #include "file_utils.h"
20 #include "FLAC/assert.h"
21 #include "FLAC/stream_encoder.h"
24 #if defined _MSC_VER || defined __MINGW32__
25 #include <io.h> /* for chmod(), unlink */
27 #include <sys/stat.h> /* for stat(), chmod() */
28 #if defined _WIN32 && !defined __CYGWIN__
30 #include <unistd.h> /* for unlink() */
36 #define min(a,b) ((a)<(b)?(a):(b))
40 } encoder_client_struct;
42 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const FLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
44 encoder_client_struct *ecd = (encoder_client_struct*)client_data;
46 (void)encoder, (void)samples, (void)current_frame;
48 if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
49 return FLAC__STREAM_ENCODER_WRITE_FATAL_ERROR;
51 return FLAC__STREAM_ENCODER_WRITE_OK;
54 static void encoder_metadata_callback_(const FLAC__StreamEncoder *encoder, const FLAC__StreamMetadata *metadata, void *client_data)
56 (void)encoder, (void)metadata, (void)client_data;
59 FLAC__bool file_utils__change_stats(const char *filename, FLAC__bool read_only)
63 if(0 == stat(filename, &stats)) {
64 #if !defined _MSC_VER && !defined __MINGW32__
66 stats.st_mode &= ~S_IWUSR;
67 stats.st_mode &= ~S_IWGRP;
68 stats.st_mode &= ~S_IWOTH;
71 stats.st_mode |= S_IWUSR;
72 stats.st_mode |= S_IWGRP;
73 stats.st_mode |= S_IWOTH;
77 stats.st_mode &= ~S_IWRITE;
79 stats.st_mode |= S_IWRITE;
81 if(0 != chmod(filename, stats.st_mode))
90 FLAC__bool file_utils__remove_file(const char *filename)
92 return file_utils__change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
95 FLAC__bool file_utils__generate_flacfile(const char *output_filename, unsigned *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
97 FLAC__int32 samples[1024];
98 FLAC__StreamEncoder *encoder;
99 encoder_client_struct encoder_client_data;
102 FLAC__ASSERT(0 != output_filename);
103 FLAC__ASSERT(0 != streaminfo);
104 FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
105 FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
107 if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
110 encoder = FLAC__stream_encoder_new();
112 fclose(encoder_client_data.file);
116 FLAC__stream_encoder_set_streamable_subset(encoder, true);
117 FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
118 FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
119 FLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
120 FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
121 FLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
122 FLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
123 FLAC__stream_encoder_set_max_lpc_order(encoder, 0);
124 FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
125 FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
126 FLAC__stream_encoder_set_do_escape_coding(encoder, false);
127 FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
128 FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
129 FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
130 FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
131 FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
132 FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
133 FLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
134 FLAC__stream_encoder_set_metadata_callback(encoder, encoder_metadata_callback_);
135 FLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
137 if(FLAC__stream_encoder_init(encoder) != FLAC__STREAM_ENCODER_OK) {
138 fclose(encoder_client_data.file);
142 /* init the dummy sample buffer */
143 for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
147 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
149 if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
150 fclose(encoder_client_data.file);
157 FLAC__stream_encoder_finish(encoder);
159 fclose(encoder_client_data.file);
161 FLAC__stream_encoder_delete(encoder);
163 if(0 != output_filesize) {
164 struct stat filestats;
166 if(stat(output_filename, &filestats) != 0)
169 *output_filesize = (unsigned)filestats.st_size;