int main(int argc, char **argv) { if (argc < 3) { printf( "Resample\n" "========\n" "Sample rate conversion utility\n" "This utility is a part of AC3Filter project (http://ac3filter.net)\n" "Copyright (c) 2008-2011 by Alexander Vigovsky\n" "\n" "Usage:\n" " > resample input.wav output.wav [-r[ate]:n] [-q[uality]:n] [-a[ttenuation]:n] [-d[ither]]\n" "\n" "Options:\n" " input.wav - file to convert\n" " output.wav - file to write the result to\n" " rate - new sample rate (deafult: 48000)\n" " quality - passband width (default: 0.99)\n" " attenuation - stopband attenuation in dB (default: 100)\n" " dither - dither the result\n" "\n" "Example:\n" " > resample input.wav output.wav -r:44100 -q:0.999 -a:150\n" " Passband width in this example is 0.999. This means that only uppper 22Hz\n" " of the destination bandwidth (22028Hz - 22050Hz) will be distorted.\n" " Note, that attenuation is large here. Such attenuation is only sensible for\n" " at least 24bit files.\n"); return 0; } const char *input_filename = argv[1]; const char *output_filename = argv[2]; int sample_rate = 48000; double q = 0.99; double a = 100; bool do_dither = false; for (int iarg = 3; iarg < argc; iarg++) { // -r[ate] if (is_arg(argv[iarg], "r", argt_num) || is_arg(argv[iarg], "rate", argt_num)) { sample_rate = (int)arg_num(argv[iarg]); continue; } // -q[uality] if (is_arg(argv[iarg], "q", argt_num) || is_arg(argv[iarg], "quality", argt_num)) { q = arg_num(argv[iarg]); continue; } // -a[ttenuation] if (is_arg(argv[iarg], "a", argt_num) || is_arg(argv[iarg], "attenuation", argt_num)) { a = arg_num(argv[iarg]); continue; } // -d[ither] if (is_arg(argv[iarg], "d", argt_bool) || is_arg(argv[iarg], "dither", argt_bool)) { do_dither = arg_bool(argv[iarg]); continue; } printf("Error: unknown option: %s\n", argv[iarg]); return 1; } if (sample_rate == 0) { printf("Error: incorrect sample rate\n"); return -1; } WAVSource src(input_filename, block_size); if (!src.is_open()) { printf("Error: cannot open file: %s\n", input_filename); return -1; } WAVSink sink(output_filename); if (!sink.is_open()) { printf("Error: cannot open file: %s\n", output_filename); return -1; } Speakers spk = src.get_output(); Converter iconv(block_size); Converter oconv(block_size); Resample resample; AGC agc(1024); Dither dither; iconv.set_format(FORMAT_LINEAR); oconv.set_format(src.get_output().format); if (!resample.set(sample_rate, a, q)) { printf("Error: cannot do sample rate conversion with given parameters\n", output_filename); return -1; } FilterChain chain; chain.add_back(&iconv, "Input converter"); chain.add_back(&resample, "Resample"); if (do_dither && !spk.is_floating_point()) { chain.add_back(&dither, "Dither"); dither.level = 0.5 / spk.level; } chain.add_back(&agc, "AGC"); chain.add_back(&oconv, "Output converter"); Chunk chunk; printf("Conversion %i -> %i\n", src.get_output().sample_rate, sample_rate); printf("0%%\r"); vtime_t t = local_time() + 0.1; while (!src.is_empty()) { src.get_chunk(&chunk); if (!chain.process_to(&chunk, &sink)) { char buf[1024]; chain.chain_text(buf, array_size(buf)); printf("Processing error. Chain dump: %s\n", buf); return -1; } if (local_time() > t) { t += 0.1; double pos = double(src.pos()) * 100 / src.size(); printf("%i%%\r", (int)pos); } } printf("100%%\n"); return 0; }
int main(int argc, char *argv[]) { if (argc < 3) { printf( "AC3 Encoder\n" "===========\n" "This utility is a part of AC3Filter project (http://ac3filter.net)\n" "Copyright (c) 2007-2009 by Alexander Vigovsky\n" "\n" "Usage:\n" " ac3enc input.wav output.ac3 [-br:bitrate]\n" ); return 1; } ///////////////////////////////////////////////////////// // Parse arguments ///////////////////////////////////////////////////////// const char *input_filename = argv[1]; const char *output_filename = argv[2]; int bitrate = 448000; for (int iarg = 3; iarg < argc; iarg++) { if (is_arg(argv[iarg], "br", argt_num)) { bitrate = (int)arg_num(argv[iarg]); continue; } printf("Error: unknown option: %s\n", argv[iarg]); return 1; } ///////////////////////////////////////////////////////// // Open files ///////////////////////////////////////////////////////// WAVSource src; if (!src.open(input_filename, 65536)) { printf("Cannot open file %s (not a PCM file?)\n", input_filename); return 1; } RAWSink sink; if (!sink.open(output_filename)) { printf("Cannot open file %s for writing!\n", argv[2]); return 1; } ///////////////////////////////////////////////////////// // Setup everything ///////////////////////////////////////////////////////// Converter conv(2048); AC3Enc enc; FilterChain chain; chain.add_back(&conv, "Converter"); chain.add_back(&enc, "Encoder"); conv.set_format(FORMAT_LINEAR); conv.set_order(win_order); if (!enc.set_bitrate(bitrate)) { printf("Wrong bitrate (%i)!\n", bitrate); return 1; } Speakers spk = src.get_output(); spk.format = FORMAT_LINEAR; if (!enc.set_input(spk)) { printf("Cannot encode this file (%s %iHz)!\n", spk.sample_rate); return 1; } ///////////////////////////////////////////////////////// // Process ///////////////////////////////////////////////////////// Chunk raw_chunk; Chunk ac3_chunk; CPUMeter cpu_usage; CPUMeter cpu_total; double ms = 0; double old_ms = 0; cpu_usage.start(); cpu_total.start(); fprintf(stderr, "0.0%% Frs/err: 0/0\tTime: 0:00.000i\tFPS: 0 CPU: 0%%\r"); int frames = 0; while (!src.is_empty()) { if (!src.get_chunk(&raw_chunk)) { printf("Data load error!\n"); return 1; } if (!chain.process(&raw_chunk)) { printf("Encoding error!\n"); return 1; } while (!chain.is_empty()) { if (!chain.get_chunk(&ac3_chunk)) { printf("Encoding error!\n"); return 1; } sink.process(&ac3_chunk); frames++; ///////////////////////////////////////////////////// // Statistics ms = double(cpu_total.get_system_time() * 1000); if (ms > old_ms + 100) { old_ms = ms; // Statistics fprintf(stderr, "%2.1f%% Frames: %i\tTime: %i:%02i.%03i\tFPS: %i CPU: %.1f%% \r", double(src.pos()) * 100.0 / src.size(), frames, int(ms/60000), int(ms) % 60000/1000, int(ms) % 1000, int(frames * 1000 / (ms+1)), cpu_usage.usage() * 100); } // if (ms > old_ms + 100) } } fprintf(stderr, "%2.1f%% Frames: %i\tTime: %i:%02i.%03i\tFPS: %i CPU: %.1f%% \n", double(src.pos()) * 100.0 / src.size(), frames, int(ms/60000), int(ms) % 60000/1000, int(ms) % 1000, int(frames * 1000 / (ms+1)), cpu_usage.usage() * 100); cpu_usage.stop(); cpu_total.stop(); printf("System time: %ims\n", int(cpu_total.get_system_time() / 10000)); printf("Process time: %ims\n", int(cpu_total.get_thread_time() / 10000)); return 0; }
int main(int argc, char **argv) { if (argc < 3) { printf( "Gain\n" "====\n" "Simple gain tool to amplify or attenuate an audio file.\n" "This utility is a part of AC3Filter project (http://ac3filter.net)\n" "Copyright (c) 2008-2009 by Alexander Vigovsky\n" "\n" "Usage:\n" " > gain input.wav output.wav [-g[ain]:n]\n" "\n" "Options:\n" " input.wav - file to process\n" " output.wav - file to write the result to\n" " -gain - gain to apply\n" "\n" "Example:\n" " > gain a.wav b.wav -gain:-10\n" " Attenuate by 10dB\n" ); return 0; } const char *input_filename = argv[1]; const char *output_filename = argv[2]; double gain = 1.0; ///////////////////////////////////////////////////////////////////////////// // Parse arguments for (int iarg = 3; iarg < argc; iarg++) { // -gain if (is_arg(argv[iarg], "gain", argt_num)) { gain = db2value(arg_num(argv[iarg])); continue; } printf("Error: unknown option: %s\n", argv[iarg]); return -1; } ///////////////////////////////////////////////////////////////////////////// // Open files WAVSource src(input_filename, block_size); if (!src.is_open()) { printf("Error: cannot open file: %s\n", input_filename); return -1; } WAVSink sink(output_filename); if (!sink.is_open()) { printf("Error: cannot open file: %s\n", output_filename); return -1; } Speakers spk = src.get_output(); ///////////////////////////////////////////////////////////////////////////// // Build the chain Converter iconv(block_size); Converter oconv(block_size); iconv.set_format(FORMAT_LINEAR); oconv.set_format(src.get_output().format); Gain gain_filter; AGC agc(1024); gain_filter.gain = gain; FilterChain chain; chain.add_back(&iconv, "Input converter"); chain.add_back(&gain_filter, "Gain"); chain.add_back(&agc, "AGC"); chain.add_back(&oconv, "Output converter"); if (!chain.set_input(spk)) { printf("Error: cannot start processing\n"); return -1; } ///////////////////////////////////////////////////////////////////////////// // Do the job Chunk chunk; printf("0%%\r"); vtime_t t = local_time() + 0.1; while (!src.is_empty()) { src.get_chunk(&chunk); if (!chain.process_to(&chunk, &sink)) { char buf[1024]; chain.chain_text(buf, array_size(buf)); printf("Processing error. Chain dump: %s\n", buf); return -1; } if (local_time() > t) { t += 0.1; double pos = double(src.pos()) * 100 / src.size(); printf("%i%%\r", (int)pos); } } printf("100%%\n"); return 0; }