Fixe
[src/app-framework-binder.git] / plugins / audio / audio-alsa.c
index 4d97c2d..4ee4804 100644 (file)
@@ -58,8 +58,10 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
     snd_mixer_selem_id_set_name (mixer_sid, "Master");
 
     mixer_elm = snd_mixer_find_selem (mixer, mixer_sid);
-    snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
-    snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
+    if (mixer_elm) {
+        snd_mixer_selem_get_playback_volume_range (mixer_elm, &vol_min, &vol_max);
+        snd_mixer_selem_get_playback_volume (mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &vol);
+    }
 
     /* allocate the global array if it hasn't been done */
     if (!dev_ctx) {
@@ -86,6 +88,8 @@ PUBLIC unsigned char _alsa_init (const char *name, audioCtxHandleT *ctx) {
     dev_ctx[num]->mixer_elm = mixer_elm;
     dev_ctx[num]->vol_max = vol_max;
     dev_ctx[num]->vol = vol;
+    dev_ctx[num]->thr_should_run = 0;
+    dev_ctx[num]->thr_finished = 0;
 
     /* make the client context aware of current card state */
     ctx->volume = _alsa_get_volume (num);
@@ -113,25 +117,34 @@ PUBLIC void _alsa_free (const char *name) {
     }
 }
 
-PUBLIC void _alsa_play (unsigned int num, void *buf, int len) {
+PUBLIC void _alsa_play (unsigned int num) {
 
-    if (!dev_ctx || !dev_ctx[num])
+    if (!dev_ctx || !dev_ctx[num] || dev_ctx[num]->thr_should_run ||
+        access (AUDIO_BUFFER, F_OK) == -1)
         return;
 
-    int16_t *cbuf = (int16_t *)buf;
-    int frames = len / 2;
-    int res;
+    dev_ctx[num]->thr_should_run = 1;
+    dev_ctx[num]->thr_finished = 0;
+    pthread_create(&dev_ctx[num]->thr, NULL, _play_thread_fn, (void*)dev_ctx[num]);
+}
+
+PUBLIC void _alsa_stop (unsigned int num) {
 
-    if ((res = snd_pcm_writei (dev_ctx[num]->dev, cbuf, frames)) != frames) {
-        snd_pcm_recover (dev_ctx[num]->dev, res, 0);
-        snd_pcm_prepare (dev_ctx[num]->dev);
-    }
-    /* snd_pcm_drain (dev_ctx[num]->dev); */
+    if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->thr_should_run)
+        return;
+
+    /* stop the "while" loop in thread */
+    dev_ctx[num]->thr_should_run = 0;
+
+    while (!dev_ctx[num]->thr_finished)
+        usleep(100000);
+
+    pthread_join(dev_ctx[num]->thr, NULL);
 }
 
 PUBLIC unsigned int _alsa_get_volume (unsigned int num) {
 
-    if (!dev_ctx || !dev_ctx[num])
+    if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
         return;
 
     snd_mixer_selem_get_playback_volume (dev_ctx[num]->mixer_elm, SND_MIXER_SCHN_FRONT_LEFT, &dev_ctx[num]->vol);
@@ -141,7 +154,7 @@ PUBLIC unsigned int _alsa_get_volume (unsigned int num) {
 
 PUBLIC unsigned int _alsa_set_volume (unsigned int num, unsigned int vol) {
 
-    if (!dev_ctx || !dev_ctx[num] || vol > 100)
+    if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm || vol > 100)
         return;
 
    snd_mixer_selem_set_playback_volume_all (dev_ctx[num]->mixer_elm, (vol*dev_ctx[num]->vol_max)/100);
@@ -151,7 +164,7 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
 
     int mute = 0;
 
-    if (!dev_ctx || !dev_ctx[num])
+    if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm)
         return;
 
     if (snd_mixer_selem_has_playback_switch (dev_ctx[num]->mixer_elm)) {
@@ -160,16 +173,16 @@ PUBLIC unsigned char _alsa_get_mute (unsigned int num) {
 
     }
 
-    return (unsigned char)mute;
+    return (unsigned char)!mute;
 }
 
 PUBLIC void _alsa_set_mute (unsigned int num, unsigned char mute) {
 
-    if (!dev_ctx || !dev_ctx[num] || 1 < mute < 0)
+    if (!dev_ctx || !dev_ctx[num] || !dev_ctx[num]->mixer_elm || 1 < mute < 0)
         return;
 
     if (snd_mixer_selem_has_playback_switch (dev_ctx[num]->mixer_elm))
-        snd_mixer_selem_set_playback_switch_all (dev_ctx[num]->mixer_elm, mute);
+        snd_mixer_selem_set_playback_switch_all (dev_ctx[num]->mixer_elm, !mute);
 }
 
 PUBLIC void _alsa_set_rate (unsigned int num, unsigned int rate) {
@@ -187,3 +200,38 @@ PUBLIC void _alsa_set_channels (unsigned int num, unsigned int channels) {
 
     snd_pcm_hw_params_set_channels (dev_ctx[num]->dev, dev_ctx[num]->params, channels);
 }
+
+ /* ---- LOCAL THREADED FUNCTIONS ---- */
+
+STATIC void* _play_thread_fn (void *ctx) {
+
+    dev_ctx_T *dev_ctx = (dev_ctx_T *)ctx;
+    FILE *file = NULL;
+    char *buf = NULL;
+    long size;
+    int frames, res;
+
+    file = fopen (AUDIO_BUFFER, "rb");
+
+    while (dev_ctx->thr_should_run && file && (access (AUDIO_BUFFER, F_OK) != -1) ) {
+        fseek (file, 0, SEEK_END);
+        size = ftell (file);
+        buf = (char*) realloc (buf, size * sizeof(char));
+        frames = (size * sizeof(char)) / 4;
+
+        fseek (file, 0, SEEK_SET);
+        fread (buf, 1, size, file);
+        fflush (file);
+
+        if ((res = snd_pcm_writei (dev_ctx->dev, buf, frames)) != frames) {
+            snd_pcm_recover (dev_ctx->dev, res, 0);
+            snd_pcm_prepare (dev_ctx->dev);
+        }
+        /* snd_pcm_drain (dev_ctx->dev); */
+    }
+    if (buf) free(buf);
+    if (file) fclose(file);
+
+    dev_ctx->thr_finished = 1;
+    return 0;
+}