2 Copyright (c) 2005, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
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.
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.
27 /*! \file wavsplit.cpp
29 \brief Black WAV file generator
35 using namespace ASDCP;
37 //------------------------------------------------------------------------------------------
39 // command line option parser class
41 static const char* PROGRAM_NAME = "blackwave"; // program name for messages
43 // Macros used to test command option data state.
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] == '-' ) \
50 fprintf(stderr, "Argument not found for option %c.\n", (c)); \
55 banner(FILE* stream = stderr)
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 PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME);
69 usage(FILE* stream = stderr)
72 USAGE: %s [-v|-h[-d]] <filename>\n\
76 -d <duration> - Number of 2k-sample frames to process, default 1440\n\
79 -v - Verbose, show extra detail during run\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\
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
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)
104 for ( int i = 1; i < argc; i++ )
106 if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
108 switch ( argv[i][1] )
110 case 'V': version_flag = true; break;
111 case 'h': help_flag = true; break;
112 case 'v': verbose_flag = true; break;
115 TEST_EXTRA_ARG(i, 'd');
116 duration = atoi(argv[i]); // TODO: test for negative value, should use strtol()
120 fprintf(stderr, "Unrecognized option: %c\n", argv[i][1]);
128 fprintf(stderr, "Unexpected extra filename.\n");
138 fputs("Output filename required.\n", stderr);
150 make_black_wav_file(CommandOptions& Options)
152 PCM::FrameBuffer FrameBuffer;
153 PCM::AudioDescriptor ADesc;
155 ADesc.SampleRate = Rational(24,1);
156 ADesc.AudioSamplingRate = ASDCP::SampleRate_48k;
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;
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());
170 if ( Options.verbose_flag )
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);
178 // set up output file
179 Kumu::FileWriter OutFile;
180 Result_t result = OutFile.OpenWrite(Options.filename);
182 if ( ASDCP_SUCCESS(result) )
184 Wav::SimpleWaveHeader WavHeader(ADesc);
185 result = WavHeader.WriteToFile(OutFile);
188 if ( ASDCP_SUCCESS(result) )
190 ui32_t write_count = 0;
193 while ( ASDCP_SUCCESS(result) && (duration++ < Options.duration) )
195 result = OutFile.Write(FrameBuffer.Data(), FrameBuffer.Size(), &write_count);
198 if ( result == RESULT_ENDOFFILE )
208 main(int argc, const char** argv)
210 Result_t result = RESULT_OK;
211 CommandOptions Options(argc, argv);
213 if ( Options.help_flag )
219 if ( Options.error_flag )
222 if ( Options.version_flag )
226 result = make_black_wav_file(Options);
228 if ( result != RESULT_OK )
230 fputs("Program stopped on error.\n", stderr);
232 if ( result != RESULT_FAIL )
234 fputs(result, stderr);