flexlay2: respect maximum box size
[iv.d.git] / opl3test / xalsa.d
blob7dfab89e412717c93401bd92ba855f9f85daf361
1 // This program is free software; you can redistribute it and/or
2 // modify it under the terms of the GNU General Public License
3 // as published by the Free Software Foundation; either version 2
4 // of the License, or (at your option) any later version.
5 //
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU General Public License for more details.
10 module xalsa /*is aliced*/;
12 import iv.alice;
13 import iv.alsa;
14 import iv.follin.utils;
17 // ////////////////////////////////////////////////////////////////////////// //
18 __gshared snd_pcm_t* pcm;
21 enum Rate = 48000;
22 __gshared uint Chans = 2;
25 // ////////////////////////////////////////////////////////////////////////// //
26 void alsaOpen (int chans=-1) {
27 if (chans >= 0) {
28 if (chans < 1 || chans > 2) assert(0, "fuck");
29 Chans = chans;
31 int err;
32 if ((err = snd_pcm_open(&pcm, "plug:default", SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
33 import core.stdc.stdio : printf;
34 import core.stdc.stdlib : exit, EXIT_FAILURE;
35 printf("Playback open error: %s\n", snd_strerror(err));
36 exit(EXIT_FAILURE);
39 if ((err = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, Chans, Rate, 1, 500000)) < 0) {
40 import core.stdc.stdio : printf;
41 import core.stdc.stdlib : exit, EXIT_FAILURE;
42 printf("Playback open error: %s\n", snd_strerror(err));
43 exit(EXIT_FAILURE);
48 // ////////////////////////////////////////////////////////////////////////// //
49 void alsaClose () {
50 if (pcm !is null) {
51 snd_pcm_close(pcm);
52 pcm = null;
57 // ////////////////////////////////////////////////////////////////////////// //
58 void alsaWriteX (short* sptr, uint frms) {
59 while (frms > 0) {
60 snd_pcm_sframes_t frames = snd_pcm_writei(pcm, sptr, frms);
61 if (frames < 0) {
62 frames = snd_pcm_recover(pcm, cast(int)frames, 0);
63 if (frames < 0) {
64 import core.stdc.stdio : printf;
65 import core.stdc.stdlib : exit, EXIT_FAILURE;
66 printf("snd_pcm_writei failed: %s\n", snd_strerror(cast(int)frames));
67 exit(EXIT_FAILURE);
69 } else {
70 frms -= frames;
71 sptr += frames*Chans;
77 // ////////////////////////////////////////////////////////////////////////// //
78 void alsaWrite2B (ubyte** eptr, uint frms) {
79 static float[4096] fbuf;
80 static short[4096] sbuf;
81 auto fptr = cast(float**)eptr;
82 while (frms > 0) {
83 uint len = cast(uint)(fbuf.length/Chans);
84 if (len > frms) len = frms;
85 uint dpos = 0;
86 foreach (immutable pos; 0..len) {
87 foreach (immutable chn; 0..Chans) {
88 //assert(*sptr[chn] >= -1.0f && *sptr[chn] <= 1.0f);
89 fbuf[dpos++] = *fptr[chn]++;
92 tflFloat2Short(fbuf[0..dpos], sbuf[0..dpos]);
93 dpos = 0;
94 while (dpos < len) {
95 snd_pcm_sframes_t frames = snd_pcm_writei(pcm, sbuf.ptr+dpos*Chans, len-dpos);
96 if (frames < 0) {
97 frames = snd_pcm_recover(pcm, cast(int)frames, 0);
98 import core.stdc.stdio : printf;
99 import core.stdc.stdlib : exit, EXIT_FAILURE;
100 printf("snd_pcm_writei failed: %s\n", snd_strerror(cast(int)frames));
101 exit(EXIT_FAILURE);
102 } else {
103 frms -= frames;
104 dpos += frames*Chans;