1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#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 : AudioDriver {
AudioOSS& self = *this;
AudioOSS(Audio& super) : AudioDriver(super) {}
~AudioOSS() { terminate(); }
auto create() -> bool override {
super.setDevice("/dev/dsp");
super.setChannels(2);
super.setFrequency(48000);
super.setLatency(3);
buffer.resize(64);
return initialize();
}
auto driver() -> string override { return "OSS"; }
auto ready() -> bool override { return _fd >= 0; }
auto hasBlocking() -> bool override { return true; }
auto hasDynamic() -> bool override { return true; }
auto hasDevices() -> vector<string> override {
vector<string> devices;
devices.append("/dev/dsp");
for(auto& device : directory::files("/dev/", "dsp?*")) devices.append(string{"/dev/", device});
return devices;
}
auto hasChannels() -> vector<u32> override {
return {1, 2};
}
auto hasFrequencies() -> vector<u32> override {
return {44100, 48000, 96000};
}
auto hasLatencies() -> vector<u32> override {
return {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
}
auto setDevice(string device) -> bool override { return initialize(); }
auto setBlocking(bool blocking) -> bool override { return updateBlocking(); }
auto setChannels(u32 channels) -> bool override { return initialize(); }
auto setFrequency(u32 frequency) -> bool override { return initialize(); }
auto setLatency(u32 latency) -> bool override { return initialize(); }
auto clear() -> void override {
buffer.resize(64);
}
auto level() -> double override {
audio_buf_info info;
ioctl(_fd, SNDCTL_DSP_GETOSPACE, &info);
return (double)(_bufferSize - info.bytes) / _bufferSize;
}
auto output(const double samples[]) -> void override {
for(u32 n : range(self.channels)) {
buffer.write(sclamp<16>(samples[n] * 32767.0));
if(buffer.full()) {
write(_fd, buffer.data(), buffer.capacity<u8>());
buffer.flush();
}
}
}
private:
auto initialize() -> bool {
terminate();
if(!hasDevices().find(self.device)) self.device = hasDevices().first();
_fd = open(self.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, self.latency);
ioctl(_fd, SNDCTL_DSP_POLICY, &policy);
int channels = self.channels;
ioctl(_fd, SNDCTL_DSP_CHANNELS, &channels);
ioctl(_fd, SNDCTL_DSP_SETFMT, &_format);
int frequency = self.frequency;
ioctl(_fd, SNDCTL_DSP_SPEED, &frequency);
updateBlocking();
audio_buf_info info;
ioctl(_fd, SNDCTL_DSP_GETOSPACE, &info);
_bufferSize = info.bytes;
return true;
}
auto terminate() -> void {
if(!ready()) return;
close(_fd);
_fd = -1;
}
auto updateBlocking() -> bool {
if(!ready()) return false;
auto flags = fcntl(_fd, F_GETFL);
if(flags < 0) return false;
self.blocking ? flags &=~ O_NONBLOCK : flags |= O_NONBLOCK;
fcntl(_fd, F_SETFL, flags);
return true;
}
s32 _fd = -1;
s32 _format = AFMT_S16_LE;
s32 _bufferSize = 1;
queue<s16> buffer;
};
|