Skip to content

Instantly share code, notes, and snippets.

@vl-80
Last active June 21, 2022 06:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vl-80/511943db52459890e30501aa0885a793 to your computer and use it in GitHub Desktop.
Save vl-80/511943db52459890e30501aa0885a793 to your computer and use it in GitHub Desktop.
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