48535cb6467399d50d52b8a3821e2d74f3e2d748
[sdk] / extras / audio / alsa.ec
1 #if defined(__linux__)
2 #define uint _uint
3 #include <alsa/asoundlib.h>
4 #undef uint
5
6 import "audio"
7
8 static byte * buffer;
9 static snd_pcm_t *handle;
10 static snd_pcm_sframes_t frames;
11 static AudioSpec audioSpec { };
12 static bool paused;
13 static Semaphore pauseSemaphore { };
14
15 static char *device = "default";
16
17 static snd_mixer_t *mixer_handle;
18
19 static double volume, balance;
20
21 public void AudioSetBalance(double percent)
22 {
23    balance = percent;
24 }
25
26 public bool AudioSetVolume(VolumeControl type, double percent)
27 {
28    bool result = false;
29    if(type == application)
30    {
31       volume = percent;
32       result = true;
33    }
34    else
35    {
36       snd_mixer_elem_t *elem;
37       snd_mixer_selem_id_t *sid;
38       int pmin, pmax;
39       int get_vol, set_vol;
40       float f_multi;
41
42         snd_mixer_load(mixer_handle);
43
44       // snd_mixer_selem_id_alloca(&sid);
45       sid = (snd_mixer_selem_id_t *) alloca(snd_mixer_selem_id_sizeof());
46       memset(sid, 0, snd_mixer_selem_id_sizeof());
47
48       snd_mixer_selem_id_set_index(sid, 0);
49       snd_mixer_selem_id_set_name(sid, (type == pcm) ? "PCM" : "Master");
50       elem = snd_mixer_find_selem(mixer_handle, sid);
51       if(elem)
52       {
53          snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
54          f_multi = 1 / (float)(pmax - pmin);
55          set_vol = (int)(percent / f_multi + pmin + 0.5);
56          snd_mixer_selem_set_playback_volume(elem, 0, set_vol);
57          snd_mixer_selem_set_playback_volume(elem, 1, set_vol);
58          result = true;
59       }
60       snd_mixer_free(mixer_handle);
61    }
62    return result;
63 }
64
65 public bool AudioGetVolume(VolumeControl type, double * percent)
66 {
67    bool result = false;
68    if(type == application)
69    {
70       *percent = volume;
71       result = true;
72    }
73    else
74    {
75       snd_mixer_elem_t *elem;
76       snd_mixer_selem_id_t *sid;
77       long pmin, pmax;
78       long get_vol, set_vol;
79       float f_multi;
80
81         snd_mixer_load(mixer_handle);
82
83       // snd_mixer_selem_id_alloca(&sid);
84       sid = (snd_mixer_selem_id_t *) alloca(snd_mixer_selem_id_sizeof());
85       memset(sid, 0, snd_mixer_selem_id_sizeof());
86
87       snd_mixer_selem_id_set_index(sid, 0);
88       snd_mixer_selem_id_set_name(sid, (type == pcm) ? "PCM" : "Master");
89       elem = snd_mixer_find_selem(mixer_handle, sid);
90
91       if(elem)
92       {
93          snd_mixer_selem_get_playback_volume_range(elem,&pmin,&pmax);
94          snd_mixer_selem_get_playback_volume(elem, 0, &set_vol);
95          f_multi = 1 / (float)(pmax - pmin);
96          *percent = f_multi * (set_vol - 0.5 - pmin);
97          result = true;
98       }
99       snd_mixer_free(mixer_handle);
100    }
101    return result;
102 }
103
104 public void OpenMixer()
105 {
106    snd_mixer_open(&mixer_handle, 0);
107    snd_mixer_attach(mixer_handle, "default");
108         snd_mixer_selem_register(mixer_handle, null, null);
109 }
110
111 public void CloseMixer()
112 {
113    snd_mixer_close(mixer_handle);
114 }
115
116 public int OpenAudio(AudioSpec wanted, AudioSpec result)
117 {
118    int err;
119    unsigned int i;
120
121    buffer = new byte[wanted.samples * wanted.channels * wanted.bits / 8];
122    memset(buffer, 0, wanted.samples * wanted.channels * wanted.bits / 8);
123
124    if ((err = snd_pcm_open(&handle, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0)
125    {
126       printf("Playback open error: %s\n", snd_strerror(err));
127       return 0;
128    }
129    if ((err = snd_pcm_set_params(handle,
130                                  (wanted.bits == 8) ? SND_PCM_FORMAT_U8 : SND_PCM_FORMAT_S16,
131                                  SND_PCM_ACCESS_RW_INTERLEAVED,
132                                  wanted.channels,
133                                  wanted.freq,
134                                  1,
135                                  50000)) < 0)
136    {   /* 0.5sec */
137       printf("Playback open error: %s\n", snd_strerror(err));
138       return 0;
139    }
140    soundThread.done = false;
141    result = wanted;
142    audioSpec = wanted;
143    volume = wanted.volume;
144    balance = 0.5;
145    return 1;
146 }
147
148 public void PauseAudio(int value)
149 {
150    if(!value)
151    {
152       soundThread.Create();
153    }
154 }
155
156 public void CloseAudio()
157 {
158    soundThread.done = true;
159    soundThread.Wait();
160    delete buffer;
161    if(handle) snd_pcm_close(handle);
162 }
163
164 static class SoundThread : Thread
165 {
166    bool done;
167    SoundThread()
168    {
169
170    }
171    ~SoundThread()
172    {
173
174    }
175
176    uint Main()
177    {
178       while(!done)
179       {
180          if(paused)
181          {
182             pauseSemaphore.Wait();
183          }
184          else
185          {
186             int c;
187             short int * samples = (short int *)buffer;
188             double m = volume / (1 + Abs(balance * 2 - 1)) / 100.0;
189             double ll = (2 - (2 * Max(balance, 0.5)))* m;
190             double lr = (-2 * Min(balance, 0.5) + 1) * m;
191             double rl = (2 * Max(balance, 0.5) - 1)  * m;
192             double rr = (2 * Min(balance, 0.5))      * m;
193             uint numSamples = audioSpec.samples/16;
194             // printf("Volume: %f, m : %f, Left: (%f, %f), Right: (%f, %f) \n", volume, m, ll, lr, rl, rr);
195
196             audioSpec.callback(audioSpec.userdata, buffer, numSamples * audioSpec.channels * audioSpec.bits / 8);
197             for(c = 0; c<numSamples; c++)
198             {
199                short sLeft = samples[0], sRight = samples[1];
200                samples[0] = (short)(sLeft * ll + sRight * lr);
201                samples[1] = (short)(sLeft * rl + sRight * rr);
202                samples += 2;
203             }
204
205             frames = snd_pcm_writei(handle, buffer, numSamples);
206             if(frames < 0)
207                frames = snd_pcm_recover(handle, frames, 0);
208             if (frames < 0)
209             {
210                printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
211                break;
212             }
213             if (frames > 0 && frames < numSamples)
214                printf("Short write (expected %li, wrote %li)\n", numSamples, frames);
215          }
216       }
217       return 0;
218    }
219 }
220
221 static SoundThread soundThread { };
222
223 #endif