简体   繁体   中英

No sound generated by SDL Audio Callback

I am trying to get a simple sinewave sound generation example working using SDL 2.0.12 on Windows 10, but no sound is being output. I have no idea if it is a problem with the code or with the output device or the audio drivers. I'd really appreciate suggestions of how I can debug the problem further.

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


float sine_freq = 200.0f;
float audio_volume = 4000.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));
    }
    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;
}

The output I get is

[SDL] Audio driver: wasapi

[SDL] Desired - frequency: 5000, format: f 256 s 32768 be 0 sz 32, channels: 2, samples: 4096

[SDL] Desired - frequency: 5000, format: f 256 s 32768 be 0 sz 32, channels: 2, samples: 118

So there is a difference between SDL_AudioSpec I want and have, in the number of samples being reduced.

I also get an error message relating to the dlls although I'm not sure if it is important. avcore\\audiocore\\client\\audioclient\\audioclientcore.cpp(1839)\\AUDIOSES.DLL!00007FFC48E00F8E: (caller: 0000000070D6BE39) ReturnHr(1) tid(46dc) 80070057 The parameter is incorrect.

It turns out there were two things I needed to do to solve this problem.

For a float type the sound wave can only take values from -1 to 1, whilst I was using a much greater volume boost. I needed to change

float audio_volume = 1.0f;

For Windows, the direct sound and winmm drivers give better sample lengths than wasapi. For SDL2 on Windows, you need SDL_AUDIODRIVER=directsound or SDL_AUDIODRIVER=winmm set as an environment variable. More details here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM