0

after some time, the audio pitch gets sharper, then makes only noices which makes me think that the problem is in the way i fill the buffer

int AudioThreadProc(AUDIOSTRUCT* audio) {
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
    HANDLE buffReady = CreateEvent(0, 0, 1, 0);
    audio->client->lpVtbl->SetEventHandle(audio->client, buffReady);

    unsigned int soundBuffSize;
    audio->client->lpVtbl->GetBufferSize(audio->client, &soundBuffSize);
    audio->client->lpVtbl->Start(audio.client);

    static float time = 0;
    float dtime = 1 / (float)44100;

    for (;;) {
        WaitForSingleObject(buffReady, INFINITE);

        unsigned int soundFrames;
        audio->client->lpVtbl->GetCurrentPadding(audio->client, &soundFrames);
        unsigned int FramesToFill = soundBuffSize - soundFrames;
        unsigned short int* soundBuff;
        audio->render->lpVtbl->GetBuffer(audio.render, FramesToFill, &soundBuff);

        for (unsigned int i = 0; i < FramesToFill; i++) {
            unsigned short int soundWave = audio->callback(time);
            time += dtime;
            *soundBuff++ = soundWave;
            *soundBuff++ = soundWave;
        }
        audio->render->lpVtbl->ReleaseBuffer(audio.render, FramesToFill, 0);

    }
    return 0;
}

the callback funtion its only a sine wave generator

float callback(float time){
 returns sin(pitch * PI2 * time) * 0xFFFF;
}

//...
audio.callback = callback;
ayawuasa
  • 3
  • 3
  • 2
    This doesn't look like valid C: `AudioThreadProc` has a `void` parameter, which isn't allowed, but probably means `void *` for a generic parameter. But even so, it would have to be cast into the proper structure type to reference it. Does this even compile? – Steve Friedl May 01 '20 at 04:40
  • it is void*, and it points to a struct that contains the renderclient and the audioclient – ayawuasa May 01 '20 at 04:49
  • The void pointer can’t be deferenced without a cast, and “audio.client” isn’t right anyway. Does this code compike without warnings? Do you turn on max warnings? – Steve Friedl May 01 '20 at 05:01

1 Answers1

0

I think the answer here is nearly equivalent to this answer I gave several years back. As time goes up, the floating point accuracy declines for computing pitch*2π*time. Adjust your code such that time resets back to zero or is and offset from zero after you complete a full wave of data.

Something like this:

time += dtime;
if (time > 1.0) {
    time -= 1.0;
}

Also, the total overhead of invoking a function call per sample might be an issue as well. Convert your callback into a macro. You won't regret it.

selbie
  • 100,020
  • 15
  • 103
  • 173