Lines Matching defs:psf
200 static int aiff_close (SF_PRIVATE *psf) ;
205 static int aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
207 static int aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt) ;
209 static int aiff_write_header (SF_PRIVATE *psf, int calc_length) ;
210 static int aiff_write_tailer (SF_PRIVATE *psf) ;
211 static void aiff_write_strings (SF_PRIVATE *psf, int location) ;
213 static int aiff_command (SF_PRIVATE *psf, int command, void *data, int datasize) ;
219 static int aiff_read_basc_chunk (SF_PRIVATE * psf, int) ;
221 static int aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword) ;
225 static int aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info) ;
226 static SF_CHUNK_ITERATOR * aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator) ;
227 static int aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
228 static int aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info) ;
235 aiff_open (SF_PRIVATE *psf)
241 subformat = SF_CODEC (psf->sf.format) ;
243 if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
246 psf->container_close = aiff_close ;
248 if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
249 { if ((error = aiff_read_header (psf, &comm_fmt)))
252 psf->next_chunk_iterator = aiff_next_chunk_iterator ;
253 psf->get_chunk_size = aiff_get_chunk_size ;
254 psf->get_chunk_data = aiff_get_chunk_data ;
256 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
259 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
260 { if (psf->is_pipe)
263 if ((SF_CONTAINER (psf->sf.format)) != SF_FORMAT_AIFF)
266 if (psf->file.mode == SFM_WRITE && (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE))
267 { if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
269 psf->peak_info->peak_loc = SF_PEAK_START ;
272 if (psf->file.mode != SFM_RDWR || psf->filelength < 40)
273 { psf->filelength = 0 ;
274 psf->datalength = 0 ;
275 psf->dataoffset = 0 ;
276 psf->sf.frames = 0 ;
279 psf->strings.flags = SF_STR_ALLOW_START | SF_STR_ALLOW_END ;
281 if ((error = aiff_write_header (psf, SF_FALSE)))
284 psf->write_header = aiff_write_header ;
285 psf->set_chunk = aiff_set_chunk ;
288 psf->command = aiff_command ;
290 switch (SF_CODEC (psf->sf.format))
292 error = pcm_init (psf) ;
296 error = pcm_init (psf) ;
302 error = pcm_init (psf) ;
306 error = ulaw_init (psf) ;
310 error = alaw_init (psf) ;
315 error = float32_init (psf) ;
319 error = double64_init (psf) ;
323 if (psf->sf.frames > comm_fmt.numSampleFrames)
324 psf->sf.frames = comm_fmt.numSampleFrames ;
328 error = dwvw_init (psf, 16) ;
329 if (psf->sf.frames > comm_fmt.numSampleFrames)
330 psf->sf.frames = comm_fmt.numSampleFrames ;
334 error = dwvw_init (psf, 24) ;
335 if (psf->sf.frames > comm_fmt.numSampleFrames)
336 psf->sf.frames = comm_fmt.numSampleFrames ;
340 if (psf->file.mode != SFM_READ)
345 { error = dwvw_init (psf, comm_fmt.sampleSize) ;
346 if (psf->sf.frames > comm_fmt.numSampleFrames)
347 psf->sf.frames = comm_fmt.numSampleFrames ;
350 psf_log_printf (psf, "AIFC/DWVW : Bad bitwidth %d\n", comm_fmt.sampleSize) ;
359 error = aiff_ima_init (psf, AIFC_IMA4_BLOCK_LEN, AIFC_IMA4_SAMPLES_PER_BLOCK) ;
364 error = gsm610_init (psf) ;
365 if (psf->sf.frames > comm_fmt.numSampleFrames)
366 psf->sf.frames = comm_fmt.numSampleFrames ;
372 if (psf->file.mode != SFM_WRITE && psf->sf.frames - comm_fmt.numSampleFrames != 0)
373 { psf_log_printf (psf,
376 comm_fmt.numSampleFrames, (uint32_t) psf->sf.frames) ;
398 aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
407 if (psf->filelength > 0xFFFFFFFFLL)
408 psf_log_printf (psf, "Warning : filelength > 0xffffffff. This is bad!!!!\n") ;
410 if ((paiff = psf->container_data) == NULL)
417 psf_binheader_readf (psf, "p", 0) ;
422 psf->endian = SF_ENDIAN_BIG ;
433 psf_binheader_readf (psf, "Ejm4", jump, &marker, &chunk_size) ;
435 { sf_count_t pos = psf_ftell (psf) ;
436 psf_log_printf (psf, "Have 0 marker at position %D (0x%x).\n", pos, pos) ;
440 if (psf->file.mode == SFM_RDWR && (found_chunk & HAVE_SSND))
443 psf_store_read_chunk_u32 (&psf->rchunks, marker, psf_ftell (psf), chunk_size) ;
453 psf_binheader_readf (psf, "m", &marker) ;
463 if (psf->fileoffset > 0 && psf->filelength > FORMsize + 8)
465 psf->filelength = FORMsize + 8 ;
466 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
468 else if (FORMsize != psf->filelength - 2 * SIGNED_SIZEOF (chunk_size))
469 { chunk_size = psf->filelength - 2 * sizeof (chunk_size) ;
470 psf_log_printf (psf, "FORM : %u (should be %u)\n %M\n", FORMsize, chunk_size, marker) ;
474 psf_log_printf (psf, "FORM : %u\n %M\n", FORMsize, marker) ;
481 paiff->comm_offset = psf_ftell (psf) - 8 ;
484 if ((error = aiff_read_comm_chunk (psf, comm_fmt)) != 0)
495 psf_log_printf (psf, "%M : %d\n", marker, chunk_size) ;
496 if (chunk_size != AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))
497 { psf_binheader_readf (psf, "j", chunk_size) ;
498 psf_log_printf (psf, "*** File PEAK chunk too big.\n") ;
502 if (psf->peak_info)
503 { psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
504 free (psf->peak_info) ;
505 psf->peak_info = NULL ;
507 if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
511 psf_binheader_readf (psf, "E44", &(psf->peak_info->version), &(psf->peak_info->timestamp)) ;
513 if (psf->peak_info->version != 1)
514 psf_log_printf (psf, " version : %d *** (should be version 1)\n", psf->peak_info->version) ;
516 psf_log_printf (psf, " version : %d\n", psf->peak_info->version) ;
518 psf_log_printf (psf, " time stamp : %d\n", psf->peak_info->timestamp) ;
519 psf_log_printf (psf, " Ch Position Value\n") ;
522 for (k = 0 ; k < psf->sf.channels ; k++)
526 psf_binheader_readf (psf, "Ef4", &value, &position) ;
527 psf->peak_info->peaks [k].value = value ;
528 psf->peak_info->peaks [k].position = position ;
531 k, psf->peak_info->peaks [k].position, psf->peak_info->peaks [k].value) ;
533 psf_log_printf (psf, "%s", cptr) ;
536 psf->peak_info->peak_loc = ((found_chunk & HAVE_SSND) == 0) ? SF_PEAK_START : SF_PEAK_END ;
541 psf_log_printf (psf, "*** Valid AIFC files should have an FVER chunk.\n") ;
543 paiff->ssnd_offset = psf_ftell (psf) - 8 ;
545 psf_binheader_readf (psf, "E44", &(ssnd_fmt.offset), &(ssnd_fmt.blocksize)) ;
547 psf->datalength = SSNDsize - sizeof (ssnd_fmt) ;
548 psf->dataoffset = psf_ftell (psf) ;
550 if (psf->datalength > psf->filelength - psf->dataoffset || psf->datalength < 0)
551 { psf_log_printf (psf, " SSND : %u (should be %D)\n", SSNDsize, psf->filelength - psf->dataoffset + sizeof (SSND_CHUNK)) ;
552 psf->datalength = psf->filelength - psf->dataoffset ;
555 psf_log_printf (psf, " SSND : %u\n", SSNDsize) ;
557 if (ssnd_fmt.offset == 0 || psf->dataoffset + ssnd_fmt.offset == ssnd_fmt.blocksize)
558 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
559 psf_log_printf (psf, " Block Size : %u\n", ssnd_fmt.blocksize) ;
561 psf->dataoffset += ssnd_fmt.offset ;
562 psf->datalength -= ssnd_fmt.offset ;
565 { psf_log_printf (psf, " Offset : %u\n", ssnd_fmt.offset) ;
566 psf_log_printf (psf, " Block Size : %u ???\n", ssnd_fmt.blocksize) ;
567 psf->dataoffset += ssnd_fmt.offset ;
568 psf->datalength -= ssnd_fmt.offset ;
572 if (psf->datalength + psf->dataoffset < psf->filelength)
573 psf->dataend = psf->datalength + psf->dataoffset ;
577 if (! psf->sf.seekable)
581 psf_fseek (psf, psf->dataoffset + psf->datalength, SEEK_SET) ;
588 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
593 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
598 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
599 psf_store_string (psf, SF_STR_COPYRIGHT, cptr) ;
607 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
612 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
614 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
615 psf_store_string (psf, SF_STR_ARTIST, cptr) ;
626 bytes -= psf_binheader_readf (psf, "E2", &count) ;
627 psf_log_printf (psf, " %M : %d\n count : %d\n", marker, chunk_size, count) ;
630 { bytes -= psf_binheader_readf (psf, "E422", ×tamp, &id, &len) ;
631 psf_log_printf (psf, " time : 0x%x\n marker : %x\n length : %d\n", timestamp, id, len) ;
634 { psf_log_printf (psf, "\nError : string length (%d) too big.\n", len) ;
639 bytes -= psf_binheader_readf (psf, "b", cptr, len) ;
641 psf_log_printf (psf, " string : %s\n", cptr) ;
645 psf_binheader_readf (psf, "j", bytes) ;
655 { psf_log_printf (psf, " %M : %u (too big, skipping)\n", marker, chunk_size) ;
656 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
661 { psf_log_printf (psf, " %M : %d (too small, skipping)\n", marker, chunk_size) ;
662 psf_binheader_readf (psf, "j", chunk_size + (chunk_size & 1)) ;
667 psf_binheader_readf (psf, "mb", &appl_marker, cptr, chunk_size + (chunk_size & 1) - 4) ;
676 psf_log_printf (psf, " %M : %d\n AppSig : %M\n Name : %s\n", marker, chunk_size, appl_marker, cptr) ;
677 psf_store_string (psf, SF_STR_SOFTWARE, cptr) ;
686 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
691 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
693 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
694 psf_store_string (psf, SF_STR_TITLE, cptr) ;
702 { psf_log_printf (psf, " %M : %d (too big)\n", marker, chunk_size) ;
707 psf_binheader_readf (psf, "b", cptr, chunk_size + (chunk_size & 1)) ;
709 psf_log_printf (psf, " %M : %s\n", marker, cptr) ;
710 psf_store_string (psf, SF_STR_COMMENT, cptr) ;
716 { psf_log_printf (psf, " %M : %d (should be %d)\n", marker, chunk_size, SIZEOF_INST_CHUNK) ;
717 psf_binheader_readf (psf, "j", chunk_size) ;
720 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
724 if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
727 psf_binheader_readf (psf, "b", bytes, 6) ;
728 psf_log_printf (psf, " Base Note : %u\n Detune : %u\n"
732 psf->instrument->basenote = bytes [0] ;
733 psf->instrument->detune = bytes [1] ;
734 psf->instrument->key_lo = bytes [2] ;
735 psf->instrument->key_hi = bytes [3] ;
736 psf->instrument->velocity_lo = bytes [4] ;
737 psf->instrument->velocity_hi = bytes [5] ;
738 psf_binheader_readf (psf, "E2", &gain) ;
739 psf->instrument->gain = gain ;
740 psf_log_printf (psf, " Gain (dB) : %d\n", gain) ;
746 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
750 { psf->instrument->loop_count = 0 ;
751 psf->instrument->loops [0].mode = SF_LOOP_NONE ;
754 { psf->instrument->loop_count = 1 ;
755 psf->instrument->loops [0].mode = SF_LOOP_FORWARD ;
756 psf->instrument->loops [0].start = begin ;
757 psf->instrument->loops [0].end = end ;
758 psf->instrument->loops [0].count = 0 ;
760 psf_log_printf (psf, " Sustain\n mode : %d => %s\n begin : %u\n end : %u\n",
762 psf_binheader_readf (psf, "E222", &mode, &begin, &end) ;
766 psf->instrument->loops [1].mode = SF_LOOP_NONE ;
768 { psf->instrument->loop_count += 1 ;
769 psf->instrument->loops [1].mode = SF_LOOP_FORWARD ;
770 psf->instrument->loops [1].start = begin ;
771 psf->instrument->loops [1].end = end ;
772 psf->instrument->loops [1].count = 0 ;
774 psf_log_printf (psf, " Release\n mode : %d => %s\n begin : %u\n end : %u\n",
781 psf_log_printf (psf, " basc : %u\n", chunk_size) ;
783 if ((error = aiff_read_basc_chunk (psf, chunk_size)))
788 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
792 bytesread = psf_binheader_readf (psf, "E2", &n) ;
794 psf_log_printf (psf, " Count : %u\n", mark_count) ;
796 { psf_log_printf (psf, "*** Second MARK chunk found. Throwing away the first.\n") ;
804 { psf_log_printf (psf, " More than 2500 markers, skipping!\n") ;
805 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
809 if (psf->cues)
810 { free (psf->cues) ;
811 psf->cues = NULL ;
813 if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
820 bytesread += psf_binheader_readf (psf, "E241", &mark_id, &position, &ch) ;
821 psf_log_printf (psf, " Mark ID : %u\n Position : %u\n", mark_id, position) ;
823 psf->cues->cue_points [n].indx = mark_id ;
824 psf->cues->cue_points [n].position = 0 ;
825 psf->cues->cue_points [n].fcc_chunk = MAKE_MARKER ('d', 'a', 't', 'a') ; /* always data */
826 psf->cues->cue_points [n].chunk_start = 0 ;
827 psf->cues->cue_points [n].block_start = 0 ;
828 psf->cues->cue_points [n].sample_offset = position ;
833 { bytesread += psf_binheader_readf (psf, "b", ubuf.scbuf, pstr_len) ;
838 bytesread += psf_binheader_readf (psf, "bj", ubuf.scbuf, read_len, pstr_len - read_len) ;
842 psf_log_printf (psf, " Name : %s\n", ubuf.scbuf) ;
844 psf_strlcpy (psf->cues->cue_points [n].name, sizeof (psf->cues->cue_points [n].name), ubuf.cbuf) ;
851 ** if (psf->instrument == NULL && (psf->instrument = psf_instrument_alloc ()) == NULL)
857 psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
865 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
866 psf_binheader_readf (psf, "j", chunk_size) ;
872 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
873 psf_binheader_readf (psf, "j", make_size_t (chunk_size)) ;
878 { psf_log_printf (psf, " %M : %d (should be >= 12)\n", marker, chunk_size) ;
879 psf_binheader_readf (psf, "j", chunk_size) ;
883 psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
885 if ((error = aiff_read_chanmap (psf, chunk_size)))
892 psf_log_printf (psf, "*** Unknown chunk marker (%X) at position %D with length %u. Exiting parser.\n", marker, psf_ftell (psf) - 8, chunk_size) ;
898 { psf_log_printf (psf, " %M : %u (unknown marker)\n", marker, chunk_size) ;
900 psf_binheader_readf (psf, "j", chunk_size) ;
904 if (psf_ftell (psf) & 0x03)
905 { psf_log_printf (psf, " Unknown chunk marker at position %D. Resynching.\n", psf_ftell (psf) - 8) ;
906 psf_binheader_readf (psf, "j", -3) ;
909 psf_log_printf (psf, "*** Unknown chunk marker %X at position %D. Exiting parser.\n", marker, psf_ftell (psf)) ;
914 if (chunk_size >= psf->filelength)
915 { psf_log_printf (psf, "*** Chunk size %u > file length %D. Exiting parser.\n", chunk_size, psf->filelength) ;
919 if ((! psf->sf.seekable) && (found_chunk & HAVE_SSND))
922 if (psf_ftell (psf) >= psf->filelength - (2 * SIGNED_SIZEOF (int32_t)))
929 for (ji = 0 ; ji < psf->instrument->loop_count ; ji ++)
930 { if (ji < ARRAY_LEN (psf->instrument->loops))
931 { psf->instrument->loops [ji].start = marker_to_position (paiff->markstr, psf->instrument->loops [ji].start, mark_count) ;
932 psf->instrument->loops [ji].end = marker_to_position (paiff->markstr, psf->instrument->loops [ji].end, mark_count) ;
933 psf->instrument->loops [ji].mode = SF_LOOP_FORWARD ;
938 if (psf->cues->cue_count > (uint32_t) (psf->instrument->loop_count * 2))
941 for (j = 0 ; j < psf->cues->cue_count - (uint32_t) (psf->instrument->loop_count * 2) ; j ++)
943 psf->cues->cue_points [j].indx = psf->cues->cue_points [j + psf->instrument->loop_count * 2].indx ;
944 psf->cues->cue_points [j].position = psf->cues->cue_points [j + psf->instrument->loop_count * 2].position ;
945 psf->cues->cue_points [j].fcc_chunk = psf->cues->cue_points [j + psf->instrument->loop_count * 2].fcc_chunk ;
946 psf->cues->cue_points [j].chunk_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].chunk_start ;
947 psf->cues->cue_points [j].block_start = psf->cues->cue_points [j + psf->instrument->loop_count * 2].block_start ;
948 psf->cues->cue_points [j].sample_offset = psf->cues->cue_points [j + psf->instrument->loop_count * 2].sample_offset ;
950 psf->cues->cue_points [j].name [str_index] = psf->cues->cue_points [j + psf->instrument->loop_count * 2].name [str_index] ;
952 psf->cues->cue_count -= psf->instrument->loop_count * 2 ;
955 free (psf->cues) ;
956 psf->cues = NULL ;
960 if (psf->sf.channels < 1)
963 if (psf->sf.channels > SF_MAX_CHANNELS)
975 if (! psf->dataoffset)
982 aiff_close (SF_PRIVATE *psf)
983 { AIFF_PRIVATE *paiff = psf->container_data ;
990 if (psf->file.mode == SFM_WRITE || psf->file.mode == SFM_RDWR)
991 { aiff_write_tailer (psf) ;
992 aiff_write_header (psf, SF_TRUE) ;
999 aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
1010 psf_binheader_readf (psf, "E242b", &(comm_fmt->numChannels), &(comm_fmt->numSampleFrames),
1014 { psf_log_printf (psf, " COMM : %d (0x%x) *** should be ", comm_fmt->size, comm_fmt->size) ;
1016 psf_log_printf (psf, "%d (0x%x)\n", comm_fmt->size, comm_fmt->size) ;
1019 psf_log_printf (psf, " COMM : %d\n", comm_fmt->size) ;
1024 psf_binheader_readf (psf, "Em", &(comm_fmt->encoding)) ;
1029 psf_binheader_readf (psf, "Em1", &(comm_fmt->encoding), &encoding_len) ;
1034 psf_binheader_readf (psf, "b", ubuf.scbuf, read_len) ;
1040 psf_log_printf (psf, " Sample Rate : %d\n", samplerate) ;
1041 psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
1044 { psf_log_printf (psf, " Channels : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
1048 psf_log_printf (psf, " Channels : %d\n", comm_fmt->numChannels) ;
1052 { psf_log_printf (psf, " Sample Size : %d (should be 32)\n", comm_fmt->sampleSize) ;
1056 { psf_log_printf (psf, " Sample Size : %d (should be 64)\n", comm_fmt->sampleSize) ;
1060 psf_log_printf (psf, " Sample Size : %d\n", comm_fmt->sampleSize) ;
1064 psf->sf.samplerate = samplerate ;
1065 psf->sf.frames = comm_fmt->numSampleFrames ;
1066 psf->sf.channels = comm_fmt->numChannels ;
1067 psf->bytewidth = BITWIDTH2BYTES (comm_fmt->sampleSize) ;
1069 psf->endian = SF_ENDIAN_BIG ;
1073 psf->sf.format = (SF_FORMAT_AIFF | subformat) ;
1079 psf->sf.format = (SF_ENDIAN_BIG | SF_FORMAT_AIFF | subformat) ;
1085 psf->endian = SF_ENDIAN_LITTLE ;
1086 psf->sf.format = (SF_ENDIAN_LITTLE | SF_FORMAT_AIFF | subformat) ;
1091 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_FLOAT) ;
1096 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ULAW) ;
1101 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_ALAW) ;
1106 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_DOUBLE) ;
1110 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_PCM_U8) ;
1114 psf->sf.format = SF_FORMAT_AIFF ;
1117 psf->sf.format |= SF_FORMAT_DWVW_12 ;
1120 psf->sf.format |= SF_FORMAT_DWVW_16 ;
1123 psf->sf.format |= SF_FORMAT_DWVW_24 ;
1127 psf->sf.format |= SF_FORMAT_DWVW_N ;
1133 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
1138 psf->endian = SF_ENDIAN_BIG ;
1139 psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_IMA_ADPCM) ;
1143 psf_log_printf (psf, "AIFC : Unimplemented format : %M\n", comm_fmt->encoding) ;
1148 psf_log_printf (psf, " Encoding : %M\n", comm_fmt->encoding) ;
1150 psf_log_printf (psf, " Encoding : %M => %s\n", comm_fmt->encoding, ubuf.scbuf) ;
1160 aiff_rewrite_header (SF_PRIVATE *psf)
1169 psf_fseek (psf, 0, SEEK_SET) ;
1170 psf_fread (psf->header.ptr, psf->dataoffset, 1, psf) ;
1172 psf->header.indx = 0 ;
1175 psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
1178 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, COMM_MARKER)) >= 0)
1179 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1180 comm_frames = psf->sf.frames ;
1181 comm_size = psf->rchunks.chunks [k].len ;
1182 psf_binheader_writef (psf, "Em42t4", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames)) ;
1186 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, PEAK_MARKER)) >= 0)
1187 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1188 psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1189 psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1190 for (ch = 0 ; ch < psf->sf.channels ; ch++)
1191 psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [ch].value), BHW8 (psf->peak_info->peaks [ch].position)) ;
1196 if ((k = psf_find_read_chunk_m32 (&psf->rchunks, SSND_MARKER)) >= 0)
1197 { psf->header.indx = psf->rchunks.chunks [k].offset - 8 ;
1198 psf_binheader_writef (psf, "Etm8", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK)) ;
1202 psf_fseek (psf, 0, SEEK_SET) ;
1203 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1209 aiff_write_header (SF_PRIVATE *psf, int calc_length)
1217 if ((paiff = psf->container_data) == NULL)
1220 current = psf_ftell (psf) ;
1222 if (current > psf->dataoffset)
1226 { psf->filelength = psf_get_filelen (psf) ;
1228 psf->datalength = psf->filelength - psf->dataoffset ;
1229 if (psf->dataend)
1230 psf->datalength -= psf->filelength - psf->dataend ;
1232 if (psf->bytewidth > 0)
1233 psf->sf.frames = psf->datalength / (psf->bytewidth * psf->sf.channels) ;
1236 if (psf->file.mode == SFM_RDWR && psf->dataoffset > 0 && psf->rchunks.count > 0)
1237 { aiff_rewrite_header (psf) ;
1239 psf_fseek (psf, current, SEEK_SET) ;
1243 endian = SF_ENDIAN (psf->sf.format) ;
1248 bit_width = psf->bytewidth * 8 ;
1249 comm_frames = (psf->sf.frames > 0xFFFFFFFF) ? 0xFFFFFFFF : psf->sf.frames ;
1251 switch (SF_CODEC (psf->sf.format) | endian)
1253 psf->endian = SF_ENDIAN_BIG ;
1260 psf->endian = SF_ENDIAN_LITTLE ;
1267 psf->endian = SF_ENDIAN_BIG ;
1274 psf->endian = SF_ENDIAN_LITTLE ;
1281 psf->endian = SF_ENDIAN_BIG ;
1288 psf->endian = SF_ENDIAN_LITTLE ;
1295 psf->endian = SF_ENDIAN_BIG ;
1302 psf->endian = SF_ENDIAN_LITTLE ;
1312 psf->endian = SF_ENDIAN_BIG ;
1319 psf->endian = SF_ENDIAN_BIG ;
1326 psf->endian = SF_ENDIAN_BIG ;
1333 psf->endian = SF_ENDIAN_BIG ;
1340 psf->endian = SF_ENDIAN_BIG ;
1347 psf->endian = SF_ENDIAN_BIG ;
1354 psf->endian = SF_ENDIAN_BIG ;
1364 psf->endian = SF_ENDIAN_BIG ;
1374 psf->endian = SF_ENDIAN_BIG ;
1384 psf->endian = SF_ENDIAN_BIG ;
1394 psf->endian = SF_ENDIAN_BIG ;
1401 comm_frames = psf->sf.frames / AIFC_IMA4_SAMPLES_PER_BLOCK ;
1408 psf->header.ptr [0] = 0 ;
1409 psf->header.indx = 0 ;
1410 psf_fseek (psf, 0, SEEK_SET) ;
1412 psf_binheader_writef (psf, "Etm8", BHWm (FORM_MARKER), BHW8 (psf->filelength - 8)) ;
1417 psf_binheader_writef (psf, "Emm44", BHWm (comm_type), BHWm (FVER_MARKER), BHW4 (4), BHW4 (0xA2805140)) ;
1419 psf_binheader_writef (psf, "Em", BHWm (comm_type)) ;
1421 paiff->comm_offset = psf->header.indx - 8 ;
1424 uint2tenbytefloat (psf->sf.samplerate, comm_sample_rate) ;
1426 psf_binheader_writef (psf, "Em42t42", BHWm (COMM_MARKER), BHW4 (comm_size), BHW2 (psf->sf.channels), BHW4 (comm_frames), BHW2 (bit_width)) ;
1427 psf_binheader_writef (psf, "b", BHWv (comm_sample_rate), BHWz (sizeof (comm_sample_rate))) ;
1431 psf_binheader_writef (psf, "mb", BHWm (comm_encoding), BHWv (comm_zero_bytes), BHWz (sizeof (comm_zero_bytes))) ;
1433 if (psf->channel_map && paiff->chanmap_tag)
1434 psf_binheader_writef (psf, "Em4444", BHWm (CHAN_MARKER), BHW4 (12), BHW4 (paiff->chanmap_tag), BHW4 (0), BHW4 (0)) ;
1437 if (psf->instrument != NULL && psf->cues != NULL)
1443 else if (psf->instrument == NULL && psf->cues != NULL)
1449 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1450 { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
1454 psf_binheader_writef (psf, "Em42",
1455 BHWm (MARK_MARKER), BHW4 (2 + psf->cues->cue_count * (2 + 4) + totalStringLength), BHW2 (psf->cues->cue_count)) ;
1457 for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
1458 psf_binheader_writef (psf, "E24p", BHW2 (psf->cues->cue_points [idx].indx), BHW4 (psf->cues->cue_points [idx].sample_offset), BHWp (psf->cues->cue_points [idx].name)) ;
1461 if (psf->strings.flags & SF_STR_LOCATE_START)
1462 aiff_write_strings (psf, SF_STR_LOCATE_START) ;
1464 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_START)
1465 { psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1466 psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1467 for (k = 0 ; k < psf->sf.channels ; k++)
1468 psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1472 for (uk = 0 ; uk < psf->wchunks.used ; uk++)
1473 psf_binheader_writef (psf, "Em4b", BHWm (psf->wchunks.chunks [uk].mark32), BHW4 (psf->wchunks.chunks [uk].len), BHWv (psf->wchunks.chunks [uk].data), BHWz (psf->wchunks.chunks [uk].len)) ;
1476 paiff->ssnd_offset = psf->header.indx ;
1477 psf_binheader_writef (psf, "Etm844", BHWm (SSND_MARKER), BHW8 (psf->datalength + SIZEOF_SSND_CHUNK), BHW4 (0), BHW4 (0)) ;
1480 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1482 if (psf->error)
1483 return psf->error ;
1485 if (has_data && psf->dataoffset != psf->header.indx)
1486 return psf->error = SFE_INTERNAL ;
1488 psf->dataoffset = psf->header.indx ;
1491 psf_fseek (psf, psf->dataoffset, SEEK_SET) ;
1493 psf_fseek (psf, current, SEEK_SET) ;
1495 return psf->error ;
1499 aiff_write_tailer (SF_PRIVATE *psf)
1503 psf->header.ptr [0] = 0 ;
1504 psf->header.indx = 0 ;
1506 psf->dataend = psf_fseek (psf, 0, SEEK_END) ;
1509 if (psf->dataend % 2 == 1)
1510 { psf_fwrite (psf->header.ptr, 1, 1, psf) ;
1511 psf->dataend ++ ;
1514 if (psf->peak_info != NULL && psf->peak_info->peak_loc == SF_PEAK_END)
1515 { psf_binheader_writef (psf, "Em4", BHWm (PEAK_MARKER), BHW4 (AIFF_PEAK_CHUNK_SIZE (psf->sf.channels))) ;
1516 psf_binheader_writef (psf, "E44", BHW4 (1), BHW4 (time (NULL))) ;
1517 for (k = 0 ; k < psf->sf.channels ; k++)
1518 psf_binheader_writef (psf, "Eft8", BHWf ((float) psf->peak_info->peaks [k].value), BHW8 (psf->peak_info->peaks [k].position)) ;
1521 if (psf->strings.flags & SF_STR_LOCATE_END)
1522 aiff_write_strings (psf, SF_STR_LOCATE_END) ;
1525 if (psf->header.indx > 0)
1526 psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
1532 aiff_write_strings (SF_PRIVATE *psf, int location)
1536 { if (psf->strings.data [k].type == 0)
1539 if (psf->strings.data [k].flags != location)
1542 switch (psf->strings.data [k].type)
1544 slen = strlen (psf->strings.storage + psf->strings.data [k].offset) ;
1545 psf_binheader_writef (psf, "Em4mb", BHWm (APPL_MARKER), BHW4 (slen + 4), BHWm (m3ga_MARKER), BHWv (psf->strings.storage + psf->strings.data [k].offset), BHWz (slen + (slen & 1))) ;
1549 psf_binheader_writef (psf, "EmS", BHWm (NAME_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1553 psf_binheader_writef (psf, "EmS", BHWm (c_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1557 psf_binheader_writef (psf, "EmS", BHWm (AUTH_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1561 psf_binheader_writef (psf, "EmS", BHWm (ANNO_MARKER), BHWS (psf->strings.storage + psf->strings.data [k].offset)) ;
1566 psf_binheader_writef (psf, "Ems", BHWm (ICRD_MARKER), BHWs (psf->strings.data [k].str)) ;
1576 aiff_command (SF_PRIVATE * psf, int command, void * UNUSED (data), int UNUSED (datasize))
1579 if ((paiff = psf->container_data) == NULL)
1584 paiff->chanmap_tag = aiff_caf_find_channel_layout_tag (psf->channel_map, psf->sf.channels) ;
1685 aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
1690 count = psf_binheader_readf (psf, "E442", &bc.version, &bc.numBeats, &bc.rootNote) ;
1691 count += psf_binheader_readf (psf, "E222", &bc.scaleType, &bc.sigNumerator, &bc.sigDenominator) ;
1692 count += psf_binheader_readf (psf, "E2j", &bc.loopType, datasize - sizeof (bc)) ;
1694 psf_log_printf (psf, " Version ? : %u\n Num Beats : %u\n Root Note : 0x%x\n",
1715 psf_log_printf (psf, " ScaleType : 0x%x (%s)\n", bc.scaleType, type_str) ;
1716 psf_log_printf (psf, " Time Sig : %d/%d\n", bc.sigNumerator, bc.sigDenominator) ;
1730 psf_log_printf (psf, " Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
1732 if (psf->loop_info)
1733 { psf_log_printf (psf, " Found existing loop info, using last one.\n") ;
1734 free (psf->loop_info) ;
1735 psf->loop_info = NULL ;
1737 if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
1740 psf->loop_info->time_sig_num = bc.sigNumerator ;
1741 psf->loop_info->time_sig_den = bc.sigDenominator ;
1742 psf->loop_info->loop_mode = (bc.loopType == basc_TYPE_ONE_SHOT) ? SF_LOOP_NONE : SF_LOOP_FORWARD ;
1743 psf->loop_info->num_beats = bc.numBeats ;
1746 psf->loop_info->bpm = (1.0 / psf->sf.frames) * psf->sf.samplerate
1748 psf->loop_info->root_key = bc.rootNote ;
1751 psf_binheader_readf (psf, "j", datasize - count) ;
1758 aiff_read_chanmap (SF_PRIVATE * psf, unsigned dword)
1763 bytesread = psf_binheader_readf (psf, "444", &layout_tag, &channel_bitmap, &channel_decriptions) ;
1768 psf_log_printf (psf, " Tag : %x\n", layout_tag) ;
1770 psf_log_printf (psf, " Layout : %s\n", map_info->name) ;
1773 psf_binheader_readf (psf, "j", dword - bytesread) ;
1776 { size_t chanmap_size = SF_MIN (psf->sf.channels, layout_tag & 0xffff) * sizeof (psf->channel_map [0]) ;
1778 free (psf->channel_map) ;
1780 if ((psf->channel_map = malloc (chanmap_size)) == NULL)
1783 memcpy (psf->channel_map, map_info->channel_map, chanmap_size) ;
1793 aiff_set_chunk (SF_PRIVATE *psf, const SF_CHUNK_INFO * chunk_info)
1794 { return psf_save_write_chunk (&psf->wchunks, chunk_info) ;
1798 aiff_next_chunk_iterator (SF_PRIVATE *psf, SF_CHUNK_ITERATOR * iterator)
1799 { return psf_next_chunk_iterator (&psf->rchunks, iterator) ;
1803 aiff_get_chunk_size (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1806 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1809 chunk_info->datalen = psf->rchunks.chunks [indx].len ;
1815 aiff_get_chunk_data (SF_PRIVATE *psf, const SF_CHUNK_ITERATOR * iterator, SF_CHUNK_INFO * chunk_info)
1819 if ((indx = psf_find_read_chunk_iterator (&psf->rchunks, iterator)) < 0)
1825 chunk_info->id_size = psf->rchunks.chunks [indx].id_size ;
1826 memcpy (chunk_info->id, psf->rchunks.chunks [indx].id, sizeof (chunk_info->id) / sizeof (*chunk_info->id)) ;
1828 pos = psf_ftell (psf) ;
1829 psf_fseek (psf, psf->rchunks.chunks [indx].offset, SEEK_SET) ;
1830 psf_fread (chunk_info->data, SF_MIN (chunk_info->datalen, psf->rchunks.chunks [indx].len), 1, psf) ;
1831 psf_fseek (psf, pos, SEEK_SET) ;