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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
|
/*******************************************************************************
* Goggles Audio Player Library *
********************************************************************************
* Copyright (C) 2010-2020 by Sander Jansen. All Rights Reserved *
* --- *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see http://www.gnu.org/licenses. *
********************************************************************************/
#include "ap_defs.h"
#include "ap_output_plugin.h"
#include <sndio.h>
using namespace ap;
namespace ap {
class SndioOutput : public OutputPlugin {
protected:
SndioConfig config;
sio_hdl * handle = nullptr;
FXint sio_delay = 0;
FXuint sio_volume = 0;
FXbool sio_started = false;
protected:
FXbool open();
void start();
void stop();
protected:
static void on_move(void * ptr, int delta) {
auto output = reinterpret_cast<SndioOutput*>(ptr);
output->sio_delay -= delta;
}
static void on_volume(void * ptr, unsigned int volume) {
auto output = reinterpret_cast<SndioOutput*>(ptr);
if (volume != output->sio_volume) {
GM_DEBUG_PRINT("[sndio] volume: %d\n", volume);
output->sio_volume = volume;
output->context->notify_volume((float)volume / (float)SIO_MAXVOL);
}
}
public:
SndioOutput(OutputContext* ctx);
/// Configure
FXbool configure(const AudioFormat &);
/// Write frames to playback buffer
FXbool write(const void*, FXuint);
/// Return delay in no. of frames
FXint delay();
/// Empty Playback Buffer Immediately
void drop();
/// Wait until playback buffer is emtpy.
void drain();
/// Pause
void pause(FXbool);
/// Change Volume
void volume(FXfloat);
/// Close Output
void close();
/// Get Device Type
FXchar type() const { return DeviceSndio; }
/// Set Device Configuration
FXbool setOutputConfig(const OutputConfig &);
/// Destructor
virtual ~SndioOutput();
};
SndioOutput::SndioOutput(OutputContext * ctx) : OutputPlugin(ctx) {
}
SndioOutput::~SndioOutput() {
close();
}
FXbool SndioOutput::setOutputConfig(const OutputConfig &c){
config=c.sndio;
return true;
}
FXbool SndioOutput::open() {
if (handle == nullptr) {
handle = sio_open(config.device.text(), SIO_PLAY, 0);
if (handle == nullptr) {
GM_DEBUG_PRINT("[sndio] Unable to open device %s.\n", config.device.text());
return false;
}
sio_onmove(handle, SndioOutput::on_move, this);
sio_onvol(handle, SndioOutput::on_volume, this);
sio_delay = 0;
}
return true;
}
void SndioOutput::close() {
if (handle != nullptr) {
drop();
sio_close(handle);
handle = nullptr;
af.reset();
}
}
void SndioOutput::start() {
if (handle != nullptr && sio_started == false) {
if (sio_start(handle) == 0) {
GM_DEBUG_PRINT("[sndio] unable to start output\n");
}
else {
sio_started = true;
GM_DEBUG_PRINT("[sndio] switch to START\n");
}
}
}
void SndioOutput::stop() {
if (handle != nullptr && sio_started == true) {
if (sio_stop(handle) == 0) {
GM_DEBUG_PRINT("[sndio] unable to stop output\n");
}
else {
sio_started = false;
sio_delay = 0;
GM_DEBUG_PRINT("[sndio] switch to STOP\n");
}
}
}
void SndioOutput::volume(FXfloat v) {
if (handle != nullptr) {
sio_volume = (unsigned int)(v * SIO_MAXVOL);
GM_DEBUG_PRINT("[sndio] volume: %d\n", sio_volume);
sio_setvol(handle, sio_volume);
}
}
FXint SndioOutput::delay() {
return sio_delay;
}
void SndioOutput::drop() {
GM_DEBUG_PRINT("[sndio] drop\n");
stop();
}
void SndioOutput::drain() {
GM_DEBUG_PRINT("[sndio] drain\n");
stop();
}
void SndioOutput::pause(FXbool pausing) {
if (pausing)
stop();
else
start();
}
FXbool SndioOutput::configure(const AudioFormat & fmt){
sio_par parameters;
sio_initpar(¶meters);
if (handle && fmt==af)
return true;
if (handle) {
drop();
}
if (!open())
return false;
parameters.bits = fmt.bps(); // bits per sample
parameters.bps = fmt.packing(); // bytes per sample
parameters.sig = (fmt.datatype() == Format::Signed || fmt.datatype() == Format::Float); // Signed or Unsigned format
parameters.le = fmt.byteorder() == Format::Little; // Byteorder
parameters.pchan = fmt.channels; // Channel Count
parameters.rate = fmt.rate; // Sample Rate
parameters.xrun = SIO_IGNORE; // xrun behaviour
if (sio_setpar(handle, ¶meters) == 0)
goto failed;
if (sio_getpar(handle, ¶meters) == 0)
goto failed;
af.set(parameters.sig ? Format::Signed : Format::Unsigned,
parameters.bits,
parameters.bps,
parameters.rate,
parameters.pchan);
return true;
failed:
GM_DEBUG_PRINT("[sndio] Unsupported configuration:\n");
af.debug();
return false;
}
FXbool SndioOutput::write(const void * buffer,FXuint nframes){
FXival nwritten;
FXival nbytes = nframes*af.framesize();
const FXchar * buf = (const FXchar*)buffer;
if (__unlikely(handle == nullptr)) {
GM_DEBUG_PRINT("[sndio] device not opened\n");
return false;
}
if (__unlikely(sio_started == false)) {
start();
}
while(nbytes>0) {
nwritten = sio_write(handle,buf,nbytes);
buf+=nwritten;
nbytes-=nwritten;
}
sio_delay += nframes;
return true;
}
}
AP_IMPLEMENT_PLUGIN(SndioOutput);
|