PABannier / bark.cpp

Suno AI's Bark model in C/C++ for fast text-to-speech
MIT License
630 stars 48 forks source link

C-API for other programming languages? #166

Closed hjkoskel closed 1 month ago

hjkoskel commented 2 months ago

It would be nice if bark.cpp had C-api like in whisper.cpp have. Even it is written in C++. C-api would enable bindings for to use library directly from other languages like golang, zig etc...

bindings in whisper.cpp

Could C api be like this? I like idea to having able to set random generator seed on each generate command if needed.

binding.h

#ifdef __cplusplus

#include <vector>
#include <string>
#include "bark.h"

extern "C" {
#endif

int LoadBarkModelFromFile(char *modelfilename, void **ctx);
int FreeBarkModel(void *ctx);
int GenerateSound(void *ctx,char *prompt,int32_t seed,int n_threads,float **out);

#ifdef __cplusplus
}
#endif

binding.c

#include "binding.h"

int LoadBarkModelFromFile(char *modelfilename, void **ctx){
    ctx[0]=bark_load_model(modelfilename, bark_verbosity_level::LOW,0);
    return 0;
}

int GenerateSound(void *ctx,char *prompt,int32_t seed,int n_threads,float **out){
    std::string txt(prompt);
    struct bark_context *b= (struct bark_context*)ctx;
    if (0<seed){
        b->rng = std::mt19937(seed);
    }
    if (!bark_generate_audio(b, txt,  n_threads)) {
        return -1;
    }
    out[0]= &b->audio_arr[0];
    return b->audio_arr.size();
}

int FreeBarkModel(void *ctx){
    bark_free((struct bark_context*)ctx);
    return 0;
}
PABannier commented 2 months ago

@hjkoskel This is a great idea. Can you open a PR?

I would opt for the following signatures (in snake case to respect the writing style):

int bark_load_model_from_file(char *fname, void **ctx);
int bark_generate_audio_from_prompt(void *ctx, char *prompt, int32_t seed, int n_threads, float **out);
int bark_free_model(void *ctx);
PABannier commented 1 month ago

Hello @hjkoskel ! I just merged #170 which make bark.h a C-header. If you generate bindings for another language, feel free to open a PR, so that we can add them to the repo.