Skip to content

Instantly share code, notes, and snippets.

@Themaister
Created May 28, 2012 12:29
Show Gist options
  • Save Themaister/2818911 to your computer and use it in GitHub Desktop.
Save Themaister/2818911 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>
#include <stdexcept>
OSS::OSS() : fd(-1) {}
OSS::~OSS()
{
stop();
}
std::string OSS::default_device() const
{
return "/dev/dsp";
}
void OSS::init(unsigned channels, unsigned rate, const std::string &dev)
{
if (fd >= 0)
close(fd);
fd = open(dev.c_str(), O_WRONLY);
if (fd < 0)
throw std::runtime_error("Failed to open OSS device.\n");
int tmp = channels;
if (ioctl(fd, SNDCTL_DSP_CHANNELS, &tmp) < 0 || tmp != static_cast<int>(channels))
throw std::runtime_error("Failed to set number of channels.\n");
tmp = rate;
if (ioctl(fd, SNDCTL_DSP_SPEED, &tmp) < 0 || tmp != static_cast<int>(rate))
throw std::runtime_error("Failed to set sample rate.\n");
tmp = AFMT_S16_LE;
if (ioctl(fd, SNDCTL_DSP_SETFMT, &tmp) < 0 || tmp != AFMT_S16_LE)
throw std::runtime_error("Failed to set sample format.\n");
}
void OSS::stop()
{
if (fd >= 0)
{
close(fd);
fd = -1;
}
}
void OSS::write(const FF::Buffer &buf)
{
const std::uint8_t *data = buf.data();
std::size_t to_write = buf.size();
while (to_write)
{
ssize_t ret = ::write(fd, data, to_write);
if (ret <= 0)
throw std::runtime_error("Failed to write data.\n");
data += ret;
to_write -= ret;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment