mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-08-28 11:30:01 +02:00
Update to v103r16 release.
byuu says: Changelog: - emulator/audio: added the ability to change the output frequency at run-time without emulator reset - tomoko: display video synchronize option again¹ - tomoko: Settings→Configuration expanded to Settings→{Video, Audio, Input, Hotkey, Advanced} Settings² - tomoko: fix default population of audio settings tab - ruby: Audio::frequency is a double now (to match both Emulator::Audio and ASIO)³ - tomoko: changing the audio device will repopulate the frequency and latency lists - tomoko: changing the audio frequency can now be done in real-time - ruby/audio/asio: added missing device() information, so devices can be changed now - ruby/audio/openal: ported to new API; added device selection support - ruby/audio/wasapi: ported to new API, but did not test yet (it's assuredly still broken)⁴ ¹: I'm uneasy about this ... but, I guess if people want to disable audio and just have smooth scrolling video ... so be it. With Screwtape's documentation, hopefully that'll help people understand that video synchronization always breaks audio synchronization. I may change this to a child menu that lets you pick between {no synchronization, video synchronization, audio synchronization} as a radio selection. ²: given how much more useful the video and audio tabs are now, I felt that four extra menu items were worth saving a click and going right to the tab you want. This also matches the behavior of the Tools menu displaying all tool options and taking you directly to each tab. This is kind of a hard change to get used to ... but I think it's for the better. ³: kind of stupid because I've never seen a hardware sound card where floor(frequency) != frequency, but whatever. Yay consistency. ⁴: I'm going to move it to be event-driven, and try to support 24-bit sample formats if possible. Who knows which cards that'll fix and which cards that'll break. I may end up making multiple WASAPI drivers so people can find one that actually works for them. We'll see.
This commit is contained in:
@@ -22,9 +22,10 @@ struct AudioASIO : Audio {
|
||||
}
|
||||
|
||||
auto context() -> uintptr { return _context; }
|
||||
auto device() -> string { return _device; }
|
||||
auto blocking() -> bool { return _blocking; }
|
||||
auto channels() -> uint { return _channels; }
|
||||
auto frequency() -> uint { return _frequency; }
|
||||
auto frequency() -> double { return _frequency; }
|
||||
auto latency() -> uint { return _latency; }
|
||||
|
||||
auto setContext(uintptr context) -> bool {
|
||||
@@ -33,6 +34,12 @@ struct AudioASIO : Audio {
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto setDevice(string device) -> bool {
|
||||
if(_device == device) return true;
|
||||
_device = device;
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto setBlocking(bool blocking) -> bool {
|
||||
if(_blocking == blocking) return true;
|
||||
_blocking = blocking;
|
||||
@@ -51,18 +58,6 @@ struct AudioASIO : Audio {
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto output(const double samples[]) -> void {
|
||||
if(!_ready) return;
|
||||
if(_blocking) {
|
||||
while(_queue.count >= _latency);
|
||||
}
|
||||
for(uint n : range(_channels)) {
|
||||
_queue.samples[_queue.write][n] = samples[n];
|
||||
}
|
||||
_queue.write++;
|
||||
_queue.count++;
|
||||
}
|
||||
|
||||
auto clear() -> void {
|
||||
if(!_ready) return;
|
||||
for(uint n : range(_channels)) {
|
||||
@@ -75,6 +70,18 @@ struct AudioASIO : Audio {
|
||||
_queue.count = 0;
|
||||
}
|
||||
|
||||
auto output(const double samples[]) -> void {
|
||||
if(!_ready) return;
|
||||
if(_blocking) {
|
||||
while(_queue.count >= _latency);
|
||||
}
|
||||
for(uint n : range(_channels)) {
|
||||
_queue.samples[_queue.write][n] = samples[n];
|
||||
}
|
||||
_queue.write++;
|
||||
_queue.count++;
|
||||
}
|
||||
|
||||
private:
|
||||
auto initialize() -> bool {
|
||||
terminate();
|
||||
@@ -236,7 +243,7 @@ private:
|
||||
string _device;
|
||||
bool _blocking = true;
|
||||
uint _channels = 2;
|
||||
uint _frequency = 48000;
|
||||
double _frequency = 48000.0;
|
||||
uint _latency = 0;
|
||||
|
||||
struct Queue {
|
||||
|
@@ -7,188 +7,188 @@
|
||||
#endif
|
||||
|
||||
struct AudioOpenAL : Audio {
|
||||
~AudioOpenAL() { term(); }
|
||||
AudioOpenAL() { initialize(); }
|
||||
~AudioOpenAL() { terminate(); }
|
||||
|
||||
struct {
|
||||
ALCdevice* handle = nullptr;
|
||||
ALCcontext* context = nullptr;
|
||||
ALuint source = 0;
|
||||
ALenum format = AL_FORMAT_STEREO16;
|
||||
unsigned latency = 0;
|
||||
unsigned queueLength = 0;
|
||||
} device;
|
||||
auto ready() -> bool { return _ready; }
|
||||
|
||||
struct {
|
||||
uint32_t* data = nullptr;
|
||||
unsigned length = 0;
|
||||
unsigned size = 0;
|
||||
} buffer;
|
||||
|
||||
struct {
|
||||
bool synchronize = true;
|
||||
unsigned frequency = 48000;
|
||||
unsigned latency = 40;
|
||||
} settings;
|
||||
|
||||
auto cap(const string& name) -> bool {
|
||||
if(name == Audio::Synchronize) return true;
|
||||
if(name == Audio::Frequency) return true;
|
||||
if(name == Audio::Latency) return true;
|
||||
return false;
|
||||
auto information() -> Information {
|
||||
Information information;
|
||||
for(auto& device : queryDevices()) information.devices.append(device);
|
||||
information.channels = {2};
|
||||
information.frequencies = {44100.0, 48000.0, 96000.0};
|
||||
information.latencies = {20, 40, 60, 80, 100};
|
||||
return information;
|
||||
}
|
||||
|
||||
auto get(const string& name) -> any {
|
||||
if(name == Audio::Synchronize) return settings.synchronize;
|
||||
if(name == Audio::Frequency) return settings.frequency;
|
||||
if(name == Audio::Latency) return settings.latency;
|
||||
return {};
|
||||
auto device() -> string { return _device; }
|
||||
auto blocking() -> bool { return _blocking; }
|
||||
auto channels() -> uint { return _channels; }
|
||||
auto frequency() -> double { return (double)_frequency; }
|
||||
auto latency() -> uint { return _latency; }
|
||||
|
||||
auto setDevice(string device) -> bool {
|
||||
if(_device == device) return true;
|
||||
_device = device;
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto set(const string& name, const any& value) -> bool {
|
||||
if(name == Audio::Synchronize && value.is<bool>()) {
|
||||
settings.synchronize = value.get<bool>();
|
||||
return true;
|
||||
}
|
||||
|
||||
if(name == Audio::Frequency && value.is<unsigned>()) {
|
||||
settings.frequency = value.get<unsigned>();
|
||||
return true;
|
||||
}
|
||||
|
||||
if(name == Audio::Latency && value.is<unsigned>()) {
|
||||
if(settings.latency != value.get<unsigned>()) {
|
||||
settings.latency = value.get<unsigned>();
|
||||
updateLatency();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
auto setBlocking(bool blocking) -> bool {
|
||||
if(_blocking == blocking) return true;
|
||||
_blocking = blocking;
|
||||
return true;
|
||||
}
|
||||
|
||||
auto sample(int16_t left, int16_t right) -> void {
|
||||
buffer.data[buffer.length++] = (uint16_t)left << 0 | (uint16_t)right << 16;
|
||||
if(buffer.length < buffer.size) return;
|
||||
auto setFrequency(double frequency) -> bool {
|
||||
if(_frequency == (uint)frequency) return true;
|
||||
_frequency = (uint)frequency;
|
||||
return initialize();
|
||||
}
|
||||
|
||||
ALuint albuffer = 0;
|
||||
auto setLatency(uint latency) -> bool {
|
||||
if(_latency == latency) return true;
|
||||
_latency = latency;
|
||||
if(_ready) updateLatency();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto output(const double samples[]) -> void {
|
||||
_buffer[_bufferLength] = int16_t(samples[0] * 32768.0) << 0;
|
||||
_buffer[_bufferLength] |= int16_t(samples[1] * 32768.0) << 16;
|
||||
if(++_bufferLength < _bufferSize) return;
|
||||
|
||||
ALuint alBuffer = 0;
|
||||
int processed = 0;
|
||||
while(true) {
|
||||
alGetSourcei(device.source, AL_BUFFERS_PROCESSED, &processed);
|
||||
alGetSourcei(_source, AL_BUFFERS_PROCESSED, &processed);
|
||||
while(processed--) {
|
||||
alSourceUnqueueBuffers(device.source, 1, &albuffer);
|
||||
alDeleteBuffers(1, &albuffer);
|
||||
device.queueLength--;
|
||||
alSourceUnqueueBuffers(_source, 1, &alBuffer);
|
||||
alDeleteBuffers(1, &alBuffer);
|
||||
_queueLength--;
|
||||
}
|
||||
//wait for buffer playback to catch up to sample generation if not synchronizing
|
||||
if(settings.synchronize == false || device.queueLength < 3) break;
|
||||
if(!_blocking || _queueLength < 3) break;
|
||||
}
|
||||
|
||||
if(device.queueLength < 3) {
|
||||
alGenBuffers(1, &albuffer);
|
||||
alBufferData(albuffer, device.format, buffer.data, buffer.size * 4, settings.frequency);
|
||||
alSourceQueueBuffers(device.source, 1, &albuffer);
|
||||
device.queueLength++;
|
||||
if(_queueLength < 3) {
|
||||
alGenBuffers(1, &alBuffer);
|
||||
alBufferData(alBuffer, _format, _buffer, _bufferSize * 4, _frequency);
|
||||
alSourceQueueBuffers(_source, 1, &alBuffer);
|
||||
_queueLength++;
|
||||
}
|
||||
|
||||
ALint playing;
|
||||
alGetSourcei(device.source, AL_SOURCE_STATE, &playing);
|
||||
if(playing != AL_PLAYING) alSourcePlay(device.source);
|
||||
buffer.length = 0;
|
||||
alGetSourcei(_source, AL_SOURCE_STATE, &playing);
|
||||
if(playing != AL_PLAYING) alSourcePlay(_source);
|
||||
_bufferLength = 0;
|
||||
}
|
||||
|
||||
auto clear() -> void {
|
||||
}
|
||||
private:
|
||||
auto initialize() -> bool {
|
||||
terminate();
|
||||
|
||||
auto init() -> bool {
|
||||
if(!queryDevices().find(_device)) _device = "";
|
||||
_queueLength = 0;
|
||||
updateLatency();
|
||||
device.queueLength = 0;
|
||||
|
||||
bool success = false;
|
||||
if(device.handle = alcOpenDevice(nullptr)) {
|
||||
if(device.context = alcCreateContext(device.handle, nullptr)) {
|
||||
alcMakeContextCurrent(device.context);
|
||||
alGenSources(1, &device.source);
|
||||
if(_openAL = alcOpenDevice(_device)) {
|
||||
if(_context = alcCreateContext(_openAL, nullptr)) {
|
||||
alcMakeContextCurrent(_context);
|
||||
alGenSources(1, &_source);
|
||||
|
||||
//alSourcef (device.source, AL_PITCH, 1.0);
|
||||
//alSourcef (device.source, AL_GAIN, 1.0);
|
||||
//alSource3f(device.source, AL_POSITION, 0.0, 0.0, 0.0);
|
||||
//alSource3f(device.source, AL_VELOCITY, 0.0, 0.0, 0.0);
|
||||
//alSource3f(device.source, AL_DIRECTION, 0.0, 0.0, 0.0);
|
||||
//alSourcef (device.source, AL_ROLLOFF_FACTOR, 0.0);
|
||||
//alSourcei (device.source, AL_SOURCE_RELATIVE, AL_TRUE);
|
||||
//alSourcef (_source, AL_PITCH, 1.0);
|
||||
//alSourcef (_source, AL_GAIN, 1.0);
|
||||
//alSource3f(_source, AL_POSITION, 0.0, 0.0, 0.0);
|
||||
//alSource3f(_source, AL_VELOCITY, 0.0, 0.0, 0.0);
|
||||
//alSource3f(_source, AL_DIRECTION, 0.0, 0.0, 0.0);
|
||||
//alSourcef (_source, AL_ROLLOFF_FACTOR, 0.0);
|
||||
//alSourcei (_source, AL_SOURCE_RELATIVE, AL_TRUE);
|
||||
|
||||
alListener3f(AL_POSITION, 0.0, 0.0, 0.0);
|
||||
alListener3f(AL_VELOCITY, 0.0, 0.0, 0.0);
|
||||
ALfloat listener_orientation[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
|
||||
alListenerfv(AL_ORIENTATION, listener_orientation);
|
||||
ALfloat listenerOrientation[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
|
||||
alListenerfv(AL_ORIENTATION, listenerOrientation);
|
||||
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(success == false) {
|
||||
term();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
if(!success) return terminate(), false;
|
||||
return _ready = true;
|
||||
}
|
||||
|
||||
auto term() -> void {
|
||||
if(alIsSource(device.source) == AL_TRUE) {
|
||||
auto terminate() -> void {
|
||||
_ready = false;
|
||||
|
||||
if(alIsSource(_source) == AL_TRUE) {
|
||||
int playing = 0;
|
||||
alGetSourcei(device.source, AL_SOURCE_STATE, &playing);
|
||||
alGetSourcei(_source, AL_SOURCE_STATE, &playing);
|
||||
if(playing == AL_PLAYING) {
|
||||
alSourceStop(device.source);
|
||||
alSourceStop(_source);
|
||||
int queued = 0;
|
||||
alGetSourcei(device.source, AL_BUFFERS_QUEUED, &queued);
|
||||
alGetSourcei(_source, AL_BUFFERS_QUEUED, &queued);
|
||||
while(queued--) {
|
||||
ALuint albuffer = 0;
|
||||
alSourceUnqueueBuffers(device.source, 1, &albuffer);
|
||||
alDeleteBuffers(1, &albuffer);
|
||||
device.queueLength--;
|
||||
ALuint alBuffer = 0;
|
||||
alSourceUnqueueBuffers(_source, 1, &alBuffer);
|
||||
alDeleteBuffers(1, &alBuffer);
|
||||
_queueLength--;
|
||||
}
|
||||
}
|
||||
|
||||
alDeleteSources(1, &device.source);
|
||||
device.source = 0;
|
||||
alDeleteSources(1, &_source);
|
||||
_source = 0;
|
||||
}
|
||||
|
||||
if(device.context) {
|
||||
if(_context) {
|
||||
alcMakeContextCurrent(nullptr);
|
||||
alcDestroyContext(device.context);
|
||||
device.context = 0;
|
||||
alcDestroyContext(_context);
|
||||
_context = nullptr;
|
||||
}
|
||||
|
||||
if(device.handle) {
|
||||
alcCloseDevice(device.handle);
|
||||
device.handle = 0;
|
||||
if(_openAL) {
|
||||
alcCloseDevice(_openAL);
|
||||
_openAL = nullptr;
|
||||
}
|
||||
|
||||
if(buffer.data) {
|
||||
delete[] buffer.data;
|
||||
buffer.data = 0;
|
||||
}
|
||||
delete[] _buffer;
|
||||
_buffer = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
auto queryDevices() -> string_vector {
|
||||
string_vector result;
|
||||
|
||||
const char* buffer = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
|
||||
if(!buffer) return result;
|
||||
const char* list = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
|
||||
if(!list) return result;
|
||||
|
||||
while(buffer[0] || buffer[1]) {
|
||||
result.append(buffer);
|
||||
while(buffer[0]) buffer++;
|
||||
while(list[0] || list[1]) {
|
||||
result.append(list);
|
||||
while(list[0]) list++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
auto updateLatency() -> void {
|
||||
if(buffer.data) delete[] buffer.data;
|
||||
buffer.size = settings.frequency * settings.latency / 1000.0 + 0.5;
|
||||
buffer.data = new uint32_t[buffer.size]();
|
||||
delete[] _buffer;
|
||||
_bufferSize = _frequency * _latency / 1000.0 + 0.5;
|
||||
_buffer = new uint32_t[_bufferSize]();
|
||||
}
|
||||
|
||||
bool _ready = false;
|
||||
string _device;
|
||||
bool _blocking = true;
|
||||
uint _channels = 2;
|
||||
uint _frequency = 48000;
|
||||
uint _latency = 20;
|
||||
|
||||
ALCdevice* _openAL = nullptr;
|
||||
ALCcontext* _context = nullptr;
|
||||
ALuint _source = 0;
|
||||
ALenum _format = AL_FORMAT_STEREO16;
|
||||
uint _queueLength = 0;
|
||||
|
||||
uint32_t* _buffer = nullptr;
|
||||
uint _bufferLength = 0;
|
||||
uint _bufferSize = 0;
|
||||
};
|
||||
|
@@ -23,7 +23,7 @@ struct AudioOSS : Audio {
|
||||
Information information;
|
||||
information.devices = {"/dev/dsp"};
|
||||
for(auto& device : directory::files("/dev/", "dsp?*")) information.devices.append(string{"/dev/", device});
|
||||
information.frequencies = {44100, 48000, 96000};
|
||||
information.frequencies = {44100.0, 48000.0, 96000.0};
|
||||
information.latencies = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
|
||||
information.channels = {1, 2};
|
||||
return information;
|
||||
@@ -32,7 +32,7 @@ struct AudioOSS : Audio {
|
||||
auto device() -> string { return _device; }
|
||||
auto blocking() -> bool { return _blocking; }
|
||||
auto channels() -> uint { return _channels; }
|
||||
auto frequency() -> uint { return _frequency; }
|
||||
auto frequency() -> double { return _frequency; }
|
||||
auto latency() -> uint { return _latency; }
|
||||
|
||||
auto setDevice(string device) -> bool {
|
||||
@@ -54,7 +54,7 @@ struct AudioOSS : Audio {
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto setFrequency(uint frequency) -> bool {
|
||||
auto setFrequency(double frequency) -> bool {
|
||||
if(_frequency == frequency) return true;
|
||||
_frequency = frequency;
|
||||
return initialize();
|
||||
@@ -78,6 +78,10 @@ private:
|
||||
auto initialize() -> bool {
|
||||
terminate();
|
||||
|
||||
if(!information().devices.find(_device)) {
|
||||
_device = information().devices.left();
|
||||
}
|
||||
|
||||
_fd = open(_device, O_WRONLY, O_NONBLOCK);
|
||||
if(_fd < 0) return false;
|
||||
|
||||
@@ -86,9 +90,11 @@ private:
|
||||
//policy: 0 = minimum latency (higher CPU usage); 10 = maximum latency (lower CPU usage)
|
||||
int policy = min(10, _latency);
|
||||
ioctl(_fd, SNDCTL_DSP_POLICY, &policy);
|
||||
ioctl(_fd, SNDCTL_DSP_CHANNELS, &_channels);
|
||||
int channels = _channels;
|
||||
ioctl(_fd, SNDCTL_DSP_CHANNELS, &channels);
|
||||
ioctl(_fd, SNDCTL_DSP_SETFMT, &_format);
|
||||
ioctl(_fd, SNDCTL_DSP_SPEED, &_frequency);
|
||||
int frequency = _frequency;
|
||||
ioctl(_fd, SNDCTL_DSP_SPEED, &frequency);
|
||||
|
||||
updateBlocking();
|
||||
return _ready = true;
|
||||
@@ -110,11 +116,11 @@ private:
|
||||
}
|
||||
|
||||
bool _ready = false;
|
||||
string _device = "/dev/dsp";
|
||||
string _device;
|
||||
bool _blocking = true;
|
||||
int _channels = 2;
|
||||
int _frequency = 48000;
|
||||
int _latency = 2;
|
||||
uint _channels = 2;
|
||||
double _frequency = 48000.0;
|
||||
uint _latency = 2;
|
||||
|
||||
int _fd = -1;
|
||||
int _format = AFMT_S16_LE;
|
||||
|
@@ -6,157 +6,161 @@
|
||||
#include <endpointvolume.h>
|
||||
|
||||
struct AudioWASAPI : Audio {
|
||||
~AudioWASAPI() { term(); }
|
||||
AudioWASAPI() { initialize(); }
|
||||
~AudioWASAPI() { terminate(); }
|
||||
|
||||
struct {
|
||||
bool exclusive = false;
|
||||
uint latency = 80;
|
||||
bool synchronize = true;
|
||||
} settings;
|
||||
auto ready() -> bool { return _ready; }
|
||||
|
||||
struct {
|
||||
uint channels = 0;
|
||||
uint frequency = 0;
|
||||
uint mode = 0;
|
||||
uint precision = 0;
|
||||
} device;
|
||||
|
||||
auto cap(const string& name) -> bool {
|
||||
if(name == Audio::Exclusive) return true;
|
||||
if(name == Audio::Latency) return true;
|
||||
if(name == Audio::Synchronize) return true;
|
||||
if(name == Audio::Frequency) return true;
|
||||
return false;
|
||||
auto information() -> Information {
|
||||
Information information;
|
||||
information.devices = {"Default"};
|
||||
information.channels = {2};
|
||||
information.frequencies = {};
|
||||
information.latencies = {20, 40, 60, 80, 100};
|
||||
return information;
|
||||
}
|
||||
|
||||
auto get(const string& name) -> any {
|
||||
if(name == Audio::Exclusive) return settings.exclusive;
|
||||
if(name == Audio::Latency) return settings.latency;
|
||||
if(name == Audio::Synchronize) return settings.synchronize;
|
||||
if(name == Audio::Frequency) return device.frequency;
|
||||
return {};
|
||||
auto exclusive() -> bool { return _exclusive; }
|
||||
auto blocking() -> bool { return _blocking; }
|
||||
auto channels() -> uint { return _channels; }
|
||||
auto frequency() -> double { return (double)_frequency; }
|
||||
auto latency() -> uint { return _latency; }
|
||||
|
||||
auto setExclusive(bool exclusive) -> bool {
|
||||
if(_exclusive == exclusive) return true;
|
||||
_exclusive = exclusive;
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto set(const string& name, const any& value) -> bool {
|
||||
if(name == Audio::Exclusive && value.get<bool>()) {
|
||||
if(audioDevice) term(), init();
|
||||
settings.exclusive = value.get<bool>();
|
||||
return true;
|
||||
}
|
||||
|
||||
if(name == Audio::Latency && value.get<uint>()) {
|
||||
if(audioDevice) term(), init();
|
||||
settings.latency = value.get<uint>();
|
||||
return true;
|
||||
}
|
||||
|
||||
if(name == Audio::Synchronize && value.is<bool>()) {
|
||||
settings.synchronize = value.get<bool>();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
auto setBlocking(bool blocking) -> bool {
|
||||
if(_blocking == blocking) return true;
|
||||
_blocking = blocking;
|
||||
return true;
|
||||
}
|
||||
|
||||
auto sample(int16_t left, int16_t right) -> void {
|
||||
queuedFrames.append((uint16_t)left << 0 | (uint16_t)right << 16);
|
||||
auto setFrequency(double frequency) -> bool {
|
||||
if(_frequency == frequency) return true;
|
||||
_frequency = frequency;
|
||||
return initialize();
|
||||
}
|
||||
|
||||
if(!available() && queuedFrames.size() >= bufferSize) {
|
||||
if(settings.synchronize) while(!available()); //wait for free sample slot
|
||||
else queuedFrames.takeLeft(); //drop sample (run ahead)
|
||||
auto setLatency(uint latency) -> bool {
|
||||
if(_latency == latency) return true;
|
||||
_latency = latency;
|
||||
return initialize();
|
||||
}
|
||||
|
||||
auto clear() -> void {
|
||||
_audioClient->Stop();
|
||||
_audioClient->Reset();
|
||||
for(auto n : range(available())) write(0, 0);
|
||||
_audioClient->Start();
|
||||
}
|
||||
|
||||
auto output(const double samples[]) -> void {
|
||||
_queuedFrames.append(int16_t(samples[0] * 32768.0) << 0 | int16_t(samples[1] * 32768.0) << 16);
|
||||
|
||||
if(!available() && _queuedFrames.size() >= _bufferSize) {
|
||||
if(_blocking) {
|
||||
while(!available()); //wait for free sample slot
|
||||
} else {
|
||||
_queuedFrames.takeLeft(); //drop sample (run ahead)
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t cachedFrame = 0;
|
||||
for(auto n : range(available())) {
|
||||
if(queuedFrames) cachedFrame = queuedFrames.takeLeft();
|
||||
if(_queuedFrames) cachedFrame = _queuedFrames.takeLeft();
|
||||
write(cachedFrame >> 0, cachedFrame >> 16);
|
||||
}
|
||||
}
|
||||
|
||||
auto clear() -> void {
|
||||
audioClient->Stop();
|
||||
audioClient->Reset();
|
||||
for(auto n : range(available())) write(0, 0);
|
||||
audioClient->Start();
|
||||
}
|
||||
private:
|
||||
auto initialize() -> bool {
|
||||
if(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&_enumerator) != S_OK) return false;
|
||||
if(_enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &_audioDevice) != S_OK) return false;
|
||||
if(_audioDevice->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void**)&_audioClient) != S_OK) return false;
|
||||
|
||||
auto init() -> bool {
|
||||
if(CoCreateInstance(CLSID_MMDeviceEnumerator, nullptr, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator) != S_OK) return false;
|
||||
if(enumerator->GetDefaultAudioEndpoint(eRender, eConsole, &audioDevice) != S_OK) return false;
|
||||
if(audioDevice->Activate(IID_IAudioClient, CLSCTX_ALL, nullptr, (void**)&audioClient) != S_OK) return false;
|
||||
|
||||
if(settings.exclusive) {
|
||||
if(audioDevice->OpenPropertyStore(STGM_READ, &propertyStore) != S_OK) return false;
|
||||
if(propertyStore->GetValue(PKEY_AudioEngine_DeviceFormat, &propVariant) != S_OK) return false;
|
||||
waveFormat = (WAVEFORMATEX*)propVariant.blob.pBlobData;
|
||||
if(audioClient->GetDevicePeriod(nullptr, &devicePeriod) != S_OK) return false;
|
||||
auto latency = max(devicePeriod, (REFERENCE_TIME)settings.latency * 10'000); //1ms to 100ns units
|
||||
if(audioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, 0, latency, latency, waveFormat, nullptr) != S_OK) return false;
|
||||
if(_exclusive) {
|
||||
if(_audioDevice->OpenPropertyStore(STGM_READ, &_propertyStore) != S_OK) return false;
|
||||
if(_propertyStore->GetValue(PKEY_AudioEngine_DeviceFormat, &_propVariant) != S_OK) return false;
|
||||
_waveFormat = (WAVEFORMATEX*)_propVariant.blob.pBlobData;
|
||||
if(_audioClient->GetDevicePeriod(nullptr, &_devicePeriod) != S_OK) return false;
|
||||
auto latency = max(_devicePeriod, (REFERENCE_TIME)_latency * 10'000); //1ms to 100ns units
|
||||
if(_audioClient->Initialize(AUDCLNT_SHAREMODE_EXCLUSIVE, 0, latency, latency, _waveFormat, nullptr) != S_OK) return false;
|
||||
DWORD taskIndex = 0;
|
||||
taskHandle = AvSetMmThreadCharacteristics(L"Pro Audio", &taskIndex);
|
||||
_taskHandle = AvSetMmThreadCharacteristics(L"Pro Audio", &taskIndex);
|
||||
} else {
|
||||
if(audioClient->GetMixFormat(&waveFormat) != S_OK) return false;
|
||||
if(audioClient->GetDevicePeriod(&devicePeriod, nullptr)) return false;
|
||||
auto latency = max(devicePeriod, (REFERENCE_TIME)settings.latency * 10'000); //1ms to 100ns units
|
||||
if(audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, latency, 0, waveFormat, nullptr) != S_OK) return false;
|
||||
if(_audioClient->GetMixFormat(&waveFormat) != S_OK) return false;
|
||||
if(_audioClient->GetDevicePeriod(&_devicePeriod, nullptr)) return false;
|
||||
auto latency = max(_devicePeriod, (REFERENCE_TIME)_latency * 10'000); //1ms to 100ns units
|
||||
if(_audioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, latency, 0, _waveFormat, nullptr) != S_OK) return false;
|
||||
}
|
||||
|
||||
if(audioClient->GetService(IID_IAudioRenderClient, (void**)&renderClient) != S_OK) return false;
|
||||
if(audioClient->GetBufferSize(&bufferSize) != S_OK) return false;
|
||||
if(_audioClient->GetService(IID_IAudioRenderClient, (void**)&_renderClient) != S_OK) return false;
|
||||
if(_audioClient->GetBufferSize(&_bufferSize) != S_OK) return false;
|
||||
|
||||
device.channels = waveFormat->nChannels;
|
||||
device.frequency = waveFormat->nSamplesPerSec;
|
||||
device.mode = ((WAVEFORMATEXTENSIBLE*)waveFormat)->SubFormat.Data1;
|
||||
device.precision = waveFormat->wBitsPerSample;
|
||||
_channels = waveFormat->nChannels;
|
||||
_frequency = waveFormat->nSamplesPerSec;
|
||||
_mode = ((WAVEFORMATEXTENSIBLE*)_waveFormat)->SubFormat.Data1;
|
||||
_precision = _waveFormat->wBitsPerSample;
|
||||
|
||||
audioClient->Start();
|
||||
return true;
|
||||
_audioClient->Start();
|
||||
return _ready = true;
|
||||
}
|
||||
|
||||
auto term() -> void {
|
||||
if(audioClient) audioClient->Stop();
|
||||
if(renderClient) renderClient->Release(), renderClient = nullptr;
|
||||
if(waveFormat) CoTaskMemFree(waveFormat), waveFormat = nullptr;
|
||||
if(audioClient) audioClient->Release(), audioClient = nullptr;
|
||||
if(audioDevice) audioDevice->Release(), audioDevice = nullptr;
|
||||
if(taskHandle) AvRevertMmThreadCharacteristics(taskHandle), taskHandle = nullptr;
|
||||
auto terminate() -> void {
|
||||
if(_audioClient) _audioClient->Stop();
|
||||
if(_renderClient) _renderClient->Release(), _renderClient = nullptr;
|
||||
if(_waveFormat) CoTaskMemFree(_waveFormat), _waveFormat = nullptr;
|
||||
if(_audioClient) _audioClient->Release(), _audioClient = nullptr;
|
||||
if(_audioDevice) _audioDevice->Release(), _audioDevice = nullptr;
|
||||
if(_taskHandle) AvRevertMmThreadCharacteristics(_taskHandle), _taskHandle = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
auto available() -> uint {
|
||||
uint32_t padding = 0;
|
||||
audioClient->GetCurrentPadding(&padding);
|
||||
_audioClient->GetCurrentPadding(&padding);
|
||||
return bufferSize - padding;
|
||||
}
|
||||
|
||||
auto write(int16_t left, int16_t right) -> void {
|
||||
if(renderClient->GetBuffer(1, &bufferData) != S_OK) return;
|
||||
if(_renderClient->GetBuffer(1, &_bufferData) != S_OK) return;
|
||||
|
||||
if(device.channels >= 2 && device.mode == 1 && device.precision == 16) {
|
||||
auto buffer = (int16_t*)bufferData;
|
||||
if(_channels >= 2 && _mode == 1 && _precision == 16) {
|
||||
auto buffer = (int16_t*)_bufferData;
|
||||
buffer[0] = left;
|
||||
buffer[1] = right;
|
||||
}
|
||||
|
||||
if(device.channels >= 2 && device.mode == 3 && device.precision == 32) {
|
||||
auto buffer = (float*)bufferData;
|
||||
buffer[0] = left / 32768.0;
|
||||
if(_channels >= 2 && _mode == 3 && _precision == 32) {
|
||||
auto buffer = (float*)_bufferData;
|
||||
buffer[0] = left / 32768.0;
|
||||
buffer[1] = right / 32768.0;
|
||||
}
|
||||
|
||||
renderClient->ReleaseBuffer(1, 0);
|
||||
_renderClient->ReleaseBuffer(1, 0);
|
||||
}
|
||||
|
||||
IMMDeviceEnumerator* enumerator = nullptr;
|
||||
IMMDevice* audioDevice = nullptr;
|
||||
IPropertyStore* propertyStore = nullptr;
|
||||
IAudioClient* audioClient = nullptr;
|
||||
IAudioRenderClient* renderClient = nullptr;
|
||||
WAVEFORMATEX* waveFormat = nullptr;
|
||||
PROPVARIANT propVariant;
|
||||
HANDLE taskHandle = nullptr;
|
||||
REFERENCE_TIME devicePeriod = 0;
|
||||
uint32_t bufferSize = 0; //in frames
|
||||
uint8_t* bufferData = nullptr;
|
||||
vector<uint32_t> queuedFrames;
|
||||
bool _exclusive = false;
|
||||
bool _blocking = true;
|
||||
uint _channels = 2;
|
||||
uint _frequency = 48000;
|
||||
uint _latency = 20;
|
||||
|
||||
uint _mode = 0;
|
||||
uint _precision = 0;
|
||||
|
||||
IMMDeviceEnumerator* _enumerator = nullptr;
|
||||
IMMDevice* _audioDevice = nullptr;
|
||||
IPropertyStore* _propertyStore = nullptr;
|
||||
IAudioClient* _audioClient = nullptr;
|
||||
IAudioRenderClient* _renderClient = nullptr;
|
||||
WAVEFORMATEX* _waveFormat = nullptr;
|
||||
PROPVARIANT _propVariant;
|
||||
HANDLE _taskHandle = nullptr;
|
||||
REFERENCE_TIME _devicePeriod = 0;
|
||||
uint32_t _bufferSize = 0; //in frames
|
||||
uint8_t* _bufferData = nullptr;
|
||||
vector<uint32_t> _queuedFrames;
|
||||
};
|
||||
|
Reference in New Issue
Block a user