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.
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*/;
14 import iv
.follin
.utils
;
17 // ////////////////////////////////////////////////////////////////////////// //
18 __gshared snd_pcm_t
* pcm
;
22 __gshared
uint Chans
= 2;
25 // ////////////////////////////////////////////////////////////////////////// //
26 void alsaOpen (int chans
=-1) {
28 if (chans
< 1 || chans
> 2) assert(0, "fuck");
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
));
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
));
48 // ////////////////////////////////////////////////////////////////////////// //
57 // ////////////////////////////////////////////////////////////////////////// //
58 void alsaWriteX (short* sptr
, uint frms
) {
60 snd_pcm_sframes_t frames
= snd_pcm_writei(pcm
, sptr
, frms
);
62 frames
= snd_pcm_recover(pcm
, cast(int)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
));
77 // ////////////////////////////////////////////////////////////////////////// //
78 void alsaWrite2B (ubyte** eptr
, uint frms
) {
79 static float[4096] fbuf
;
80 static short[4096] sbuf
;
81 auto fptr
= cast(float**)eptr
;
83 uint len
= cast(uint)(fbuf
.length
/Chans
);
84 if (len
> frms
) len
= frms
;
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
]);
95 snd_pcm_sframes_t frames
= snd_pcm_writei(pcm
, sbuf
.ptr
+dpos
*Chans
, len
-dpos
);
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
));
104 dpos
+= frames
*Chans
;