38 lines
908 B
C++
38 lines
908 B
C++
|
|
#include "sound_server.h"
|
||
|
|
|
||
|
|
#include <SDL/SDL.h>
|
||
|
|
|
||
|
|
#include <iostream>
|
||
|
|
|
||
|
|
void audio_callback(void* param, uint8_t* stream, int len) {
|
||
|
|
sound_producer_1bit* producer = reinterpret_cast<sound_producer_1bit*>(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<void*>(&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(); }
|