Added library names
[asdcplib.git] / src / wavesplit.cpp
index 2a40e1ecacfad7153c851612bc55dbb57f233eb7..71448ce37015cc3f1e4dec6d0aece42b120086a3 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-Copyright (c) 2005-2006, John Hurst
+Copyright (c) 2005-2009, John Hurst
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
@@ -39,18 +39,18 @@ using namespace ASDCP;
 //
 // command line option parser class
 
-static const char* PACKAGE = "wavesplit";    // program name for messages
+static const char* PROGRAM_NAME = "wavesplit";    // program name for messages
 
 // Macros used to test command option data state.
 
 // True if a major mode has already been selected.
-#define TEST_MAJOR_MODE()     ( create_flag )
+#define TEST_MAJOR_MODE()     ( create_flag || info_flag )
 
 // Causes the caller to return if a major mode has already been selected,
 // otherwise sets the given flag.
 #define TEST_SET_MAJOR_MODE(f) if ( TEST_MAJOR_MODE() ) \
                                  { \
-                                   fputs("Conflicting major mode, choose one of -(ic)).\n", stderr); \
+                                   fputs("Conflicting major mode, choose one of -(ic).\n", stderr); \
                                    return; \
                                  } \
                                  (f) = true;
@@ -69,12 +69,12 @@ banner(FILE* stream = stderr)
 {
   fprintf(stream, "\n\
 %s (asdcplib %s)\n\n\
-Copyright (c) 2005-2006 John Hurst\n\n\
-wavesplit is part of asdcplib.\n\
+Copyright (c) 2005-2009 John Hurst\n\n\
+%s is part of asdcplib.\n\
 asdcplib may be copied only under the terms of the license found at\n\
 the top of every file in the asdcplib distribution kit.\n\n\
 Specify the -h (help) option for further information about %s\n\n",
-         PACKAGE, ASDCP::Version(), PACKAGE, PACKAGE);
+         PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME, PROGRAM_NAME);
 }
 
 //
@@ -82,21 +82,23 @@ void
 usage(FILE* stream = stderr)
 {
   fprintf(stream, "\
-USAGE: %s [-i|-c <root-name> [-v]] <filename>\n\
+USAGE: %s [-v] -V\n\
+       %s [-v] -c <root-name> [-d <duration>] [-f <start-frame>] <filename>\n\
+       %s [-v] -i <filename>\n\
 \n\
 Major modes:\n\
-  -c <root-name>  - Create a WAV file for each channel in the input file (default is two channel files)\n\
-  -V              - Show version\n\
-  -h              - Show help\n\
-\n\
-Read/Write Options:\n\
-  -f <frame-num>  - Starting frame number, default 0\n\
+  -c <root-name>  - Create a WAV file for each channel in the input file\n\
+                    (default is two-channel files)\n\
   -d <duration>   - Number of frames to process, default all\n\
+  -f <frame-num>  - Starting frame number, default 0\n\
+  -h              - Show help\n\
+  -i              - Show input file metadata (no output created)\n\
+  -V              - Show version\n\
   -v              - Print extra info while processing\n\
 \n\
   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
          o All option arguments must be separated from the option by whitespace.\n\
-\n", PACKAGE);
+\n", PROGRAM_NAME, PROGRAM_NAME, PROGRAM_NAME);
 }
 
 //
@@ -108,6 +110,7 @@ class CommandOptions
 public:
   bool   error_flag;     // true if the given options are in error or not complete
   bool   create_flag;    // true if the file create mode was selected
+  bool   info_flag;      // true if the file info mode was selected
   bool   version_flag;   // true if the version display option was selected
   bool   help_flag;      // true if the help display option was selected
   bool   verbose_flag;   // true for extra info during procesing
@@ -117,7 +120,7 @@ public:
   const char* filename;  // filename to be processed
 
   CommandOptions(int argc, const char** argv) :
-    error_flag(true), create_flag(false),
+    error_flag(true), create_flag(false), info_flag(false),
     version_flag(false), help_flag(false), start_frame(0),
     duration(0xffffffff), file_root(0), filename(0)
   {
@@ -127,23 +130,25 @@ public:
          {
            switch ( argv[i][1] )
              {
-             case 'V': version_flag = true; break;
-             case 'h': help_flag = true; break;
              case 'c':
                TEST_SET_MAJOR_MODE(create_flag);
                TEST_EXTRA_ARG(i, 'c');
                file_root = argv[i];
                break;
 
+             case 'd':
+               TEST_EXTRA_ARG(i, 'd');
+               duration = Kumu::xabs(strtol(argv[i], 0, 10));
+               break;
+
              case 'f':
                TEST_EXTRA_ARG(i, 'f');
-               start_frame = atoi(argv[i]); // TODO: test for negative value, should use strtol()
+               start_frame = Kumu::xabs(strtol(argv[i], 0, 10));
                break;
 
-             case 'd':
-               TEST_EXTRA_ARG(i, 'd');
-               duration = atoi(argv[i]); // TODO: test for negative value, should use strtol()
-               break;
+             case 'h': help_flag = true; break;
+             case 'i': TEST_SET_MAJOR_MODE(info_flag); break;
+             case 'V': version_flag = true; break;
 
              default:
                fprintf(stderr, "Unrecognized option: %c\n", argv[i][1]);
@@ -181,8 +186,30 @@ public:
   }
 };
 
-
 //
+Result_t
+wav_file_info(CommandOptions& Options)
+{
+  PCM::AudioDescriptor ADesc;
+  Rational         PictureRate = EditRate_24;
+  PCM::WAVParser Parser;
+
+  // set up essence parser
+  Result_t result = Parser.OpenRead(Options.filename, PictureRate);
+
+  if ( ASDCP_SUCCESS(result) )
+    {
+      Parser.FillAudioDescriptor(ADesc);
+      ADesc.EditRate = PictureRate;
+      fprintf(stderr, "48Khz PCM Audio, %s fps (%u spf)\n", "24",
+             PCM::CalcSamplesPerFrame(ADesc));
+      fputs("AudioDescriptor:\n", stderr);
+      PCM::AudioDescriptorDump(ADesc);
+    }
+
+  return result;
+}
+
 //
 void
 split_buffer(ui32_t sample_size, PCM::FrameBuffer& FrameBuffer,
@@ -208,8 +235,6 @@ split_buffer(ui32_t sample_size, PCM::FrameBuffer& FrameBuffer,
   R_FrameBuffer.Size(R_FrameBuffer.Capacity());
 }
 
-
-// 
 //
 Result_t
 split_wav_file(CommandOptions& Options)
@@ -228,7 +253,7 @@ split_wav_file(CommandOptions& Options)
     {
       Parser.FillAudioDescriptor(ADesc);
 
-      ADesc.SampleRate = PictureRate;
+      ADesc.EditRate = PictureRate;
       ui32_t fb_size = PCM::CalcFrameBufferSize(ADesc);
       assert((fb_size % 2) == 0);
       FrameBuffer.Capacity(fb_size);
@@ -237,7 +262,7 @@ split_wav_file(CommandOptions& Options)
 
       if ( Options.verbose_flag )
        {
-         fprintf(stderr, "48Khz PCM Audio, %s fps (%lu spf)\n", "24",
+         fprintf(stderr, "48Khz PCM Audio, %s fps (%u spf)\n", "24",
                  PCM::CalcSamplesPerFrame(ADesc));
          fputs("AudioDescriptor:\n", stderr);
          PCM::AudioDescriptorDump(ADesc);
@@ -253,12 +278,12 @@ split_wav_file(CommandOptions& Options)
   if ( ASDCP_SUCCESS(result) )
     {
       char filename[256];
-      sprintf(filename, "%s_l.wav", Options.file_root);
+      snprintf(filename, 256, "%s_l.wav", Options.file_root);
       result = L_OutFile.OpenWrite(filename);
 
       if ( ASDCP_SUCCESS(result) )
        {
-         sprintf(filename, "%s_r.wav", Options.file_root);
+         snprintf(filename, 256, "%s_r.wav", Options.file_root);
          result = R_OutFile.OpenWrite(filename);
        }
     }
@@ -285,7 +310,7 @@ split_wav_file(CommandOptions& Options)
          if ( FrameBuffer.Size() != FrameBuffer.Capacity() )
            {
              fprintf(stderr, "WARNING: Last frame read was short, PCM input is possibly not frame aligned.\n");
-             fprintf(stderr, "Expecting %lu bytes, got %lu.\n", FrameBuffer.Capacity(), FrameBuffer.Size());
+             fprintf(stderr, "Expecting %u bytes, got %u.\n", FrameBuffer.Capacity(), FrameBuffer.Size());
              result = RESULT_ENDOFFILE;
              continue;
            }
@@ -346,7 +371,10 @@ main(int argc, const char** argv)
   if ( Options.version_flag )
     banner();
 
-  if ( Options.create_flag )
+  if ( Options.info_flag )
+    result = wav_file_info(Options);
+
+  else if ( Options.create_flag )
     result = split_wav_file(Options);
 
   if ( result != RESULT_OK )