Hi Fluidsynth developers,
I'm Hak from HcMusic, Taiwan.
We are making some optical pickups for string instruments(i.e. guitar,
violins) to do the midi stuff by installing our product, which is still in
the early stage.

We want to make a simple sampler to demo our product. Something like
playing an A4 note that will make a dog bark sample. We found that
fluidsynth is a great project that helps us to work out with prototyping
demo.

We had some problems with implying fluidsynth. We had tried functions like
new_fluid_sample(), readDataFromWav(), fluid_sample_set_sound_data(). After
that, we thought there should be sample sound when we ran the function
fluid_synth_noteon(). But not a noise came out.

Is there some simple example of how to make a simple sampler that just
loads a wavefile and assign to a note, and makes a sound when notion().

Below's attachment is a simple code that we tried, there might be plenty of
mistaken function calls. Hope this indicates what we had done wrong.

Thanks for your help.

Best regards,
Hak.

-- 
駭音科技有限公司

Founder/CEO
林季伯
ps2...@thehcmusic.com
+886 937626316
#include <cstdlib>
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <fluidsynth.h>

fluid_synth_t *synth;
fluid_audio_driver_t *audiodriver;
fluid_sequencer_t *sequencer;
fluid_sample_t *sample;
short synth_destination, client_destination;
unsigned int time_marker;
/* duration of the pattern in ticks. */
unsigned int duration = 1440;
/* notes of the arpeggio */
// unsigned int notes[] = { 60, 64, 67, 72, 76, 79, 84, 79, 76, 72, 67, 64 };
unsigned int notes[] = { 60 };
/* number of notes in one pattern */
unsigned int pattern_size;

struct riff_chunk_t{
    char ID[4];
    uint32_t size;
    char format[4]; 
};

#pragma pack(2)
struct subchunk1_t{
    char ID[4];
    uint32_t size;
    uint16_t format; 
};

struct wav_header_t{
    uint16_t numChannels;
    uint32_t sampleRate;
    uint32_t byteRate;
    uint16_t blockAlign;
    uint16_t bitsPerSample;
};

#pragma pack(4)
struct subchunk2_t{
    char ID[4];
    uint32_t size;
};

struct wav_data_t{
    riff_chunk_t riff;
    subchunk1_t sub1;
    wav_header_t header;
    subchunk2_t sub2;
    short int *datal;
};

wav_data_t readDataFromWav(const char *fileName){
    FILE *fin = fopen(fileName, "rb");

    //Read WAV head
    wav_data_t data;

    fread(&data.riff, sizeof(data.riff), 1, fin);
    fread(&data.sub1, sizeof(data.sub1), 1, fin);
    fread(&data.header, sizeof(data.header), 1, fin);
    fread(&data.sub2, sizeof(data.sub2), 1, fin);

    // Number of samples
    int sample_size = data.header.bitsPerSample /8;
    int samples_count = data.sub2.size * 8/ data.header.bitsPerSample;

    auto tmp = new int16_t [samples_count];
    data.datal = new int16_t [samples_count/2];

    // //Reading data
    fread(tmp, 1, samples_count*sample_size, fin);

    for(int i =0; i< samples_count; i+=2){
        data.datal[i/2] = tmp[i];
    }

    for(int i = 0; i< 10 ;i ++){
        printf("%d\n", data.datal[i]);
    }

    fclose(fin);
    return data;
}

int main(int argc, char *argv[]){
    int n;
    fluid_settings_t *settings;
    settings = new_fluid_settings();
    
    pattern_size = sizeof(notes) / sizeof(int);
    if(argc < 2){
        return 0;
    }
    else{
        /* create the synth, driver and sequencer instances */
        synth = new_fluid_synth(settings);
        /* load a SoundFont */
        n = fluid_synth_sfload(synth, argv[1], 1);
        if(n != -1){
            /* register the synth with the sequencer */
            synth_destination = fluid_sequencer_register_fluidsynth(sequencer, 
synth);
            sample = new_fluid_sample();

            auto wavData = 
readDataFromWav("doberman-pincher_daniel-simion.wav");
            printf("Sample number : %d\n", wavData.sub2.size/2 / 
(wavData.header.bitsPerSample/8));
            if(fluid_sample_set_sound_data(
                sample, wavData.datal, NULL, 
                wavData.sub2.size / 2 / (wavData.header.bitsPerSample/8), 
                wavData.header.sampleRate, 
                false
                ) != FLUID_OK){
                printf("Sample not loaded\n");
            }
            else{
                printf("Sample loaded\n");
            }

            auto mySampleVoice = fluid_synth_alloc_voice(synth,sample, 0, 60, 
60000);
            fluid_voice_gen_set(mySampleVoice, GEN_SAMPLEMODE, 0);
            fluid_synth_start_voice(synth, mySampleVoice);
            if(sample != NULL) printf("Sample not null\n");
            else printf("Sample is null\n");

            audiodriver = new_fluid_audio_driver(settings, synth);
            fluid_synth_noteon(synth, 0, 60, 127);
            printf("press <Enter> to stop\n");
            n = getchar();
        }
        /* clean and exit */
        delete_fluid_audio_driver(audiodriver);
        delete_fluid_sequencer(sequencer);
        delete_fluid_synth(synth);
    }
    delete_fluid_settings(settings);
    return 0;
}
_______________________________________________
fluid-dev mailing list
fluid-dev@nongnu.org
https://lists.nongnu.org/mailman/listinfo/fluid-dev

Reply via email to