Last active
June 21, 2022 06:27
-
-
Save vl-80/511943db52459890e30501aa0885a793 to your computer and use it in GitHub Desktop.
SDL_OpenAudioDevice() C++ example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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