2010-08-09 13:28:56 +00:00
|
|
|
#include <ao/ao.h>
|
|
|
|
|
|
|
|
namespace ruby {
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
struct pAudioAO {
|
2010-08-09 13:28:56 +00:00
|
|
|
int driver_id;
|
|
|
|
ao_sample_format driver_format;
|
2015-06-15 22:16:43 +00:00
|
|
|
ao_device* audio_device = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
struct {
|
2015-06-15 22:16:43 +00:00
|
|
|
unsigned frequency = 22050;
|
2010-08-09 13:28:56 +00:00
|
|
|
} settings;
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
pAudioAO() {
|
|
|
|
ao_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
~pAudioAO() {
|
|
|
|
term();
|
|
|
|
//ao_shutdown(); //FIXME: this is causing a segfault for some reason when called ...
|
|
|
|
}
|
|
|
|
|
|
|
|
auto cap(const string& name) -> bool {
|
2010-08-09 13:28:56 +00:00
|
|
|
if(name == Audio::Frequency) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto get(const string& name) -> any {
|
2010-08-09 13:28:56 +00:00
|
|
|
if(name == Audio::Frequency) return settings.frequency;
|
2015-06-15 22:16:43 +00:00
|
|
|
return {};
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto set(const string& name, const any& value) -> bool {
|
|
|
|
if(name == Audio::Frequency && value.is<unsigned>()) {
|
|
|
|
settings.frequency = value.get<unsigned>();
|
2010-08-09 13:28:56 +00:00
|
|
|
if(audio_device) init();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto sample(uint16_t l_sample, uint16_t r_sample) -> void {
|
2010-08-09 13:28:56 +00:00
|
|
|
uint32_t samp = (l_sample << 0) + (r_sample << 16);
|
|
|
|
ao_play(audio_device, (char*)&samp, 4); //This may need to be byte swapped for Big Endian
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto clear() -> void {
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto init() -> bool {
|
2010-08-09 13:28:56 +00:00
|
|
|
term();
|
|
|
|
|
|
|
|
driver_id = ao_default_driver_id(); //ao_driver_id((const char*)driver)
|
|
|
|
if(driver_id < 0) return false;
|
|
|
|
|
|
|
|
driver_format.bits = 16;
|
|
|
|
driver_format.channels = 2;
|
|
|
|
driver_format.rate = settings.frequency;
|
|
|
|
driver_format.byte_format = AO_FMT_LITTLE;
|
|
|
|
|
2013-05-02 11:25:45 +00:00
|
|
|
ao_option* options = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
ao_info *di = ao_driver_info(driver_id);
|
|
|
|
if(!di) return false;
|
|
|
|
if(!strcmp(di->short_name, "alsa")) {
|
|
|
|
ao_append_option(&options, "buffer_time", "100000"); //100ms latency (default was 500ms)
|
|
|
|
}
|
|
|
|
|
|
|
|
audio_device = ao_open_live(driver_id, &driver_format, options);
|
|
|
|
if(!audio_device) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto term() -> void {
|
2010-08-09 13:28:56 +00:00
|
|
|
if(audio_device) {
|
|
|
|
ao_close(audio_device);
|
2015-06-15 22:16:43 +00:00
|
|
|
audio_device = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DeclareAudio(AO)
|
|
|
|
|
|
|
|
};
|