/*
-** Copyright (C) 1999-2016 Erik de Castro Lopo <erikd@mega-nerd.com>
+** Copyright (C) 1999-2018 Erik de Castro Lopo <erikd@mega-nerd.com>
** Copyright (C) 2005 David Viens <davidv@plogue.com>
**
** This program is free software; you can redistribute it and/or modify
if ((psf->container_data = calloc (1, sizeof (AIFF_PRIVATE))) == NULL)
return SFE_MALLOC_FAILED ;
+ psf->container_close = aiff_close ;
+
if (psf->file.mode == SFM_READ || (psf->file.mode == SFM_RDWR && psf->filelength > 0))
{ if ((error = aiff_read_header (psf, &comm_fmt)))
return error ;
psf->set_chunk = aiff_set_chunk ;
} ;
- psf->container_close = aiff_close ;
psf->command = aiff_command ;
switch (SF_CODEC (psf->sf.format))
return SFE_WAV_BAD_PEAK ;
} ;
+ if (psf->peak_info)
+ { psf_log_printf (psf, "*** Found existing peak info, using last one.\n") ;
+ free (psf->peak_info) ;
+ psf->peak_info = NULL ;
+ } ;
if ((psf->peak_info = peak_info_calloc (psf->sf.channels)) == NULL)
return SFE_MALLOC_FAILED ;
if (paiff->markstr == NULL)
return SFE_MALLOC_FAILED ;
- if (mark_count > 1000)
- { psf_log_printf (psf, " More than 1000 markers, skipping!\n") ;
+ if (mark_count > 2500) /* 2500 is close to the largest number of cues possible because of block sizes */
+ { psf_log_printf (psf, " More than 2500 markers, skipping!\n") ;
psf_binheader_readf (psf, "j", chunk_size - bytesread) ;
break ;
} ;
+ if (psf->cues)
+ { free (psf->cues) ;
+ psf->cues = NULL ;
+ } ;
if ((psf->cues = psf_cues_alloc (mark_count)) == NULL)
return SFE_MALLOC_FAILED ;
case FVER_MARKER :
found_chunk |= HAVE_FVER ;
- /* Fall through to next case. */
+ /* Falls through. */
case SFX_MARKER :
psf_log_printf (psf, " %M : %d\n", marker, chunk_size) ;
if (psf->sf.channels < 1)
return SFE_CHANNEL_COUNT_ZERO ;
- if (psf->sf.channels >= SF_MAX_CHANNELS)
+ if (psf->sf.channels > SF_MAX_CHANNELS)
return SFE_CHANNEL_COUNT ;
if (! (found_chunk & HAVE_FORM))
psf_log_printf (psf, " Sample Rate : %d\n", samplerate) ;
psf_log_printf (psf, " Frames : %u%s\n", comm_fmt->numSampleFrames, (comm_fmt->numSampleFrames == 0 && psf->filelength > 104) ? " (Should not be 0)" : "") ;
- if (comm_fmt->numChannels < 1 || comm_fmt->numChannels >= SF_MAX_CHANNELS)
+ if (comm_fmt->numChannels < 1 || comm_fmt->numChannels > SF_MAX_CHANNELS)
{ psf_log_printf (psf, " Channels : %d (should be >= 1 and < %d)\n", comm_fmt->numChannels, SF_MAX_CHANNELS) ;
return SFE_CHANNEL_COUNT_BAD ;
} ;
break ;
case GSM_MARKER :
- psf->sf.format = SF_FORMAT_AIFF ;
psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
break ;
return ;
} /* aiff_rewrite_header */
-static uint16_t
-convert_loop_mode (int type_mode)
-{ switch (type_mode)
- { case SF_LOOP_NONE:
- return 0 ;
- case SF_LOOP_FORWARD :
- return 1 ;
- case SF_LOOP_ALTERNATING :
- return 2 ;
- default : break ;
- } ;
-
- return 0 ;
-} /* convert_loop_mode */
-
static int
aiff_write_header (SF_PRIVATE *psf, int calc_length)
{ sf_count_t current ;
/* Check if there's a INST chunk to write */
if (psf->instrument != NULL && psf->cues != NULL)
- { /* Both loops and cues exist */
- uint16_t sustainLoopMode, releaseLoopMode ;
- uint32_t idx, sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
- int totalStringLength = 0, stringLength ;
-
- /* Here we count how many bytes will the pascal strings need */
- for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
- { stringLength = strlen (psf->cues->cue_points [idx].name) + 1 ; /* We'll count the first byte also of every pascal string */
- if (stringLength % 2 == 0)
- totalStringLength += stringLength ;
- else
- totalStringLength += (stringLength + 1) ; /* The pascal string must have an even count */
- }
-
- /* First we check which loops are active and create the necessary MARK chunk for markers */
- /* The first written markers will be references from loop points then comes the real markers */
- if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
- { /* There's both a sustain loop and a release loop */
- psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
- MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 4 + psf->cues->cue_count,
- 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
- 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
- 3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
- 4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
- /* Now comes true markers from cues struct */
- for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
- psf_binheader_writef (psf, "E24p", 5 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
-
- /* Change the loops to be references to the markers */
- sLoopStart = 1 ;
- sLoopEnd = 2 ;
- rLoopStart = 3 ;
- rLoopEnd = 4 ;
- }
- else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
- { /* There's a sustain loop but no release loop */
- psf_binheader_writef (psf, "Em42241b241b",
- MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
- 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
- 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
- /* Now comes true markers from cues struct */
- for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
- psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
-
- /* Change the loops to be references to the markers */
- sLoopStart = 1 ;
- sLoopEnd = 2 ;
- rLoopStart = 0 ;
- rLoopEnd = 0 ;
- }
- else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
- { /* There's a release loop but no sustain loop! Strange indeed! */
- psf_binheader_writef (psf, "Em42241b241b",
- MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17) + psf->cues->cue_count * (2 + 4) + totalStringLength, 2 + psf->cues->cue_count,
- 1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
- 2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
- /* Now comes true markers from cues struct */
- for (idx = 0 ; idx < psf->cues->cue_count ; idx++)
- psf_binheader_writef (psf, "E24p", 3 + idx, psf->cues->cue_points [idx].sample_offset, psf->cues->cue_points [idx].name) ;
-
- /* Change the loops to be references to the markers */
- sLoopStart = 0 ;
- sLoopEnd = 0 ;
- rLoopStart = 1 ;
- rLoopEnd = 2 ;
- } ;
-
- /* First convert loop modes to aiff standard */
- sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
- releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
-
- /* Now we finally write the actual INST chunk */
- psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
- psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
- psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
- sustainLoopMode, sLoopStart, sLoopEnd,
- releaseLoopMode, rLoopStart, rLoopEnd) ;
-
- }
- else if (psf->instrument != NULL && psf->cues == NULL)
- { /* There are loops but no cues */
- uint16_t sustainLoopMode, releaseLoopMode ;
- uint32_t sLoopStart = 0, sLoopEnd = 0, rLoopStart = 0, rLoopEnd = 0 ;
-
- /* First we check which loops are active and create the necessary MARK chunk for markers */
- if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
- { /* There's both a sustain loop and a release loop */
- psf_binheader_writef (psf, "Em42 241b 241b 241b 241b",
- MARK_MARKER, 2 + 2 * (2 + 4 + 1 + 19) + 2 * (2 + 4 + 1 + 17), 4,
- 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
- 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17),
- 3, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
- 4, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
- /* Change the loops to be references to the markers */
- sLoopStart = 1 ;
- sLoopEnd = 2 ;
- rLoopStart = 3 ;
- rLoopEnd = 4 ;
- }
- else if (psf->instrument->loops [0].mode != SF_LOOP_NONE && psf->instrument->loops [1].mode == SF_LOOP_NONE)
- { /* There's a sustain loop but no release loop */
- psf_binheader_writef (psf, "Em42241b241b",
- MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
- 1, psf->instrument->loops [0].start, 18, "sustain loop start", make_size_t (19),
- 2, psf->instrument->loops [0].end, 16, "sustain loop end", make_size_t (17)) ;
- /* Change the loops to be references to the markers */
- sLoopStart = 1 ;
- sLoopEnd = 2 ;
- rLoopStart = 0 ;
- rLoopEnd = 0 ;
- }
- else if (psf->instrument->loops [0].mode == SF_LOOP_NONE && psf->instrument->loops [1].mode != SF_LOOP_NONE)
- { /* There's a release loop but no sustain loop! Strange indeed! */
- psf_binheader_writef (psf, "Em42241b241b",
- MARK_MARKER, 2 + (2 + 4 + 1 + 19) + (2 + 4 + 1 + 17), 2,
- 1, psf->instrument->loops [1].start, 18, "release loop start", make_size_t (19),
- 2, psf->instrument->loops [1].end, 16, "release loop end", make_size_t (17)) ;
- /* Change the loops to be references to the markers */
- sLoopStart = 0 ;
- sLoopEnd = 0 ;
- rLoopStart = 1 ;
- rLoopEnd = 2 ;
- } ;
-
- /* First convert loop modes to aiff standard */
- sustainLoopMode = convert_loop_mode (psf->instrument->loops [0].mode) ;
- releaseLoopMode = convert_loop_mode (psf->instrument->loops [1].mode) ;
-
- /* Now we finally write the actual INST chunk */
- psf_binheader_writef (psf, "Em4111111", INST_MARKER, SIZEOF_INST_CHUNK, psf->instrument->basenote, psf->instrument->detune,
- psf->instrument->key_lo, psf->instrument->key_hi, psf->instrument->velocity_lo, psf->instrument->velocity_hi) ;
- psf_binheader_writef (psf, "E2222222", (short) psf->instrument->gain,
- sustainLoopMode, sLoopStart, sLoopEnd,
- releaseLoopMode, rLoopStart, rLoopEnd) ;
-
+ { /* Huge chunk of code removed here because it had egregious errors that were
+ ** not detected by either the compiler or the tests. It was found when updating
+ ** the way psf_binheader_writef works.
+ */
}
else if (psf->instrument == NULL && psf->cues != NULL)
{ /* There are cues but no loops */
psf_log_printf (psf, " Loop Type : 0x%x (%s)\n", bc.loopType, type_str) ;
+ if (psf->loop_info)
+ { psf_log_printf (psf, " Found existing loop info, using last one.\n") ;
+ free (psf->loop_info) ;
+ psf->loop_info = NULL ;
+ } ;
if ((psf->loop_info = calloc (1, sizeof (SF_LOOP_INFO))) == NULL)
return SFE_MALLOC_FAILED ;