libsndfile Emscripten environment

185 Views Asked by At

I am running just a little code using libsndfile, in the emscripten environment

#include <iostream>
#include <sndfile.h>

int main() 
{
    SF_INFO info;
    const char * path = "~/data/somefile.wav";
    SNDFILE* sf = sf_open(path,SFM_READ, &info);
    if(sf == NULL) 
    {
        std::cout<< sf_strerror(sf) << std::endl;
        return 1;
    }

    std::cout<<info.samplerate<<std::endl;
    
    std::cout<<"Hello world" << std::endl;
}

So ideally if I run this with normal cmake (Apple Clang compiler) everything works fine, the samplerate and hello world are printed, but when I run this with emcmake cmake (em++ compiler) and run the compiled node main.js file it says System error: no such file or directory. Who can help me with this? Who has experienced such thing?

1

There are 1 best solutions below

0
On

So I figured it out.

The problem is that Emscripten has its virtual file environment. So if you want this file to be uploaded and later be seen in compiled .js file, you need to add compile flag --preload-file <FILE_PATH> , after that the file with given path will be recognized by emscripten environment.