Denis' bug fixes
[asdcplib.git] / src / blackwave.cpp
1 /*
2 Copyright (c) 2005, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 /*! \file    wavsplit.cpp
28     \version $Id$
29     \brief   Black WAV file generator
30 */
31
32 #include "Wav.h"
33 #include <assert.h>
34
35 using namespace ASDCP;
36
37 //------------------------------------------------------------------------------------------
38 //
39 // command line option parser class
40
41 static const char* PACKAGE = "wavsplit";    // program name for messages
42
43 // Macros used to test command option data state.
44
45 // Increment the iterator, test for an additional non-option command line argument.
46 // Causes the caller to return if there are no remaining arguments or if the next
47 // argument begins with '-'.
48 #define TEST_EXTRA_ARG(i,c)    if ( ++i >= argc || argv[(i)][0] == '-' ) \
49                                  { \
50                                    fprintf(stderr, "Argument not found for option %c.\n", (c)); \
51                                    return; \
52                                  }
53 //
54 void
55 banner(FILE* stream = stderr)
56 {
57   fprintf(stream, "\n\
58 %s (asdcplib %s)\n\n\
59 Copyright (c) 2005-2006 John Hurst\n\n\
60 wavesplit is part of asdcplib.\n\
61 asdcplib may be copied only under the terms of the license found at\n\
62 the top of every file in the asdcplib distribution kit.\n\n\
63 Specify the -h (help) option for further information about %s\n\n",
64           PACKAGE, ASDCP::Version(), PACKAGE);
65 }
66
67 //
68 void
69 usage(FILE* stream = stderr)
70 {
71   fprintf(stream, "\
72 USAGE: %s [-v|-h[-d]] <filename>\n\
73 \n\
74   -V              - Show version\n\
75   -h              - Show help\n\
76   -d <duration>   - Number of 2k-sample frames to process, default 1440\n\
77 \n\
78 Other Options:\n\
79   -v              - Verbose, show extra detail during run\n\
80 \n\
81   NOTES: o There is no option grouping, all options must be distinct arguments.\n\
82          o All option arguments must be separated from the option by whitespace.\n\
83 \n", PACKAGE);
84 }
85
86 //
87 //
88 class CommandOptions
89 {
90   CommandOptions();
91
92 public:
93   bool   error_flag;     // true if the given options are in error or not complete
94   bool   verbose_flag;   // true if the verbose option was selected
95   bool   version_flag;   // true if the version display option was selected
96   bool   help_flag;      // true if the help display option was selected
97   ui32_t duration;       // number of frames to be processed
98   const char* filename;  // filename prefix for files written by the extract mode
99
100   CommandOptions(int argc, const char** argv) :
101     error_flag(true), verbose_flag(false), version_flag(false), help_flag(false),
102     duration(1440), filename(0)
103   {
104     for ( int i = 1; i < argc; i++ )
105       {
106         if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
107           {
108             switch ( argv[i][1] )
109               {
110               case 'V': version_flag = true; break;
111               case 'h': help_flag = true; break;
112               case 'v': verbose_flag = true; break;
113
114               case 'd':
115                 TEST_EXTRA_ARG(i, 'd');
116                 duration = atoi(argv[i]); // TODO: test for negative value, should use strtol()
117                 break;
118
119               default:
120                 fprintf(stderr, "Unrecognized option: %c\n", argv[i][1]);
121                 return;
122               }
123           }
124         else
125           {
126             if ( filename )
127               {
128                 fprintf(stderr, "Unexpected extra filename.\n");
129                 return;
130               }
131
132             filename = argv[i];
133           }
134       }
135
136     if ( filename == 0 )
137       {
138         fputs("Output filename required.\n", stderr);
139         return;
140       }
141
142     error_flag = false;
143   }
144 };
145
146
147 // 
148 //
149 Result_t
150 make_black_wav_file(CommandOptions& Options)
151 {
152   PCM::FrameBuffer FrameBuffer;
153   PCM::AudioDescriptor ADesc;
154
155   ADesc.SampleRate = Rational(24,1);
156   ADesc.AudioSamplingRate = ASDCP::SampleRate_48k;
157   ADesc.Locked = 0;
158   ADesc.ChannelCount = 1;
159   ADesc.QuantizationBits = 24;
160   ADesc.BlockAlign = 3;
161   ADesc.AvgBps = 14400;
162   ADesc.LinkedTrackID = 1;
163   ADesc.ContainerDuration = Options.duration;
164
165   // fill the frame buffer with a frame (2000 samples) of black
166   FrameBuffer.Capacity(PCM::CalcFrameBufferSize(ADesc));
167   memset(FrameBuffer.Data(), 0, FrameBuffer.Capacity());
168   FrameBuffer.Size(FrameBuffer.Capacity());
169
170   if ( 1 ) // Options.verbose_flag )
171     {
172       fprintf(stderr, "48Khz PCM Audio, %s fps (%u spf)\n", "24",
173               PCM::CalcSamplesPerFrame(ADesc));
174       fputs("AudioDescriptor:\n", stderr);
175       PCM::AudioDescriptorDump(ADesc);
176     }
177
178   // set up output file
179   Kumu::FileWriter OutFile;
180   Result_t result = OutFile.OpenWrite(Options.filename);
181
182   if ( ASDCP_SUCCESS(result) )
183     {
184       Wav::SimpleWaveHeader WavHeader(ADesc);
185       result = WavHeader.WriteToFile(OutFile);
186     }
187
188   if ( ASDCP_SUCCESS(result) )
189     {
190       ui32_t write_count = 0;
191       ui32_t duration = 0;
192
193       while ( ASDCP_SUCCESS(result) && (duration++ < Options.duration) )
194         {
195           result = OutFile.Write(FrameBuffer.Data(), FrameBuffer.Size(), &write_count);
196         }
197
198       if ( result == RESULT_ENDOFFILE )
199         result = RESULT_OK;
200     }
201
202   return RESULT_OK;
203 }
204
205
206 //
207 int
208 main(int argc, const char** argv)
209 {
210   Result_t result = RESULT_OK;
211   CommandOptions Options(argc, argv);
212
213   if ( Options.help_flag )
214     {
215       usage();
216       return 0;
217     }
218
219   if ( Options.error_flag )
220     return 3;
221
222   if ( Options.version_flag )
223     banner();
224
225   else
226     result = make_black_wav_file(Options);
227
228   if ( result != RESULT_OK )
229     {
230       fputs("Program stopped on error.\n", stderr);
231
232       if ( result != RESULT_FAIL )
233         {
234           fputs(result, stderr);
235           fputc('\n', stderr);
236         }
237
238       return 1;
239     }
240
241   return 0;
242 }
243
244
245 //