sndio(7)は音声を統一的に扱うための枠組み(フレームワーク)で、Alexandre Ratchovらによって2008年リリースのOpenBSD 4.5に始めて導入された。 現在はFreeBSD/NetBSD/Linuxにも移植されている。
ALSA, JACK, OSS, PulseAudioなどの音声フレームワークと同様、sndioは音声を扱うハードウェアとアプリケーションとの橋渡しをする。 具体的には、sndioは以下のような機能を持っている。
以下に、OpenBSDでsndioフレームワークが動作している様子の例を示す。
$ audioctl name=azalia0 mode=play,record pause=0 active=1 nblks=8 blksz=960 rate=48000 encoding=s16le play.channels=2 play.bytes=3796930560 play.errors=883200 record.channels=2 record.bytes=3796930560 record.errors=552960mixerctlの実行例
$ mixerctl -v | sort
inputs.dac-0:1=234,234
inputs.beep=119
inputs.beep_mute=off [ off on ]
inputs.dac-2:3=234,234
inputs.hp_source=sel6,mix6 { sel6 mix6 }
inputs.mic2=0,0
inputs.mic3=0,0
inputs.mic3_source=sel7,mix6 { sel7 mix6 }
inputs.mic=0,0
inputs.mix4_source=sel3,mix6 { sel3 mix6 }
inputs.mix6_mic2=0,0
inputs.mix6_mic=0,0
inputs.mix6_source=mic,mic2 { mic mic2 }
inputs.sel3_source=dac-0:1 [ dac-0:1 dac-2:3 ]
inputs.sel4_source=dac-0:1 [ dac-0:1 dac-2:3 ]
inputs.sel6_source=dac-0:1 [ dac-0:1 dac-2:3 ]
inputs.sel7_source=dac-0:1 [ dac-0:1 dac-2:3 ]
inputs.spkr_source=dac-2:3,mix6 { dac-2:3 mix6 }
outputs.hp_boost=off [ off on ]
outputs.hp_mute=off [ off on ]
outputs.hp_sense=plugged [ unplugged plugged ]
outputs.master.mute=off [ off on ]
outputs.master.slaves=dac-0:1,dac-2:3,hp,spkr { dac-0:1 dac-2:3 beep hp spkr mic3 mix6 mic3 }
outputs.master=255,255
outputs.mic2_dir=input-vr80 [ none input input-vr0 input-vr50 input-vr80 input-vr100 ]
outputs.mic3_dir=input-vr80 [ none output input input-vr0 input-vr50 input-vr80 input-vr100 ]
outputs.mic3_mute=off [ off on ]
outputs.mic3_sense=unplugged [ unplugged plugged ]
outputs.mic_dir=input-vr80 [ none input input-vr0 input-vr50 input-vr80 input-vr100 ]
outputs.mic_sense=plugged [ unplugged plugged ]
outputs.mix6=0,0
outputs.mix6_mute=off [ off on ]
outputs.spkr_boost=off [ off on ]
outputs.spkr_eapd=on [ off on ]
outputs.spkr_mute=on [ off on ]
outputs.spkr_muters=hp,mic3 { hp mic3 }
record.adc-0:1=200,200
record.adc-0:1_mute=off [ off on ]
record.adc-0:1_source=mic [ mic mic2 ]
record.adc-2:3=200,200
record.adc-2:3_mute=off [ off on ]
record.adc-2:3_source=mic2 [ mic mic2 ]
record.volume.mute=off [ off on ]
record.volume.slaves=adc-2:3,adc-0:1 { adc-2:3 adc-0:1 mic mic2 }
record.volume=200,200 portaudioを使っているportsを数えてみる $ cd /usr/ports $ find * -type d -name patches | xargs grep -rils 'portaudio' | cut -d/ -f1,2 \ | sort | uniq | wc -l 12
sndio対応のパッチを当てているportsを数えてみる $ cd /usr/ports $ find * -type d -name patches | xargs fgrep -rls 'sndio.h' | cut -d/ -f1,2 \ | sort | uniq | wc -l 41
音声デバイスが無制限に共有されないように、sndioではセッションクッキーを使った認証を行う。
sndiodに接続時、音声アプリケーションは$HOME/.aucat_cookieに可能された128ビットのランダムなデータをクッキーとしてsndioに提示する。
クッキーとして$HOME/.aucat_cookieを使うため、通常は同一のユーザからの接続のみが同時に受け入れられるが、$HOME/.aucat_cookieを同じ内容にすれば異ったユーザ間でもsndiodの共有が可能となる。
sndiodはデフォルトではUNIXドメインソケットでの接続のみ受け付ける。INETドメインでの接続を受け付ける場合は-Lオプションで明示的に指定する必要がある。
sndio APIを使用するには、libsndioより提供される各種関数を使う。
sio_*が音声ストリーム関連、mio_*がMIDIストリーム関連の関数となる。
処理の流れ:
sio_initpar() /* パラメータの初期化 */ ↓ sio_open() /* 音声デバイスや音声サーバに接続 */ ↓ sio_setpar() /* パラメータの設定 */ ←←←← sio_getpar() /* 設定結果の確認 */ ↑ ↓ ↑ sio_start() /* 処理の起動 */ ←←←-↑ ↓ ↑ sio_read(), sio_write() /* 入出力 */ ↑ ↓ ↑ ↓→→パラメータを変更する場合→→ sio_stop() →↑ ↓ 処理を一時的に中断する場合など ↓ sio_close() /* 接続を閉じる */
MIDI接続の場合も、これに準ずる。
sio_open()では、一番目の引数で、以下の記法によって接続先を指定する:
type[@hostname][,unit]/devnum[.option]
この記法はsndiodやaucatなどで音源のデバイスやサーバに接続する場合の接続先指定としても使用される。
例:
rsnd/0 ... /dev/audio0 に直接アクセス (sndiodを介さない)
snd/0.rear ... デフォルトで起動しているsndiodのリアスピーカ出力
参考:
# rcctl set sndiod flags '-s default -m play,mon -s mon' # rcctl restart sndiodこの設定を行っておくとffmpeg実行時、-i snd/0.mon を指定することで、スクリーンキャプチャ時にアプリケーションの再生音も同時に収録できる。
$ ffmpeg -f x11grab -s 800x600 -i :0.0+100,100 -f sndio -i snd/0.mon screencapt.mpg
remote_host # rcctl set sndiod flags '-L -' remote_host # rcctl restart sndiodローカルでのコマンド実行
local_host $ aucat -f snd@remote_host/0 -o - | lame -r -s 48 - recinput.mp3
/* sin_osc ... generates sine waves
* and outputs to sndio default device
*
* Ch. 1: 1kHz, -6dBFS
* Ch. 2: 400Hz, -6dBFS
*
* Duration: 60 secs
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <sndio.h>
/* frequency parameters in Hz */
#define SAMPLE_FREQ 48000.0
#define OUTPUT_FREQ1 1000.0
#define OUTPUT_FREQ2 400.0
/* buffer size of waveform */
#define SAMPLES 480
/* SAMPLES per CYCLE */
#define SPC1 (SAMPLE_FREQ/OUTPUT_FREQ1)
#define SPC2 (SAMPLE_FREQ/OUTPUT_FREQ2)
/* output level of sine wave with:
* sampling point 't',
* samples per cycle 'spc'
*/
#define SINVAL(t, spc) ((int16_t)(INT16_MAX*sin(2.0*M_PI*t/spc)/2))
void fail_exit(const char *msg) {
fprintf(stderr, "%s failed\n", msg);
exit(1);
}
int main() {
/* waveform buffer */
int16_t wf[2*SAMPLES], /* signed 16bit per sample */
*p_wf;
/* fill buffer */
p_wf = wf;
for (int i = 0; i<SAMPLES; i++) {
*p_wf++ = SINVAL(i, SPC1); /* 1kHz to Ch1 */
*p_wf++ = SINVAL(i, SPC2); /* 400Hz to Ch2 */
}
/* initialization of sndio */
struct sio_par par;
struct sio_hdl *hdl;
ssize_t n;
sio_initpar(&par);
par.sig = 1;
par.bits = 16;
par.pchan = 2;
par.rate = 48000;
hdl = sio_open("snd/0", SIO_PLAY, 0); /* "snd/0" may be SIO_DEVANY */
if (hdl == NULL) /* or "default". */
fail_exit("sio_open()");
if (!sio_setpar(hdl, &par))
fail_exit("sio_setpar()");
if (!sio_getpar(hdl, &par))
fail_exit("sio_getpar()");
if (!sio_start(hdl))
fail_exit("sio_start()");
/* output for 60 secs */
for (int i=0; i<6000; i++) {
n = sio_write(hdl, wf, sizeof(wf));
if (n == 0)
fail_exit("sio_write()");
}
sio_close(hdl);
return 0;
}
$ LDFLAGS='-lm -lsndio' make sin_osc cc -O2 -pipe -lm -lsndio -o sin_osc sin_osc.c $ ./sin_osc