shithub: sox

Download patch

ref: 4ccbf42231100d5fb72d9633952043af3d34c36a
parent: d6059b8fd3040c6f96f1cc170eff42431a542af9
author: robs <robs>
date: Tue Oct 30 15:42:28 EDT 2007

clean up a bit

--- a/src/mp3-duration.h
+++ b/src/mp3-duration.h
@@ -83,7 +83,7 @@
   struct mad_frame    mad_frame;
   mad_timer_t         time = mad_timer_zero;
   sox_size_t          initial_bitrate, tagsize = 0, consumed = 0, frames = 0;
-  sox_bool            vbr = sox_false, padded = sox_false;
+  sox_bool            vbr = sox_false, depadded = sox_false;
 
 #if HAVE_ID3TAG && HAVE_UNISTD_H
   sox_size_t duration_ms = id3tag_duration_ms(fp);
@@ -97,9 +97,10 @@
   mad_header_init(&mad_header);
   mad_frame_init(&mad_frame);
 
-  while (sox_true) {
+  do {  /* Read data from the MP3 file */
     int read, padding = 0;
     size_t leftover = mad_stream.bufend - mad_stream.next_frame;
+
     memcpy(buffer, mad_stream.this_frame, leftover);
     read = fread(buffer + leftover, 1, INPUT_BUFFER_SIZE - leftover, fp);
     if (read <= 0) {
@@ -106,15 +107,15 @@
       sox_debug("got exact duration by scan to EOF (frames=%u leftover=%u)", frames, leftover);
       break;
     }
-    for (; !padded && padding < read && !buffer[padding]; ++padding);
-    padded = sox_true;
+    for (; !depadded && padding < read && !buffer[padding]; ++padding);
+    depadded = sox_true;
     mad_stream_buffer(&mad_stream, buffer + padding, leftover + read - padding);
 
-    while (sox_true) {
+    while (sox_true) {  /* Decode frame headers */
       mad_stream.error = MAD_ERROR_NONE;
       if (mad_header_decode(&mad_header, &mad_stream) == -1) {
         if (mad_stream.error == MAD_ERROR_BUFLEN)
-          break;  /* Get some more data from the file */
+          break;  /* Normal behaviour; get some more data from the file */
         if (!MAD_RECOVERABLE(mad_stream.error)) {
           sox_warn("unrecoverable MAD error");
           break;
@@ -122,19 +123,17 @@
         if (mad_stream.error == MAD_ERROR_LOSTSYNC) {
           unsigned available = (mad_stream.bufend - mad_stream.this_frame);
           tagsize = tagtype(mad_stream.this_frame, available);
-          if (tagsize > 0) {   /* It's just some ID3 tags, so skip */
+          if (tagsize) {   /* It's some ID3 tags, so just skip */
             if (tagsize >= available) {
               fseeko(fp, (off_t)(tagsize - available), SEEK_CUR);
-              padded = sox_false;
+              depadded = sox_false;
             }
             mad_stream_skip(&mad_stream, min(tagsize, available));
-            continue;
           }
-          sox_warn("MAD lost sync");
-          continue;
+          else sox_warn("MAD lost sync");
         }
-        sox_warn("recoverable MAD error");
-        continue;
+        else sox_warn("recoverable MAD error");
+        continue; /* Not an audio frame */
       }
 
       mad_timer_add(&time, mad_header.duration);
@@ -156,7 +155,7 @@
           break;
         }
       }
-      else vbr |= initial_bitrate != mad_header.bitrate;
+      else vbr |= mad_header.bitrate != initial_bitrate;
 
       /* If not VBR, we can time just a few frames then extrapolate */
       if (++frames == 10 && !vbr) {
@@ -163,14 +162,12 @@
         struct stat filestat;
         fstat(fileno(fp), &filestat);
         mad_timer_mult(&time, (double)(filestat.st_size - tagsize) / consumed);
-        sox_debug("got approx. duration by FBR extrapolation");
+        sox_debug("got approx. duration by CBR extrapolation");
         break;
       }
     }
+  } while (mad_stream.error == MAD_ERROR_BUFLEN);
 
-    if (mad_stream.error != MAD_ERROR_BUFLEN)
-      break;
-  }
   mad_frame_finish(&mad_frame);
   mad_header_finish(&mad_header);
   mad_stream_finish(&mad_stream);
--- a/src/mp3.c
+++ b/src/mp3.c
@@ -56,13 +56,6 @@
 
 static int tagtype(const unsigned char *data, size_t length)
 {
-    /* TODO: It would be nice to look for Xing VBR headers
-     * or TLE fields in ID3 to detect length of file
-     * and set ft->length.
-     * For CBR, we should fstat the file and divided
-     * by bitrate to find length.
-     */
-
     if (length >= 3 && data[0] == 'T' && data[1] == 'A' && data[2] == 'G')
     {
         return 128; /* ID3V1 */
@@ -79,7 +72,7 @@
         size = 10 + (data[6]<<21) + (data[7]<<14) + (data[8]<<7) + data[9];
         if (flags & ID3_TAG_FLAG_FOOTERPRESENT)
             size += 10;
-        for (; size < length && ! data[size]; ++size);  /* Consume padding */
+        for (; size < length && !data[size]; ++size);  /* Consume padding */
         return size;
     }
 
@@ -89,7 +82,7 @@
 #include "mp3-duration.h"
 
 /*
- * (Re)fill the stream buffer whish is to be decoded.  If any data
+ * (Re)fill the stream buffer that is to be decoded.  If any data
  * still exists in the buffer then they are first shifted to be
  * front of the stream buffer.
  */