work related to moving some file utils into the new file_utils convenience lib
[flac.git] / src / test_libOggFLAC / file_utils.c
1 /* test_libOggFLAC - Unit tester for libOggFLAC
2  * Copyright (C) 2002  Josh Coalson
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include "file_utils.h"
20 #include "FLAC/assert.h"
21 #include "OggFLAC/stream_encoder.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h> /* for stat() */
25
26 #ifdef min
27 #undef min
28 #endif
29 #define min(a,b) ((a)<(b)?(a):(b))
30
31 const long file_utils__serial_number = 12345;
32
33 typedef struct {
34         FILE *file;
35 } encoder_client_struct;
36
37 static FLAC__StreamEncoderWriteStatus encoder_write_callback_(const OggFLAC__StreamEncoder *encoder, const FLAC__byte buffer[], unsigned bytes, unsigned samples, unsigned current_frame, void *client_data)
38 {
39         encoder_client_struct *ecd = (encoder_client_struct*)client_data;
40
41         (void)encoder, (void)samples, (void)current_frame;
42
43         if(fwrite(buffer, 1, bytes, ecd->file) != bytes)
44                 return FLAC__STREAM_ENCODER_WRITE_STATUS_FATAL_ERROR;
45         else
46                 return FLAC__STREAM_ENCODER_WRITE_STATUS_OK;
47 }
48
49 FLAC__bool file_utils__generate_oggflacfile(const char *output_filename, unsigned *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata)
50 {
51         FLAC__int32 samples[1024];
52         OggFLAC__StreamEncoder *encoder;
53         encoder_client_struct encoder_client_data;
54         unsigned i, n;
55
56         FLAC__ASSERT(0 != output_filename);
57         FLAC__ASSERT(0 != streaminfo);
58         FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO);
59         FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0));
60
61         if(0 == (encoder_client_data.file = fopen(output_filename, "wb")))
62                 return false;
63
64         encoder = OggFLAC__stream_encoder_new();
65         if(0 == encoder) {
66                 fclose(encoder_client_data.file);
67                 return false;
68         }
69
70         OggFLAC__stream_encoder_set_serial_number(encoder, file_utils__serial_number);
71         OggFLAC__stream_encoder_set_verify(encoder, true);
72         OggFLAC__stream_encoder_set_streamable_subset(encoder, true);
73         OggFLAC__stream_encoder_set_do_mid_side_stereo(encoder, false);
74         OggFLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false);
75         OggFLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels);
76         OggFLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample);
77         OggFLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate);
78         OggFLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize);
79         OggFLAC__stream_encoder_set_max_lpc_order(encoder, 0);
80         OggFLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0);
81         OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false);
82         OggFLAC__stream_encoder_set_do_escape_coding(encoder, false);
83         OggFLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false);
84         OggFLAC__stream_encoder_set_min_residual_partition_order(encoder, 0);
85         OggFLAC__stream_encoder_set_max_residual_partition_order(encoder, 0);
86         OggFLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0);
87         OggFLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples);
88         OggFLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata);
89         OggFLAC__stream_encoder_set_write_callback(encoder, encoder_write_callback_);
90         OggFLAC__stream_encoder_set_client_data(encoder, &encoder_client_data);
91
92         if(OggFLAC__stream_encoder_init(encoder) != OggFLAC__STREAM_ENCODER_OK) {
93                 fclose(encoder_client_data.file);
94                 return false;
95         }
96
97         /* init the dummy sample buffer */
98         for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++)
99                 samples[i] = i & 7;
100
101         while(length > 0) {
102                 n = min(length, sizeof(samples) / sizeof(FLAC__int32));
103
104                 if(!OggFLAC__stream_encoder_process_interleaved(encoder, samples, n)) {
105                         fclose(encoder_client_data.file);
106                         return false;
107                 }
108
109                 length -= n;
110         }
111
112         OggFLAC__stream_encoder_finish(encoder);
113
114         fclose(encoder_client_data.file);
115
116         OggFLAC__stream_encoder_delete(encoder);
117
118         if(0 != output_filesize) {
119                 struct stat filestats;
120
121                 if(stat(output_filename, &filestats) != 0)
122                         return false;
123                 else
124                         *output_filesize = (unsigned)filestats.st_size;
125         }
126
127         return true;
128 }