Skip to content

Instantly share code, notes, and snippets.

@mckelvin
Created August 7, 2012 09:03
Show Gist options
  • Save mckelvin/3283555 to your computer and use it in GitHub Desktop.
Save mckelvin/3283555 to your computer and use it in GitHub Desktop.
two examples of `marsyas `
#include <string>
#include "marsyas/MarSystemManager.h"
#include "marsyas/SoundFileSource.h"
#include "marsyas/SoundFileSink.h"
#include "marsyas/Series.h"
using namespace std;
using namespace Marsyas;
void
sfplay(string inSfName = "sample.mp3",string outSfName = "sample.wav"){
MarSystemManager mng;
// Create a series Composite
MarSystem* series = mng.create("Series", "series");
series->addMarSystem(mng.create("SoundFileSource", "src"));
series->addMarSystem(mng.create("SoundFileSink", "dest"));
// only update controls from Composite level
series->updctrl("mrs_natural/inSamples", 64);
series->updctrl("SoundFileSource/src/mrs_string/filename", inSfName);
series->updctrl("SoundFileSink/dest/mrs_string/filename", outSfName);
while (series->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
{
series->tick();
}
delete series;
}
void //extract Spectral centroid
extract_centroid_feature(string sfName)
{
cout << "Centroid Feature Extraction for: " << sfName << endl;
MarSystemManager mng;
// Create a series Composite
MarSystem* series = mng.create("Series", "series");
series->addMarSystem(mng.create("SoundFileSource", "src"));
// hamming window -> complex spectrum -> power spectrum
series->addMarSystem(mng.create("Windowing", "hamming"));
series->addMarSystem(mng.create("Spectrum", "spk"));
series->addMarSystem(mng.create("PowerSpectrum", "pspk"));
series->updctrl("PowerSpectrum/pspk/mrs_string/spectrumType", "power");
series->addMarSystem(mng.create("Centroid", "centroid"));
// only update controls from Composite level
mrs_natural inSamples = 4096;
series->updctrl("mrs_natural/inSamples", inSamples);
series->updctrl("SoundFileSource/src/mrs_string/filename", sfName);
mrs_real srate = series->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
mrs_natural size = series->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>();
realvec in(series->getctrl("mrs_natural/inObservations")->to<mrs_natural>(),
series->getctrl("mrs_natural/inSamples")->to<mrs_natural>());
realvec out(series->getctrl("mrs_natural/onObservations")->to<mrs_natural>(),
series->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
int count = 0;
while (series->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
{
series->process(in,out);
cout << "centroid = " << out(0,0) << endl;
count ++;
}
//in_Count * in_Samples == channel * Sample_Rate * Seconds
cout << "in count: " << count << endl;
cout << "in sample rate: " << inSamples << endl;
cout << count << "*" << inSamples << "=" << count * inSamples <<endl;
cout << "size: " << size << endl;
cout << "sample rate: " << srate << endl;
int channel_count = 2;
cout << "time: " << channel_count * size/srate << endl;
}
int main(int argc, const char **argv){
string sound_file = "sample.mp3",
out_file = "sample.wav";
sfplay(sound_file, out_file);
extract_centroid_feature(sound_file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment