1 module xalsa
/*is aliced*/;
5 import iv
.follin
.utils
;
8 // ////////////////////////////////////////////////////////////////////////// //
9 __gshared snd_pcm_t
* pcm
;
10 __gshared
uint Chans
= 2;
15 // ////////////////////////////////////////////////////////////////////////// //
16 void alsaOpen (int chans
) {
17 if (chans
< 1 || chans
> 2) assert(0, "invalid number of channels");
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
));
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
));
36 // ////////////////////////////////////////////////////////////////////////// //
45 // ////////////////////////////////////////////////////////////////////////// //
46 void alsaWriteX (short* sptr
, uint frms
) {
48 snd_pcm_sframes_t frames
= snd_pcm_writei(pcm
, sptr
, frms
);
50 frames
= snd_pcm_recover(pcm
, cast(int)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
));
65 // ////////////////////////////////////////////////////////////////////////// //
66 void alsaWrite2B (ubyte** eptr
, uint frms
) {
67 static float[4096] fbuf
;
68 static short[4096] sbuf
;
69 auto fptr
= cast(float**)eptr
;
71 uint len
= cast(uint)(fbuf
.length
/Chans
);
72 if (len
> frms
) len
= frms
;
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
]);
83 snd_pcm_sframes_t frames
= snd_pcm_writei(pcm
, sbuf
.ptr
+dpos
*Chans
, len
-dpos
);
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
));