int runPlugin(string myname, string soname, string id,
              string output, int outputNo, string wavname,
              string outfilename, bool useFrames,
              map<string,float> parameters)
{
    PluginLoader *loader = PluginLoader::getInstance();

    PluginLoader::PluginKey key = loader->composePluginKey(soname, id);
    
    SNDFILE *sndfile;
    SF_INFO sfinfo;
    memset(&sfinfo, 0, sizeof(SF_INFO));

    if (wavname == "") 
        sndfile = sf_open_fd(0, SFM_READ, &sfinfo, true);
    else
        sndfile = sf_open(wavname.c_str(), SFM_READ, &sfinfo);
    if (!sndfile) {
        cerr << myname << ": ERROR: Failed to open input file \""
             << wavname << "\": " << sf_strerror(sndfile) << endl;
        return 1;
    }

    ofstream *out = 0;
    if (outfilename != "") {
        out = new ofstream(outfilename.c_str(), ios::out);
        if (!*out) {
            cerr << myname << ": ERROR: Failed to open output file \""
                 << outfilename << "\" for writing" << endl;
            delete out;
            return 1;
        }
    }

    Plugin *plugin = loader->loadPlugin
        (key, sfinfo.samplerate, PluginLoader::ADAPT_ALL_SAFE);
    if (!plugin) {
        cerr << myname << ": ERROR: Failed to load plugin \"" << id
             << "\" from library \"" << soname << "\"" << endl;
        sf_close(sndfile);
        if (out) {
            out->close();
            delete out;
        }
        return 1;
    }

    cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;

    // parameters
    for (map<string,float>::iterator iter = parameters.begin(); iter!=parameters.end(); iter++) {
        plugin->setParameter(iter->first, iter->second);
        cerr << "Set parameter " << iter->first << " = " << iter->second << endl;
    }
  
    // Note that the following would be much simpler if we used a
    // PluginBufferingAdapter as well -- i.e. if we had passed
    // PluginLoader::ADAPT_ALL to loader->loadPlugin() above, instead
    // of ADAPT_ALL_SAFE.  Then we could simply specify our own block
    // size, keep the step size equal to the block size, and ignore
    // the plugin's bleatings.  However, there are some issues with
    // using a PluginBufferingAdapter that make the results sometimes
    // technically different from (if effectively the same as) the
    // un-adapted plugin, so we aren't doing that here.  See the
    // PluginBufferingAdapter documentation for details.

    int blockSize = plugin->getPreferredBlockSize();
    int stepSize = plugin->getPreferredStepSize();

    if (blockSize == 0) {
        blockSize = 1024;
    }
    if (stepSize == 0) {
        if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
            stepSize = blockSize/2;
        } else {
            stepSize = blockSize;
        }
    } else if (stepSize > blockSize) {
        cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
        if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
            blockSize = stepSize * 2;
        } else {
            blockSize = stepSize;
        }
        cerr << blockSize << endl;
    }
    int overlapSize = blockSize - stepSize;
    sf_count_t currentStep = 0;
    int finalStepsRemaining = max(1, (blockSize / stepSize) - 1); // at end of file, this many part-silent frames needed after we hit EOF

    int channels = sfinfo.channels;

    float *filebuf = new float[blockSize * channels];
    float **plugbuf = new float*[channels];
    for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];

    cerr << "Using block size = " << blockSize << ", step size = "
              << stepSize << endl;

    // The channel queries here are for informational purposes only --
    // a PluginChannelAdapter is being used automatically behind the
    // scenes, and it will take case of any channel mismatch

    int minch = plugin->getMinChannelCount();
    int maxch = plugin->getMaxChannelCount();
    cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
    cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;

    Plugin::OutputList outputs = plugin->getOutputDescriptors();
    Plugin::OutputDescriptor od;

    int returnValue = 1;
    int progress = 0;

    RealTime rt;
    PluginWrapper *wrapper = 0;
    RealTime adjustment = RealTime::zeroTime;

    if (outputs.empty()) {
        cerr << "ERROR: Plugin has no outputs!" << endl;
        goto done;
    }

    if (outputNo < 0) {

        for (size_t oi = 0; oi < outputs.size(); ++oi) {
            if (outputs[oi].identifier == output) {
                outputNo = oi;
                break;
            }
        }

        if (outputNo < 0) {
            cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
            goto done;
        }

    } else {

        if (int(outputs.size()) <= outputNo) {
            cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
            goto done;
        }        
    }

    od = outputs[outputNo];
    cerr << "Output is: \"" << od.identifier << "\"" << endl;

    if (!plugin->initialise(channels, stepSize, blockSize)) {
        cerr << "ERROR: Plugin initialise (channels = " << channels
             << ", stepSize = " << stepSize << ", blockSize = "
             << blockSize << ") failed." << endl;
        goto done;
    }

    wrapper = dynamic_cast<PluginWrapper *>(plugin);
    if (wrapper) {
        // See documentation for
        // PluginInputDomainAdapter::getTimestampAdjustment
        PluginInputDomainAdapter *ida =
            wrapper->getWrapper<PluginInputDomainAdapter>();
        if (ida) adjustment = ida->getTimestampAdjustment();
    }
    
    // Here we iterate over the frames, avoiding asking the numframes in case it's streaming input.
    do {

        int count;

        if ((blockSize==stepSize) || (currentStep==0)) {
            // read a full fresh block
            if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
                cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
                break;
            }
            if (count != blockSize) --finalStepsRemaining;
        } else {
            //  otherwise shunt the existing data down and read the remainder.
            memmove(filebuf, filebuf + (stepSize * channels), overlapSize * channels * sizeof(float));
            if ((count = sf_readf_float(sndfile, filebuf + (overlapSize * channels), stepSize)) < 0) {
                cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
                break;
            }
            if (count != stepSize) --finalStepsRemaining;
            count += overlapSize;
        }

        for (int c = 0; c < channels; ++c) {
            int j = 0;
            while (j < count) {
                plugbuf[c][j] = filebuf[j * sfinfo.channels + c];
                ++j;
            }
            while (j < blockSize) {
                plugbuf[c][j] = 0.0f;
                ++j;
            }
        }

        rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);

        printFeatures
            (RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),
             sfinfo.samplerate, outputNo, plugin->process(plugbuf, rt),
             out, useFrames);

        if (sfinfo.frames > 0){
            int pp = progress;
            progress = (int)((float(currentStep * stepSize) / sfinfo.frames) * 100.f + 0.5f);
            if (progress != pp && out) {
                cerr << "\r" << progress << "%";
            }
        }

        ++currentStep;

    } while (finalStepsRemaining > 0);

    if (out) cerr << "\rDone" << endl;

    rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);

    printFeatures(RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),
                  sfinfo.samplerate, outputNo,
                  plugin->getRemainingFeatures(), out, useFrames);

    returnValue = 0;

done:
    delete plugin;
    if (out) {
        out->close();
        delete out;
    }
    sf_close(sndfile);
    return returnValue;
}
int runPlugin(string myname, string soname, string id,
              string output, int outputNo, bool useFrames, PaStreamParameters inputParameters)
{
    float				*recordedSamples;
    float				*fifo;
    PaStream*           stream;
    PaError             err = paNoError;
    int					elapsed = 0;
    int					returnValue = 1;
    RealTime			rt;
    PluginWrapper		*wrapper = 0;
    RealTime			adjustment = RealTime::zeroTime;
    PluginLoader		*loader = PluginLoader::getInstance();
    PluginLoader::PluginKey key = loader->composePluginKey(soname, id);

    // load plugin
    Plugin *plugin = loader->loadPlugin(key, SAMPLE_RATE, PluginLoader::ADAPT_ALL_SAFE);
    if (!plugin) {
        cerr << myname << ": ERROR: Failed to load plugin \"" << id
             << "\" from library \"" << soname << "\"" << endl;
        return 1;
    }

    // Find block/step size
    int blockSize = plugin->getPreferredBlockSize();
    int stepSize = plugin->getPreferredStepSize();

    if (blockSize == 0) {
        blockSize = 1024;
    }
    if (stepSize == 0) {
        if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
            stepSize = blockSize/2;
        } else {
            stepSize = blockSize;
        }
    } else if (stepSize > blockSize) {
        cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
        if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
            blockSize = stepSize * 2;
        } else {
            blockSize = stepSize;
        }
        cerr << blockSize << endl;
    }

    // set up port audio
    fifo = new float[blockSize]();
    recordedSamples = new float[stepSize]();

    ofstream *out = 0;
    cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;
    cerr << "Using block size = " << blockSize << ", step size = " << stepSize << endl;

    // display output name
    Plugin::OutputList outputs = plugin->getOutputDescriptors();
    Plugin::OutputDescriptor od;

    if (outputs.empty()) {
    	cerr << "ERROR: Plugin has no outputs!" << endl;
    	goto done;
    }

    if (outputNo < 0) {

        for (size_t oi = 0; oi < outputs.size(); ++oi) {
            if (outputs[oi].identifier == output) {
                outputNo = oi;
                break;
            }
        }

        if (outputNo < 0) {
            cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
            goto done;
        }

    } else {

        if (int(outputs.size()) <= outputNo) {
            cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
            goto done;
        }
    }

    od = outputs[outputNo];
    cerr << "Output is: \"" << od.identifier << "\"" << endl;

    // Initialise plugin
    if (!plugin->initialise(1, stepSize, blockSize)) {
        cerr << "ERROR: Plugin initialise (stepSize = " << stepSize << ", blockSize = "
             << blockSize << ") failed." << endl;
        goto done;
    }

    // Compensate timestamp if in freq domain
    wrapper = dynamic_cast<PluginWrapper *>(plugin);
    if (wrapper) {
        PluginInputDomainAdapter *ida =  wrapper->getWrapper<PluginInputDomainAdapter>();
        if (ida) adjustment = ida->getTimestampAdjustment();
    }

    // Open portaudio stream
    err = Pa_OpenStream(
    		&stream,
    		&inputParameters,
    		NULL,
    		SAMPLE_RATE,
    		stepSize,
    		paClipOff,
    		NULL,
    		NULL );
    if( err != paNoError ) throwError(err);

    // Start the audio stream
    err = Pa_StartStream( stream );
    if( err != paNoError ) throwError(err);
//    printf("Now recording!!\n"); fflush(stdout);

    // do until interruptFlag is true
    while (1)
    {
    	// read step of audio data
    	err = Pa_ReadStream( stream, recordedSamples, stepSize );
    	if( err != paNoError ) throwError(err);

    	// shift buffer along a step size
    	for (int i=stepSize; i<blockSize; i++) fifo[i-stepSize] = fifo[i];

    	// add new step onto end
    	for (int i=0; i<stepSize; i++) fifo[blockSize-stepSize+i] = recordedSamples[i];

    	// process and print features
    	rt = RealTime::frame2RealTime(elapsed*stepSize, SAMPLE_RATE);
    	printFeatures(RealTime::realTime2Frame(rt + adjustment, SAMPLE_RATE), SAMPLE_RATE, outputNo, plugin->process(&fifo, rt), out, useFrames);

    	// note number of blocks processed
    	elapsed++;

    	// break out of loop
    	if (interruptFlag) break;
    }

    // stop the audio stream
    err = Pa_CloseStream( stream );
    if( err != paNoError ) throwError(err);

    // clean up variables
    delete [] recordedSamples;
    delete [] fifo;

    returnValue = 0;

done:
    delete plugin;
    return returnValue;
}
Exemple #3
0
int main(int argc, char **argv)
{
    const char *myname = argv[0];

    if (argc != 2) {
	cerr << "usage: " << myname << " file.wav" << endl;
	return 2;
    }

    const char *infile = argv[1];

    SF_INFO sfinfo;
    SNDFILE *sndfile = sf_open(infile, SFM_READ, &sfinfo);

    if (!sndfile) {
	cerr << myname << ": Failed to open input file " << infile
	     << ": " << sf_strerror(sndfile) << endl;
	return 1;
    }

    Chordino *chordino = new Chordino(sfinfo.samplerate);
    PluginInputDomainAdapter *ia = new PluginInputDomainAdapter(chordino);
    ia->setProcessTimestampMethod(PluginInputDomainAdapter::ShiftData);
    PluginBufferingAdapter *adapter = new PluginBufferingAdapter(ia);

    int blocksize = adapter->getPreferredBlockSize();

    // Plugin requires 1 channel (we will mix down)
    if (!adapter->initialise(1, blocksize, blocksize)) {
	cerr << myname << ": Failed to initialise Chordino adapter!" << endl;
	return 1;
    }

    float *filebuf = new float[sfinfo.channels * blocksize];
    float *mixbuf = new float[blocksize];

    Plugin::FeatureList chordFeatures;
    Plugin::FeatureSet fs;

    int chordFeatureNo = -1;
    Plugin::OutputList outputs = adapter->getOutputDescriptors();
    for (int i = 0; i < int(outputs.size()); ++i) {
	if (outputs[i].identifier == "simplechord") {
	    chordFeatureNo = i;
	}
    }
    if (chordFeatureNo < 0) {
	cerr << myname << ": Failed to identify chords output!" << endl;
	return 1;
    }
    
    int frame = 0;
    while (frame < sfinfo.frames) {

	int count = -1;
	if ((count = sf_readf_float(sndfile, filebuf, blocksize)) <= 0) break;

	// mix down
	for (int i = 0; i < blocksize; ++i) {
	    mixbuf[i] = 0.f;
	    if (i < count) {
		for (int c = 0; c < sfinfo.channels; ++c) {
		    mixbuf[i] += filebuf[i * sfinfo.channels + c] / sfinfo.channels;
		}
	    }
	}

	RealTime timestamp = RealTime::frame2RealTime(frame, sfinfo.samplerate);
	
	// feed to plugin: can just take address of buffer, as only one channel
	fs = adapter->process(&mixbuf, timestamp);

	chordFeatures.insert(chordFeatures.end(),
			     fs[chordFeatureNo].begin(),
			     fs[chordFeatureNo].end());

	frame += count;
    }

    sf_close(sndfile);

    // features at end of processing (actually Chordino does all its work here)
    fs = adapter->getRemainingFeatures();

    // chord output is output index 0
    chordFeatures.insert(chordFeatures.end(),
			 fs[chordFeatureNo].begin(),
			 fs[chordFeatureNo].end());

    for (int i = 0; i < (int)chordFeatures.size(); ++i) {
	cout << chordFeatures[i].timestamp.toString() << ": "
	     << chordFeatures[i].label << endl;
    }

    delete[] filebuf;
    delete[] mixbuf;
    
    delete adapter;
}