mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Uncaught RuntimeError: memory access out of bounds after starting playing the sound. #816

Closed oregu1 closed 9 months ago

oregu1 commented 9 months ago

Greetings @mackron.

Sir I hope for your help. There is an issue while running built project and I can't understand what I'm doing wrong.

When game starts the sound begins to play. Everything seems fine but when 1st sound starts playing I get these errors - Screenshot 2024-02-07 at 11 15 07 Screenshot 2024-02-07 at 11 15 13

After reloading the page only 2nd error appears. Screenshot 2024-02-07 at 11 15 13

Here is my cmake-config. I upload buffered sounds to browser filesystem and when game engine starts I decode it and play.

if(CMAKE_BUILD_TYPE STREQUAL "Debug") set(USE_FLAGS "-s USE_ZLIB=1 -sUSE_LIBJPEG=1 -sUSE_LIBPNG=1 -sWASM_WORKERS=1 -frtti -flto -O0 -g" CACHE STRING "Compilation flags" FORCE) set(LINKING_FLAGS "--preload-file ${ASSETS_PATH}/assets@/assets -sFULL_ES2 -sWASM=1 -sFETCH=1 --use-preload-plugins -sEXIT_RUNTIME=1 -sALLOW_MEMORY_GROWTH=1 -sEXPORTED_FUNCTIONS=['_main','_photoTaken','_malloc','_setNotificationToken','_interceptPushNotification'] -sEXPORTED_RUNTIME_METHODS=[cwrap] -sFORCE_FILESYSTEM=1 -lidbfs.js -frtti -flto -O0 -g -sAUDIO_WORKLET=1 -sWASM_WORKERS=1 -sASYNCIFY -sASYNCIFY_STACK_SIZE=65536" CACHE STRING "Linking flags" FORCE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${USE_FLAGS}") add_definitions(-DTARGET_WEB -DDEBUG -DMA_ENABLE_AUDIO_WORKLETS)

Sound engine initialization

void _KBSoundHelper::Init()
{
    // init auidio context

    ma_engine* engine = new ma_engine();
    ma_result result = ma_engine_init(NULL, engine);
    if (result != MA_SUCCESS)
    {
        LOG("[SoundHelper] Cannot initalize engine, result %d", result);
        return;  // Failed to initialize the engine.
    }
    LOG("[SoundHelper] Engine has been initialized");
    _engine = engine;
    _initialized = true;

}

Sound load & play

/// load sound into context (or prepare it as stream audio)
int _KBSoundHelper::Load(const char*name, int sampleLimit, bool buffered, bool loopable)
{
    if(!_initialized) return -1;

    _KBSound* sound =  new _KBSound(_engine, _lastSoundId++, sampleLimit, name, buffered, loopable);
//    if(buffered) sound->Buffering();
    _sounds[sound->GetId()] = sound;
    return sound->GetId();
}

/// play preloaded sound with attributes
void _KBSoundHelper::Play(int soundId, float volume, float rate, float overDuration, bool changeIfExist)
{
    if(!_initialized) return;

    auto it = _sounds.find(soundId);
    if(it == _sounds.end()) return;
    LOG("play in helper");
    it->second->Play(volume, rate, overDuration, changeIfExist);
}

Sound-files are buffered here

ma_sound* _KBSound::Buffering()
{
    ma_sound* sound = new ma_sound();
    ma_sound_config soundConfig;
    soundConfig = ma_sound_config_init_2(_engine);
    soundConfig.pFilePath   = _fileName; // Set this to load from a file path.
    soundConfig.endCallback = &soundPlaybackEnd;
    soundConfig.pEndCallbackUserData = this;
    soundConfig.flags              = MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_NO_PITCH;// | MA_SOUND_FLAG_STREAM;
    soundConfig.isLooping = _loopable;
    soundConfig.pInitialAttachment = NULL;
    soundConfig.pDoneFence         = NULL;
    soundConfig.channelsOut = 0;

    ma_result result = ma_sound_init_ex(_engine, &soundConfig, sound);
    if (result != MA_SUCCESS)
    {
        LOG("Sound %d cannot be buffered, result %d", _id, result);
        return 0;
    }
    _nativeSounds.push_back(sound);
    return sound;
}

If you need more info, please let me know.

mackron commented 9 months ago

Your code looks OK on the surface. But looking at your call stack, I can't see anything relating to miniaudio in there? Are you sure this is miniaudio related? How have you determined this is related to miniaudio and not something else in your code?

oregu1 commented 9 months ago

Your code looks OK on the surface. But looking at your call stack, I can't see anything relating to miniaudio in there? Are you sure this is miniaudio related? How have you determined this is related to miniaudio and not something else in your code?

Thanks for your quick response.

Without invoking sound methods everything works fine without any errors. I've checked it several times commenting code relative to sound initialization and play.

Sanitizer shows this - Screenshot 2024-02-07 at 12 57 39

wasm streaming compile failed: CompileError: WebAssembly.instantiateStreaming(): Compiling function #23759:"ma_dr_flac__decode_samples_with_residual__rice_..." failed: local count too large @+60197772
oregu1 commented 9 months ago

@mackron What it could be? I've spent a week trying to figure it out but no success.

mackron commented 9 months ago

I'm not familiar with that error. I've never seen that before nor have I had a report about it so I can't be of much help. However, it looks like some kind of issue with the build environment. Just doing a quick search now, maybe it's related to this? https://github.com/emscripten-core/emscripten/issues/19346

mackron commented 9 months ago

Is this error fixed now that the stack size error is fixed? FYI, I've update the dev branch to default the stack size to 131072.

oregu1 commented 9 months ago

Yeah. Upping of stack size helped me out. Thanks.

mackron commented 9 months ago

So this issue can be closed now too?

oregu1 commented 9 months ago

Yes. Everything looks fine.