shithub: sox

Download patch

ref: b423e6ad1feba1b4eef07719c68e4e45fcce179c
parent: 42bd9a0c01da335d4db474acf153bf1e36f72068
author: robs <robs>
date: Sat Dec 2 10:31:04 EST 2006

Added command line options for specifying the output file comment.

--- a/Changelog
+++ b/Changelog
@@ -57,6 +57,8 @@
   o Improved synth usage (by introducing -n option) and improved synth
     entry in the man-page.  (robs)
   o Documented the butterworth filter effects.  (robs)
+  o Added command line options for specifying the output file
+    comment.  (robs)
 
 sox-12.18.2
 -----------
--- a/TODO
+++ b/TODO
@@ -88,8 +88,6 @@
     side having 1 more value then positive side.  That means instead
     of -1:1 range, its more like -0.999:1 or -1:0.9999.
 
-  o Add command line option to override comment string.
-
   o WAV handler is not using world alignment for at least the
     main data chunk (expect for GSM).
 
--- a/sox.1
+++ b/sox.1
@@ -58,6 +58,8 @@
 can work with.  The first is "self-describing".  Such formats
 include a header that completely describe the characteristics of
 the audio data that follows.
+The header may also allow the inclusion of textual "comments" that can
+be used to describe the audio in some way.
 .P
 The second type is header-less data, often called raw data.
 For this type, a
@@ -212,7 +214,7 @@
 sets it to 0.
 .IP
 .PP
-\fBFormat options:\fR
+\fBInput And Output File Format Options:\fR
 .PP
 Format options effect the input or output file that they immediately precede.
 .PP
@@ -231,12 +233,6 @@
 avg effect must be used.  If the avg effect is not specified on the 
 command line it will be invoked internally with default parameters.
 .TP 10
-\fB-C \fIcompression-factor\fR
-The compression factor for variably compressing output file formats.
-If this option is not given, then a default compression factor will apply.
-The compression factor is interpreted differently for different compressing file formats.
-See the description of the file formats that use this parameter for more information.
-.TP 10
 \fB-r \fIrate\fR
 Gives the sample rate in Hz of the file.  To cause the output file to have
 a different sample rate than the input file, include this option as a part
@@ -333,6 +329,23 @@
 \fB-b/-w/-l/-d\fR
 The sample data size is in bytes, 16-bit words, 32-bit long words, 
 or 64-bit double long (long long) words.
+.PP
+\fBOutput File Format Options:\fR
+.PP
+These options may precede only the output file.
+.TP 10
+\fB--comment \fItext\fR
+Specify the comment text to store in the output file header (where applicable).
+.TP 10
+\fB--comment-file \fIfilename\fR
+Specify a file containing the comment text to store in the output
+file header (where applicable).
+.TP 10
+\fB-C \fIcompression-factor\fR
+The compression factor for variably compressing output file formats.
+If this option is not given, then a default compression factor will apply.
+The compression factor is interpreted differently for different compressing file formats.
+See the description of the file formats that use this parameter for more information.
 .SH FILE TYPES
 .B Determining The File Type
 .br
--- a/src/sox.c
+++ b/src/sox.c
@@ -253,10 +253,17 @@
 
         if (file_opts[i]->info.compression != HUGE_VAL)
         {
-            st_fail("-C only allowed for output file");
+            st_fail("A compression factor can only be given for an output file");
             cleanup();
             exit(1);
         }
+
+        if (file_opts[i]->comment != NULL)
+        {
+            st_fail("A comment can only be given for an output file");
+            cleanup();
+            exit(1);
+        }
     }
 
     /* Loop through the reset of the arguments looking for effects */
@@ -293,6 +300,43 @@
     return(0);
 }
 
+static char * read_comment_file(char const * const filename)
+{
+  bool file_error;
+  long file_length;
+  char * result;
+  FILE * file = fopen(filename, "rt");
+
+  if (file == NULL) {
+    st_fail("Cannot open comment file %s", filename);
+    exit(1);
+  }
+  file_error = fseek(file, 0, SEEK_END);
+  if (!file_error) {
+    file_length = ftell(file);
+    file_error |= file_length < 0;
+    if (!file_error) {
+      result = malloc(file_length + 1);
+      if (result == NULL) {
+        st_fail("Out of memory reading comment file %s", filename);
+        exit(1);
+      }
+      rewind(file);
+      file_error |= fread(result, file_length, 1, file) != 1;
+    }
+  }
+  if (file_error) {
+    st_fail("Error reading comment file %s", filename);
+    exit(1);
+  }
+  fclose(file);
+
+  while (file_length && result[file_length - 1] == '\n')
+    --file_length;
+  result[file_length] = '\0';
+  return result;
+}
+
 static char *getoptstr = "+r:v:t:c:C:phsuUAaig1b2w34lfdxV::Sqoen";
 
 static struct option long_options[] =
@@ -300,6 +344,8 @@
     {"version", 0, NULL, 0},
     {"help", 0, NULL, 'h'},
     {"help-effect", 1, NULL, 0},
+    {"comment", required_argument, NULL, 0},
+    {"comment-file", required_argument, NULL, 0},
     {NULL, 0, NULL, 0}
 };
 
@@ -313,7 +359,17 @@
                             long_options, &option_index)) != -1) {
         switch(c) {
             case 0:
-                if (strncmp("help-effect", long_options[option_index].name,
+                if (option_index == 3)
+                {
+                  fo->comment = strdup(optarg);
+                  break;
+                }
+                else if (option_index == 4)
+                {
+                  fo->comment = read_comment_file(optarg);
+                  break;
+                }
+                else if (strncmp("help-effect", long_options[option_index].name,
                             11) == 0)
                     usage_effect(optarg);
                 else if (strncmp("version", long_options[option_index].name,
@@ -563,18 +619,27 @@
         st_loopinfo_t loops[ST_MAX_NLOOPS];
         double factor;
         int i;
+        file_options_t * options = file_opts[file_count-1];
+        char const * comment = NULL;
 
-        if (file_opts[file_count-1]->info.rate == 0)
-            file_opts[file_count-1]->info.rate = file_desc[0]->info.rate;
-        if (file_opts[file_count-1]->info.size == -1)
-            file_opts[file_count-1]->info.size = file_desc[0]->info.size;
-        if (file_opts[file_count-1]->info.encoding == -1)
-            file_opts[file_count-1]->info.encoding = 
-                file_desc[0]->info.encoding;
-        if (file_opts[file_count-1]->info.channels == -1)
-            file_opts[file_count-1]->info.channels = 
-                file_desc[0]->info.channels;
+        if (options->info.rate == 0)
+            options->info.rate = file_desc[0]->info.rate;
+        if (options->info.size == -1)
+            options->info.size = file_desc[0]->info.size;
+        if (options->info.encoding == -1)
+            options->info.encoding = file_desc[0]->info.encoding;
+        if (options->info.channels == -1)
+            options->info.channels = file_desc[0]->info.channels;
 
+        if (options->comment != NULL)
+        {
+          if (*options->comment == '\0')
+            free(options->comment);
+          else comment = options->comment;
+        }
+        else comment = strdup(file_desc[0]->comment? file_desc[0]->comment :
+            "Processed by SoX");
+
         /*
          * copy loop info, resizing appropriately
          * it's in samples, so # channels don't matter
@@ -581,7 +646,7 @@
          * FIXME: This doesn't work for multi-file processing or
          * effects that change file length.
          */
-        factor = (double) file_opts[file_count-1]->info.rate / (double) 
+        factor = (double) options->info.rate / (double) 
             file_desc[0]->info.rate;
         for(i = 0; i < ST_MAX_NLOOPS; i++) {
             loops[i].start = file_desc[0]->loops[i].start * factor;
@@ -591,10 +656,10 @@
         }
 
         file_desc[file_count-1] = 
-            st_open_write_instr(file_opts[file_count-1]->filename,
-                                &file_opts[file_count-1]->info, 
-                                file_opts[file_count-1]->filetype,
-                                file_desc[0]->comment,
+            st_open_write_instr(options->filename,
+                                &options->info, 
+                                options->filetype,
+                                comment,
                                 &file_desc[0]->instr,
                                 loops);
 
--- a/src/stio.c
+++ b/src/stio.c
@@ -267,10 +267,7 @@
         ft->seekable = is_seekable(ft);
     }
 
-    if (ft->comment == NULL && comment != NULL)
-        ft->comment = strdup(comment);
-    else
-        ft->comment = strdup("Processed by SoX");
+    ft->comment = comment;
 
     if (loops)
     {