emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.73k stars 3.3k forks source link

I can hear small noises when I use alSourceQueueBuffers() of openal. #11380

Open popomen opened 4 years ago

popomen commented 4 years ago

The following code reproduces the problem I described, but you have to listen very carefully to hear the noise. I played music for ten seconds, and the noise came out nine times.

#include <iostream>
#include <string.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <emscripten/emscripten.h>

int main() {
    // open device
    ALCdevice* device = alcOpenDevice(NULL);
    ALCcontext* context = alcCreateContext(device, NULL);
    alcMakeContextCurrent(context);

    ALuint buffers[10];
    alGenBuffers(10, buffers);
    ALuint sources[1];
    alGenSources(1, sources);

    // file_example_WAV_10MG.wav is donwload at here:
    // https://file-examples.com/index.php/sample-audio-files/sample-wav-download/
    FILE* file_wav = fopen("file_example_WAV_10MG.wav", "rb");
    fseek(file_wav, 0, SEEK_END);
    int file_size = ftell(file_wav);
    fseek(file_wav, 0, SEEK_SET);
    unsigned char* file_buffer = (unsigned char*) malloc(file_size);
    fread(file_buffer, file_size, 1, file_wav);
    int offset = 44;

    int size = 176400;   //176400 = 44100 * 2(channals) * 2(ALshort)
    ALshort music[10][88200];
    memcpy(&music, &file_buffer[offset], size*10);

    for (int i = 0 ; i < 10 ; i++)
    {
        alBufferData(buffers[i], AL_FORMAT_STEREO16, music[i], size, 44100);
        alSourceQueueBuffers(sources[0], 1, &buffers[i]);
    }

    alSourcePlay(sources[0]);

    emscripten_sleep(1000000);

    // close device
    context = alcGetCurrentContext();
    device = alcGetContextsDevice(context);
    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return 0;
}

Compile Command: em++ openal.cc -o openal.html -s ALLOW_MEMORY_GROWTH=1 -s EXIT_RUNTIME=1 --preload-file ./file_example_WAV_10MG.wav

popomen commented 4 years ago

@juj @kripken please~

juj commented 4 years ago

This is likely https://github.com/WebAudio/web-audio-api/issues/1323 / https://github.com/WebAudio/web-audio-api/issues/1322 . Try to ensure that the sample rate of the WAV file and the audio context match, and if it does, try out multiple browsers / OSes.

popomen commented 4 years ago

I've made a few changes to make the noise more noticeable. I put ten seconds of music into 100 buffers, and now there are ten noises per second.

#include <iostream>
#include <string.h>
#include <AL/al.h>
#include <AL/alc.h>
#include <emscripten/emscripten.h>

int main() {
    // open device
    ALCdevice* device = alcOpenDevice(NULL);
    ALCcontext* context = alcCreateContext(device, NULL);
    alcMakeContextCurrent(context);

    ALuint buffers[100];
    alGenBuffers(100, buffers);
    ALuint sources[1];
    alGenSources(1, sources);

    // file_example_WAV_10MG.wav is donwload at here:
    // https://file-examples.com/index.php/sample-audio-files/sample-wav-download/
    FILE* file_wav = fopen("file_example_WAV_10MG.wav", "rb");
    fseek(file_wav, 0, SEEK_END);
    int file_size = ftell(file_wav);
    fseek(file_wav, 0, SEEK_SET);
    unsigned char* file_buffer = (unsigned char*) malloc(file_size);
    fread(file_buffer, file_size, 1, file_wav);
    int offset = 44;

    int size = 176400;   //176400 = 44100 * 2(channals) * 2(ALshort)
    ALshort music[100][8820];
    memcpy(&music, &file_buffer[offset], size*10);

    for (int i = 0 ; i < 100 ; i++)
    {
        alBufferData(buffers[i], AL_FORMAT_STEREO16, music[i], size/10, 44100);
        alSourceQueueBuffers(sources[0], 1, &buffers[i]);
    }

    alSourcePlay(sources[0]);

    emscripten_sleep(1000000);

    // close device
    context = alcGetCurrentContext();
    device = alcGetContextsDevice(context);
    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return 0;
}
popomen commented 4 years ago

This is likely WebAudio/web-audio-api#1323 / WebAudio/web-audio-api#1322 . Try to ensure that the sample rate of the WAV file and the audio context match, and if it does, try out multiple browsers / OSes.

I tried chrome and firefox, and noises appeared. Also, I don't think the problem I've had has anything to do with web-audio-api#1322.

everywill commented 4 years ago

@popomen I think the small noises are introduced by web-audio's output latency. The subsequent pcm does not immediately come into audio device.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because there has been no activity in the past year. It will be closed automatically if no further activity occurs in the next 30 days. Feel free to re-open at any time if this issue is still relevant.