#include "sound_server.h" #include #include void audio_callback(void* param, uint8_t* stream, int len) { sound_producer_1bit* producer = reinterpret_cast(param); for (int i = 0; i < len; i++) { *stream++ = producer->tick() ? 255 : 0; } } sound_server_1bit::sound_server_1bit(uint16_t samplerate, sound_producer_1bit& producer) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { std::cerr << "Init failed" << std::endl; exit(1); } SDL_AudioSpec as; as.freq = samplerate; as.format = AUDIO_U8; as.samples = 256; as.callback = audio_callback; as.userdata = reinterpret_cast(&producer); as.channels = 1; if (SDL_OpenAudio(&as, NULL) < 0) { std::cerr << "Unable to open audio" << std::endl; exit(1); } SDL_PauseAudio(0); } sound_server_1bit::~sound_server_1bit() { SDL_Quit(); }