swapnilgt

Untitled

Apr 11th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. JNIEXPORT jint JNICALL Java_com_musicmuni_fluidsynth_FluidSynth_getFileFromMIDIAndSoundFont
  2.         (JNIEnv *env, jobject obj, jstring outFile, jstring midiFile, jstring soundfontFile,
  3.          jint sampleRate) {
  4.  
  5.     const char *outFileChar = env->GetStringUTFChars(outFile, 0);
  6.     const char *midiFileChar = env->GetStringUTFChars(midiFile, 0);
  7.     const char *soundfontFileChar = env->GetStringUTFChars(soundfontFile, 0);
  8.  
  9.     fluid_settings_t* settings;
  10.     fluid_synth_t* synth;
  11.     fluid_player_t* player;
  12.     fluid_file_renderer_t* renderer;
  13.     settings = new_fluid_settings();
  14.  
  15.     // specify the file to store the audio to
  16.     // make sure you compiled fluidsynth with libsndfile to get a real wave file
  17.     // otherwise this file will only contain raw s16 stereo PCM
  18.     int success = fluid_settings_setstr(settings, "audio.file.name", outFileChar);
  19.     if(FLUID_OK == success) {
  20.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in setting the out file name\n");
  21.     } else {
  22.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in setting the out file name\n");
  23.         return 0;
  24.     }
  25.  
  26.     // Setting the sample rate
  27.     success = fluid_settings_setnum(settings, "synth.sample-rate", sampleRate);
  28.     if(FLUID_OK == success) {
  29.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in setting sample rate\n");
  30.     } else {
  31.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in setting the sample rate\n");
  32.         return 0;
  33.     }
  34.  
  35.     // use number of samples processed as timing source, rather than the system timer
  36.     success = fluid_settings_setstr(settings, "player.timing-source", "sample");
  37.     if(FLUID_OK == success) {
  38.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in setting timing source\n");
  39.     } else {
  40.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in setting the timing source\n");
  41.         return 0;
  42.     }
  43.  
  44.     // since this is a non-realtime szenario, there is no need to pin the sample data
  45.     success = fluid_settings_setint(settings, "synth.lock-memory", 0);
  46.     if(FLUID_OK == success) {
  47.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in setting lock memory\n");
  48.     } else {
  49.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in setting the lock memory\n");
  50.         return 0;
  51.     }
  52.  
  53.     synth = new_fluid_synth(settings);
  54.  
  55.     // Setting the soundfont ..
  56.     success = fluid_synth_sfload(synth, soundfontFileChar, 0);
  57.     if(FLUID_FAILED == success) {
  58.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in setting the soundfont file\n");
  59.         return 0;
  60.     } else {
  61.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in setting the soundfont file\n");
  62.     }
  63.  
  64.     player = new_fluid_player(synth);
  65.     success = fluid_player_add(player, midiFileChar);
  66.     if(FLUID_OK == success) {
  67.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in fluid_player_add\n");
  68.     } else {
  69.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in fluid_player_add\n");
  70.         return 0;
  71.     }
  72.  
  73.  
  74.     success = fluid_player_play(player);
  75.     if(FLUID_OK == success) {
  76.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Successful in fluid_player_play\n");
  77.     } else {
  78.         __android_log_print(ANDROID_LOG_ERROR, "NDK_LOG","Unable in fluid_player_play\n");
  79.         return 0;
  80.     }
  81.  
  82.     renderer = new_fluid_file_renderer (synth);
  83.     while (fluid_player_get_status(player) == FLUID_PLAYER_PLAYING)
  84.     {
  85.         __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","FLUID_PLAYER_PLAYING\n");
  86.         if (fluid_file_renderer_process_block(renderer) != FLUID_OK)
  87.         {
  88.             break;
  89.         } else {
  90.             __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","fluid_file_renderer_process_block != FLUID_OK\n");
  91.         }
  92.     }
  93.  
  94.     // just for sure: stop the playback explicitly and wait until finished
  95.     fluid_player_stop(player);
  96.     fluid_player_join(player);
  97.     delete_fluid_file_renderer(renderer);
  98.     delete_fluid_player(player);
  99.     delete_fluid_synth(synth);
  100.     delete_fluid_settings(settings);
  101.  
  102.     env->ReleaseStringUTFChars(outFile, outFileChar);
  103.     env->ReleaseStringUTFChars(midiFile, midiFileChar);
  104.     env->ReleaseStringUTFChars(soundfontFile, soundfontFileChar);
  105.  
  106.     __android_log_print(ANDROID_LOG_VERBOSE, "NDK_LOG","Success in going over the function for generating file\n");
  107.  
  108.     return 1;
  109.  
  110. }
Add Comment
Please, Sign In to add comment