Blame SOURCES/flite-1.3-alsa_support.patch

559698
diff -uNr flite-1.3-release/configure.in flite-1.3-release-mod/configure.in
559698
--- flite-1.3-release/configure.in	2005-08-13 13:43:21.000000000 +0200
559698
+++ flite-1.3-release-mod/configure.in	2006-11-13 21:16:27.000000000 +0200
559698
@@ -206,10 +206,10 @@
559698
 AC_CHECK_HEADER(sys/audioio.h,
559698
               [AUDIODRIVER="sun"
559698
                AUDIODEFS=-DCST_AUDIO_SUNOS])
559698
-dnl AC_CHECK_HEADER(sys/asoundlib.h,
559698
-dnl              [AUDIODRIVER="alsa"
559698
-dnl	       AUDIODEFS=-DCST_AUDIO_ALSA
559698
-dnl               AUDIOLIBS=-lasound])
559698
+AC_CHECK_HEADER(alsa/asoundlib.h,
559698
+              [AUDIODRIVER="alsa"
559698
+	       AUDIODEFS=-DCST_AUDIO_ALSA
559698
+               AUDIOLIBS=-lasound])
559698
 AC_CHECK_HEADER(mmsystem.h,
559698
 	      [AUDIODRIVER="wince"
559698
 	       AUDIODEFS=-DCST_AUDIO_WINCE
559698
diff -uNr flite-1.3-release/src/audio/au_alsa.c flite-1.3-release-mod/src/audio/au_alsa.c
559698
--- flite-1.3-release/src/audio/au_alsa.c	1970-01-01 02:00:00.000000000 +0200
559698
+++ flite-1.3-release-mod/src/audio/au_alsa.c	2006-11-13 21:16:54.000000000 +0200
559698
@@ -0,0 +1,311 @@
559698
+/*************************************************************************/
559698
+/*                                                                       */
559698
+/*                  Language Technologies Institute                      */
559698
+/*                     Carnegie Mellon University                        */
559698
+/*                        Copyright (c) 2000                             */
559698
+/*                        All Rights Reserved.                           */
559698
+/*                                                                       */
559698
+/*  Permission is hereby granted, free of charge, to use and distribute  */
559698
+/*  this software and its documentation without restriction, including   */
559698
+/*  without limitation the rights to use, copy, modify, merge, publish,  */
559698
+/*  distribute, sublicense, and/or sell copies of this work, and to      */
559698
+/*  permit persons to whom this work is furnished to do so, subject to   */
559698
+/*  the following conditions:                                            */
559698
+/*   1. The code must retain the above copyright notice, this list of    */
559698
+/*      conditions and the following disclaimer.                         */
559698
+/*   2. Any modifications must be clearly marked as such.                */
559698
+/*   3. Original authors' names are not deleted.                         */
559698
+/*   4. The authors' names are not used to endorse or promote products   */
559698
+/*      derived from this software without specific prior written        */
559698
+/*      permission.                                                      */
559698
+/*                                                                       */
559698
+/*  CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK         */
559698
+/*  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      */
559698
+/*  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   */
559698
+/*  SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE      */
559698
+/*  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    */
559698
+/*  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   */
559698
+/*  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          */
559698
+/*  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       */
559698
+/*  THIS SOFTWARE.                                                       */
559698
+/*                                                                       */
559698
+/*********************************************************************** */
559698
+/*             Author:  Lukas Loehrer (                                  */
559698
+/*               Date:  January 2005                                     */
559698
+/*************************************************************************/
559698
+/*                                                                       */
559698
+/*  Native access to alsa audio devices on Linux                         */
559698
+/*  Tested with libasound version 1.0.10                                 */
559698
+/*************************************************************************/
559698
+
559698
+#include <stdlib.h>
559698
+#include <unistd.h>
559698
+#include <sys/types.h>
559698
+#include <assert.h>
559698
+#include <errno.h>
559698
+
559698
+#include "cst_string.h"
559698
+#include "cst_wave.h"
559698
+#include "cst_audio.h"
559698
+
559698
+#include <alsa/asoundlib.h>
559698
+
559698
+
559698
+/*static char *pcm_dev_name = "hw:0,0"; */
559698
+static char *pcm_dev_name ="default";
559698
+
559698
+static inline void print_pcm_state(snd_pcm_t *handle, char *msg)
559698
+{
559698
+  fprintf(stderr, "PCM state at %s = %s\n", msg,
559698
+		  snd_pcm_state_name(snd_pcm_state(handle)));
559698
+}
559698
+
559698
+cst_audiodev *audio_open_alsa(int sps, int channels, cst_audiofmt fmt)
559698
+{
559698
+  cst_audiodev *ad;
559698
+  unsigned 	int real_rate;
559698
+  int err;
559698
+
559698
+  /* alsa specific stuff */
559698
+  snd_pcm_t *pcm_handle;          
559698
+  snd_pcm_stream_t stream = SND_PCM_STREAM_PLAYBACK;
559698
+  snd_pcm_hw_params_t *hwparams;
559698
+  snd_pcm_format_t format;
559698
+  snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED;
559698
+
559698
+  /* Allocate the snd_pcm_hw_params_t structure on the stack. */
559698
+  snd_pcm_hw_params_alloca(&hwparams);
559698
+
559698
+  /* Open pcm device */
559698
+  err = snd_pcm_open(&pcm_handle, pcm_dev_name, stream, 0);
559698
+  if (err < 0) 
559698
+  {
559698
+	cst_errmsg("audio_open_alsa: failed to open audio device %s. %s\n",
559698
+			   pcm_dev_name, snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+
559698
+  /* Init hwparams with full configuration space */
559698
+  err = snd_pcm_hw_params_any(pcm_handle, hwparams);
559698
+  if (err < 0) 
559698
+  {
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to get hardware parameters from audio device. %s\n", snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+
559698
+  /* Set access mode */
559698
+  err = snd_pcm_hw_params_set_access(pcm_handle, hwparams, access);
559698
+  if (err < 0) 
559698
+  {
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to set access mode. %s.\n", snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+
559698
+  /* Determine matching alsa sample format */
559698
+  /* This could be implemented in a more */
559698
+  /* flexible way (byte order conversion). */
559698
+  switch (fmt)
559698
+  {
559698
+  case CST_AUDIO_LINEAR16:
559698
+	if (CST_LITTLE_ENDIAN)
559698
+	  format = SND_PCM_FORMAT_S16_LE;
559698
+	else
559698
+	  format = SND_PCM_FORMAT_S16_BE;
559698
+	break;
559698
+  case CST_AUDIO_LINEAR8:
559698
+	format = SND_PCM_FORMAT_U8;
559698
+	break;
559698
+  case CST_AUDIO_MULAW:
559698
+	format = SND_PCM_FORMAT_MU_LAW;
559698
+	break;
559698
+  default:
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to find suitable format.\n");
559698
+	return NULL;
559698
+	break;
559698
+  }
559698
+
559698
+  /* Set samble format */
559698
+  err = snd_pcm_hw_params_set_format(pcm_handle, hwparams, format);
559698
+  if (err <0) 
559698
+  {
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to set format. %s.\n", snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+
559698
+  /* Set sample rate near the disired rate */
559698
+  real_rate = sps;
559698
+  err = snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams, &real_rate, 0);
559698
+  if (err < 0)   
559698
+  {
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to set sample rate near %d. %s.\n", sps, snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+  /*FIXME:  This is probably too strict */
559698
+  assert(sps == real_rate);
559698
+
559698
+  /* Set number of channels */
559698
+  assert(channels >0);
559698
+  err = snd_pcm_hw_params_set_channels(pcm_handle, hwparams, channels);
559698
+  if (err < 0) 
559698
+  {
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to set number of channels to %d. %s.\n", channels, snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+
559698
+  /* Commit hardware parameters */
559698
+  err = snd_pcm_hw_params(pcm_handle, hwparams);
559698
+  if (err < 0) 
559698
+  {
559698
+	snd_pcm_close(pcm_handle);
559698
+	cst_errmsg("audio_open_alsa: failed to set hw parameters. %s.\n", snd_strerror(err));
559698
+	return NULL;
559698
+  }
559698
+
559698
+  /* Make sure the device is ready to accept data */
559698
+  assert(snd_pcm_state(pcm_handle) == SND_PCM_STATE_PREPARED);
559698
+
559698
+  /* Write hardware parameters to flite audio device data structure */
559698
+  ad = cst_alloc(cst_audiodev, 1);
559698
+  assert(ad != NULL);
559698
+  ad->real_sps = ad->sps = sps;
559698
+  ad->real_channels = ad->channels = channels;
559698
+  ad->real_fmt = ad->fmt = fmt;
559698
+  ad->platform_data = (void *) pcm_handle;
559698
+
559698
+  return ad;
559698
+}
559698
+
559698
+int audio_close_alsa(cst_audiodev *ad)
559698
+{
559698
+  int result;
559698
+  snd_pcm_t *pcm_handle;
559698
+
559698
+  if (ad == NULL)
559698
+	return 0;
559698
+
559698
+  pcm_handle = (snd_pcm_t *) ad->platform_data;
559698
+  result = snd_pcm_close(pcm_handle);
559698
+  if (result < 0)
559698
+  {
559698
+	cst_errmsg("audio_close_alsa: Error: %s.\n", snd_strerror(result));
559698
+  }
559698
+  cst_free(ad);
559698
+  return result;
559698
+}
559698
+
559698
+/* Returns zero if recovery was successful. */
559698
+static int recover_from_error(snd_pcm_t *pcm_handle, ssize_t res)
559698
+{
559698
+  if (res == -EPIPE) /* xrun */
559698
+  {
559698
+	res = snd_pcm_prepare(pcm_handle);
559698
+	if (res < 0) 
559698
+	{
559698
+	  /* Failed to recover from xrun */
559698
+	  cst_errmsg("recover_from_write_error: failed to recover from xrun. %s\n.", snd_strerror(res));
559698
+	  return res;
559698
+	}
559698
+  } 
559698
+  else if (res == -ESTRPIPE) /* Suspend */
559698
+  {
559698
+	while ((res = snd_pcm_resume(pcm_handle)) == -EAGAIN) 
559698
+	{
559698
+	  snd_pcm_wait(pcm_handle, 1000);
559698
+	}
559698
+	if (res < 0) 
559698
+	{
559698
+	  res = snd_pcm_prepare(pcm_handle);
559698
+	  if (res <0) 
559698
+	  {
559698
+		/* Resume failed */
559698
+		cst_errmsg("audio_recover_from_write_error: failed to resume after suspend. %s\n.", snd_strerror(res));
559698
+		return res;
559698
+	  }
559698
+	}
559698
+  } 
559698
+  else if (res < 0) 
559698
+  {
559698
+	/* Unknown failure */
559698
+	cst_errmsg("audio_recover_from_write_error: %s.\n", snd_strerror(res));
559698
+	return res;
559698
+  }
559698
+  return 0;
559698
+}
559698
+
559698
+int audio_write_alsa(cst_audiodev *ad, void *samples, int num_bytes)
559698
+{
559698
+  size_t frame_size;
559698
+  ssize_t num_frames, res;
559698
+  snd_pcm_t *pcm_handle;
559698
+  char *buf = (char *) samples;
559698
+
559698
+  /* Determine frame size in bytes */
559698
+  frame_size  = audio_bps(ad->real_fmt) * ad->real_channels;
559698
+  /* Require that only complete frames are handed in */
559698
+  assert((num_bytes % frame_size) == 0);
559698
+  num_frames = num_bytes / frame_size;
559698
+  pcm_handle = (snd_pcm_t *) ad->platform_data;
559698
+
559698
+  while (num_frames > 0) 
559698
+  {
559698
+	res = snd_pcm_writei(pcm_handle, buf, num_frames);
559698
+	if (res != num_frames) 
559698
+	{
559698
+	  if (res == -EAGAIN || (res > 0 && res < num_frames)) 
559698
+	  {
559698
+		snd_pcm_wait(pcm_handle, 100);
559698
+	  }
559698
+	  else if (recover_from_error(pcm_handle, res) < 0) 
559698
+	  {
559698
+		return -1;
559698
+	  }
559698
+	}
559698
+
559698
+	if (res >0) 
559698
+	{
559698
+	  num_frames -= res;
559698
+	  buf += res * frame_size;
559698
+	}
559698
+  }
559698
+  return num_bytes;
559698
+}
559698
+
559698
+int audio_flush_alsa(cst_audiodev *ad)
559698
+{
559698
+  int result;
559698
+  result = snd_pcm_drain((snd_pcm_t *) ad->platform_data);
559698
+  if (result < 0)
559698
+  {
559698
+	cst_errmsg("audio_flush_alsa: Error: %s.\n", snd_strerror(result));
559698
+  }
559698
+	/* Prepare device for more data */
559698
+  result = snd_pcm_prepare((snd_pcm_t *) ad->platform_data);
559698
+if (result < 0)
559698
+  {
559698
+	cst_errmsg("audio_flush_alsa: Error: %s.\n", snd_strerror(result));
559698
+  }
559698
+  return result;
559698
+}
559698
+
559698
+int audio_drain_alsa(cst_audiodev *ad)
559698
+{
559698
+  int result;
559698
+  result = snd_pcm_drop((snd_pcm_t *) ad->platform_data);
559698
+  if (result < 0)
559698
+  {
559698
+	cst_errmsg("audio_drain_alsa: Error: %s.\n", snd_strerror(result));
559698
+  }
559698
+/* Prepare device for more data */
559698
+  result = snd_pcm_prepare((snd_pcm_t *) ad->platform_data);
559698
+if (result < 0)
559698
+  {
559698
+	cst_errmsg("audio_drain_alsa: Error: %s.\n", snd_strerror(result));
559698
+  }
559698
+  return result;
559698
+}