divideconcept / FluidLite

Very light version of FluidSynth designed to be hardware, platform and external dependency independent.
Other
133 stars 36 forks source link

How to play sound ? #22

Open HectorRicardo opened 4 years ago

HectorRicardo commented 4 years ago

Co you have an example where there is actual sound output from the synthesizer?

If I am not wrong, the code in example/main.c produces an output file of float32output.pcm . I am guessing this is the output raw data. How can I play this sound ?

I am on Windows 10, and I compiled and ran the example/main.c with Cygwin.

Thanks

HectorRicardo commented 4 years ago

My question on SO: https://stackoverflow.com/questions/63431072/how-to-use-the-fluidlite-synthesizer

divideconcept commented 4 years ago

Playing sound to the speakers is beyond the scope of FluidLite, as described in the README: "FluidLite keeps very minimal functionnalities (settings and synth), therefore MIDI file reading, realtime MIDI events and audio output must be implemented externally."

You can use the RtAudio library to output sound for instance: https://github.com/thestk/rtaudio

HectorRicardo commented 4 years ago

Hey @divideconcept, thanks for your quick response.

Yes, I know that audio output is beyond the scope of this project. In fact, I didn't want to open an issue because this is not an issue, but I didn't find anywhere else to post the question.

May I do a suggestion for your project?

I am looking to play midi notes in an Android app. I have looked for Midi synthesizers around the web and found these projects:

https://github.com/billthefarmer/mididriver - this one works great, there's a sample app ready to go, but there's no way to change the default soundbank https://github.com/KyoSherlock/MidiDriver - the soundfount can be changed on this one, but there's some performance and lagging issues. It is totally written in java and not in C, so performance is affected. https://github.com/FluidSynth/fluidsynth - I think this one works, but I did not succeed in how to make it compile and work in Android https://github.com/VolcanoMobile/fluidsynth-android - This one should be easier to compile, but I am still stuck.

I think that your project might do the job: a lite version of FluidSynth with ability to change soundbanks and without performance issues. However, I don't have experience in building and compiling C application, so I am kind of stuck.

If you could provide a sample Android app that plays a simple midi note (like in BillTheFramer's repo), I think more people would gravitate towards your repo, since it will offer user friendliness, relief to the developer of having to know how to compile, build and link C code, and flexibility to change soundbanks.

Just my two cents, I really thank you for having this project!

Ghabry commented 4 years ago

There are ports of fluidsynth to Android, you probably want one of them as they are more feature complete (they support MIDI playing and audio output - Both stuff that is out of scope in fluidlite)

Audio output is highly platform dependent and a complex topic so I can understand why there are zero examples about this. For using Fludlite I recommend that you look at SDL2 Audio example code. In SDL_OpenAudioDevice register a callback function. This callback function is called when the audio buffer is exhausted. In that callback ask fluidlite to write audio in the buffer.

For just targeting Android without SDL2 you likely have to do something like exporting fluidlite API through JNI, copying the audio buffer to Java and playing it through the Android Audio API (no idea how to do this). This is also annoying and hard to get right.

coderofsalvation commented 3 years ago

I succesfully got it to work using SOKOL (https://github.com/floooh/sokol) which can also compile to ANDROID:

  // INIT
  #define SAMPLE_SIZE sizeof(float)
  #define NUM_SAMPLES (64)  // FLUID_BUFSIZE is 64
  fluid_synth_t* synth;
  float* flbuffer;
  int audio_channels;

  saudio_setup(saudio_desc{0});
  audio_channels = saudio_channels();
  fluid_settings_t* settings = new_fluid_settings();
  synth = new_fluid_synth(settings);
  fluid_synth_sfload(synth, "C:\\Users\\You\\Desktop\\test.sf2", 1);
  flbuffer = (float *)calloc( SAMPLE_SIZE, NUM_SAMPLES );

  fluid_synth_noteon(synth, 0, 60, 127);
  printf("triggered note\n");

...

    // MAIN LOOP
    int num_frames  = saudio_expect();
      if( num_frames > 0  ){
        for( int i = 0; i < num_frames; i+= NUM_SAMPLES ){
          fluid_synth_write_float(synth, NUM_SAMPLES, flbuffer, 0, audio_channels, flbuffer, 0, audio_channels ); // optional: change to stereo
          saudio_push(flbuffer, NUM_SAMPLES );
        }
      }

My AHA moment was that there's many ways to forward audio inside C, but in the end it boils down to understanding the audiobuffer (sizes) of the parent process and the child process (fluid).

netpipe commented 3 years ago

would be nice to have an alsa example of the same or sdl sound

netpipe commented 3 years ago

https://github.com/netpipe/emscripten-projects/blob/master/SDL/SDL_sound/playsound/playsound_simple.c

https://gist.github.com/ghedo/963382/815c98d1ba0eda1b486eb9d80d9a91a81d995283

https://stackoverflow.com/questions/68414260/playing-pcm-audio-wav-on-linux

netpipe commented 3 years ago

ALSA-example.zip working great :)

netpipe commented 2 years ago

openaldemo.c.zip -- needs work still in the buffer converter

netpipe commented 2 years ago

sdldemo.cpp.zip -- still needs work

netpipe commented 2 years ago

https://stackoverflow.com/questions/39997137/how-do-i-make-a-qaudiooutput-object-play-from-a-buffer

https://github.com/netpipe/qCow-Synth/ trying a bit with qt

netpipe commented 2 years ago

qFluidlite.zip https://github.com/netpipe/qCow-Synth/tree/main/qCOW2 for simultanious keypresses and a mini piano

netpipe commented 2 years ago

sdldemo.cpp.zip working

netpipe commented 2 years ago

OpenalDemo.cpp.zip sdl2_mixer_wav.cpp.zip

working now too

netpipe commented 2 years ago

might make a plugin for cAudio next. also raycasted reverb

netpipe commented 2 years ago

https://github.com/netpipe/cAudio my fork of caudio that can play fluidlite in 3d check examples folder for example 10

netpipe commented 2 years ago

https://github.com/netpipe/AgAudio

GeoffreyPlitt commented 11 months ago

@netpipe Do you have a tutorial for putting it all together? I'm interested in combining fluidlite with realtime audio playback on a Raspberry Pi Pico, or SAMD51 or something.

netpipe commented 11 months ago

you will need a microchip that supports linux or windows.

GeoffreyPlitt commented 11 months ago

@netpipe Do you think it can run on a microcontroller (Raspberry Pi Pico or SAMD51) that have no OS, just bare metal? Since this variant of FluidSynth has few dependencies and is all static, I was hoping to run it on one of those minimal chips.

netpipe commented 11 months ago

its not designed to because you'll need sd card library and other stuff to make filesystem work.