Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SDL_OpenAudioDevice() C++ example
#include <chrono>
#include <iostream>
#include <thread>
#include <SDL2/SDL.h>
void callback( void *, Uint8 * stream, int len )
{
memset( stream, 0, len );
}
int main()
{
if( SDL_Init( SDL_INIT_AUDIO ) != 0 )
{
std::cerr << "SDL_Init(): " << SDL_GetError() << std::endl;
SDL_ClearError();
return 1;
}
SDL_AudioSpec specs = {};
specs.freq = 48000;
specs.format = AUDIO_S32;
specs.channels = 2;
specs.samples = 4096;
specs.callback = callback;
constexpr int PLAYBACK_DEV = 0;
const int audio_dev_id = SDL_OpenAudioDevice( nullptr, PLAYBACK_DEV, &specs,
nullptr, SDL_AUDIO_ALLOW_CHANNELS_CHANGE );
if( audio_dev_id == 0 )
{
std::cerr << "Error opening audio device: " << SDL_GetError() << std::endl;
return 1;
}
while( true )
{
std::this_thread::sleep_for( std::chrono::seconds( 5 ) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment