Blame SOURCES/flite-1.3-alsa_support.patch

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