sq3: show SQLite error messages on stderr by default
[iv.d.git] / clibcelt / d / xalsa.d
blob2ad03f67607f187ad4957d8b2d86add6c6c7d381
1 module xalsa /*is aliced*/;
3 import iv.alice;
4 import iv.alsa;
5 import iv.follin.utils;
8 // ////////////////////////////////////////////////////////////////////////// //
9 __gshared snd_pcm_t* pcm;
10 __gshared uint Chans = 2;
12 enum Rate = 48000;
15 // ////////////////////////////////////////////////////////////////////////// //
16 void alsaOpen (int chans) {
17 if (chans < 1 || chans > 2) assert(0, "invalid number of channels");
18 Chans = chans;
19 int err;
20 if ((err = snd_pcm_open(&pcm, "plug:default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
21 import core.stdc.stdio : printf;
22 import core.stdc.stdlib : exit, EXIT_FAILURE;
23 printf("Playback open error: %s\n", snd_strerror(err));
24 exit(EXIT_FAILURE);
27 if ((err = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, Chans, Rate, 1, 500000)) < 0) {
28 import core.stdc.stdio : printf;
29 import core.stdc.stdlib : exit, EXIT_FAILURE;
30 printf("Playback open error: %s\n", snd_strerror(err));
31 exit(EXIT_FAILURE);
36 // ////////////////////////////////////////////////////////////////////////// //
37 void alsaClose () {
38 if (pcm !is null) {
39 snd_pcm_close(pcm);
40 pcm = null;
45 // ////////////////////////////////////////////////////////////////////////// //
46 void alsaWriteX (short* sptr, uint frms) {
47 while (frms > 0) {
48 snd_pcm_sframes_t frames = snd_pcm_writei(pcm, sptr, frms);
49 if (frames < 0) {
50 frames = snd_pcm_recover(pcm, cast(int)frames, 0);
51 if (frames < 0) {
52 import core.stdc.stdio : printf;
53 import core.stdc.stdlib : exit, EXIT_FAILURE;
54 printf("snd_pcm_writei failed: %s\n", snd_strerror(cast(int)frames));
55 exit(EXIT_FAILURE);
57 } else {
58 frms -= frames;
59 sptr += frames*Chans;
65 // ////////////////////////////////////////////////////////////////////////// //
66 void alsaWrite2B (ubyte** eptr, uint frms) {
67 static float[4096] fbuf;
68 static short[4096] sbuf;
69 auto fptr = cast(float**)eptr;
70 while (frms > 0) {
71 uint len = cast(uint)(fbuf.length/Chans);
72 if (len > frms) len = frms;
73 uint dpos = 0;
74 foreach (immutable pos; 0..len) {
75 foreach (immutable chn; 0..Chans) {
76 //assert(*sptr[chn] >= -1.0f && *sptr[chn] <= 1.0f);
77 fbuf[dpos++] = *fptr[chn]++;
80 tflFloat2Short(fbuf[0..dpos], sbuf[0..dpos]);
81 dpos = 0;
82 while (dpos < len) {
83 snd_pcm_sframes_t frames = snd_pcm_writei(pcm, sbuf.ptr+dpos*Chans, len-dpos);
84 if (frames < 0) {
85 frames = snd_pcm_recover(pcm, cast(int)frames, 0);
86 import core.stdc.stdio : printf;
87 import core.stdc.stdlib : exit, EXIT_FAILURE;
88 printf("snd_pcm_writei failed: %s\n", snd_strerror(cast(int)frames));
89 exit(EXIT_FAILURE);
90 } else {
91 frms -= frames;
92 dpos += frames*Chans;