bsnes/ruby/audio/openal.cpp
Tim Allen afa8ea61c5 Update to v104r06 release.
byuu says:

Changelog:

  - gba,ws: removed Thread::step() override¹
  - processor/m68k: move.b (a7)+ and move.b (a7)- adjust a7 by two, not
    by one²
  - tomoko: created new initialize(Video,Audio,Input)Driver() functions³
  - ruby/audio: split Audio::information into
    Audio::available(Devices,Frequencies,Latencies,Channels)³
  - ws: added Model::(WonderSwan,WonderSwanColor,SwanCrystal)()
    functions for consistency with other cores

¹: this should hopefully fix GBA Pokemon Pinball. Thanks to
SuperMikeMan for pointing out the underlying cause.

²: this fixes A Ressaha de Ikou, Mega Bomberman, and probably more
games.

³: this is the big change: so there was a problem with WASAPI where
you might change your device under the audio settings panel. And your
new device may not support the frequency that your old device used. This
would end up not updating the frequency, and the pitch would be
distorted.

The old Audio::information() couldn't tell you what frequencies,
latencies, or channels were available for all devices simultaneously, so
I had to split them up. The new initializeAudioDriver() function
validates you have a correct driver, or it defaults to none. Then it
validates a correct device name, or it defaults to the first entry in
the list. Then it validates a correct frequency, or defaults to the
first in the list. Then finally it validates a correct latency, or
defaults to the first in the list.

In this way ... we have a clear path now with no API changes required to
select default devices, frequencies, latencies, channel counts: they
need to be the first items in their respective lists.

So, what we need to do now is go through and for every audio driver that
enumerates devices, we need to make sure the default device gets added
to the top of the list. I'm ... not really sure how to do this with most
drivers, so this is definitely going to take some time.

Also, when you change a device, initializeAudioDriver() is called again,
so if it's a bad device, it will disable the audio driver instead of
continuing to send samples at it and hoping that the driver blocked
those API calls when it failed to initialize properly.

Now then ... since it was a decently-sized API change, it's possible
I've broken compilation of the Linux drivers, so please report any
compilation errors so that I can fix them.
2017-08-26 11:15:49 +10:00

202 lines
5.2 KiB
C++

#if defined(PLATFORM_MACOSX)
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif
struct AudioOpenAL : Audio {
AudioOpenAL() { initialize(); }
~AudioOpenAL() { terminate(); }
auto availableDevices() -> string_vector {
string_vector devices;
for(auto& device : queryDevices()) devices.append(device);
return devices;
}
auto availableFrequencies() -> vector<double> {
return {44100.0, 48000.0, 96000.0};
}
auto availableLatencies() -> vector<uint> {
return {20, 40, 60, 80, 100};
}
auto availableChannels() -> vector<uint> {
return {2};
}
auto ready() -> bool { return _ready; }
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 setBlocking(bool blocking) -> bool {
if(_blocking == blocking) return true;
_blocking = blocking;
return true;
}
auto setFrequency(double frequency) -> bool {
if(_frequency == (uint)frequency) return true;
_frequency = (uint)frequency;
return initialize();
}
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(_source, AL_BUFFERS_PROCESSED, &processed);
while(processed--) {
alSourceUnqueueBuffers(_source, 1, &alBuffer);
alDeleteBuffers(1, &alBuffer);
_queueLength--;
}
//wait for buffer playback to catch up to sample generation if not synchronizing
if(!_blocking || _queueLength < 3) break;
}
if(_queueLength < 3) {
alGenBuffers(1, &alBuffer);
alBufferData(alBuffer, _format, _buffer, _bufferSize * 4, _frequency);
alSourceQueueBuffers(_source, 1, &alBuffer);
_queueLength++;
}
ALint playing;
alGetSourcei(_source, AL_SOURCE_STATE, &playing);
if(playing != AL_PLAYING) alSourcePlay(_source);
_bufferLength = 0;
}
private:
auto initialize() -> bool {
terminate();
if(!queryDevices().find(_device)) _device = "";
_queueLength = 0;
updateLatency();
bool success = false;
if(_openAL = alcOpenDevice(_device)) {
if(_context = alcCreateContext(_openAL, nullptr)) {
alcMakeContextCurrent(_context);
alGenSources(1, &_source);
//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 listenerOrientation[] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
alListenerfv(AL_ORIENTATION, listenerOrientation);
success = true;
}
}
if(!success) return terminate(), false;
return _ready = true;
}
auto terminate() -> void {
_ready = false;
if(alIsSource(_source) == AL_TRUE) {
int playing = 0;
alGetSourcei(_source, AL_SOURCE_STATE, &playing);
if(playing == AL_PLAYING) {
alSourceStop(_source);
int queued = 0;
alGetSourcei(_source, AL_BUFFERS_QUEUED, &queued);
while(queued--) {
ALuint alBuffer = 0;
alSourceUnqueueBuffers(_source, 1, &alBuffer);
alDeleteBuffers(1, &alBuffer);
_queueLength--;
}
}
alDeleteSources(1, &_source);
_source = 0;
}
if(_context) {
alcMakeContextCurrent(nullptr);
alcDestroyContext(_context);
_context = nullptr;
}
if(_openAL) {
alcCloseDevice(_openAL);
_openAL = nullptr;
}
delete[] _buffer;
_buffer = nullptr;
}
auto queryDevices() -> string_vector {
string_vector result;
const char* list = alcGetString(nullptr, ALC_DEVICE_SPECIFIER);
if(!list) return result;
while(list && *list) {
result.append(list);
list += strlen(list) + 1;
}
return result;
}
auto updateLatency() -> void {
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;
};