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

Strange sound in the first seconds of streaming audio #12059

Open
renebarto opened this issue Jan 22, 2025 · 3 comments
Open

Strange sound in the first seconds of streaming audio #12059

renebarto opened this issue Jan 22, 2025 · 3 comments
Assignees
Milestone

Comments

@renebarto
Copy link

I have an issue similar to #11724 on Windows.
I'm placing two sine waves of different frequencies on each channel of a stereo pair.
When I create the stream (16 bit setero) I get callbacks for 1920 bytes per call, and fill up the buffer then add 480 sample frames of 4 bytes each with sine wave (using a continuing phase to make sure the audio is smooth).
In the first seconds I hear a strange deformation on both channels, which disappears after a few seconds, first on the left, and a few seconds later on the right channel. After that the sounds is what I'd expect.
I tried clearing the buffer first, but that does not help.
I have the feeling either SDL_PutAudioStreamData writes more or less data to the buffer than specified, or that later on something else messes up the audio.

To be clear, I do not open a device first, I just create as stream:

SDL_AudioSpec spec { SDL_AUDIO_S16LE, 2, 48000 };
m_stream = SDL_OpenAudioDeviceStream(id, &spec, callback, nullptr);

Callback:
static uint8_t audioBuffer[65536];
float phaseL = 0.0F;
float phaseR = 0.0F;
float sampleFreq = 48000.0F;
float freqL = 1000.0F;
float freqR = 2000.0F;
float twoPI = 2 * SDL_PI_D;
float stepL = freqL / sampleFreq * twoPI;
float stepR = freqR / sampleFreq * twoPI;

void audio_callback(void */userdata/, SDL_AudioStream *stream, int additional_amount, int total_amount)
{
size_t offset{};
int sampleFrames = additional_amount / 4;
for (int i = 0; i < sampleFrames; ++i)
{
int16_t sampleL = static_cast<int16_t>(32768.0F * sin(phaseL) + 0.5F);
int16_t sampleR = static_cast<int16_t>(32768.0F * sin(phaseR) + 0.5F);
memcpy(audioBuffer + offset, &sampleL, 2);
offset += 2;
memcpy(audioBuffer + offset, &sampleR, 2);
offset += 2;
phaseL += stepL;
phaseR += stepR;
if (phaseL > twoPI)
phaseL -= twoPI;
if (phaseR > twoPI)
phaseR -= twoPI;
}
// Put the audio data into the stream
//std::cout << "Get audio, size " << additional_amount << "\r\n";
SDL_PutAudioStreamData(stream, audioBuffer, sampleFrames * 4);
}

I play sound for about 10 secs, then pause and destroy the stream.

After about 2 seconds the left channel sounds normal, after about 4 seconds the right is also normal.

@renebarto
Copy link
Author

Sorry, forgot to mention I'm on SDL3.1.9

@slouken slouken added this to the 3.2.2 milestone Jan 22, 2025
@renebarto
Copy link
Author

For additional information, I just checked out the 3.2.0 commit and rebuilt. Same issue.

@milq
Copy link

milq commented Jan 22, 2025

@renebarto, same for me using Debian GNU/Linux.

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

No branches or pull requests

4 participants