Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio capture: the state or buffer may be wrong if there is a pause or other trigger #9700

Closed
joey-cyt opened this issue May 5, 2024 · 0 comments · Fixed by #9708
Closed
Assignees
Milestone

Comments

@joey-cyt
Copy link

joey-cyt commented May 5, 2024

When performing audio capture/playback, sometimes the playbacked audio adds noise or distortion. It's difficult to determine when and how the issue is triggered; the program may run for a whole day without encountering this problem, while at other times, the issue may start to appear after a couple of hours. Once the noise/distortion begins, the way to return to normal is to close the input and then reopen it (please refer to the following test code), or to restart the program.

I have written a simple test program as follows. When the program starts, it plays sound from the default input nicely. If debug break the while loop, for example, at the line "if (false)," and then continues, the sound will have noise or distortion. I'm not sure if debug break trigger the same issue as our application does after a long run, but the symptoms are the same. If, after debug break, move the running line to inside "if (false){}" to close/open the input, the issue will go away.

The test is on Windows 11 x64, with main branch code at 4/30.
Attached is a recorded example of the problematic audio.
SDL-sound-noise.zip

#include <SDL3/SDL.h>

int main(int, char **)
{
    if (SDL_Init(SDL_INIT_AUDIO) != 0) {
        printf("Error: SDL_Init(): %s\n", SDL_GetError());
        return 1;
    }

    const SDL_AudioSpec AudioSpec{ SDL_AUDIO_S16, 2, 48000 };

    SDL_AudioStream *pOutputStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_OUTPUT, &AudioSpec, nullptr, nullptr);
    if (pOutputStream == nullptr) {
        printf("Couldn't create audio playback stream. Error: %s", SDL_GetError());
        return 1;
    }
    SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(pOutputStream));

    SDL_AudioStream *pInputStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_CAPTURE, &AudioSpec, nullptr, nullptr);
    if (pInputStream == nullptr) {
        printf("Couldn't create audio capture stream. Error: %s", SDL_GetError());
        SDL_DestroyAudioStream(pOutputStream);
        return 1;
    }
    SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(pInputStream));

    uint8_t buffer[8192];
    while (true) {
        while (SDL_GetAudioStreamAvailable(pInputStream) > 0) {
            const int nRead = SDL_GetAudioStreamData(pInputStream, buffer, sizeof(buffer));
            if (nRead > 0) {
                if (0 != SDL_PutAudioStreamData(pOutputStream, buffer, nRead)) {
                    printf("Failed to put stream data, error %s", SDL_GetError());
                }
            }
        }

        if (false) {
            SDL_DestroyAudioStream(pInputStream);
            pInputStream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_CAPTURE, &AudioSpec, nullptr, nullptr);
            SDL_ResumeAudioDevice(SDL_GetAudioStreamDevice(pInputStream));
        }

        SDL_Delay(10);
    }

    // not reachable
    SDL_DestroyAudioStream(pInputStream);
    SDL_DestroyAudioStream(pOutputStream);
    SDL_Quit();
    return 0;
}```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants