Changeset f56f795 for src/io/sink_wavwrite.c
- Timestamp:
- Dec 20, 2018, 8:31:00 PM (6 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/crepe, fix/ffmpeg5, master
- Children:
- 65e1ec6
- Parents:
- 65628c4 (diff), e2f1e6d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/io/sink_wavwrite.c
r65628c4 rf56f795 163 163 unsigned char buf[5]; 164 164 uint_t byterate, blockalign; 165 size_t written = 0; 165 166 166 167 /* open output file */ 167 168 s->fid = fopen((const char *)s->path, "wb"); 168 169 if (!s->fid) { 169 AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, strerror(errno)); 170 char errorstr[256]; 171 AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); 172 AUBIO_ERR("sink_wavwrite: could not open %s (%s)\n", s->path, errorstr); 170 173 goto beach; 171 174 } 172 175 173 176 // ChunkID 174 fwrite("RIFF", 4, 1, s->fid);177 written += fwrite("RIFF", 4, 1, s->fid); 175 178 176 179 // ChunkSize (0 for now, actual size will be written in _close) 177 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid);180 written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); 178 181 179 182 // Format 180 fwrite("WAVE", 4, 1, s->fid);183 written += fwrite("WAVE", 4, 1, s->fid); 181 184 182 185 // Subchunk1ID 183 fwrite("fmt ", 4, 1, s->fid);186 written += fwrite("fmt ", 4, 1, s->fid); 184 187 185 188 // Subchunk1Size 186 fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid);189 written += fwrite(write_little_endian(16, buf, 4), 4, 1, s->fid); 187 190 188 191 // AudioFormat 189 fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid);192 written += fwrite(write_little_endian(1, buf, 2), 2, 1, s->fid); 190 193 191 194 // NumChannels 192 fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid);195 written += fwrite(write_little_endian(s->channels, buf, 2), 2, 1, s->fid); 193 196 194 197 // SampleRate 195 fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid);198 written += fwrite(write_little_endian(s->samplerate, buf, 4), 4, 1, s->fid); 196 199 197 200 // ByteRate 198 201 byterate = s->samplerate * s->channels * s->bitspersample / 8; 199 fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid);202 written += fwrite(write_little_endian(byterate, buf, 4), 4, 1, s->fid); 200 203 201 204 // BlockAlign 202 205 blockalign = s->channels * s->bitspersample / 8; 203 fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid);206 written += fwrite(write_little_endian(blockalign, buf, 2), 2, 1, s->fid); 204 207 205 208 // BitsPerSample 206 fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid);209 written += fwrite(write_little_endian(s->bitspersample, buf, 2), 2, 1, s->fid); 207 210 208 211 // Subchunk2ID 209 fwrite("data", 4, 1, s->fid);212 written += fwrite("data", 4, 1, s->fid); 210 213 211 214 // Subchunk1Size (0 for now, actual size will be written in _close) 212 fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); 215 written += fwrite(write_little_endian(0, buf, 4), 4, 1, s->fid); 216 217 // fwrite(*, *, 1, s->fid) was called 13 times, check success 218 if (written != 13) { 219 char errorstr[256]; 220 AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); 221 AUBIO_WRN("sink_wavwrite: writing header to %s failed, expected %d" 222 " write but got only %d (%s)\n", s->path, 13, written, errorstr); 223 return AUBIO_FAIL; 224 } 213 225 214 226 s->scratch_size = s->max_size * s->channels; … … 227 239 } 228 240 241 static 242 void aubio_sink_wavwrite_write_frames(aubio_sink_wavwrite_t *s, uint_t write) 243 { 244 uint_t written_frames = 0; 245 246 written_frames = fwrite(s->scratch_data, 2 * s->channels, write, s->fid); 247 248 if (written_frames != write) { 249 char errorstr[256]; 250 AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); 251 AUBIO_WRN("sink_wavwrite: trying to write %d frames to %s, but only %d" 252 " could be written (%s)\n", write, s->path, written_frames, errorstr); 253 } 254 s->total_frames_written += written_frames; 255 } 229 256 230 257 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;258 uint_t c = 0, i = 0; 232 259 uint_t length = aubio_sink_validate_input_length("sink_wavwrite", s->path, 233 260 s->max_size, write_data->length, write); … … 238 265 } 239 266 } 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; 267 268 aubio_sink_wavwrite_write_frames(s, length); 248 269 } 249 270 250 271 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;272 uint_t c = 0, i = 0; 252 273 253 274 uint_t channels = aubio_sink_validate_input_channels("sink_wavwrite", s->path, … … 261 282 } 262 283 } 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; 284 285 aubio_sink_wavwrite_write_frames(s, length); 271 286 } 272 287 … … 274 289 uint_t data_size = s->total_frames_written * s->bitspersample * s->channels / 8; 275 290 unsigned char buf[5]; 291 size_t written = 0, err = 0; 276 292 if (!s->fid) return AUBIO_FAIL; 277 293 // ChunkSize 278 fseek(s->fid, 4, SEEK_SET);279 fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid);294 err += fseek(s->fid, 4, SEEK_SET); 295 written += fwrite(write_little_endian(data_size + 36, buf, 4), 4, 1, s->fid); 280 296 // Subchunk2Size 281 fseek(s->fid, 40, SEEK_SET); 282 fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); 297 err += fseek(s->fid, 40, SEEK_SET); 298 written += fwrite(write_little_endian(data_size, buf, 4), 4, 1, s->fid); 299 if (written != 2 || err != 0) { 300 char errorstr[256]; 301 AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); 302 AUBIO_WRN("sink_wavwrite: updating header of %s failed, expected %d" 303 " write but got only %d (%s)\n", s->path, 2, written, errorstr); 304 } 283 305 // close file 284 306 if (fclose(s->fid)) { 285 AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, strerror(errno)); 307 char errorstr[256]; 308 AUBIO_STRERROR(errno, errorstr, sizeof(errorstr)); 309 AUBIO_ERR("sink_wavwrite: Error closing file %s (%s)\n", s->path, errorstr); 286 310 } 287 311 s->fid = NULL;
Note: See TracChangeset
for help on using the changeset viewer.