Handle partial ALSA PCM writes
authorKeith Packard <keithp@keithp.com>
Sun, 9 Aug 2009 23:08:07 +0000 (16:08 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 9 Aug 2009 23:08:07 +0000 (16:08 -0700)
The ALSA spec says that snd_pcm_writei will not return a partial write, but
at least on the OLPC, that's not true. Deal with this.

Signed-off-by: Keith Packard <keithp@keithp.com>
aoview/aoview_flite.c

index 0ada4e141bd5fd37cc3989b1e4357da80538714d..e1b758983d171211d5ee9ade5ec578e7dfa02575 100644 (file)
@@ -37,6 +37,8 @@ aoview_flite_task(gpointer data)
        int             rate;
        int             channels;
        int             err;
        int             rate;
        int             channels;
        int             err;
+       char            *samples;
+       int             num_samples;
 
        err = snd_pcm_open(&alsa_handle, "default",
                           SND_PCM_STREAM_PLAYBACK, 0);
 
        err = snd_pcm_open(&alsa_handle, "default",
                           SND_PCM_STREAM_PLAYBACK, 0);
@@ -73,12 +75,19 @@ aoview_flite_task(gpointer data)
                if (err < 0)
                        fprintf(stderr, "alsa pcm_prepare error %s\n",
                                strerror(-err));
                if (err < 0)
                        fprintf(stderr, "alsa pcm_prepare error %s\n",
                                strerror(-err));
-               err = snd_pcm_writei(alsa_handle,
-                                    wave->samples,
-                                    wave->num_samples);
-               if (err < 0)
-                       fprintf(stderr, "alsa write error %s\n",
-                               strerror(-err));
+               samples = (char *) wave->samples;
+               num_samples = wave->num_samples;
+               while (num_samples > 0) {
+                       err = snd_pcm_writei(alsa_handle,
+                                            samples, num_samples);
+                       if (err <= 0) {
+                               fprintf(stderr, "alsa write error %s\n",
+                                       strerror(-err));
+                               break;
+                       }
+                       num_samples -= err;
+                       samples += err * 2 * channels;
+               }
                snd_pcm_drain(alsa_handle);
                delete_wave(wave);
        }
                snd_pcm_drain(alsa_handle);
                delete_wave(wave);
        }