Lines Matching defs:psf
142 static int wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock) ;
143 static int wav_write_header (SF_PRIVATE *psf, int calc_length) ;
145 static int wav_write_tailer (SF_PRIVATE *psf) ;
146 static int wav_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
147 static int wav_close (SF_PRIVATE *psf) ;
149 static int wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
150 static int wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen) ;
152 static int wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
153 static SF_CHUNK_ITERATOR * wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
154 static int wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
155 static int wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
162 wav_open (SF_PRIVATE *psf)
168 psf->container_data = wpriv ;
171 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
173 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
174 { if ((error = wav_read_header (psf, &blockalign, &framesperblock)))
177 psf->next_chunk_iterator = wav_next_chunk_iterator ;
178 psf->get_chunk_size = wav_get_chunk_size ;
179 psf->get_chunk_data = wav_get_chunk_data ;
182 subformat = SF_CODEC (psf->sf.format) ;
184 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
185 { if (psf->is_pipe)
190 format = SF_CONTAINER (psf->sf.format) ;
194 psf->blockwidth = psf->bytewidth * psf->sf.channels ;
197 psf->endian = SF_ENDIAN (psf->sf.format) ;
198 if (CPU_IS_BIG_ENDIAN && psf->endian == SF_ENDIAN_CPU)
199 psf->endian = SF_ENDIAN_BIG ;
200 else if (psf->endian != SF_ENDIAN_BIG)
201 psf->endian = SF_ENDIAN_LITTLE ;
203 if (psf->file.mode != SFM_RDWR || psf->filelength < 44)
204 { psf->filelength = 0 ;
205 psf->datalength = 0 ;
206 psf->dataoffset = 0 ;
207 psf->sf.frames = 0 ;
219 { blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
226 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
227 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
229 psf->peak_info->peak_loc = SF_PEAK_START ;
232 psf->write_header = wav_write_header ;
233 psf->set_chunk = wav_set_chunk ;
236 psf->container_close = wav_close ;
237 psf->command = wav_command ;
244 error = pcm_init (psf) ;
248 error = ulaw_init (psf) ;
252 error = alaw_init (psf) ;
257 error = float32_init (psf) ;
261 error = double64_init (psf) ;
265 error = wavlike_ima_init (psf, blockalign, framesperblock) ;
269 error = wavlike_msadpcm_init (psf, blockalign, framesperblock) ;
273 error = g72x_init (psf) ;
279 error = nms_adpcm_init (psf) ;
285 error = gsm610_init (psf) ;
289 error = mpeg_init (psf, SF_BITRATE_MODE_CONSTANT, SF_FALSE) ;
295 if (psf->file.mode == SFM_WRITE || (psf->file.mode == SFM_RDWR && psf->filelength == 0))
296 return psf->write_header (psf, SF_FALSE) ;
306 wav_read_header (SF_PRIVATE *psf, int *blockalign, int *framesperblock)
313 if (psf->is_pipe == 0 && psf->filelength > 0xFFFFFFFFLL)
314 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
316 if ((wpriv = psf->container_data) == NULL)
321 psf_binheader_readf (psf, "pmj", 0, &marker, -4) ;
322 psf->header.indx = 0 ;
325 ** lots of code copying here, we'll set the psf->rwf_endian flag once here,
328 psf->rwf_endian = (marker == RIFF_MARKER) ? SF_ENDIAN_LITTLE : SF_ENDIAN_BIG ;
334 psf_binheader_readf (psf, "jm4", jump, &marker, &chunk_size) ;
336 { sf_count_t pos = psf_ftell (psf) ;
337 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
341 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
353 if (psf->fileoffset > 0 && psf->filelength > RIFFsize + 8)
355 psf->filelength = RIFFsize + 8 ;
357 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
359 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
361 else if (psf->filelength < RIFFsize + 2 * SIGNED_SIZEOF (marker))
363 psf_log_printf (psf, "RIFF : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
365 psf_log_printf (psf, "RIFX : %u (should be %D)\n", RIFFsize, psf->filelength - 2 * SIGNED_SIZEOF (marker)) ;
367 RIFFsize = psf->filelength - 2 * SIGNED_SIZEOF (RIFFsize) ;
371 psf_log_printf (psf, "RIFF : %u\n", RIFFsize) ;
373 psf_log_printf (psf, "RIFX : %u\n", RIFFsize) ;
376 psf_binheader_readf (psf, "m", &marker) ;
380 psf_log_printf (psf, "WAVE\n") ;
394 psf_log_printf (psf, "fmt : %d\n", chunk_size) ;
396 if ((error = wavlike_read_fmt_chunk (psf, chunk_size)))
406 if (psf->file.mode == SFM_RDWR && (parsestage & HAVE_other) != 0)
411 psf->datalength = chunk_size ;
412 if (psf->datalength & 1)
413 psf_log_printf (psf, "*** 'data' chunk should be an even number of bytes in length.\n") ;
415 psf->dataoffset = psf_ftell (psf) ;
417 if (psf->dataoffset > 0)
418 { if (chunk_size == 0 && RIFFsize == 8 && psf->filelength > 44)
419 { psf_log_printf (psf, "*** Looks like a WAV file which wasn't closed properly. Fixing it.\n") ;
420 psf->datalength = psf->filelength - psf->dataoffset ;
423 if (psf->datalength > psf->filelength - psf->dataoffset)
424 { psf_log_printf (psf, "data : %D (should be %D)\n", psf->datalength, psf->filelength - psf->dataoffset) ;
425 psf->datalength = psf->filelength - psf->dataoffset ;
428 psf_log_printf (psf, "data : %D\n", psf->datalength) ;
431 if (psf->datalength + psf->dataoffset < psf->filelength)
432 psf->dataend = psf->datalength + psf->dataoffset ;
434 psf->datalength += chunk_size & 1 ;
438 if (! psf->sf.seekable || psf->dataoffset < 0)
442 psf_fseek (psf, psf->datalength, SEEK_CUR) ;
444 if (psf_ftell (psf) != psf->datalength + psf->dataoffset)
445 psf_log_printf (psf, "*** psf_fseek past end error ***\n") ;
455 psf_log_printf (psf, "*** Should have 'fmt ' chunk before 'fact'\n") ;
457 psf_binheader_readf (psf, "4", & (fact_chunk.frames)) ;
460 psf_binheader_readf (psf, "j", (int) (chunk_size - SIGNED_SIZEOF (fact_chunk))) ;
463 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
465 psf_log_printf (psf, "%M : %u (should not be zero)\n", marker, chunk_size) ;
467 psf_log_printf (psf, " frames : %d\n", fact_chunk.frames) ;
476 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
477 if ((error = wavlike_read_peak_chunk (psf, chunk_size)) != 0)
479 psf->peak_info->peak_loc = ((parsestage & HAVE_data) == 0) ? SF_PEAK_START : SF_PEAK_END ;
488 bytesread = psf_binheader_readf (psf, "4", &cue_count) ;
489 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
492 { psf_log_printf (psf, " Count : %u (skipping)\n", cue_count) ;
493 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
497 psf_log_printf (psf, " Count : %d\n", cue_count) ;
499 if (psf->cues)
500 { free (psf->cues) ;
501 psf->cues = NULL ;
504 if ((psf->cues = psf_cues_alloc (cue_count)) == NULL)
511 if ((thisread = psf_binheader_readf (psf, "e44m444", &id, &position, &chunk_id, &chunk_start, &block_start, &offset)) == 0)
516 psf_log_printf (psf, " Cue ID : %2d"
522 psf_log_printf (psf, " (Skipping)\n") ;
524 psf->cues->cue_points [cue_index].indx = id ;
525 psf->cues->cue_points [cue_index].position = position ;
526 psf->cues->cue_points [cue_index].fcc_chunk = chunk_id ;
527 psf->cues->cue_points [cue_index].chunk_start = chunk_start ;
528 psf->cues->cue_points [cue_index].block_start = block_start ;
529 psf->cues->cue_points [cue_index].sample_offset = offset ;
530 psf->cues->cue_points [cue_index].name [0] = '\0' ;
536 { psf_log_printf (psf, "**** Chunk size weirdness (%d != %d)\n", chunk_size, bytesread) ;
537 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
545 psf_log_printf (psf, "smpl : %u\n", chunk_size) ;
547 if ((error = wav_read_smpl_chunk (psf, chunk_size)))
554 psf_log_printf (psf, "acid : %u\n", chunk_size) ;
556 if ((error = wav_read_acid_chunk (psf, chunk_size)))
564 if ((error = wavlike_subchunk_parse (psf, marker, chunk_size)) != 0)
573 if ((error = wavlike_read_bext_chunk (psf, chunk_size)))
582 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
583 psf_binheader_readf (psf, "j", chunk_size) ;
587 if ((error = wavlike_read_cart_chunk (psf, chunk_size)))
612 psf_log_printf (psf, "%M : %u\n", marker, chunk_size) ;
613 psf_binheader_readf (psf, "j", chunk_size) ;
619 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
624 psf_ftell (psf) - 8 + 128 == psf->filelength)
625 { psf_log_printf (psf, "*** Hit ID3v1 trailer. Exiting parser.\n") ;
634 { psf_log_printf (psf, "*** %M : %u (unknown marker)\n", marker, chunk_size) ;
635 psf_binheader_readf (psf, "j", chunk_size) ;
638 if (psf_ftell (psf) & 0x03)
639 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
640 psf_binheader_readf (psf, "j", -3) ;
645 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D. Exiting parser.\n", marker, psf_ftell (psf) - 8) ;
650 if (chunk_size >= psf->filelength)
651 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
655 if (! psf->sf.seekable && (parsestage & HAVE_data))
658 if (psf_ftell (psf) >= psf->filelength - SIGNED_SIZEOF (chunk_size))
659 { psf_log_printf (psf, "End\n") ;
664 if (psf->dataoffset <= 0)
667 if (psf->sf.channels < 1)
670 if (psf->sf.channels > SF_MAX_CHANNELS)
674 psf_log_printf (psf, "**** All non-PCM format files should have a 'fact' chunk.\n") ;
677 psf->endian = psf->rwf_endian ;
679 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
681 if (psf->is_pipe == 0)
686 psf_binheader_readf (psf, "4", &marker) ;
692 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
694 if (psf->blockwidth)
695 { if (psf->filelength - psf->dataoffset < psf->datalength)
696 psf->sf.frames = (psf->filelength - psf->dataoffset) / psf->blockwidth ;
698 psf->sf.frames = psf->datalength / psf->blockwidth ;
703 if (psf->sf.format == (SF_FORMAT_WAVEX | SF_FORMAT_MS_ADPCM))
712 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_16 ;
715 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_24 ;
718 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_NMS_ADPCM_32 ;
727 psf->sf.format = SF_FORMAT_WAV | u_bitwidth_to_subformat (psf->bytewidth * 8) ;
732 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ULAW) ;
737 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_ALAW) ;
741 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
747 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
753 psf->sf.format = (SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
757 psf->sf.format = SF_FORMAT_WAV ;
758 psf->sf.format |= (psf->bytewidth == 8) ? SF_FORMAT_DOUBLE : SF_FORMAT_FLOAT ;
762 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_G721_32 ;
766 psf->sf.format = SF_FORMAT_WAV | SF_FORMAT_MPEG_LAYER_III ;
768 psf->sf.frames = fact_chunk.frames ;
775 wavlike_analyze (psf) ;
778 if (psf->endian == SF_ENDIAN_BIG)
779 psf->sf.format |= SF_ENDIAN_BIG ;
785 wav_write_fmt_chunk (SF_PRIVATE *psf)
788 subformat = SF_CODEC (psf->sf.format) ;
798 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_PCM), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
800 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
802 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
810 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IEEE_FLOAT), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
812 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
814 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
823 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MULAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
825 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
827 psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
836 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_ALAW), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
838 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
840 psf_binheader_writef (psf, "222", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (8), BHW2 (0)) ;
849 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
850 framesperblock = 2 * (blockalign - 4 * psf->sf.channels) / psf->sf.channels + 1 ;
851 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
857 psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_IMA_ADPCM),
858 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
861 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (4), BHW2 (2), BHW2 (framesperblock)) ;
870 blockalign = wavlike_srate2blocksize (psf->sf.samplerate * psf->sf.channels) ;
871 framesperblock = 2 + 2 * (blockalign - 7 * psf->sf.channels) / psf->sf.channels ;
872 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
879 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MS_ADPCM), BHW2 (psf->sf.channels)) ;
882 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
885 psf_binheader_writef (psf, "22222", BHW2 (blockalign), BHW2 (4), BHW2 (extrabytes), BHW2 (framesperblock), BHW2 (7)) ;
887 wavlike_msadpcm_write_adapt_coeffs (psf) ;
899 psf_binheader_writef (psf, "42244", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_G721_ADPCM),
900 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate), BHW4 (psf->sf.samplerate * psf->sf.channels / 2)) ;
903 psf_binheader_writef (psf, "2222", BHW2 (64), BHW2 (4), BHW2 (2), BHW2 (0)) ;
915 bytespersec = psf->sf.samplerate * blockalign / 160 ;
921 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_NMS_VBXADPCM),
922 BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
924 psf_binheader_writef (psf, "422", BHW4 (bytespersec), BHW2 (blockalign), BHW2 (bitwidth)) ;
937 bytespersec = (psf->sf.samplerate * blockalign) / framesperblock ;
943 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_GSM610), BHW2 (psf->sf.channels)) ;
946 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
949 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (2), BHW2 (framesperblock)) ;
962 bytespersec = psf->byterate (psf) ;
965 blocksize = (1152 * bytespersec) / psf->sf.samplerate ;
981 samplesperblock = psf->sf.samplerate >= 32000 ? 1152 : 576 ;
992 psf_binheader_writef (psf, "422", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_MPEGLAYER3), BHW2 (psf->sf.channels)) ;
995 psf_binheader_writef (psf, "44", BHW4 (psf->sf.samplerate), BHW4 (bytespersec)) ;
998 psf_binheader_writef (psf, "2222", BHW2 (blockalign), BHW2 (0), BHW2 (12), BHW2 (1)) ;
1001 psf_binheader_writef (psf, "4222", BHW4 (flags), BHW2 (blocksize), BHW2 (samplesperblock), BHW2 (codecdelay)) ;
1012 psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
1018 wavex_write_fmt_chunk (SF_PRIVATE *psf)
1022 if ((wpriv = psf->container_data) == NULL)
1025 subformat = SF_CODEC (psf->sf.format) ;
1040 psf_binheader_writef (psf, "4224", BHW4 (fmt_size), BHW2 (WAVE_FORMAT_EXTENSIBLE), BHW2 (psf->sf.channels), BHW4 (psf->sf.samplerate)) ;
1042 psf_binheader_writef (psf, "4", BHW4 (psf->sf.samplerate * psf->bytewidth * psf->sf.channels)) ;
1044 psf_binheader_writef (psf, "22", BHW2 (psf->bytewidth * psf->sf.channels), BHW2 (psf->bytewidth * 8)) ;
1047 psf_binheader_writef (psf, "2", BHW2 (22)) ;
1050 psf_binheader_writef (psf, "2", BHW2 (psf->bytewidth * 8)) ;
1056 psf_binheader_writef (psf, "4", BHW4 (0)) ;
1058 psf_binheader_writef (psf, "4", BHW4 (wpriv->wavex_channelmask)) ;
1065 switch (psf->sf.channels)
1067 psf_binheader_writef (psf, "4", BHW4 (0x4)) ;
1071 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2)) ;
1075 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x10 | 0x20)) ;
1079 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20)) ;
1083 psf_binheader_writef (psf, "4", BHW4 (0x1 | 0x2 | 0x4 | 0x8 | 0x10 | 0x20 | 0x40 | 0x80)) ;
1087 psf_binheader_writef (psf, "4", BHW4 (0x0)) ;
1105 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
1111 wavlike_write_guid (psf, wpriv->wavex_ambisonic == SF_AMBISONIC_NONE ?
1116 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MULAW) ;
1120 wavlike_write_guid (psf, &MSGUID_SUBTYPE_ALAW) ;
1126 wavlike_write_guid (psf, &MSGUID_SUBTYPE_MS_ADPCM) ;
1134 psf_binheader_writef (psf, "tm48", BHWm (fact_MARKER), BHW4 (4), BHW8 (psf->sf.frames)) ;
1141 wav_write_header (SF_PRIVATE *psf, int calc_length)
1145 current = psf_ftell (psf) ;
1147 if (current > psf->dataoffset)
1151 { psf->filelength = psf_get_filelen (psf) ;
1153 psf->datalength = psf->filelength - psf->dataoffset ;
1155 if (psf->dataend)
1156 psf->datalength -= psf->filelength - psf->dataend ;
1157 else if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1158 psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1162 psf->header.ptr [0] = 0 ;
1163 psf->header.indx = 0 ;
1164 psf_fseek (psf, 0, SEEK_SET) ;
1168 ** To prevent lots of code copying here, we'll set the psf->rwf_endian flag
1174 if (psf->endian == SF_ENDIAN_LITTLE)
1175 psf_binheader_writef (psf, "etm8", BHWm (RIFF_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
1177 psf_binheader_writef (psf, "Etm8", BHWm (RIFX_MARKER), BHW8 ((psf->filelength < 8) ? 8 : psf->filelength - 8)) ;
1180 psf_binheader_writef (psf, "mm", BHWm (WAVE_MARKER), BHWm (fmt_MARKER)) ;
1183 switch (SF_CONTAINER (psf->sf.format))
1185 if ((error = wav_write_fmt_chunk (psf)) != 0)
1190 if ((error = wavex_write_fmt_chunk (psf)) != 0)
1199 if (psf->strings.flags & SF_STR_LOCATE_START)
1200 wavlike_write_strings (psf, SF_STR_LOCATE_START) ;
1202 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1203 wavlike_write_peak_chunk (psf) ;
1205 if (psf->broadcast_16k != NULL)
1206 wavlike_write_bext_chunk (psf) ;
1208 if (psf->cart_16k != NULL)
1209 wavlike_write_cart_chunk (psf) ;
1211 if (psf->cues != NULL)
1214 psf_binheader_writef (psf, "em44", BHWm (cue_MARKER), BHW4 (4 + psf->cues->cue_count * 6 * 4), BHW4 (psf->cues->cue_count)) ;
1216 for (k = 0 ; k < psf->cues->cue_count ; k++)
1217 psf_binheader_writef (psf, "e44m444", BHW4 (psf->cues->cue_points [k].indx), BHW4 (psf->cues->cue_points [k].position),
1218 BHWm (psf->cues->cue_points [k].fcc_chunk), BHW4 (psf->cues->cue_points [k].chunk_start),
1219 BHW4 (psf->cues->cue_points [k].block_start), BHW4 (psf->cues->cue_points [k].sample_offset)) ;
1222 if (psf->instrument != NULL)
1226 psf_binheader_writef (psf, "m4", BHWm (smpl_MARKER), BHW4 (9 * 4 + psf->instrument->loop_count * 6 * 4)) ;
1227 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* Manufacturer zero is everyone */
1228 tmp = (int) (1.0e9 / psf->sf.samplerate) ; /* Sample period in nano seconds */
1229 psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (psf->instrument->basenote)) ;
1230 tmp = (uint32_t) (psf->instrument->detune * dtune + 0.5) ;
1231 psf_binheader_writef (psf, "4", BHW4 (tmp)) ;
1232 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (0)) ; /* SMTPE format */
1233 psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loop_count), BHW4 (0)) ;
1236 if (psf->instrument->loop_count > ARRAY_LEN (psf->instrument->loops))
1237 psf->instrument->loop_count = ARRAY_LEN (psf->instrument->loops) ;
1239 for (tmp = 0 ; tmp < psf->instrument->loop_count ; tmp++)
1242 type = psf->instrument->loops [tmp].mode ;
1245 psf_binheader_writef (psf, "44", BHW4 (tmp), BHW4 (type)) ;
1246 psf_binheader_writef (psf, "44", BHW4 (psf->instrument->loops [tmp].start), BHW4 (psf->instrument->loops [tmp].end - 1)) ;
1247 psf_binheader_writef (psf, "44", BHW4 (0), BHW4 (psf->instrument->loops [tmp].count)) ;
1252 if (psf->wchunks.used > 0)
1253 wavlike_write_custom_chunks (psf) ;
1255 if (psf->header.indx + 16 < psf->dataoffset)
1257 size_t k = psf->dataoffset - (psf->header.indx + 16) ;
1258 psf_binheader_writef (psf, "m4z", BHWm (PAD_MARKER), BHW4 (k), BHWz (k)) ;
1261 psf_binheader_writef (psf, "tm8", BHWm (data_MARKER), BHW8 (psf->datalength)) ;
1262 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1263 if (psf->error)
1264 return psf->error ;
1266 if (has_data && psf->dataoffset != psf->header.indx)
1267 { psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
1268 return psf->error = SFE_INTERNAL ;
1271 psf->dataoffset = psf->header.indx ;
1274 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1276 psf_fseek (psf, current, SEEK_SET) ;
1278 return psf->error ;
1283 wav_write_tailer (SF_PRIVATE *psf)
1286 psf->header.ptr [0] = 0 ;
1287 psf->header.indx = 0 ;
1289 if (psf->bytewidth > 0 && psf->sf.seekable == SF_TRUE)
1290 { psf->datalength = psf->sf.frames * psf->bytewidth * psf->sf.channels ;
1291 psf->dataend = psf->dataoffset + psf->datalength ;
1294 if (psf->dataend > 0)
1295 psf_fseek (psf, psf->dataend, SEEK_SET) ;
1297 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1299 if (psf->dataend & 1)
1300 psf_binheader_writef (psf, "z", BHWz (1)) ;
1303 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1304 wavlike_write_peak_chunk (psf) ;
1306 if (psf->strings.flags & SF_STR_LOCATE_END)
1307 wavlike_write_strings (psf, SF_STR_LOCATE_END) ;
1310 if (psf->header.indx > 0)
1311 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1317 wav_close (SF_PRIVATE *psf)
1319 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
1320 { wav_write_tailer (psf) ;
1322 if (psf->file.mode == SFM_RDWR)
1323 { sf_count_t current = psf_ftell (psf) ;
1330 if (current < psf->filelength)
1331 { psf_ftruncate (psf, current) ;
1332 psf->filelength = current ;
1336 psf->write_header (psf, SF_TRUE) ;
1343 wav_command (SF_PRIVATE *psf, int command, void * UNUSED (data), int datasize)
1346 if ((wpriv = psf->container_data) == NULL)
1351 if ((SF_CONTAINER (psf->sf.format)) == SF_FORMAT_WAVEX)
1365 wpriv->wavex_channelmask = wavlike_gen_channel_mask (psf->channel_map, psf->sf.channels) ;
1376 wav_read_smpl_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1384 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1385 psf_log_printf (psf, " Manufacturer : %X\n", dword) ;
1387 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1388 psf_log_printf (psf, " Product : %u\n", dword) ;
1390 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1391 psf_log_printf (psf, " Period : %u nsec\n", dword) ;
1393 bytesread += psf_binheader_readf (psf, "4", ¬e) ;
1394 psf_log_printf (psf, " Midi Note : %u\n", note) ;
1396 bytesread += psf_binheader_readf (psf, "4", &pitch) ;
1400 psf_log_printf (psf, " Pitch Fract. : %s\n", buffer) ;
1403 psf_log_printf (psf, " Pitch Fract. : 0\n") ;
1405 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1406 psf_log_printf (psf, " SMPTE Format : %u\n", dword) ;
1408 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1412 psf_log_printf (psf, " SMPTE Offset : %s\n", buffer) ;
1414 bytesread += psf_binheader_readf (psf, "4", &loop_count) ;
1415 psf_log_printf (psf, " Loop Count : %u\n", loop_count) ;
1423 bytesread += psf_binheader_readf (psf, "4", &sampler_data) ;
1425 if (psf->instrument)
1426 { psf_log_printf (psf, " Found more than one SMPL chunk, using last one.\n") ;
1427 free (psf->instrument) ;
1428 psf->instrument = NULL ;
1430 if ((psf->instrument = psf_instrument_alloc ()) == NULL)
1433 psf->instrument->loop_count = loop_count ;
1436 { if ((thisread = psf_binheader_readf (psf, "4", &dword)) == 0)
1439 psf_log_printf (psf, " Cue ID : %2u", dword) ;
1441 bytesread += psf_binheader_readf (psf, "4", &type) ;
1442 psf_log_printf (psf, " Type : %2u", type) ;
1444 bytesread += psf_binheader_readf (psf, "4", &start) ;
1445 psf_log_printf (psf, " Start : %5u", start) ;
1447 bytesread += psf_binheader_readf (psf, "4", &end) ;
1448 psf_log_printf (psf, " End : %5u", end) ;
1450 bytesread += psf_binheader_readf (psf, "4", &dword) ;
1451 psf_log_printf (psf, " Fraction : %5u", dword) ;
1453 bytesread += psf_binheader_readf (psf, "4", &count) ;
1454 psf_log_printf (psf, " Count : %5u\n", count) ;
1456 if (j < ARRAY_LEN (psf->instrument->loops))
1457 { psf->instrument->loops [j].start = start ;
1458 psf->instrument->loops [j].end = end + 1 ;
1459 psf->instrument->loops [j].count = count ;
1463 psf->instrument->loops [j].mode = SF_LOOP_FORWARD ;
1466 psf->instrument->loops [j].mode = SF_LOOP_ALTERNATING ;
1469 psf->instrument->loops [j].mode = SF_LOOP_BACKWARD ;
1472 psf->instrument->loops [j].mode = SF_LOOP_NONE ;
1479 if (actually_loop_count > ARRAY_LEN (psf->instrument->loops))
1481 psf_log_printf (psf, "*** Warning, actual Loop Points count exceeds %u, changing Loop Count from %u to %u\n", ARRAY_LEN (psf->instrument->loops), loop_count, ARRAY_LEN (psf->instrument->loops)) ;
1482 psf->instrument->loop_count = ARRAY_LEN (psf->instrument->loops) ;
1485 { psf_log_printf (psf, "*** Warning, actual Loop Points count != Loop Count, changing Loop Count from %u to %u\n", loop_count, actually_loop_count) ;
1486 psf->instrument->loop_count = actually_loop_count ;
1491 psf_log_printf (psf, " Sampler Data : %u (should be 0)\n", sampler_data) ;
1493 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1497 { psf_log_printf (psf, " Sampler Data : %u (should have been %u)\n", sampler_data, chunklen - bytesread) ;
1501 psf_log_printf (psf, " Sampler Data : %u\n", sampler_data) ;
1503 psf_log_printf (psf, " ") ;
1508 psf_log_printf (psf, "\n ") ;
1510 if ((thisread = psf_binheader_readf (psf, "1", &ch)) == 0)
1513 psf_log_printf (psf, "%02X ", ch & 0xFF) ;
1516 psf_log_printf (psf, "\n") ;
1519 psf->instrument->basenote = note ;
1520 psf->instrument->detune = (int8_t) (pitch / (0x40000000 / 25.0) + 0.5) ;
1521 psf->instrument->gain = 1 ;
1522 psf->instrument->velocity_lo = psf->instrument->key_lo = 0 ;
1523 psf->instrument->velocity_hi = psf->instrument->key_hi = 127 ;
1560 wav_read_acid_chunk (SF_PRIVATE *psf, uint32_t chunklen)
1569 bytesread += psf_binheader_readf (psf, "422f", &flags, &rootnote, &q1, &q2) ;
1573 psf_log_printf (psf, " Flags : 0x%04x (%s,%s,%s,%s,%s)\n", flags,
1580 psf_log_printf (psf, " Root note : 0x%x\n ???? : 0x%04x\n ???? : %s\n",
1583 bytesread += psf_binheader_readf (psf, "422f", &beats, &meter_denom, &meter_numer, &tempo) ;
1585 psf_log_printf (psf, " Beats : %d\n Meter : %d/%d\n Tempo : %s\n",
1588 psf_binheader_readf (psf, "j", chunklen - bytesread) ;
1590 if (psf->loop_info)
1591 { psf_log_printf (psf, " Found existing loop info, using last one.\n") ;
1592 free (psf->loop_info) ;
1593 psf->loop_info = NULL ;
1595 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1598 psf->loop_info->time_sig_num = meter_numer ;
1599 psf->loop_info->time_sig_den = meter_denom ;
1600 psf->loop_info->loop_mode = (flags & 0x01) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1601 psf->loop_info->num_beats = beats ;
1602 psf->loop_info->bpm = tempo ;
1603 psf->loop_info->root_key = (flags & 0x02) ? rootnote : -1 ;
1612 wav_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1613 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1617 wav_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1618 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1622 wav_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1625 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1628 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1634 wav_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1638 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1644 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1645 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1647 pos = psf_ftell (psf) ;
1648 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1649 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1650 psf_fseek (psf, pos, SEEK_SET) ;