stsaz / fmedia

fast audio player/recorder/converter
BSD 2-Clause "Simplified" License
216 stars 21 forks source link

Documentation for library usage #63

Closed llfbandit closed 11 months ago

llfbandit commented 2 years ago

First of all, thank you for working on this!

I'm digging to support a personal package for flutter to record audio from microphone "record". Using fmedia seems to fit my needs in multiples ways:

Maybe lacking of feedback to get amplitudes : avg & max, still discovering tough...

But... It's been years since I developed in C/C++, so I'm a bit lost from where to start to work with core library. So far, I can only see core_init as exported function.

Any help would appreciated to go in the right way.

stsaz commented 2 years ago

Hello! Please take a look at src/main.c - this is how you should initialize the core and add some playback/recording tasks to it with any available parameters. Having a separate tutorial document for this would be a waste of time because I will need to support it as well every time something changes.

llfbandit commented 2 years ago

Thanks for the answer. I'm not fully agree with documentation but OK I'll try to find myself what I need to achieve my recording tasks.

stsaz commented 2 years ago

Here's a simple example in pseudocode on how to add 1 mp3 file to fmedia's playback queue. I'll try to make a complete example (including recording tracks) as soon as I find more time. Until then, let's keep this issue open.

#include <fmedia.h>
// this code doesn't check for return codes and possible errors

static void start_jobs(void *udata)
{
    // add an MP3 file for playback
    fmed_que_entry entry = {};
    ffstr_setz(&entry.url, "/file.mp3");
    fmed_que_entry *added_entry = qu->add(&entry);

    // issue playback command
    qu->cmd(FMED_QUE_PLAY, added_entry);

    // after the function exits, the playback will start
}

void main()
{
    // open fmedia core library
    ffdl dl = ffdl_open("/fmedia/mod/core.so", 0);

    // import functions from core
    fmed_core* (*core_init)(char **argv, char **env);
    void (*core_free)(void);
    core_init = (void*)ffdl_addr(dl, "core_init");
    core_free = (void*)ffdl_addr(dl, "core_free");

    // get core interface
    fmed_core *core = core_init(argv, env);

    // load configuration file
    core->cmd(FMED_CONF, "/fmedia/fmedia.conf");

    // initialize core
    core->sig(FMED_OPEN);

    // get interface for 'queue' module
    const fmed_queue *qu = core->getmod("#queue.queue");

    // add the task which the core will execute
    fftask tsk = {};
    fftask_set(&tsk, &start_jobs, NULL);
    core->task(&tsk, FMED_TASK_POST);

    // Begin queued operations
    // The function returns only after FMED_STOP command is received
    core->sig(FMED_START);

    // issue this command at any time to stop the core's worker loop
    // core->sig(FMED_STOP);

    // free core's private data and close the library
    if (core != NULL) {
        core_free();
    }
    ffdl_close(dl);
}
llfbandit commented 2 years ago

I did not react to your answer back then, sorry for that.

With your example, I better see how to interact with the core interface.

FYI, in the meatime I published a first "crappy" version using SFML for windows...

Well, this was a comment to say I'm still in need of guidance. Thanks.

stsaz commented 2 years ago

I've prepared an example code for recording audio, it should work for all platforms. Every code block is explained by the comments, but please ask here if there's still something unclear. https://github.com/stsaz/fmedia/tree/master/doc/api-examples