mirror of
https://github.com/bsnes-emu/bsnes.git
synced 2025-02-24 07:02:27 +01:00
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.
128 lines
3.2 KiB
C++
128 lines
3.2 KiB
C++
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ioctl.h>
|
|
#include <sys/soundcard.h>
|
|
|
|
//OSSv4 features: define fallbacks for OSSv3 (where these ioctls are ignored)
|
|
|
|
#ifndef SNDCTL_DSP_COOKEDMODE
|
|
#define SNDCTL_DSP_COOKEDMODE _IOW('P', 30, int)
|
|
#endif
|
|
|
|
#ifndef SNDCTL_DSP_POLICY
|
|
#define SNDCTL_DSP_POLICY _IOW('P', 45, int)
|
|
#endif
|
|
|
|
struct AudioOSS : Audio {
|
|
AudioOSS() { initialize(); }
|
|
~AudioOSS() { terminate(); }
|
|
|
|
auto ready() -> bool { return _ready; }
|
|
|
|
auto information() -> Information {
|
|
Information information;
|
|
information.devices = {"/dev/dsp"};
|
|
for(auto& device : directory::files("/dev/", "dsp?*")) information.devices.append(string{"/dev/", device});
|
|
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;
|
|
}
|
|
|
|
auto device() -> string { return _device; }
|
|
auto blocking() -> bool { return _blocking; }
|
|
auto channels() -> uint { return _channels; }
|
|
auto frequency() -> double { return _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;
|
|
updateBlocking();
|
|
return true;
|
|
}
|
|
|
|
auto setChannels(uint channels) -> bool {
|
|
if(_channels == channels) return true;
|
|
_channels = channels;
|
|
return initialize();
|
|
}
|
|
|
|
auto setFrequency(double frequency) -> bool {
|
|
if(_frequency == frequency) return true;
|
|
_frequency = frequency;
|
|
return initialize();
|
|
}
|
|
|
|
auto setLatency(uint latency) -> bool {
|
|
if(_latency == latency) return true;
|
|
_latency = latency;
|
|
return initialize();
|
|
}
|
|
|
|
auto output(const double samples[]) -> void {
|
|
if(!_ready) return;
|
|
for(auto n : range(_channels)) {
|
|
int16_t sample = samples[n] * 32768.0;
|
|
auto unused = write(_fd, &sample, 2);
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
int cooked = 1;
|
|
ioctl(_fd, SNDCTL_DSP_COOKEDMODE, &cooked);
|
|
//policy: 0 = minimum latency (higher CPU usage); 10 = maximum latency (lower CPU usage)
|
|
int policy = min(10, _latency);
|
|
ioctl(_fd, SNDCTL_DSP_POLICY, &policy);
|
|
int channels = _channels;
|
|
ioctl(_fd, SNDCTL_DSP_CHANNELS, &channels);
|
|
ioctl(_fd, SNDCTL_DSP_SETFMT, &_format);
|
|
int frequency = _frequency;
|
|
ioctl(_fd, SNDCTL_DSP_SPEED, &frequency);
|
|
|
|
updateBlocking();
|
|
return _ready = true;
|
|
}
|
|
|
|
auto terminate() -> void {
|
|
_ready = false;
|
|
if(_fd < 0) return;
|
|
close(_fd);
|
|
_fd = -1;
|
|
}
|
|
|
|
auto updateBlocking() -> void {
|
|
if(!_ready) return;
|
|
auto flags = fcntl(_fd, F_GETFL);
|
|
if(flags < 0) return;
|
|
_blocking ? flags &=~ O_NONBLOCK : flags |= O_NONBLOCK;
|
|
fcntl(_fd, F_SETFL, flags);
|
|
}
|
|
|
|
bool _ready = false;
|
|
string _device;
|
|
bool _blocking = true;
|
|
uint _channels = 2;
|
|
double _frequency = 48000.0;
|
|
uint _latency = 2;
|
|
|
|
int _fd = -1;
|
|
int _format = AFMT_S16_LE;
|
|
};
|