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