void MATLABengine::putVariable(const mrs_string value, mrs_string MATLABname) { //----------------------------------- //send C/C++ string to MATLAB string //----------------------------------- mxArray *mxVector = mxCreateString(value.c_str()); engPutVariable(engine_, MATLABname.c_str(), mxVector); mxDestroyArray(mxVector); }
void MP3FileSink::putHeader(mrs_string filename) { #ifdef MARSYAS_LAME sfp_ = fopen(filename.c_str(), "wb"); #endif }
void AuFileSink::putHeader(mrs_string filename) { mrs_natural nChannels = (mrs_natural)getctrl("mrs_natural/inObservations")->to<mrs_natural>(); written_ = 0; const char *comment = "MARSYAS 2001, George Tzanetakis.\n"; mrs_natural commentSize = strlen(comment); sfp_ = fopen(filename.c_str(), "wb"); hdr_->pref[0] = '.'; hdr_->pref[1] = 's'; hdr_->pref[2] = 'n'; hdr_->pref[3] = 'd'; #if defined(MARSYAS_BIGENDIAN) hdr_->hdrLength = 24 + commentSize; hdr_->fileLength = 0; hdr_->mode = SND_FORMAT_LINEAR_16; hdr_->srate = (mrs_natural)getctrl("mrs_real/israte")->to<mrs_real>(); hdr_->channels = nChannels; #else hdr_->hdrLength = ByteSwapLong(24 + (unsigned long)commentSize); hdr_->fileLength = ByteSwapLong(0); hdr_->mode = ByteSwapLong(SND_FORMAT_LINEAR_16); hdr_->srate = ByteSwapLong((mrs_natural)getctrl("mrs_real/israte")->to<mrs_real>()); hdr_->channels = ByteSwapLong(nChannels); #endif fwrite(hdr_, 24, 1, sfp_); // Write comment part of header fwrite(comment, commentSize, 1, sfp_); sfp_begin_ = ftell(sfp_); }
//debug helper funtion to dump table to an ascii file void WekaData::Dump(const mrs_string& filename, const vector<mrs_string>& classNames) const { char buffer[32]; ofstream *mis = new ofstream; mis->open(filename.c_str(), ios_base::out | ios_base::trunc ); MRSASSERT( mis->is_open() ); for(vector<vector<mrs_real>*>::const_iterator citer = this->begin(); citer!=this->end(); citer++) { bool first = true; const vector<mrs_real> *row = (*citer); int ii; for(ii=0; ii<(int)row->size()-1; ++ii) { if(!first) mis->write(", ", 2); first = false; sprintf(buffer, "%09.4f", row->at(ii)); mis->write(buffer, strlen(buffer)); } mis->write(", ", 2); mrs_natural classIndex = (mrs_natural)row->at(ii); mis->write(classNames[classIndex].c_str(), strlen(classNames[classIndex].c_str())); mis->write("\n", 1); } mis->close(); delete mis; }//Dump
void Collection::read(mrs_string filename) { ifstream is(filename.c_str()); name_ = filename.substr(0, filename.rfind(".", filename.length())); is >> (*this); }
bool realvec::read(mrs_string filename) { ifstream from(filename.c_str()); if (from.is_open()) { from >> (*this); return true; }
void MATLABengine::putVariable(const double *const value, unsigned int size, mrs_string MATLABname) { //----------------------------------- //send C/C++ vector to MATLAB vector //----------------------------------- mwSize dims[2]; dims[0] = 1; //row vector dims[1] = size; mxArray *mxVector = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); memcpy(mxGetData(mxVector), (void *)value, size*mxGetElementSize(mxVector)); engPutVariable(engine_, MATLABname.c_str(), mxVector); mxDestroyArray(mxVector); }
void MATLABengine::putVariable(mrs_complex value, mrs_string MATLABname) { mwSize dims[2]; dims[0] = 1; dims[1] = 1; mxArray *mxVector = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxCOMPLEX); double *xr = mxGetPr(mxVector); double *xi = mxGetPi(mxVector); *xr = (double)value.real(); *xi = (double)value.imag(); engPutVariable(engine_, MATLABname.c_str(), mxVector); mxDestroyArray(mxVector); }
void MATLABengine::putVariable(realvec value, mrs_string MATLABname) { //---------------------------------- // send a realvec to a MATLAB matrix //---------------------------------- mwSize dims[2]; //realvec is 2D dims[0] = value.getRows(); dims[1] = value.getCols(); //realvec are by default double precision matrices => mxDOUBLE_CLASS mxArray *mxMatrix = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); mrs_real *data = value.getData(); memcpy((void *)mxGetPr(mxMatrix), (void *)(data), dims[0]*dims[1]*mxGetElementSize(mxMatrix)); engPutVariable(engine_, MATLABname.c_str(), mxMatrix); mxDestroyArray(mxMatrix); }
void MATLABengine::putVariable(vector<mrs_real> value, mrs_string MATLABname) { mwSize dims[2]; dims[0] = 1; //row vector dims[1] = (mwSize)value.size(); mxArray *mxVector = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL); double *x = mxGetPr(mxVector); for(unsigned int i = 0; i < value.size(); ++i) { *(x + i) = (double)value[i]; } engPutVariable(engine_, MATLABname.c_str(), mxVector); mxDestroyArray(mxVector); }
void MATLABengine::putVariable(const float *const value, unsigned int size, mrs_string MATLABname) { //----------------------------------- //send C/C++ vector to MATLAB vector //----------------------------------- mwSize dims[2]; dims[0] = 1; //row vector dims[1] = size; mxArray *mxVector = mxCreateNumericArray(2, dims, mxSINGLE_CLASS, mxREAL); memcpy(mxGetData(mxVector), (void *)value, size*mxGetElementSize(mxVector)); engPutVariable(engine_, MATLABname.c_str(), mxVector); //Convert to MATLAB double type mrs_string MatCmd = MATLABname + "=double(" + MATLABname + ");"; engEvalString(engine_, MatCmd.c_str()); mxDestroyArray(mxVector); }
void ViconFileSource::getHeader(mrs_string filename) { // Need to read Vicon File Header to get number and name of markers vfp_ = fopen(filename.c_str(), "r"); if (vfp_) { // read first line from file char buffer[4096]; fgets(buffer, 4096, vfp_); stringstream line(buffer); char entry[256]; fileObs_ = 0; while (line.getline(entry, 256, ',')) { fileObs_++; } setctrl("mrs_natural/onObservations", fileObs_); setctrl("mrs_string/markers", buffer); } }
void Talk::cmd_segment(mrs_string systemName, unsigned int memSize, unsigned int numPeaks, unsigned int peakSpacing, unsigned int start, unsigned int end, unsigned int winSize) { // FIXME Unused parameters (void) memSize; (void) numPeaks; (void) peakSpacing; (void) start; (void) end; (void) winSize; TimeLine tline; mrs_natural hops = src_->getctrl("mrs_natural/size")->to<mrs_natural>() * src_->getctrl("mrs_natural/nChannels")->to<mrs_natural>() / src_->getctrl("mrs_natural/inSamples")->to<mrs_natural>() + 1; if(!strcmp(systemName.c_str(), "REG")) tline.regular(100, hops); realvec peaks(hops); tline.send(communicator_); peaks.send(communicator_); // tline.print(stdout); // cerr << "cmd_segment::systemName " << systemName << endl; // cerr << "cmd_segment::memSize " << memSize << endl; // cerr << "cmd_segment::numPeaks " << numPeaks << endl; // cerr << "cmd_segment::peakSpacing " << peakSpacing << endl; // cerr << "cmd_segment::start " << start << endl; // cerr << "cmd_segment::end " << end << endl; // cerr << "cmd_segment::winSize " << winSize << endl; // mrs_string extractorstr = systemName; // mrs_string rextractorstr = systemName; // if (!strcmp(rextractorstr.c_str(), "REG")) // extractorstr = "FFT_SEGM"; // if (winSize != DEFAULT_WIN_SIZE) // { // start = (unsigned int)(start * ((float)winSize / DEFAULT_WIN_SIZE)); // end = (unsigned int) (end * ((float)winSize/ DEFAULT_WIN_SIZE)); // winSize = DEFAULT_WIN_SIZE; // } // src_->initWindow(winSize); // cerr << "Src winSize = " << src_->winSize() << endl; // Spectral spectral(src_); // SpectralSegm spectralsegm(src_,10); // MemMFCC mfcc(src_); // FeatExtractor mfccExtractor(src_, &mfcc); // FeatExtractor spectralExtractor(src_, &spectral); // FeatExtractor segmExtractor(src_, &spectralsegm); // SfxExtractor sfxExtractor(src_); // FeatMatrix mfccRes(src_->iterations(), mfcc.outSize()); // FeatMatrix spectralRes(src_->iterations(), spectral.outSize()); // FeatMatrix spectralSegmRes(src_->iterations(), spectralsegm.outSize()); // FeatMatrix sfxRes(1, 2); // map<const char *, FeatMatrix *, ltstr> results; // results["FFT"] = &spectralRes; // results["FFT_SEGM"] = &spectralSegmRes; // results["MFCC"] = &mfccRes; // results["SFX"] = &sfxRes; // map<const char *, Extractor *, ltstr> extractors; // extractors["FFT"] = &spectralExtractor; // extractors["FFT_SEGM"] = &segmExtractor; // extractors["MFCC"] = &mfccExtractor; // extractors["SFX"] = &sfxExtractor; // map<const char *, Extractor *, ltstr>::iterator cur; // const char *ch = extractorstr.c_str(); // cur = extractors.find(ch); // if (cur == extractors.end()) // { // cerr << "Extractor " << extractorstr << " is not supported\n" << endl; // return; // } // else // { // extractors[extractorstr.c_str()]->extract(*(results[extractorstr.c_str()])); // } // TimeLine tline; // SegmentorSortedPeaks segmentor; // segmentor.init(numPeaks, peakSpacing); // fvec res((*(results[extractorstr.c_str()])).rows()); // fvec peaks((*(results[extractorstr.c_str()])).rows()); // segmentor.segment(*(results[extractorstr.c_str()]), res); // segmentor.peaks(*(results[extractorstr.c_str()]), peaks); }
void pitchextract_caricature(mrs_string sfName, mrs_natural winSize, mrs_natural hopSize, mrs_real lowPitch, mrs_real highPitch, mrs_real threshold, mrs_bool playPitches, mrs_string ofName) { (void) winSize; (void) threshold; MRSDIAG("pitchextract.cpp - pitchextract"); MarSystemManager mng; // Build pitch contour extraction network MarSystem* pitchContour = mng.create("Series", "pitchContour"); MarSystem* pitchExtractor = mng.create("Series", "pitchExtractor"); pitchExtractor->addMarSystem(mng.create("SoundFileSource", "src")); pitchExtractor->addMarSystem(mng.create("Stereo2Mono", "s2m")); if (mode == "praat") { pitchExtractor->addMarSystem(mng.create("PitchPraat", "pitchPraat")); } else { pitchExtractor->addMarSystem(mng.create("PitchSACF", "pitchSACF")); } pitchExtractor->updControl("SoundFileSource/src/mrs_string/filename", sfName); mrs_natural fileSize; fileSize= pitchExtractor->getctrl("SoundFileSource/src/mrs_natural/size")->to<mrs_natural>(); mrs_natural contourSize = fileSize / hopSize; // Accumulate the extracted pitches and confidences in a single vector // of size contourSize MarSystem* pitchAccumulator = mng.create("Accumulator", "pitchAccumulator"); pitchAccumulator->addMarSystem(pitchExtractor); pitchAccumulator->updControl("mrs_natural/nTimes", contourSize); pitchContour->addMarSystem(pitchAccumulator); pitchExtractor->updControl("mrs_natural/inSamples", hopSize); mrs_real srate = pitchExtractor->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>(); ofstream ofs1; ofs1.open("p.mpl"); ofs1 << *pitchExtractor << endl; ofs1.close(); // Using explicit loop mrs_natural len = contourSize; mrs_realvec pitches(len); mrs_realvec confidences(len); mrs_realvec chords(len); mrs_realvec booms(len); mrs_realvec chicks(len); vector<mrs_string> chord_names; mrs_realvec pitchres; mrs_realvec peak_in; ofstream ofs; ofs.open(ofName.c_str()); for (int i=0; i < contourSize; ++i) { pitchExtractor->tick(); pitchres = pitchExtractor->getctrl("mrs_realvec/processedData")->to<mrs_realvec>(); confidences(i) = pitchres(0); pitches(i) = samples2hertz(pitchres(1), srate); // cout << "Pitch = " << pitches(i) << "- (conf) - " << confidences(i) << endl; float scaled_pitch = pitches(i); if (frsopt == "bark") { scaled_pitch = hertz2bark(pitches(i)); } if (frsopt == "mel") { scaled_pitch = hertz2mel(pitches(i),1); } if (frsopt == "midi") { scaled_pitch = hertz2pitch(pitches(i)); } if (pitches(i) <= pitch2hertz(lowPitch)) { // confidences(i) = 0.0; pitches(i) += 12; } if (pitches(i) >= pitch2hertz(highPitch)) { pitches(i) -= 12; // confidences(i) = 0.0; } ofs << scaled_pitch << endl; /* peak_in = pitchExtractor->getctrl("PitchPraat/pitchPraat/AutoCorrelation/acr/mrs_realvec/processedData")->to<mrs_realvec>(); mrs_natural pos = pitchExtractor->getctrl("SoundFileSource/src/mrs_natural/pos")->to<mrs_natural>(); MATLAB_PUT(peak_in, "peak_in"); MATLAB_PUT(pos, "pos"); MATLAB_EVAL("plot(peak_in); title(num2str(pos));"); getchar(); */ } // Normalize confidence to 0-1 range confidences.normMaxMin(); ofs.close(); // Optionally plot the pitches #ifdef MARSYAS_MATLAB mrs_realvec foo(len); MATLAB_PUT(confidences, "confidences"); MATLAB_PUT(pitches, "pitches"); MATLAB_EVAL("plot(confidences)"); cerr << "Enter any character to continue" << endl; getchar(); MATLAB_EVAL("a = pitches .* pitches;"); MATLAB_GET("a", foo); MATLAB_EVAL("plot(a)"); getchar(); MATLAB_EVAL("plot(pitches)"); cerr << "Enter any character to continue" << endl; getchar(); MATLAB_CLOSE(); #endif // extract chords MarSystem* chordExtract = mng.create("Series/chordExtract"); chordExtract->addMarSystem(mng.create("SoundFileSource/src")); chordExtract->addMarSystem(mng.create("Windowing/win")); chordExtract->addMarSystem(mng.create("Spectrum/spk")); chordExtract->addMarSystem(mng.create("PowerSpectrum/pspk")); chordExtract->addMarSystem(mng.create("Spectrum2Chroma/s2c")); chordExtract->addMarSystem(mng.create("Memory/mem")); chordExtract->addMarSystem(mng.create("Mean/mean")); chordExtract->addMarSystem(mng.create("Krumhansl_key_finder/kkf")); chordExtract->updControl("mrs_natural/inSamples", hopSize); chordExtract->updControl("Memory/mem/mrs_natural/memSize", 200); chordExtract->updControl("SoundFileSource/src/mrs_string/filename", sfName); for (int i=0; i < contourSize; ++i) { chordExtract->tick(); chords(i) = chordExtract->getControl("Krumhansl_key_finder/kkf/mrs_natural/key")->to<mrs_natural>(); cout << "chords(i) = " << chords(i) << endl; chord_names.push_back(chordExtract->getControl("Krumhansl_key_finder/kkf/mrs_string/key_name")->to<mrs_string>()); } // extra boom-chick pattern // high and low bandpass filters MarSystem *filters = mng.create("Fanout", "filters"); realvec al(5),bl(5); al(0) = 1.0; al(1) = -3.9680; al(2) = 5.9062; al(3) = -3.9084; al(4) = 0.9702; bl(0) = 0.0001125; bl(1) = 0.0; bl(2) = -0.0002250; bl(3) = 0.0; bl(4) = 0.0001125; MarSystem *lfilter = mng.create("Series", "lfilter"); lfilter->addMarSystem(mng.create("Filter", "llfilter")); lfilter->updControl("Filter/llfilter/mrs_realvec/ncoeffs", bl); lfilter->updControl("Filter/llfilter/mrs_realvec/dcoeffs", al); MarSystem *lowpkr = mng.create("PeakerAdaptive","lowpkr"); lowpkr->updControl("mrs_real/peakSpacing", 0.3); lowpkr->updControl("mrs_real/peakStrength", 0.95); lowpkr->updControl("mrs_natural/peakStart", 0); lowpkr->updControl("mrs_natural/peakEnd", hopSize); lowpkr->updControl("mrs_real/peakGain", 1.0); lowpkr->updControl("mrs_natural/peakStrengthReset", 4); lowpkr->updControl("mrs_real/peakDecay", 0.99); lfilter->addMarSystem(lowpkr); realvec ah(5),bh(5); ah(0) = 1.0; ah(1) = -3.5797; ah(2) = 4.9370; ah(3) = -3.1066; ah(4) = 0.7542; bh(0) = 0.0087; bh(1) = 0.0; bh(2) = -0.0174; bh(3) = 0; bh(4) = 0.0087; MarSystem *hfilter = mng.create("Series", "hfilter"); hfilter->addMarSystem(mng.create("Filter", "hhfilter")); hfilter->addMarSystem(mng.create("Gain", "gain")); hfilter->updControl("Filter/hhfilter/mrs_realvec/ncoeffs", bh); hfilter->updControl("Filter/hhfilter/mrs_realvec/dcoeffs", ah); MarSystem* hipkr = mng.create("PeakerAdaptive", "hipkr"); hipkr->updControl("mrs_real/peakSpacing", 0.5); hipkr->updControl("mrs_real/peakStrength", 0.90); hipkr->updControl("mrs_natural/peakStart", 0); hipkr->updControl("mrs_natural/peakEnd", hopSize); hipkr->updControl("mrs_real/peakGain", 1.0); hipkr->updControl("mrs_natural/peakStrengthReset", 4); hipkr->updControl("mrs_real/peakDecay", 0.99); hfilter->addMarSystem(hipkr); filters->addMarSystem(lfilter); filters->addMarSystem(hfilter); MarSystem* boomchickExtract = mng.create("Series/boomchickExtract"); boomchickExtract->addMarSystem(mng.create("SoundFileSource/src")); boomchickExtract->addMarSystem(filters); boomchickExtract->addMarSystem(mng.create("SoundFileSink/dest")); boomchickExtract->updControl("mrs_natural/inSamples", hopSize); boomchickExtract->updControl("SoundFileSource/src/mrs_string/filename", sfName); boomchickExtract->updControl("SoundFileSink/dest/mrs_string/filename", "boom_chick.wav"); for (int i=0; i < contourSize; ++i) { boomchickExtract->tick(); booms(i) = boomchickExtract->getControl("Fanout/filters/Series/lfilter/PeakerAdaptive/lowpkr/mrs_bool/peakFound")->to<mrs_bool>(); chicks(i) = boomchickExtract->getControl("Fanout/filters/Series/hfilter/PeakerAdaptive/hipkr/mrs_bool/peakFound")->to<mrs_bool>(); } // Playback the pitches if (playPitches) { MarSystem* playback = mng.create("Series/playback"); MarSystem* mix = mng.create("Fanout/mix"); MarSystem* ch0 = mng.create("Series/ch0"); ch0->addMarSystem(mng.create("SineSource/ss")); ch0->addMarSystem(mng.create("Gain/sinegain")); MarSystem* ch1 = mng.create("Series/ch1"); ch1->addMarSystem(mng.create("SoundFileSource/src")); ch1->addMarSystem(mng.create("Gain/soundgain")); MarSystem* ch2 = mng.create("Series/ch2"); ch2->addMarSystem(mng.create("SineSource/ss")); ch2->addMarSystem(mng.create("Gain/sinegain")); MarSystem* ch3 = mng.create("Series/ch3"); ch3->addMarSystem(mng.create("SoundFileSource/bdsrc")); ch3->addMarSystem(mng.create("Gain/bdsrcgain")); MarSystem* ch4 = mng.create("Series/ch4"); ch4->addMarSystem(mng.create("SoundFileSource/sdsrc")); ch4->addMarSystem(mng.create("Gain/sdsrcgain")); mix->addMarSystem(ch0); mix->addMarSystem(ch1); mix->addMarSystem(ch2); // mix->addMarSystem(ch3); // mix->addMarSystem(ch4); playback->addMarSystem(mix); playback->addMarSystem(mng.create("Sum/sum")); playback->addMarSystem(mng.create("Gain", "g")); playback->addMarSystem(mng.create("AudioSink", "dest")); playback->addMarSystem(mng.create("SoundFileSink/dest")); playback->updControl("mrs_natural/inSamples", hopSize); playback->updControl("mrs_real/israte", srate); playback->updControl("AudioSink/dest/mrs_bool/initAudio", true); playback->updControl("mrs_real/israte", pitchContour->getctrl("mrs_real/osrate")); playback->updControl("SoundFileSink/dest/mrs_string/filename", "caricature.wav"); playback->updControl("Fanout/mix/Series/ch1/SoundFileSource/src/mrs_string/filename", sfName); playback->updControl("Fanout/mix/Series/ch3/SoundFileSource/bdsrc/mrs_string/filename", "bd22k.wav"); playback->updControl("Fanout/mix/Series/ch4/SoundFileSource/sdsrc/mrs_string/filename", "sd22k.wav"); for (int i=0; i < len; ++i) { playback->updControl("Fanout/mix/Series/ch0/SineSource/ss/mrs_real/frequency", pitches(i)); cout << "pitches(i) = " << pitches(i) << endl; cout << "chords(i) = " << chords(i) << endl; if (chords(i) > 12) chords(i) -= 12; cout << "chords(i) = " << chords(i) << endl; playback->updControl("Fanout/mix/Series/ch2/SineSource/ss/mrs_real/frequency", pitch2hertz(57 + chords(i))); cout << "hertz = = " << pitch2hertz(57 + chords(i)) << endl; playback->updControl("Fanout/mix/Series/ch0/Gain/sinegain/mrs_real/gain", 0.5 ); playback->updControl("Fanout/mix/Series/ch1/Gain/soundgain/mrs_real/gain", 0.15); playback->updControl("Fanout/mix/Series/ch2/Gain/sinegain/mrs_real/gain", 0.5); playback->tick(); #ifdef MARSYAS_OSC #define ADDRESS "127.0.0.1" #define PORT 7000 #define OUTPUT_BUFFER_SIZE 1024 UdpTransmitSocket transmitSocket( IpEndpointName( ADDRESS, PORT ) ); char buffer[OUTPUT_BUFFER_SIZE]; #endif if (booms(i)) { playback->updControl("Fanout/mix/Series/ch3/SoundFileSource/bdsrc/mrs_natural/pos",0); #ifdef MARSYAS_OSC osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE ); p << osc::BeginBundleImmediate << osc::BeginMessage( "/notomoton" ) << 1 << 127 << osc::EndMessage; transmitSocket.Send(p.Data(), p.Size()); #endif } if (chicks(i)) playback->updControl("Fanout/mix/Series/ch4/SoundFileSource/sdsrc/mrs_natural/pos",0); // cout << chord_names[i] << endl; } delete playback; } delete pitchExtractor; }
int main(int argc, const char **argv) { MRSDIAG("pitchextract.cpp - main"); string progName = argv[0]; initOptions(); cmd_options.readOptions(argc, argv); loadOptions(); vector<string> soundfiles = cmd_options.getRemaining(); if (helpopt) printHelp(progName); if (usageopt) printUsage(progName); // If the user didn't specify the filename to extract, show the // usage information. if (argc < 2) printUsage(progName); // cout << "PitchExtract windowSize = " << wopt << endl; // cout << "PitchExtract hopSize = " << hopt << endl; // cout << "PitchExtract lowerPitch = " << lpopt << endl; // cout << "PitchExtract upperPitch = " << upopt << endl; // cout << "PitchExtract threshold = " << topt << endl; // cout << "PitchExtract playback = " << plopt << endl; vector<string>::iterator sfi; for (sfi = soundfiles.begin(); sfi != soundfiles.end(); ++sfi) { string sfname = *sfi; cout << "Processing: " << sfname << endl; FileName fn(sfname); if (fn.ext() != "mf") { if (mode == "sacf" || mode == "praat") { pitchextract(sfname, wopt, hopt, lpopt, upopt, topt, plopt != 0, ofnameopt); } else if (mode == "yin") { yinpitchextract(sfname, wopt, hopt, plopt != 0, ofnameopt); } else if (mode == "caricature") { pitchextract_caricature(sfname, wopt, hopt, lpopt, upopt, topt, plopt != 0, ofnameopt); } else if (mode == "key") { ofstream ofs; ofs.open(output_fname.c_str()); int prediction = pitchextract_key(sfname, wopt, hopt, lpopt, upopt, topt, plopt != 0, ofnameopt); vector<string> key_names; key_names.push_back("A"); key_names.push_back("Bb"); key_names.push_back("B"); key_names.push_back("C"); key_names.push_back("C#"); key_names.push_back("D"); key_names.push_back("Eb"); key_names.push_back("E"); key_names.push_back("F"); key_names.push_back("F#"); key_names.push_back("G"); key_names.push_back("G#"); if (prediction < 12) { cout << key_names[prediction] << "\t" << "major" << endl; ofs << key_names[prediction] << "\t" << "major" << endl; } else { cout << key_names[prediction-12] << "\t" << "minor" << endl; ofs << key_names[prediction-12] << "\t" << "minor" << endl; } } else { cout << "Unsupported pitch extraction mode (" << mode << ")" << endl; printUsage(progName); } } else { Collection l; l.read(sfname); int correct_predictions = 0; int predictions = 0; for (unsigned int i=0; i < l.size(); i++) { FileName fn(l.entry(i)); sfname = l.entry(i); mrs_string ofname = fn.nameNoExt() + ".txt"; cout << ofname << endl; cout << "GT = " << l.labelEntry(i); if (mode == "sacf" || mode == "praat") { pitchextract(sfname, wopt, hopt, lpopt, upopt, topt, plopt != 0, ofname); } else if (mode == "yin") { yinpitchextract(sfname, wopt, hopt, plopt != 0, ofname); } else if (mode == "key") { int predicted = pitchextract_key(sfname, wopt, hopt, lpopt, upopt, topt, plopt != 0, ofnameopt); cout << " PR = " << predicted << endl; if (predicted == atoi(l.labelEntry(i).c_str())) correct_predictions++; predictions++; cout << "Correct Predictions = " << correct_predictions * 1.0 / predictions << endl; } else { cout << "Unsupported pitch extraction mode (" << mode << ")" << endl; printUsage(progName); } } } } exit(0); }
void Collection::write(mrs_string filename) { ofstream os(filename.c_str()); os << (*this) << endl; }
void Filter::write(mrs_string filename) { ofstream os(filename.c_str()); os << (*this) << endl; }
void predict(mrs_string mode) { MarSystemManager mng; cout << "Predicting using " << trainedclassifier_ << endl; ifstream pluginStream(trainedclassifier_.c_str()); MRS_WARNINGS_OFF; MarSystem* net = mng.getMarSystem(pluginStream); MRS_WARNINGS_ON; if (!twekafname_Set()) return; vector<string> classNames; string s = net->getctrl("WekaSource/wsrc/mrs_string/classNames")->to<mrs_string>(); char *str = (char *)s.c_str(); char * pch; pch = strtok (str,","); classNames.push_back(pch); while (pch != NULL) { pch = strtok (NULL, ","); if (pch != NULL) classNames.push_back(pch); } //////////////////////////////////////////////////////////// // // Predict the classes of the test data // net->updControl("WekaSource/wsrc/mrs_string/filename", twekafname_); net->updControl("Classifier/cl/mrs_string/mode", "predict"); //////////////////////////////////////////////////////////// // // Tick over the test WekaSource until all lines in the // test file have been read. // ofstream prout; prout.open(predictcollectionfname_.c_str()); ofstream prtout; prtout.open(predicttimeline_.c_str()); realvec data; int end=0; int start=0; mrs_string prev_name = ""; mrs_string name; mrs_real srate; while (!net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>()) { net->tick(); data = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>(); srate = net->getctrl("WekaSource/wsrc/mrs_real/currentSrate")->to<mrs_real>(); if (mode == "default") { cout << net->getctrl("WekaSource/wsrc/mrs_string/currentFilename")->to<mrs_string>() << "\t"; cout << classNames[(int)data(0,0)] << endl; prout << net->getctrl("WekaSource/wsrc/mrs_string/currentFilename")->to<mrs_string>() << "\t"; prout << classNames[(int)data(0,0)] << endl; } else if (mode == "timeline") { name = classNames[(int)data(0,0)]; if (name != prev_name) { if ((end * (1.0/srate)-start*(1.0 / srate) > minspan_)) { if (predicttimeline_ == EMPTYSTRING) { cout << start*(1.0 / srate) << "\t" << end*(1.0 / srate) << "\t"; cout << prev_name << endl; } else { prtout << start*(1.0 / srate) << "\t" << end*(1.0 / srate) << "\t"; prtout << prev_name << endl; } } start = end; } else { } prev_name = name; } else cout << "Unsupported mode" << endl; // cout << data(0,0) << endl; end++; } prout.close(); // cout << "DONE" << endl; // sness - hmm, I really should be able to delete net, but I get a // coredump when I do. Maybe I need to destroy something else first? // delete net; }
void train_classifier() { if (!wekafname_Set()) return; wekafname_ = inputdir_ + wekafname_; cout << "Training classifier using .arff file: " << wekafname_ << endl; cout << "Classifier type : " << classifier_ << endl; MarSystemManager mng; //////////////////////////////////////////////////////////// // // The network that we will use to train and predict // MarSystem* net = mng.create("Series", "series"); //////////////////////////////////////////////////////////// // // The WekaSource we read the train and test .arf files into // net->addMarSystem(mng.create("WekaSource", "wsrc")); //////////////////////////////////////////////////////////// // // The classifier // MarSystem* classifier = mng.create("Classifier", "cl"); net->addMarSystem(classifier); //////////////////////////////////////////////////////////// // // Which classifier function to use // if (classifier_ == "GS") net->updControl("Classifier/cl/mrs_string/enableChild", "GaussianClassifier/gaussiancl"); if (classifier_ == "ZEROR") net->updControl("Classifier/cl/mrs_string/enableChild", "ZeroRClassifier/zerorcl"); if (classifier_ == "SVM") net->updControl("Classifier/cl/mrs_string/enableChild", "SVMClassifier/svmcl"); //////////////////////////////////////////////////////////// // // The training file we are feeding into the WekaSource // net->updControl("WekaSource/wsrc/mrs_string/filename", wekafname_); net->updControl("mrs_natural/inSamples", 1); //////////////////////////////////////////////////////////// // // Set the classes of the Summary and Classifier to be // the same as the WekaSource // net->updControl("Classifier/cl/mrs_natural/nClasses", net->getctrl("WekaSource/wsrc/mrs_natural/nClasses")); net->updControl("Classifier/cl/mrs_string/mode", "train"); //////////////////////////////////////////////////////////// // // Tick over the training WekaSource until all lines in the // training file have been read. // while (!net->getctrl("WekaSource/wsrc/mrs_bool/done")->to<mrs_bool>()) { string mode = net->getctrl("WekaSource/wsrc/mrs_string/mode")->to<mrs_string>(); net->tick(); net->updControl("Classifier/cl/mrs_string/mode", mode); } ofstream clout; clout.open(trainedclassifier_.c_str()); net->updControl("Classifier/cl/mrs_string/mode", "predict"); clout << *net << endl; cout << "Done training " << endl; }
void AuFileSource::getHeader(mrs_string filename) { if (sfp_ != NULL) fclose(sfp_); sfp_ = fopen(filename.c_str(), "rb"); if (sfp_) { mrs_natural n = fread(hdr_, sizeof(snd_header), 1, sfp_); if ((n != 1) ||((hdr_->pref[0] != '.') &&(hdr_->pref[1] != 's'))) { MRSWARN("Filename " + filename + " is not correct .au file \n or has settings that are not supported in Marsyas"); setctrl("mrs_natural/onObservations", (mrs_natural)1); setctrl("mrs_real/israte", (mrs_real)22050.0); setctrl("mrs_natural/size", (mrs_natural)0); hasData_ = false; lastTickWithData_ = true; setctrl("mrs_bool/hasData", false); setctrl("mrs_bool/lastTickWithData", true); } else { #if defined(MARSYAS_BIGENDIAN) hdr_->hdrLength = hdr_->hdrLength; hdr_->comment[hdr_->hdrLength-24] = '\0'; hdr_->srate = hdr_->srate; hdr_->channels = hdr_->channels; hdr_->mode = hdr_->mode; hdr_->fileLength = hdr_->fileLength; #else hdr_->hdrLength = ByteSwapLong(hdr_->hdrLength); hdr_->comment[hdr_->hdrLength-24] = '\0'; hdr_->srate = ByteSwapLong(hdr_->srate); hdr_->channels = ByteSwapLong(hdr_->channels); hdr_->mode = ByteSwapLong(hdr_->mode); hdr_->fileLength = ByteSwapLong(hdr_->fileLength); #endif sampleSize_ = 2; size_ = (hdr_->fileLength) / sndFormatSizes_[hdr_->mode] / hdr_->channels; // csize_ = size_ * hdr_->channels; csize_ = size_; fseek(sfp_, hdr_->hdrLength, 0); sfp_begin_ = ftell(sfp_); setctrl("mrs_natural/onObservations", (mrs_natural)hdr_->channels); setctrl("mrs_real/israte", (mrs_real)hdr_->srate); setctrl("mrs_natural/size", size_); ctrl_currentlyPlaying_->setValue(filename, NOUPDATE); ctrl_previouslyPlaying_->setValue(filename, NOUPDATE); ctrl_currentLabel_->setValue(0.0, NOUPDATE); ctrl_previousLabel_->setValue(0.0, NOUPDATE); ctrl_labelNames_->setValue(",", NOUPDATE); ctrl_nLabels_->setValue(0, NOUPDATE); setctrl("mrs_bool/hasData", true); hasData_ = true; lastTickWithData_ = false; samplesOut_ = 0; pos_ = 0; setctrl("mrs_natural/pos", 0); } } else { setctrl("mrs_natural/onObservations", (mrs_natural)1); setctrl("mrs_real/israte", (mrs_real)22050.0); setctrl("mrs_natural/size", (mrs_natural)0); hasData_ = false; setctrl("mrs_bool/hasData", false); lastTickWithData_ = true; setctrl("mrs_bool/lastTickWithData", true); pos_ = 0; } nChannels_ = getctrl("mrs_natural/onObservations")->to<mrs_natural>(); samplesRead_ = 0; }
void MP3FileSink::putHeader(mrs_string filename) { sfp_ = fopen(filename.c_str(), "wb"); }
void outputSpectrogramPNG(string inFileName, string outFileName) { cout << "SPECTROGRAM " << endl; #ifdef MARSYAS_PNG double fftBins = windowSize_ / 2.0 + 1; // N/2 + 1 double min = 99999999999.9; double max = -99999999999.9; double average; int length = getFileLengthForSpectrogram(inFileName,min,max,average); MarSystemManager mng; MarSystem* net = mng.create("Series", "net"); net->addMarSystem(mng.create("SoundFileSource", "src")); net->addMarSystem(mng.create("Stereo2Mono", "s2m")); net->addMarSystem(mng.create("ShiftInput", "si")); net->addMarSystem(mng.create("Spectrum","spk")); net->addMarSystem(mng.create("PowerSpectrum","pspk")); net->updControl("PowerSpectrum/pspk/mrs_string/spectrumType", "decibels"); net->updControl("SoundFileSource/src/mrs_string/filename", inFileName); net->updControl("SoundFileSource/src/mrs_natural/pos", position_); net->updControl("SoundFileSource/src/mrs_natural/inSamples", hopSize_); net->updControl("ShiftInput/si/mrs_natural/winSize", windowSize_); net->updControl("mrs_natural/inSamples", int(hopSize_)); mrs_real frequency = net->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>(); double pngLength = length; mrs_natural nChannels = net->getctrl("SoundFileSource/src/mrs_natural/onObservations")->to<mrs_natural>(); double pngHeight = fftBins * ((highFreq_-lowFreq_) / (frequency / nChannels)); if (verboseopt_) { cout << "highFreq_ = " << highFreq_ << endl; cout << "lowFreq_ = " << lowFreq_ << endl; cout << "fftBins = " << fftBins << endl; cout << "pngLength = " << pngLength << endl; cout << "pngHeight = " << pngHeight << endl; cout << "width = " << width_ << endl; cout << "height = " << height_ << endl; } pngwriter png(int(pngLength),int(pngHeight),0,outFileName.c_str()); realvec processedData; double normalizedData; // Iterate over the whole input file by ticking, outputting columns // of data to the .png file with each tick double x = 0; double y = 0; double colour = 0; double diff; double pdiff; while (net->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>() && (ticks_ == -1 || x < ticks_)) { net->tick(); processedData = net->getctrl("mrs_realvec/processedData")->to<mrs_realvec>(); diff = 0.0; for (int i = 0; i < pngHeight; ++i) { double data_y = i; double data = processedData(int(data_y),0); normalizedData = ((data - min) / (max - min)) * gain_; diff += normalizedData; // Make the spectrogram black on white instead of white on black // TODO - Add the ability to generate different color maps, like Sonic Visualiser colour = 1.0 - normalizedData; if (colour > 1.0) { colour = 1.0; } if (colour < 0.0) { colour = 0.0; } y = i; png.plot(int(x),int(y),colour,colour,colour); } /* if (fabs(pdiff-diff) > 4.0) for (int i=0; i < 20; i++) png.plot(int(x),pngHeight- i, 1.0, 0.0, 0.0); */ pdiff = diff; x++; } png.plot_text(const_cast<char *>(fontfile_.c_str()), 12, 20, 20, 0.0, "THIS IS A SPECTROGRAM", 1.0, 0.0, 0.0); if (width_ !=-1) pngLength = width_; if (height_ != -1) pngHeight = height_; if ((width_ !=-1)||(height_ != -1)) { png.scale_wh(pngLength, pngHeight); } png.close(); delete net; #endif }
bool TimeLine::load(mrs_string filename, mrs_string lexicon_labels) { ifstream in; filename_ = filename; if(filename == "") return false; in.open(filename.c_str()); if(!in.is_open()) { MRSWARN("TimeLine::load() - Problem opening file " << filename_); return false; } FileName f(filename); vector<mrs_string> labels; // Load lexicon dictionary if available mrs_string lexicon_label; mrs_string remainder; size_t nLabels; nLabels = std::count(lexicon_labels.begin(), lexicon_labels.end(), ','); if (lexicon_labels != ",") { for (size_t i=0; i < nLabels; i++) { lexicon_label = lexicon_labels.substr(0, lexicon_labels.find(",")); labels.push_back(lexicon_label); sort(labels.begin(), labels.end()); remainder = lexicon_labels.substr(lexicon_labels.find(",") + 1, lexicon_labels.length()); lexicon_labels = remainder; } } else nLabels = 0; if (f.ext() == "txt") // audacity label format { numRegions_ = 0; mrs_real start, end; mrs_string label; regions_.clear(); while (!in.eof()) { in >> start >> end >> label; TimeRegion region; region.start = (mrs_natural) (start * srate_); region.end = (mrs_natural) (end * srate_); region.classId = 1; region.name = label; mrs_bool label_found = false; for (unsigned int i=0; i < labels.size(); i++) { if (label == labels[i]) { label_found = true; region.classId = i; } } if (!label_found) { if (lexicon_labels == ",") { labels.push_back(label); sort(labels.begin(), labels.end()); } } regions_.push_back(region); numRegions_ ++; } // relabel classIds so that they correspond to sorted labels for (mrs_natural i=0; i < numRegions_; ++i) { mrs_string label = regions_[i].name; vector<mrs_string>::iterator it = find(labels.begin(), labels.end(), label); if (it == labels.end()) regions_[i].classId = (mrs_natural)-1; mrs_natural l = distance(labels.begin(), it); regions_[i].classId = l; } // last region is a duplicate due to empty last line // kind of a hack but works numRegions_ --; regions_.pop_back(); lineSize_ = 1; size_ = (mrs_natural) (end * srate_); in.close(); return true; }
void GStreamerSource::getHeader(mrs_string filename) { if(!pipe_created_) { init_pipeline(); if(!pipe_created_) { // Pipe creation failed size_ = -1; return; } } /* (Re)Set the uridecodebin's filename */ gst_element_set_state(pipe_, GST_STATE_NULL); if(gst_uri_is_valid(filename.c_str())) { /* "filename" is already a valid uri, just use it as-is */ g_object_set(G_OBJECT(dec_), "uri", filename.c_str(), NULL); } else { g_object_set(G_OBJECT(dec_), "uri", gst_filename_to_uri(filename.c_str(), NULL), NULL); } /* Set to PAUSED so we can get a preroll buffer */ GstStateChangeReturn ret = gst_element_set_state(pipe_, GST_STATE_PAUSED); if(ret == GST_STATE_CHANGE_FAILURE) { MRSERR("GStreamer pipeline failed to change state. This could be due to an invalid filename"); } GstSample *sample = gst_app_sink_pull_preroll(GST_APP_SINK(sink_)); /* Grab the sample's caps so we can get some useful info */ GstCaps *samp_caps = gst_sample_get_caps(sample); GstStructure *samp_struct = gst_caps_get_structure(samp_caps, 0); /* Get sample rate from buffer */ gint rate; if(gst_structure_get_int(samp_struct, "rate", &rate)) { osrate_ = (mrs_real)rate; } /* Get number of channels from buffer */ gint channels; if(gst_structure_get_int(samp_struct, "channels", &channels)) { onObservations_ = channels; } /* Get the rough size of the audio. Use GST_FORMAT_TIME since it works with most file types */ gint64 duration; GstFormat format = GST_FORMAT_TIME; // Force blocking on state change completion gst_element_get_state(pipe_, NULL, NULL, GST_SECOND); if (gst_element_query_duration(GST_ELEMENT(pipe_), format, &duration) && format==GST_FORMAT_TIME) { size_ = (mrs_natural)(duration*1e-9*osrate_ + 0.5); } else { /* GStreamer can't tell us the size of the stream */ MRSWARN("Query Duration failed:"); size_ = -1; } /* Reset these in case this isn't our first song */ pos_ = 0; hasData_ = true; lastTickWithData_ = false; /* Clean up */ gst_caps_unref(samp_caps); /* Set Controls */ //setctrl("mrs_real/israte", osrate_); setctrl("mrs_real/osrate", osrate_); setctrl("mrs_natural/onObservations", onObservations_); //setctrl("mrs_natural/inObservations", onObservations_); setctrl("mrs_natural/size", size_); setctrl("mrs_natural/pos", pos_); ctrl_hasData_->setValue(hasData_); ctrl_lastTickWithData_->setValue(lastTickWithData_); ctrl_currentHasData_->setValue(hasData_); ctrl_currentLastTickWithData_->setValue(lastTickWithData_); /* Start playing, so queue fills with buffers [Should we do this?] */ gst_element_set_state(pipe_, GST_STATE_PLAYING); playing_ = true; // Should we check if set_state worked before doing this? }