2010-08-09 13:28:56 +00:00
|
|
|
#include <pulse/pulseaudio.h>
|
|
|
|
|
2015-06-20 05:44:05 +00:00
|
|
|
struct AudioPulseAudio : Audio {
|
|
|
|
~AudioPulseAudio() { term(); }
|
2010-08-09 13:28:56 +00:00
|
|
|
|
|
|
|
struct {
|
2015-06-15 22:16:43 +00:00
|
|
|
pa_mainloop* mainloop = nullptr;
|
|
|
|
pa_context* context = nullptr;
|
|
|
|
pa_stream* stream = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
pa_sample_spec spec;
|
|
|
|
pa_buffer_attr buffer_attr;
|
|
|
|
bool first;
|
|
|
|
} device;
|
|
|
|
|
|
|
|
struct {
|
2015-06-15 22:16:43 +00:00
|
|
|
uint32_t* data = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
size_t size;
|
|
|
|
unsigned offset;
|
|
|
|
} buffer;
|
|
|
|
|
|
|
|
struct {
|
2015-06-15 22:16:43 +00:00
|
|
|
bool synchronize = false;
|
Update to v098r01 release.
byuu says:
Changelog:
- SFC: balanced profile removed
- SFC: performance profile removed
- SFC: code for handling non-threaded CPU, SMP, DSP, PPU removed
- SFC: Coprocessor, Controller (and expansion port) shared Thread code
merged to SFC::Cothread
- Cothread here just means "Thread with CPU affinity" (couldn't think
of a better name, sorry)
- SFC: CPU now has vector<Thread*> coprocessors, peripherals;
- this is the beginning of work to allow expansion port devices to be
dynamically changed at run-time
- ruby: all audio drivers default to 48000hz instead of 22050hz now if
no frequency is assigned
- note: the WASAPI driver can default to whatever the native frequency
is; doesn't have to be 48000hz
- tomoko: removed the ability to change the frequency from the UI (but
it will display the frequency used)
- tomoko: removed the timing settings panel
- the goal is to work toward smooth video via adaptive sync
- the model is broken by not being in control of the audio frequency
anyway
- it's further broken by PAL running at 50hz and WSC running at 75hz
- it was always broken anyway by SNES interlace timing varying from
progressive timing
- higan: audio/ stub created (for now, it's just nall/dsp/ moved here
and included as a header)
- higan: video/ stub created
- higan/GNUmakefile: now includes build rules for essential components
(libco, emulator, audio, video)
The audio changes are in preparation to merge wareya's awesome WASAPI
work without the need for the nall/dsp resampler.
2016-04-09 03:40:12 +00:00
|
|
|
unsigned frequency = 48000;
|
2015-06-15 22:16:43 +00:00
|
|
|
unsigned latency = 60;
|
2010-08-09 13:28:56 +00:00
|
|
|
} settings;
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto cap(const string& name) -> bool {
|
2010-08-09 13:28:56 +00:00
|
|
|
if(name == Audio::Synchronize) return true;
|
|
|
|
if(name == Audio::Frequency) return true;
|
|
|
|
if(name == Audio::Latency) return true;
|
2015-06-15 22:16:43 +00:00
|
|
|
return false;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
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::Synchronize) return settings.synchronize;
|
|
|
|
if(name == Audio::Frequency) return settings.frequency;
|
|
|
|
if(name == Audio::Latency) return settings.latency;
|
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::Synchronize && value.is<bool>()) {
|
|
|
|
settings.synchronize = value.get<bool>();
|
2010-08-09 13:28:56 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
if(name == Audio::Frequency && value.is<unsigned>()) {
|
|
|
|
settings.frequency = value.get<unsigned>();
|
2010-08-09 13:28:56 +00:00
|
|
|
if(device.stream) {
|
|
|
|
pa_operation_unref(pa_stream_update_sample_rate(device.stream, settings.frequency, NULL, NULL));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
if(name == Audio::Latency && value.is<unsigned>()) {
|
|
|
|
settings.latency = value.get<unsigned>();
|
2010-08-09 13:28:56 +00:00
|
|
|
if(device.stream) {
|
|
|
|
device.buffer_attr.tlength = pa_usec_to_bytes(settings.latency * PA_USEC_PER_MSEC, &device.spec);
|
|
|
|
pa_stream_set_buffer_attr(device.stream, &device.buffer_attr, NULL, NULL);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-06-15 22:16:43 +00:00
|
|
|
|
|
|
|
return false;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 10:49:45 +00:00
|
|
|
auto sample(int16_t left, int16_t right) -> void {
|
2010-08-09 13:28:56 +00:00
|
|
|
pa_stream_begin_write(device.stream, (void**)&buffer.data, &buffer.size);
|
2016-04-18 10:49:45 +00:00
|
|
|
buffer.data[buffer.offset++] = (uint16_t)left << 0 | (uint16_t)right << 16;
|
2010-08-09 13:28:56 +00:00
|
|
|
if((buffer.offset + 1) * pa_frame_size(&device.spec) <= buffer.size) return;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
if(device.first) {
|
|
|
|
device.first = false;
|
|
|
|
pa_mainloop_iterate(device.mainloop, 0, NULL);
|
|
|
|
} else {
|
|
|
|
pa_mainloop_iterate(device.mainloop, 1, NULL);
|
|
|
|
}
|
|
|
|
unsigned length = pa_stream_writable_size(device.stream);
|
|
|
|
if(length >= buffer.offset * pa_frame_size(&device.spec)) break;
|
|
|
|
if(settings.synchronize == false) {
|
|
|
|
buffer.offset = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pa_stream_write(device.stream, (const void*)buffer.data, buffer.offset * pa_frame_size(&device.spec), NULL, 0LL, PA_SEEK_RELATIVE);
|
|
|
|
buffer.data = 0;
|
|
|
|
buffer.offset = 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
device.mainloop = pa_mainloop_new();
|
|
|
|
|
|
|
|
device.context = pa_context_new(pa_mainloop_get_api(device.mainloop), "ruby::pulseaudio");
|
|
|
|
pa_context_connect(device.context, NULL, PA_CONTEXT_NOFLAGS, NULL);
|
|
|
|
|
|
|
|
pa_context_state_t cstate;
|
|
|
|
do {
|
|
|
|
pa_mainloop_iterate(device.mainloop, 1, NULL);
|
|
|
|
cstate = pa_context_get_state(device.context);
|
|
|
|
if(!PA_CONTEXT_IS_GOOD(cstate)) return false;
|
|
|
|
} while(cstate != PA_CONTEXT_READY);
|
|
|
|
|
|
|
|
device.spec.format = PA_SAMPLE_S16LE;
|
|
|
|
device.spec.channels = 2;
|
|
|
|
device.spec.rate = settings.frequency;
|
|
|
|
device.stream = pa_stream_new(device.context, "audio", &device.spec, NULL);
|
|
|
|
|
|
|
|
device.buffer_attr.maxlength = -1;
|
|
|
|
device.buffer_attr.tlength = pa_usec_to_bytes(settings.latency * PA_USEC_PER_MSEC, &device.spec);
|
|
|
|
device.buffer_attr.prebuf = -1;
|
|
|
|
device.buffer_attr.minreq = -1;
|
|
|
|
device.buffer_attr.fragsize = -1;
|
|
|
|
|
|
|
|
pa_stream_flags_t flags = (pa_stream_flags_t)(PA_STREAM_ADJUST_LATENCY | PA_STREAM_VARIABLE_RATE);
|
|
|
|
pa_stream_connect_playback(device.stream, NULL, &device.buffer_attr, flags, NULL, NULL);
|
|
|
|
|
|
|
|
pa_stream_state_t sstate;
|
|
|
|
do {
|
|
|
|
pa_mainloop_iterate(device.mainloop, 1, NULL);
|
|
|
|
sstate = pa_stream_get_state(device.stream);
|
|
|
|
if(!PA_STREAM_IS_GOOD(sstate)) return false;
|
|
|
|
} while(sstate != PA_STREAM_READY);
|
|
|
|
|
|
|
|
buffer.size = 960;
|
|
|
|
buffer.offset = 0;
|
|
|
|
device.first = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:43 +00:00
|
|
|
auto term() -> void {
|
2010-08-09 13:28:56 +00:00
|
|
|
if(buffer.data) {
|
|
|
|
pa_stream_cancel_write(device.stream);
|
2015-06-15 22:16:43 +00:00
|
|
|
buffer.data = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(device.stream) {
|
|
|
|
pa_stream_disconnect(device.stream);
|
|
|
|
pa_stream_unref(device.stream);
|
2015-06-15 22:16:43 +00:00
|
|
|
device.stream = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(device.context) {
|
|
|
|
pa_context_disconnect(device.context);
|
|
|
|
pa_context_unref(device.context);
|
2015-06-15 22:16:43 +00:00
|
|
|
device.context = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(device.mainloop) {
|
|
|
|
pa_mainloop_free(device.mainloop);
|
2015-06-15 22:16:43 +00:00
|
|
|
device.mainloop = nullptr;
|
2010-08-09 13:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|