Merge tag 'upstream/1.0.31' into tizen
[platform/upstream/libsndfile.git] / src / aiff.c
index 9cd02fb..763b493 100644 (file)
@@ -243,6 +243,8 @@ aiff_open (SF_PRIVATE *psf)
        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 ;
@@ -283,7 +285,6 @@ aiff_open (SF_PRIVATE *psf)
                psf->set_chunk          = aiff_set_chunk ;
                } ;
 
-       psf->container_close = aiff_close ;
        psf->command = aiff_command ;
 
        switch (SF_CODEC (psf->sf.format))
@@ -492,6 +493,11 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
                                                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 ;
 
@@ -788,12 +794,16 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
                                                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 ;
 
@@ -843,7 +853,7 @@ aiff_read_header (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
 
                        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) ;
@@ -1114,7 +1124,6 @@ aiff_read_comm_chunk (SF_PRIVATE *psf, COMM_CHUNK *comm_fmt)
                                break ;
 
                case GSM_MARKER :
-                               psf->sf.format = SF_FORMAT_AIFF ;
                                psf->sf.format = (SF_FORMAT_AIFF | SF_FORMAT_GSM610) ;
                                break ;
 
@@ -1190,21 +1199,6 @@ aiff_rewrite_header (SF_PRIVATE *psf)
        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 ;
@@ -1435,141 +1429,10 @@ aiff_write_header (SF_PRIVATE *psf, int calc_length)
 
        /* 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 */
@@ -1860,6 +1723,11 @@ aiff_read_basc_chunk (SF_PRIVATE * psf, int datasize)
 
        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 ;