Changes in src/io/sink_wavwrite.c [3e1c482:65a4fb4]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/io/sink_wavwrite.c
r3e1c482 r65a4fb4 28 28 #include "io/sink_wavwrite.h" 29 29 #include "io/ioutils.h" 30 31 #include <errno.h>32 30 33 31 #define MAX_SIZE 4096 … … 163 161 unsigned char buf[5]; 164 162 uint_t byterate, blockalign; 163 size_t written = 0; 165 164 166 165 /* open output file */ 167 166 s->fid = fopen((const char *)s->path, "wb"); 168 167 if (!s->fid) { 169 AUBIO_ ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno));168 AUBIO_STRERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr); 170 169 goto beach; 171 170 } 172 171 173 172 // ChunkID 174 fwrite("RIFF", 4, 1, s->fid);173 written += fwrite("RIFF", 4, 1, s->fid); 175 174 176 175 // ChunkSize (0 for now, actual size will be written in _close) 177 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);176 written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); 178 177 179 178 // Format 180 fwrite("WAVE", 4, 1, s->fid);179 written += fwrite("WAVE", 4, 1, s->fid); 181 180 182 181 // Subchunk1ID 183 fwrite("fmt ", 4, 1, s->fid);182 written += fwrite("fmt ", 4, 1, s->fid); 184 183 185 184 // Subchunk1Size 186 fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);185 written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid); 187 186 188 187 // AudioFormat 189 fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);188 written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid); 190 189 191 190 // NumChannels 192 fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);191 written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid); 193 192 194 193 // SampleRate 195 fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);194 written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid); 196 195 197 196 // ByteRate 198 197 byterate = s->samplerate * s->channels * s->bitspersample / 8; 199 fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);198 written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid); 200 199 201 200 // BlockAlign 202 201 blockalign = s->channels * s->bitspersample / 8; 203 fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);202 written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid); 204 203 205 204 // BitsPerSample 206 fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);205 written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid); 207 206 208 207 // Subchunk2ID 209 fwrite("data", 4, 1, s->fid);208 written += fwrite("data", 4, 1, s->fid); 210 209 211 210 // Subchunk1Size (0 for now, actual size will be written in _close) 212 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); 211 written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); 212 213 // fwrite(*, *, 1, s->fid) was called 13 times, check success 214 if (written != 13 || fflush(s->fid)) { 215 AUBIO_STRERR("sink_wavwrite: writing header to %s failed" 216 " (wrote %d/%d, %s)\n", s->path, written, 13, errorstr); 217 fclose(s->fid); 218 s->fid = NULL; 219 return AUBIO_FAIL; 220 } 213 221 214 222 s->scratch_size = s->max_size * s->channels; … … 227 235 } 228 236 237 static 238 void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write) 239 { 240 uint_t written_frames = 0; 241 242 written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid); 243 244 if (written_frames != write) { 245 AUBIO_STRERR("sink_wavwrite: trying to write %d frames to %s, but only %d" 246 " could be written (%s)\n", write, s->path, written_frames, errorstr); 247 } 248 s->total_frames_written += written_frames; 249 } 229 250 230 251 void aubio_sink_wavwrite_do(aubio_sink_wavwrite_t *s, fvec_t * write_data, uint_t write){ 231 uint_t c = 0, i = 0 , written_frames = 0;252 uint_t c = 0, i = 0; 232 253 uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path, 233 254 s->max_size, write_data->length, write); … … 238 259 } 239 260 } 240 written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid); 241 242 if (written_frames != write) { 243 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " 244 "but only %d could be written\n", write, s->path, written_frames); 245 } 246 s->total_frames_written += written_frames; 247 return; 261 262 aubio_sink_wavwrite_write_frames(s, length); 248 263 } 249 264 250 265 void aubio_sink_wavwrite_do_multi(aubio_sink_wavwrite_t *s, fmat_t * write_data, uint_t write){ 251 uint_t c = 0, i = 0 , written_frames = 0;266 uint_t c = 0, i = 0; 252 267 253 268 uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path, … … 261 276 } 262 277 } 263 written_frames = fwrite(s->scratch_data, 2, length * s->channels, s->fid); 264 265 if (written_frames != write * s->channels) { 266 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, " 267 "but only %d could be written\n", write, s->path, written_frames / s->channels); 268 } 269 s->total_frames_written += written_frames; 270 return; 278 279 aubio_sink_wavwrite_write_frames(s, length); 271 280 } 272 281 … … 274 283 uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8; 275 284 unsigned char buf[5]; 285 size_t written = 0, err = 0; 276 286 if (!s->fid) return AUBIO_FAIL; 277 287 // ChunkSize 278 fseek(s->fid, 4, SEEK_SET);279 fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);288 err += fseek(s->fid, 4, SEEK_SET); 289 written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid); 280 290 // Subchunk2Size 281 fseek(s->fid, 40, SEEK_SET); 282 fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); 291 err += fseek(s->fid, 40, SEEK_SET); 292 written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); 293 if (written != 2 || err != 0) { 294 AUBIO_STRERR("sink_wavwrite: updating header of %s failed, expected %d" 295 " write but got only %d (%s)\n", s->path, 2, written, errorstr); 296 } 283 297 // close file 284 298 if (fclose(s->fid)) { 285 AUBIO_ ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno));299 AUBIO_STRERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr); 286 300 } 287 301 s->fid = NULL;
Note: See TracChangeset
for help on using the changeset viewer.