Skip to content

Strange sound in the first seconds of streaming audio #12059

Closed
@renebarto

Description

@renebarto

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions