]> mj.ucw.cz Git - vcut.git/blobdiff - vorbiscut.c
Bug fixes.
[vcut.git] / vorbiscut.c
index f4ad4b5f72dfd58a639ce3f1c728fd50353b4c6f..be2e0f6bd590e03c52bbd38eb77ef305cac641d0 100644 (file)
@@ -9,12 +9,15 @@
 #include <alsa/asoundlib.h>
 #include <sndfile.h>
 
+static void key_cleanup(void);
+
 static void __attribute__((noreturn)) die(char *msg, ...)
 {
   va_list args;
   va_start(args, msg);
   vfprintf(stderr, msg, args);
   fputc('\n', stderr);
+  key_cleanup();
   exit(1);
 }
 
@@ -25,6 +28,7 @@ typedef short s16;
 #define TRIPLE(pos) (int)(((pos)/rate)/60), (int)(((pos)/rate)%60), (int)((pos)%rate)
 
 static struct termios tios, tios_old;
+static int tios_inited;
 
 static void key_init(void)
 {
@@ -38,11 +42,16 @@ static void key_init(void)
   if (tcsetattr(0, 0, &tios) < 0)
     die("tcsetattr failed: %m");
   fcntl(0, F_SETFL, O_NONBLOCK);
+  tios_inited = 1;
 }
 
 static void key_cleanup(void)
 {
-  tcsetattr(0, 0, &tios_old);
+  if (tios_inited)
+    {
+      tcsetattr(0, 0, &tios_old);
+      tios_inited = 0;
+    }
 }
 
 static int key_get(void)
@@ -151,11 +160,13 @@ static void scan_streams(void)
 
       total_samples += samples;
     }
+  if (ov_pcm_total(&vf, -1) != total_samples)
+    die("ov_pcm_total mismatch");
 
   if (start_pos < 0)
     {
       if (find_title)
-       printf("WARNING: Title not found, marking whole file");
+       printf("WARNING: Title not found, marking whole file\n");
       start_pos = 0;
       end_pos = total_samples;
     }
@@ -194,7 +205,7 @@ static void in_open(char *name)
       SF_INFO si;
       bzero(&si, sizeof(si));
       lseek(fileno(infile), 0, SEEK_SET);
-      sndf = sf_open_fd(fileno(infile), O_RDONLY, &si, 0);
+      sndf = sf_open_fd(fileno(infile), SFM_READ, &si, 0);
       if (!sndf)
        die("sf_open_fd() failed: %s", sf_strerror(NULL));
       total_samples = si.frames;
@@ -233,7 +244,12 @@ static int in_read(s16 *buf, s64 pos, int nsamp)
   if (inmode == IN_OGG)
     {
       if (ov_pcm_tell(&vf) != pos)
-       printf("!!! CONFUSED POSITION\n");
+       {
+         printf("\n!!! CONFUSED POSITION\n");
+         //ov_pcm_seek(&vf, pos);
+         //if (ov_pcm_tell(&vf) != pos)
+         //printf(">>> unable to correct :(\n");
+       }
       for (;;)
        {
          int bp;
@@ -244,6 +260,8 @@ static int in_read(s16 *buf, s64 pos, int nsamp)
            printf("!!! BAD LINK\n");
          else if (e % 4)
            die("ov_read returned %d bytes, which means non-integer number of samples. Huh.", e);
+         else if (e < 0)
+           die("ov_read returned %d. Huh!", e);
          else if (e/4 <= nsamp)
            return e/4;
          else
@@ -391,8 +409,8 @@ static void editor(void)
     M_POST,
   } mode = M_START;
   int silence = 0;
-  int step = rate;
-  double fst = 1;
+  int step = rate*4;
+  double fst = 4;
   double fsil = 1;
   int lback = 3*rate;
   for(;;)
@@ -504,6 +522,7 @@ static void editor(void)
          break;
        case 0x3027e:   // Delete
          mode = M_END;
+         go = end_pos - lback;
          break;
        case '[':
          mode = M_PRE;
@@ -532,7 +551,7 @@ static void editor(void)
              else
                *f = MAX(*f - fst, 0);
              recalc_faders();
-             if (mode == 1)
+             if (mode == M_PRE)
                go = start_pos;
              else
                go = CLAMP(end_pos - lback);
@@ -575,8 +594,8 @@ static void editor(void)
       else
        {
          nsamp = sizeof(buf)/4;
-         if (pos + nsamp > end_pos)
-           nsamp = end_pos - pos;
+         if (pos + nsamp > end)
+           nsamp = end - pos;
          nsamp = cooked_read(buf, pos, nsamp, (mode == M_PRE), (mode == M_POST));
          nread = nsamp;
        }
@@ -703,7 +722,7 @@ static void render(char *name)
   for (;;)
     {
       int n = sizeof(buf) / 4;
-      if (pos + n > end_pos)
+      if (end_pos - pos < n)
        n = end_pos - pos;
       if (!n)
        break;