#include #include #include #include #include #include #include using namespace std; using namespace essentia; using namespace essentia::standard; extern "C" { JNIEXPORT jint JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv *env, jobject thiz, jstring infilename, jstring outfilename); }; JNIEXPORT jint JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI(JNIEnv *env, jobject thiz, jstring infilename, jstring outfilename) { essentia::init(); Pool pool; const char *audioFilename = env->GetStringUTFChars(infilename, 0); const char *resultFilename = env->GetStringUTFChars(outfilename, 0); /////// PARAMS ////////////// int sampleRate = 44100; int frameSize = 2048; int hopSize = 1024; AlgorithmFactory& factory = standard::AlgorithmFactory::instance(); Algorithm *audio = factory.create("MonoLoader", "filename", audioFilename, "sampleRate", sampleRate); __android_log_write(ANDROID_LOG_INFO, "Tag", "C++: Audio created successfully."); Algorithm* fc = factory.create("FrameCreator", "frameSize", frameSize, "hopSize", hopSize); Algorithm* w = factory.create("Windowing", "type", "blackmanharris62"); Algorithm* spec = factory.create("Spectrum"); Algorithm* mfcc = factory.create("MFCC"); /////////// CONNECTING THE ALGORITHMS //////////////// // Audio -> FrameCutter std::vector audioBuffer; audio->output("audio").set(audioBuffer); fc->input("signal").set(audioBuffer); // FrameCutter -> Windowing -> Spectrum std::vector frame, windowedFrame; fc->output("frame").set(frame); w->input("signal").set(frame); w->output("windowedSignal").set(windowedFrame); spec->input("signal").set(windowedFrame); // Spectrum -> MFCC std::vector spectrum, mfccCoeffs, mfccBands; spec->output("spectrum").set(spectrum); mfcc->input("spectrum").set(spectrum); mfcc->output("bands").set(mfccBands); mfcc->output("mfcc").set(mfccCoeffs); /////////// STARTING THE ALGORITHMS ////////////////// audio->compute(); while (true) { // compute a frame fc->compute(); // if it was the last one (ie: it was empty), then we're done. if (!frame.size()) { break; } // if the frame is silent, just drop it and go on processing if (isSilent(frame)) continue; w->compute(); spec->compute(); mfcc->compute(); pool.add("lowlevel.mfcc", mfccCoeffs); } // aggregate the results Pool aggrPool; // the pool with the aggregated MFCC values const char* stats[] = { "mean", "var", "min", "max" }; Algorithm* aggr = AlgorithmFactory::create("PoolAggregator", "defaultStats", arrayToVector(*stats)); aggr->input("input").set(pool); aggr->output("output").set(aggrPool); aggr->compute(); Algorithm* output = AlgorithmFactory::create("YamlOutput", "filename", resultFilename); output->input("pool").set(pool); output->compute(); delete audio; delete fc; delete w; delete spec; delete mfcc; delete output; essentia::shutdown(); return 1; }