Chris@10: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@10: Chris@10: /* Chris@10: Rubber Band Library Chris@10: An audio time-stretching and pitch-shifting library. Chris@10: Copyright 2007-2012 Particular Programs Ltd. Chris@10: Chris@10: This program is free software; you can redistribute it and/or Chris@10: modify it under the terms of the GNU General Public License as Chris@10: published by the Free Software Foundation; either version 2 of the Chris@10: License, or (at your option) any later version. See the file Chris@10: COPYING included with this distribution for more information. Chris@10: Chris@10: Alternatively, if you have a valid commercial licence for the Chris@10: Rubber Band Library obtained by agreement with the copyright Chris@10: holders, you may redistribute and/or modify it under the terms Chris@10: described in that licence. Chris@10: Chris@10: If you wish to distribute code using the Rubber Band Library Chris@10: under terms other than those of the GNU General Public License, Chris@10: you must obtain a valid commercial licence before doing so. Chris@10: */ Chris@10: Chris@10: #include "rubberband/RubberBandStretcher.h" Chris@10: Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: Chris@10: #include Chris@10: Chris@10: #include "system/sysutils.h" Chris@10: Chris@10: #ifdef __MSVC__ Chris@10: #include "getopt/getopt.h" Chris@10: #else Chris@10: #include Chris@10: #include Chris@10: #include Chris@10: #endif Chris@10: Chris@10: #include "base/Profiler.h" Chris@10: Chris@10: using namespace std; Chris@10: using namespace RubberBand; Chris@10: Chris@10: #ifdef _WIN32 Chris@10: using RubberBand::gettimeofday; Chris@10: #endif Chris@10: Chris@10: #ifdef __MSVC__ Chris@10: using RubberBand::usleep; Chris@10: #endif Chris@10: Chris@10: double tempo_convert(const char *str) Chris@10: { Chris@10: char *d = strchr((char *)str, ':'); Chris@10: Chris@10: if (!d || !*d) { Chris@10: double m = atof(str); Chris@10: if (m != 0.0) return 1.0 / m; Chris@10: else return 1.0; Chris@10: } Chris@10: Chris@10: char *a = strdup(str); Chris@10: char *b = strdup(d+1); Chris@10: a[d-str] = '\0'; Chris@10: double m = atof(a); Chris@10: double n = atof(b); Chris@10: free(a); Chris@10: free(b); Chris@10: if (n != 0.0 && m != 0.0) return m / n; Chris@10: else return 1.0; Chris@10: } Chris@10: Chris@10: int main(int argc, char **argv) Chris@10: { Chris@10: int c; Chris@10: Chris@10: double ratio = 1.0; Chris@10: double duration = 0.0; Chris@10: double pitchshift = 0.0; Chris@10: double frequencyshift = 1.0; Chris@10: int debug = 0; Chris@10: bool realtime = false; Chris@10: bool precise = true; Chris@10: int threading = 0; Chris@10: bool lamination = true; Chris@10: bool longwin = false; Chris@10: bool shortwin = false; Chris@10: bool smoothing = false; Chris@10: bool hqpitch = false; Chris@10: bool formant = false; Chris@10: bool together = false; Chris@10: bool crispchanged = false; Chris@10: int crispness = -1; Chris@10: bool help = false; Chris@10: bool version = false; Chris@10: bool quiet = false; Chris@10: Chris@10: bool haveRatio = false; Chris@10: Chris@10: std::string mapfile; Chris@10: Chris@10: enum { Chris@10: NoTransients, Chris@10: BandLimitedTransients, Chris@10: Transients Chris@10: } transients = Transients; Chris@10: Chris@10: enum { Chris@10: CompoundDetector, Chris@10: PercussiveDetector, Chris@10: SoftDetector Chris@10: } detector = CompoundDetector; Chris@10: Chris@10: while (1) { Chris@10: int optionIndex = 0; Chris@10: Chris@10: static struct option longOpts[] = { Chris@10: { "help", 0, 0, 'h' }, Chris@10: { "version", 0, 0, 'V' }, Chris@10: { "time", 1, 0, 't' }, Chris@10: { "tempo", 1, 0, 'T' }, Chris@10: { "duration", 1, 0, 'D' }, Chris@10: { "pitch", 1, 0, 'p' }, Chris@10: { "frequency", 1, 0, 'f' }, Chris@10: { "crisp", 1, 0, 'c' }, Chris@10: { "crispness", 1, 0, 'c' }, Chris@10: { "debug", 1, 0, 'd' }, Chris@10: { "realtime", 0, 0, 'R' }, Chris@10: { "loose", 0, 0, 'L' }, Chris@10: { "precise", 0, 0, 'P' }, Chris@10: { "formant", 0, 0, 'F' }, Chris@10: { "no-threads", 0, 0, '0' }, Chris@10: { "no-transients", 0, 0, '1' }, Chris@10: { "no-lamination", 0, 0, '2' }, Chris@10: { "centre-focus", 0, 0, '7' }, Chris@10: { "window-long", 0, 0, '3' }, Chris@10: { "window-short", 0, 0, '4' }, Chris@10: { "bl-transients", 0, 0, '8' }, Chris@10: { "detector-perc", 0, 0, '5' }, Chris@10: { "detector-soft", 0, 0, '6' }, Chris@10: { "smoothing", 0, 0, '9' }, Chris@10: { "pitch-hq", 0, 0, '%' }, Chris@10: { "threads", 0, 0, '@' }, Chris@10: { "quiet", 0, 0, 'q' }, Chris@10: { "timemap", 1, 0, 'M' }, Chris@10: { 0, 0, 0, 0 } Chris@10: }; Chris@10: Chris@10: c = getopt_long(argc, argv, Chris@10: "t:p:d:RLPFc:f:T:D:qhVM:", Chris@10: longOpts, &optionIndex); Chris@10: if (c == -1) break; Chris@10: Chris@10: switch (c) { Chris@10: case 'h': help = true; break; Chris@10: case 'V': version = true; break; Chris@10: case 't': ratio *= atof(optarg); haveRatio = true; break; Chris@10: case 'T': ratio *= tempo_convert(optarg); haveRatio = true; break; Chris@10: case 'D': duration = atof(optarg); haveRatio = true; break; Chris@10: case 'p': pitchshift = atof(optarg); haveRatio = true; break; Chris@10: case 'f': frequencyshift = atof(optarg); haveRatio = true; break; Chris@10: case 'd': debug = atoi(optarg); break; Chris@10: case 'R': realtime = true; break; Chris@10: case 'L': precise = false; break; Chris@10: case 'P': precise = true; break; Chris@10: case 'F': formant = true; break; Chris@10: case '0': threading = 1; break; Chris@10: case '@': threading = 2; break; Chris@10: case '1': transients = NoTransients; crispchanged = true; break; Chris@10: case '2': lamination = false; crispchanged = true; break; Chris@10: case '3': longwin = true; crispchanged = true; break; Chris@10: case '4': shortwin = true; crispchanged = true; break; Chris@10: case '5': detector = PercussiveDetector; crispchanged = true; break; Chris@10: case '6': detector = SoftDetector; crispchanged = true; break; Chris@10: case '7': together = true; break; Chris@10: case '8': transients = BandLimitedTransients; crispchanged = true; break; Chris@10: case '9': smoothing = true; crispchanged = true; break; Chris@10: case '%': hqpitch = true; break; Chris@10: case 'c': crispness = atoi(optarg); break; Chris@10: case 'q': quiet = true; break; Chris@10: case 'M': mapfile = optarg; break; Chris@10: default: help = true; break; Chris@10: } Chris@10: } Chris@10: Chris@10: if (version) { Chris@10: cerr << RUBBERBAND_VERSION << endl; Chris@10: return 0; Chris@10: } Chris@10: Chris@10: if (help || !haveRatio || optind + 2 != argc) { Chris@10: cerr << endl; Chris@10: cerr << "Rubber Band" << endl; Chris@10: cerr << "An audio time-stretching and pitch-shifting library and utility program." << endl; Chris@10: cerr << "Copyright 2007-2012 Particular Programs Ltd." << endl; Chris@10: cerr << endl; Chris@10: cerr << " Usage: " << argv[0] << " [options] " << endl; Chris@10: cerr << endl; Chris@10: cerr << "You must specify at least one of the following time and pitch ratio options." << endl; Chris@10: cerr << endl; Chris@10: cerr << " -t, --time Stretch to X times original duration, or" << endl; Chris@10: cerr << " -T, --tempo Change tempo by multiple X (same as --time 1/X), or" << endl; Chris@10: cerr << " -T, --tempo : Change tempo from X to Y (same as --time X/Y), or" << endl; Chris@10: cerr << " -D, --duration Stretch or squash to make output file X seconds long" << endl; Chris@10: cerr << endl; Chris@10: cerr << " -p, --pitch Raise pitch by X semitones, or" << endl; Chris@10: cerr << " -f, --frequency Change frequency by multiple X" << endl; Chris@10: cerr << endl; Chris@10: cerr << " -M, --timemap Use file F as the source for key frame map" << endl; Chris@10: cerr << endl; Chris@10: cerr << "A map file consists of a series of lines each having two numbers separated" << endl; Chris@10: cerr << "by a single space. These are source and target sample frame numbers for fixed" << endl; Chris@10: cerr << "time points within the audio data, defining a varying stretch factor through" << endl; Chris@10: cerr << "the audio. You must specify an overall stretch factor using e.g. -t as well." << endl; Chris@10: cerr << endl; Chris@10: cerr << "The following options provide a simple way to adjust the sound. See below" << endl; Chris@10: cerr << "for more details." << endl; Chris@10: cerr << endl; Chris@10: cerr << " -c, --crisp Crispness (N = 0,1,2,3,4,5,6); default 5 (see below)" << endl; Chris@10: cerr << " -F, --formant Enable formant preservation when pitch shifting" << endl; Chris@10: cerr << endl; Chris@10: cerr << "The remaining options fine-tune the processing mode and stretch algorithm." << endl; Chris@10: cerr << "These are mostly included for test purposes; the default settings and standard" << endl; Chris@10: cerr << "crispness parameter are intended to provide the best sounding set of options" << endl; Chris@10: cerr << "for most situations. The default is to use none of these options." << endl; Chris@10: cerr << endl; Chris@10: cerr << " -L, --loose Relax timing in hope of better transient preservation" << endl; Chris@10: cerr << " -P, --precise Ignored: The opposite of -L, this is default from 1.6" << endl; Chris@10: cerr << " -R, --realtime Select realtime mode (implies --no-threads)" << endl; Chris@10: cerr << " --no-threads No extra threads regardless of CPU and channel count" << endl; Chris@10: cerr << " --threads Assume multi-CPU even if only one CPU is identified" << endl; Chris@10: cerr << " --no-transients Disable phase resynchronisation at transients" << endl; Chris@10: cerr << " --bl-transients Band-limit phase resync to extreme frequencies" << endl; Chris@10: cerr << " --no-lamination Disable phase lamination" << endl; Chris@10: cerr << " --window-long Use longer processing window (actual size may vary)" << endl; Chris@10: cerr << " --window-short Use shorter processing window" << endl; Chris@10: cerr << " --smoothing Apply window presum and time-domain smoothing" << endl; Chris@10: cerr << " --detector-perc Use percussive transient detector (as in pre-1.5)" << endl; Chris@10: cerr << " --detector-soft Use soft transient detector" << endl; Chris@10: cerr << " --pitch-hq In RT mode, use a slower, higher quality pitch shift" << endl; Chris@10: cerr << " --centre-focus Preserve focus of centre material in stereo" << endl; Chris@10: cerr << " (at a cost in width and individual channel quality)" << endl; Chris@10: cerr << endl; Chris@10: cerr << " -d, --debug Select debug level (N = 0,1,2,3); default 0, full 3" << endl; Chris@10: cerr << " (N.B. debug level 3 includes audible ticks in output)" << endl; Chris@10: cerr << " -q, --quiet Suppress progress output" << endl; Chris@10: cerr << endl; Chris@10: cerr << " -V, --version Show version number and exit" << endl; Chris@10: cerr << " -h, --help Show this help" << endl; Chris@10: cerr << endl; Chris@10: cerr << "\"Crispness\" levels:" << endl; Chris@10: cerr << " -c 0 equivalent to --no-transients --no-lamination --window-long" << endl; Chris@10: cerr << " -c 1 equivalent to --detector-soft --no-lamination --window-long (for piano)" << endl; Chris@10: cerr << " -c 2 equivalent to --no-transients --no-lamination" << endl; Chris@10: cerr << " -c 3 equivalent to --no-transients" << endl; Chris@10: cerr << " -c 4 equivalent to --bl-transients" << endl; Chris@10: cerr << " -c 5 default processing options" << endl; Chris@10: cerr << " -c 6 equivalent to --no-lamination --window-short (may be good for drums)" << endl; Chris@10: cerr << endl; Chris@10: return 2; Chris@10: } Chris@10: Chris@10: if (crispness >= 0 && crispchanged) { Chris@10: cerr << "WARNING: Both crispness option and transients, lamination or window options" << endl; Chris@10: cerr << " provided -- crispness will override these other options" << endl; Chris@10: } Chris@10: Chris@10: switch (crispness) { Chris@10: case -1: crispness = 5; break; Chris@10: case 0: detector = CompoundDetector; transients = NoTransients; lamination = false; longwin = true; shortwin = false; break; Chris@10: case 1: detector = SoftDetector; transients = Transients; lamination = false; longwin = true; shortwin = false; break; Chris@10: case 2: detector = CompoundDetector; transients = NoTransients; lamination = false; longwin = false; shortwin = false; break; Chris@10: case 3: detector = CompoundDetector; transients = NoTransients; lamination = true; longwin = false; shortwin = false; break; Chris@10: case 4: detector = CompoundDetector; transients = BandLimitedTransients; lamination = true; longwin = false; shortwin = false; break; Chris@10: case 5: detector = CompoundDetector; transients = Transients; lamination = true; longwin = false; shortwin = false; break; Chris@10: case 6: detector = CompoundDetector; transients = Transients; lamination = false; longwin = false; shortwin = true; break; Chris@10: }; Chris@10: Chris@10: if (!quiet) { Chris@10: cerr << "Using crispness level: " << crispness << " ("; Chris@10: switch (crispness) { Chris@10: case 0: cerr << "Mushy"; break; Chris@10: case 1: cerr << "Piano"; break; Chris@10: case 2: cerr << "Smooth"; break; Chris@10: case 3: cerr << "Balanced multitimbral mixture"; break; Chris@10: case 4: cerr << "Unpitched percussion with stable notes"; break; Chris@10: case 5: cerr << "Crisp monophonic instrumental"; break; Chris@10: case 6: cerr << "Unpitched solo percussion"; break; Chris@10: } Chris@10: cerr << ")" << endl; Chris@10: } Chris@10: Chris@10: std::map mapping; Chris@10: Chris@10: if (mapfile != "") { Chris@10: std::ifstream ifile(mapfile.c_str()); Chris@10: if (!ifile.is_open()) { Chris@10: cerr << "ERROR: Failed to open time map file \"" << mapfile << "\"" Chris@10: << endl; Chris@10: return 1; Chris@10: } Chris@10: std::string line; Chris@10: int lineno = 0; Chris@10: while (!ifile.eof()) { Chris@10: std::getline(ifile, line); Chris@10: while (line.length() > 0 && line[0] == ' ') line = line.substr(1); Chris@10: if (line == "") { Chris@10: ++lineno; Chris@10: continue; Chris@10: } Chris@10: std::string::size_type i = line.find_first_of(" "); Chris@10: if (i == std::string::npos) { Chris@10: cerr << "ERROR: Time map file \"" << mapfile Chris@10: << "\" is malformed at line " << lineno << endl; Chris@10: return 1; Chris@10: } Chris@10: size_t source = atoi(line.substr(0, i).c_str()); Chris@10: while (i < line.length() && line[i] == ' ') ++i; Chris@10: size_t target = atoi(line.substr(i).c_str()); Chris@10: mapping[source] = target; Chris@10: if (debug > 0) { Chris@10: cerr << "adding mapping from " << source << " to " << target << endl; Chris@10: } Chris@10: ++lineno; Chris@10: } Chris@10: ifile.close(); Chris@10: Chris@10: if (!quiet) { Chris@10: cerr << "Read " << mapping.size() << " line(s) from map file" << endl; Chris@10: } Chris@10: } Chris@10: Chris@10: char *fileName = strdup(argv[optind++]); Chris@10: char *fileNameOut = strdup(argv[optind++]); Chris@10: Chris@10: SNDFILE *sndfile; Chris@10: SNDFILE *sndfileOut; Chris@10: SF_INFO sfinfo; Chris@10: SF_INFO sfinfoOut; Chris@10: memset(&sfinfo, 0, sizeof(SF_INFO)); Chris@10: Chris@10: sndfile = sf_open(fileName, SFM_READ, &sfinfo); Chris@10: if (!sndfile) { Chris@10: cerr << "ERROR: Failed to open input file \"" << fileName << "\": " Chris@10: << sf_strerror(sndfile) << endl; Chris@10: return 1; Chris@10: } Chris@10: Chris@10: if (duration != 0.0) { Chris@10: if (sfinfo.frames == 0 || sfinfo.samplerate == 0) { Chris@10: cerr << "ERROR: File lacks frame count or sample rate in header, cannot use --duration" << endl; Chris@10: return 1; Chris@10: } Chris@10: double induration = double(sfinfo.frames) / double(sfinfo.samplerate); Chris@10: if (induration != 0.0) ratio = duration / induration; Chris@10: } Chris@10: Chris@10: sfinfoOut.channels = sfinfo.channels; Chris@10: sfinfoOut.format = sfinfo.format; Chris@10: sfinfoOut.frames = int(sfinfo.frames * ratio + 0.1); Chris@10: sfinfoOut.samplerate = sfinfo.samplerate; Chris@10: sfinfoOut.sections = sfinfo.sections; Chris@10: sfinfoOut.seekable = sfinfo.seekable; Chris@10: Chris@10: sndfileOut = sf_open(fileNameOut, SFM_WRITE, &sfinfoOut) ; Chris@10: if (!sndfileOut) { Chris@10: cerr << "ERROR: Failed to open output file \"" << fileNameOut << "\" for writing: " Chris@10: << sf_strerror(sndfileOut) << endl; Chris@10: return 1; Chris@10: } Chris@10: Chris@10: int ibs = 1024; Chris@10: size_t channels = sfinfo.channels; Chris@10: Chris@10: RubberBandStretcher::Options options = 0; Chris@10: if (realtime) options |= RubberBandStretcher::OptionProcessRealTime; Chris@10: if (precise) options |= RubberBandStretcher::OptionStretchPrecise; Chris@10: if (!lamination) options |= RubberBandStretcher::OptionPhaseIndependent; Chris@10: if (longwin) options |= RubberBandStretcher::OptionWindowLong; Chris@10: if (shortwin) options |= RubberBandStretcher::OptionWindowShort; Chris@10: if (smoothing) options |= RubberBandStretcher::OptionSmoothingOn; Chris@10: if (formant) options |= RubberBandStretcher::OptionFormantPreserved; Chris@10: if (hqpitch) options |= RubberBandStretcher::OptionPitchHighQuality; Chris@10: if (together) options |= RubberBandStretcher::OptionChannelsTogether; Chris@10: Chris@10: switch (threading) { Chris@10: case 0: Chris@10: options |= RubberBandStretcher::OptionThreadingAuto; Chris@10: break; Chris@10: case 1: Chris@10: options |= RubberBandStretcher::OptionThreadingNever; Chris@10: break; Chris@10: case 2: Chris@10: options |= RubberBandStretcher::OptionThreadingAlways; Chris@10: break; Chris@10: } Chris@10: Chris@10: switch (transients) { Chris@10: case NoTransients: Chris@10: options |= RubberBandStretcher::OptionTransientsSmooth; Chris@10: break; Chris@10: case BandLimitedTransients: Chris@10: options |= RubberBandStretcher::OptionTransientsMixed; Chris@10: break; Chris@10: case Transients: Chris@10: options |= RubberBandStretcher::OptionTransientsCrisp; Chris@10: break; Chris@10: } Chris@10: Chris@10: switch (detector) { Chris@10: case CompoundDetector: Chris@10: options |= RubberBandStretcher::OptionDetectorCompound; Chris@10: break; Chris@10: case PercussiveDetector: Chris@10: options |= RubberBandStretcher::OptionDetectorPercussive; Chris@10: break; Chris@10: case SoftDetector: Chris@10: options |= RubberBandStretcher::OptionDetectorSoft; Chris@10: break; Chris@10: } Chris@10: Chris@10: if (pitchshift != 0.0) { Chris@10: frequencyshift *= pow(2.0, pitchshift / 12); Chris@10: } Chris@10: Chris@10: cerr << "Using time ratio " << ratio; Chris@10: cerr << " and frequency ratio " << frequencyshift << endl; Chris@10: Chris@10: #ifdef _WIN32 Chris@10: RubberBand:: Chris@10: #endif Chris@10: timeval tv; Chris@10: (void)gettimeofday(&tv, 0); Chris@10: Chris@10: RubberBandStretcher::setDefaultDebugLevel(debug); Chris@10: Chris@10: RubberBandStretcher ts(sfinfo.samplerate, channels, options, Chris@10: ratio, frequencyshift); Chris@10: Chris@10: ts.setExpectedInputDuration(sfinfo.frames); Chris@10: Chris@10: float *fbuf = new float[channels * ibs]; Chris@10: float **ibuf = new float *[channels]; Chris@10: for (size_t i = 0; i < channels; ++i) ibuf[i] = new float[ibs]; Chris@10: Chris@10: int frame = 0; Chris@10: int percent = 0; Chris@10: Chris@10: sf_seek(sndfile, 0, SEEK_SET); Chris@10: Chris@10: if (!realtime) { Chris@10: Chris@10: if (!quiet) { Chris@10: cerr << "Pass 1: Studying..." << endl; Chris@10: } Chris@10: Chris@10: while (frame < sfinfo.frames) { Chris@10: Chris@10: int count = -1; Chris@10: Chris@10: if ((count = sf_readf_float(sndfile, fbuf, ibs)) <= 0) break; Chris@10: Chris@10: for (size_t c = 0; c < channels; ++c) { Chris@10: for (int i = 0; i < count; ++i) { Chris@10: float value = fbuf[i * channels + c]; Chris@10: ibuf[c][i] = value; Chris@10: } Chris@10: } Chris@10: Chris@10: bool final = (frame + ibs >= sfinfo.frames); Chris@10: Chris@10: ts.study(ibuf, count, final); Chris@10: Chris@10: int p = int((double(frame) * 100.0) / sfinfo.frames); Chris@10: if (p > percent || frame == 0) { Chris@10: percent = p; Chris@10: if (!quiet) { Chris@10: cerr << "\r" << percent << "% "; Chris@10: } Chris@10: } Chris@10: Chris@10: frame += ibs; Chris@10: } Chris@10: Chris@10: if (!quiet) { Chris@10: cerr << "\rCalculating profile..." << endl; Chris@10: } Chris@10: Chris@10: sf_seek(sndfile, 0, SEEK_SET); Chris@10: } Chris@10: Chris@10: frame = 0; Chris@10: percent = 0; Chris@10: Chris@10: if (!mapping.empty()) { Chris@10: ts.setKeyFrameMap(mapping); Chris@10: } Chris@10: Chris@10: size_t countIn = 0, countOut = 0; Chris@10: Chris@10: while (frame < sfinfo.frames) { Chris@10: Chris@10: int count = -1; Chris@10: Chris@10: if ((count = sf_readf_float(sndfile, fbuf, ibs)) < 0) break; Chris@10: Chris@10: countIn += count; Chris@10: Chris@10: for (size_t c = 0; c < channels; ++c) { Chris@10: for (int i = 0; i < count; ++i) { Chris@10: float value = fbuf[i * channels + c]; Chris@10: ibuf[c][i] = value; Chris@10: } Chris@10: } Chris@10: Chris@10: bool final = (frame + ibs >= sfinfo.frames); Chris@10: Chris@10: if (debug > 2) { Chris@10: cerr << "count = " << count << ", ibs = " << ibs << ", frame = " << frame << ", frames = " << sfinfo.frames << ", final = " << final << endl; Chris@10: } Chris@10: Chris@10: ts.process(ibuf, count, final); Chris@10: Chris@10: int avail = ts.available(); Chris@10: if (debug > 1) cerr << "available = " << avail << endl; Chris@10: Chris@10: if (avail > 0) { Chris@10: float **obf = new float *[channels]; Chris@10: for (size_t i = 0; i < channels; ++i) { Chris@10: obf[i] = new float[avail]; Chris@10: } Chris@10: ts.retrieve(obf, avail); Chris@10: countOut += avail; Chris@10: float *fobf = new float[channels * avail]; Chris@10: for (size_t c = 0; c < channels; ++c) { Chris@10: for (int i = 0; i < avail; ++i) { Chris@10: float value = obf[c][i]; Chris@10: if (value > 1.f) value = 1.f; Chris@10: if (value < -1.f) value = -1.f; Chris@10: fobf[i * channels + c] = value; Chris@10: } Chris@10: } Chris@10: // cout << "fobf mean: "; Chris@10: // double d = 0; Chris@10: // for (int i = 0; i < avail; ++i) { Chris@10: // d += fobf[i]; Chris@10: // } Chris@10: // d /= avail; Chris@10: // cout << d << endl; Chris@10: sf_writef_float(sndfileOut, fobf, avail); Chris@10: delete[] fobf; Chris@10: for (size_t i = 0; i < channels; ++i) { Chris@10: delete[] obf[i]; Chris@10: } Chris@10: delete[] obf; Chris@10: } Chris@10: Chris@10: if (frame == 0 && !realtime && !quiet) { Chris@10: cerr << "Pass 2: Processing..." << endl; Chris@10: } Chris@10: Chris@10: int p = int((double(frame) * 100.0) / sfinfo.frames); Chris@10: if (p > percent || frame == 0) { Chris@10: percent = p; Chris@10: if (!quiet) { Chris@10: cerr << "\r" << percent << "% "; Chris@10: } Chris@10: } Chris@10: Chris@10: frame += ibs; Chris@10: } Chris@10: Chris@10: if (!quiet) { Chris@10: cerr << "\r " << endl; Chris@10: } Chris@10: int avail; Chris@10: Chris@10: while ((avail = ts.available()) >= 0) { Chris@10: Chris@10: if (debug > 1) { Chris@10: cerr << "(completing) available = " << avail << endl; Chris@10: } Chris@10: Chris@10: if (avail > 0) { Chris@10: float **obf = new float *[channels]; Chris@10: for (size_t i = 0; i < channels; ++i) { Chris@10: obf[i] = new float[avail]; Chris@10: } Chris@10: ts.retrieve(obf, avail); Chris@10: countOut += avail; Chris@10: float *fobf = new float[channels * avail]; Chris@10: for (size_t c = 0; c < channels; ++c) { Chris@10: for (int i = 0; i < avail; ++i) { Chris@10: float value = obf[c][i]; Chris@10: if (value > 1.f) value = 1.f; Chris@10: if (value < -1.f) value = -1.f; Chris@10: fobf[i * channels + c] = value; Chris@10: } Chris@10: } Chris@10: Chris@10: sf_writef_float(sndfileOut, fobf, avail); Chris@10: delete[] fobf; Chris@10: for (size_t i = 0; i < channels; ++i) { Chris@10: delete[] obf[i]; Chris@10: } Chris@10: delete[] obf; Chris@10: } else { Chris@10: usleep(10000); Chris@10: } Chris@10: } Chris@10: Chris@10: sf_close(sndfile); Chris@10: sf_close(sndfileOut); Chris@10: Chris@10: if (!quiet) { Chris@10: Chris@10: cerr << "in: " << countIn << ", out: " << countOut << ", ratio: " << float(countOut)/float(countIn) << ", ideal output: " << lrint(countIn * ratio) << ", error: " << abs(lrint(countIn * ratio) - int(countOut)) << endl; Chris@10: Chris@10: #ifdef _WIN32 Chris@10: RubberBand:: Chris@10: #endif Chris@10: timeval etv; Chris@10: (void)gettimeofday(&etv, 0); Chris@10: Chris@10: etv.tv_sec -= tv.tv_sec; Chris@10: if (etv.tv_usec < tv.tv_usec) { Chris@10: etv.tv_usec += 1000000; Chris@10: etv.tv_sec -= 1; Chris@10: } Chris@10: etv.tv_usec -= tv.tv_usec; Chris@10: Chris@10: double sec = double(etv.tv_sec) + (double(etv.tv_usec) / 1000000.0); Chris@10: cerr << "elapsed time: " << sec << " sec, in frames/sec: " << countIn/sec << ", out frames/sec: " << countOut/sec << endl; Chris@10: } Chris@10: Chris@10: RubberBand::Profiler::dump(); Chris@10: Chris@10: return 0; Chris@10: } Chris@10: Chris@10: