@@ -44,6 +44,14 @@ const uint32_t HEADER_TOTAL_SIZE = (HEADER_RIFF_SIZE + HEADER_FMT_SIZE + HEADER_
44
44
45
45
46
46
typedef struct audio_wav_file_tag audio_wav_file ;
47
+
48
+
49
+ static inline audio_wav_file * UNWRAP (output_audio_file * audiofile )
50
+ {
51
+ return (audiofile != NULL ) ? (audio_wav_file * )audiofile -> data : NULL ;
52
+ }
53
+
54
+
47
55
struct audio_wav_file_tag
48
56
{
49
57
Logger logger ;
@@ -64,74 +72,25 @@ struct audio_wav_file_tag
64
72
};
65
73
66
74
67
- static int wav_file_write_multichannel (audio_wav_file * wavfile , unsigned char * inbuffer , int sample_count , int channel_count )
68
- {
69
- SAMPLE_FORMAT * sample_buffer = (SAMPLE_FORMAT * )inbuffer ;
70
- SAMPLE_FORMAT * output_buffer = (SAMPLE_FORMAT * )wavfile -> buffer + 2 * wavfile -> current_pair_count ;
71
-
72
- for (int sindex = sample_count / channel_count ; sindex > 0 ; -- sindex )
73
- {
74
- * output_buffer ++ = * sample_buffer ++ ;
75
- * output_buffer ++ = * sample_buffer ++ ;
76
- output_buffer += (wavfile -> channels - 2 );
77
- }
78
-
79
- wavfile -> current_pair_count ++ ;
80
- if (wavfile -> current_pair_count < wavfile -> channels / 2 ) return 0 ;
81
-
82
- wavfile -> current_pair_count = 0 ;
83
- int count = wavfile -> channels * SAMPLES_PER_FRAME ;
84
- int written = fwrite (wavfile -> buffer , sizeof (SAMPLE_FORMAT ), count , wavfile -> file );
85
- if (count != written )
86
- {
87
- wavfile -> logger (LOGGER_ERROR , "wav_file_write_multichannel_16: error writing wav file: %d, %s\n" , errno , strerror (errno ));
88
- return -1 ;
89
- }
90
-
91
- return 0 ;
92
- }
93
-
94
-
95
- static int wav_file_write (output_audio_file * audiofile , unsigned char * samples , int sample_count , int channel_count )
96
- {
97
- audio_wav_file * wavfile = (audiofile != NULL ) ? (audio_wav_file * )audiofile -> data : NULL ;
98
- if (wavfile == NULL || wavfile -> file == NULL )
99
- {
100
- wavfile -> logger (LOGGER_ERROR , "wav_file_write: can not write samples, wav file is not open.\n" );
101
- return -1 ;
102
- }
103
-
104
- if (wavfile -> total_samples_written + sample_count > wavfile -> max_samples )
105
- {
106
- wavfile -> logger (LOGGER_ERROR , "wav_file_write: maximum possible file size exceeded.\n" );
107
- return -1 ;
108
- }
109
-
110
- int result = wavfile -> writer (wavfile , samples , sample_count , channel_count );
111
- wavfile -> total_samples_written += sample_count ;
112
- return result ;
113
- }
114
-
115
-
116
75
typedef struct
117
76
{
118
77
/* RIFF chunk */
119
- int8_t riff [4 ];
78
+ uint8_t riff [4 ];
120
79
uint32_t filesize ;
121
- int8_t wave [4 ];
80
+ uint8_t wave [4 ];
122
81
123
82
/* fmt chunk */
124
- int8_t fmt [4 ];
125
- int32_t format_length ;
126
- int16_t format ;
127
- int16_t channels ;
128
- int32_t sample_rate ;
129
- int32_t byte_rate ;
130
- int16_t block_align ;
131
- int16_t bits_per_sample ;
83
+ uint8_t fmt [4 ];
84
+ uint32_t format_length ;
85
+ uint16_t format ;
86
+ uint16_t channels ;
87
+ uint32_t sample_rate ;
88
+ uint32_t byte_rate ;
89
+ uint16_t block_align ;
90
+ uint16_t bits_per_sample ;
132
91
133
92
/* data chunk */
134
- int8_t data [4 ];
93
+ uint8_t data [4 ];
135
94
uint32_t data_size ;
136
95
}
137
96
audio_wav_file_header ;
@@ -173,9 +132,8 @@ static void wav_file_initialize_header(audio_wav_file *wavfile, audio_wav_file_h
173
132
}
174
133
175
134
176
- static int wav_file_write_header (output_audio_file * audiofile )
135
+ static int wav_file_write_header (audio_wav_file * wavfile )
177
136
{
178
- audio_wav_file * wavfile = (audiofile != NULL ) ? (audio_wav_file * )audiofile -> data : NULL ;
179
137
if (wavfile == NULL || wavfile -> file == NULL )
180
138
{
181
139
wavfile -> logger (LOGGER_ERROR , "wav_file_write_header: can not write header, wav file is not open.\n" );
@@ -224,9 +182,64 @@ static void wav_file_rewrite_header(audio_wav_file *wavfile)
224
182
}
225
183
226
184
185
+ static int wav_file_write_multichannel (audio_wav_file * wavfile , unsigned char * inbuffer , int sample_count , int channel_count )
186
+ {
187
+ SAMPLE_FORMAT * sample_buffer = (SAMPLE_FORMAT * )inbuffer ;
188
+ SAMPLE_FORMAT * output_buffer = (SAMPLE_FORMAT * )wavfile -> buffer + 2 * wavfile -> current_pair_count ;
189
+
190
+ for (int sindex = sample_count / channel_count ; sindex > 0 ; -- sindex )
191
+ {
192
+ * output_buffer ++ = * sample_buffer ++ ;
193
+ * output_buffer ++ = * sample_buffer ++ ;
194
+ output_buffer += (wavfile -> channels - 2 );
195
+ }
196
+
197
+ wavfile -> current_pair_count ++ ;
198
+ if (wavfile -> current_pair_count < wavfile -> channels / 2 ) return 0 ;
199
+
200
+ wavfile -> current_pair_count = 0 ;
201
+ int count = wavfile -> channels * SAMPLES_PER_FRAME ;
202
+ int written = fwrite (wavfile -> buffer , sizeof (SAMPLE_FORMAT ), count , wavfile -> file );
203
+ if (count != written )
204
+ {
205
+ wavfile -> logger (LOGGER_ERROR , "wav_file_write_multichannel_16: error writing wav file: %d, %s\n" , errno , strerror (errno ));
206
+ return -1 ;
207
+ }
208
+
209
+ return 0 ;
210
+ }
211
+
212
+
213
+ static int wav_file_write (output_audio_file * audiofile , unsigned char * samples , int sample_count , int channel_count )
214
+ {
215
+ audio_wav_file * wavfile = UNWRAP (audiofile );
216
+ if (wavfile == NULL || wavfile -> file == NULL )
217
+ {
218
+ wavfile -> logger (LOGGER_ERROR , "wav_file_write: can not write samples, wav file is not open.\n" );
219
+ return -1 ;
220
+ }
221
+
222
+ if (!wavfile -> header_written )
223
+ {
224
+ int result = wav_file_write_header (wavfile );
225
+ if (FAILED (result )) return result ;
226
+ }
227
+
228
+ if (wavfile -> total_samples_written + sample_count > wavfile -> max_samples )
229
+ {
230
+ wavfile -> logger (LOGGER_ERROR , "wav_file_write: maximum possible file size exceeded.\n" );
231
+ return -1 ;
232
+ }
233
+
234
+ int result = wavfile -> writer (wavfile , samples , sample_count , channel_count );
235
+ wavfile -> total_samples_written += sample_count ;
236
+ return result ;
237
+ }
238
+
239
+
227
240
static void wav_file_close (output_audio_file * audiofile )
228
241
{
229
- audio_wav_file * wavfile = (audiofile != NULL ) ? ( audio_wav_file * ) audiofile -> data : NULL ;
242
+ audio_wav_file * wavfile = UNWRAP (audiofile ) ;
230
243
if (wavfile == NULL || wavfile -> file == NULL ) return ;
231
244
232
245
wav_file_rewrite_header (wavfile );
@@ -237,7 +250,7 @@ static void wav_file_close(output_audio_file *audiofile)
237
250
238
251
static int wav_file_open (output_audio_file * audiofile , char * filename )
239
252
{
240
- audio_wav_file * wavfile = (audiofile != NULL ) ? ( audio_wav_file * ) audiofile -> data : NULL ;
253
+ audio_wav_file * wavfile = UNWRAP (audiofile ) ;
241
254
if (wavfile == NULL ) return -1 ;
242
255
243
256
if (filename == NULL )
@@ -261,7 +274,7 @@ static void wav_file_release(output_audio_file *audiofile)
261
274
{
262
275
if (audiofile == NULL ) return ;
263
276
264
- audio_wav_file * wavfile = ( audio_wav_file * ) audiofile -> data ;
277
+ audio_wav_file * wavfile = UNWRAP ( audiofile ) ;
265
278
if (wavfile != NULL )
266
279
{
267
280
if (wavfile -> file != NULL ) wav_file_close (audiofile );
@@ -317,7 +330,6 @@ output_audio_file *create_audio_wav_file(Logger logger, cmdline_options *options
317
330
audiofile -> data = wavfile ;
318
331
audiofile -> open = wav_file_open ;
319
332
audiofile -> close = wav_file_close ;
320
- audiofile -> writeheader = wav_file_write_header ;
321
333
audiofile -> write = wav_file_write ;
322
334
audiofile -> release = wav_file_release ;
323
335
0 commit comments