English 中文(简体)
• 使SDL_AudioCallback打断(即无机)?
原标题:Make SDL_AudioCallback seamless (ie. no hiccup)?
  • 时间:2024-01-28 01:08:51
  •  标签:
  • c++
  • sdl
  • sdl-2

我正在撰写一个简单的方案,通过SDL发挥一个声音:

#include <iostream>
#include <SDL.h>


float sine_freq = 200.0f;
float audio_volume = 1.0f;
float audio_frequency;

void SineAudioCallback(void* userdata, Uint8* stream, int len) {
    float* buf = (float*)stream;
    for (int i = 0; i < len / 4; ++i) {
        buf[i] = (float)(audio_volume * sin(2 * M_PI * i * audio_frequency));
    }
    std::cout << "here" << std::endl;
    return;
}

int main(int argc, char* argv[])
{
    if (SDL_Init(SDL_INIT_AUDIO)) {
        return 1;
    }

    std::cout << "[SDL] Audio driver: " << SDL_GetCurrentAudioDriver() << std::endl;

    SDL_AudioSpec want, have;
    SDL_zero(want);

    want.freq = 5000;
    want.format = AUDIO_F32;
    want.channels = 2;
    want.samples = 4096;
    want.callback = SineAudioCallback;

    std::cout <<"[SDL] Desired - frequency: " << want.freq
        << ", format: f " << SDL_AUDIO_ISFLOAT(want.format)  << " s " << SDL_AUDIO_ISSIGNED(want.format) << " be " << SDL_AUDIO_ISBIGENDIAN(want.format) << " sz " << SDL_AUDIO_BITSIZE(want.format)
        << ", channels: " << (int)want.channels << ", samples: " << want.samples << std::endl;


    SDL_AudioDeviceID dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);

    if (!dev) {
        SDL_Quit();
        return 1;
    }


    std::cout << "[SDL] Desired - frequency: " << have.freq
        << ", format: f " << SDL_AUDIO_ISFLOAT(have.format) << " s " << SDL_AUDIO_ISSIGNED(have.format) << " be " << SDL_AUDIO_ISBIGENDIAN(have.format) << " sz " << SDL_AUDIO_BITSIZE(have.format)
        << ", channels: " << (int)have.channels << ", samples: " << have.samples << std::endl;

    audio_frequency = sine_freq / have.freq;

    SDL_PauseAudioDevice(dev, 0);
    SDL_Delay(10000);

    SDL_CloseAudioDevice(dev);
    SDL_Quit();

    return 0;
}

然而,当我执行这一方案时,我注意到,当叫退时,“沉默的占领”是非常轻松的。 我猜测,由于只是在缓冲本身失去数据时才被叫回,沉默是重新填补缓冲所需的稍微拖延。

我想摆脱这种“沉默的占领”。 我的用法是,我从其他地方收到了一套音频数据;我希望同时用这一数据填补SDL的声传缓冲,同时允许SDL消耗缓冲。 然而,这似乎不像我能够接触到实际的溪流/水,除非是在回击中,但到那时,缓冲已经超出数据。

是否有更可取的解决办法? 我倾向于尽可能不使用SDL_mixer。

问题回答

然而,这似乎不像我能够接触到实际的溪流,除非它在回击中屈服。

That s what the SDL_QueueAudio function is for, you can take over the buffer upload timing.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签