- add PortAudio sound backend

pp [2008-03-12 15:16:54]
- add PortAudio sound backend
- audio format argument removed from lingot_audio_new(), as it was specific to OSS backend
- DIE macro, so on Windows error message will be displayed in a dialog box instead of being lost



git-svn-id: https://lampka.siedziba.pl:790/svn/repos/lingot-win32@292 455248ca-bdda-0310-9134-f4ebb693071a
Filename
src/lingot-audio.c
src/lingot-audio.h
src/lingot-core.c
src/lingot-defs.h
diff --git a/src/lingot-audio.c b/src/lingot-audio.c
index a0776f6..92d4f2c 100644
--- a/src/lingot-audio.c
+++ b/src/lingot-audio.c
@@ -23,21 +23,43 @@

 #include <stdio.h>
 #include <unistd.h>
+
+#ifdef PORTAUDIO
+#else
 #include <sys/ioctl.h>
 #include <fcntl.h>
 #include <sys/soundcard.h>
+#endif
+
 #include <stdlib.h>

 #include "lingot-defs.h"
 #include "lingot-audio.h"

+#ifdef __WIN32__
+#include "windows.h"
+char _die_err[100];
+#define DIE(...) \
+do { snprintf(_die_err, sizeof(_die_err), __VA_ARGS__); \
+     MessageBox(NULL, _die_err, "Fatal error", MB_OK | MB_ICONEXCLAMATION); \
+     exit(1); } \
+while (0)
+#else
+#define DIE(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
+#endif
+
 LingotAudio *
-lingot_audio_new (int channels, int rate, int format, char *fdsp)
+lingot_audio_new (int channels, int rate, char *fdsp)
 {
   LingotAudio *audio;
 #ifdef ALSA
   snd_pcm_hw_params_t *hw_params;
   int err;
+#elif defined PORTAUDIO
+  PaError err;
+
+#else
+  int format = AFMT_S16_LE;
 #endif

   audio = malloc (sizeof (LingotAudio));
@@ -116,6 +138,26 @@ lingot_audio_new (int channels, int rate, int format, char *fdsp)
       exit (1);
     }

+#elif defined PORTAUDIO
+
+  if ((err = Pa_Initialize()) != paNoError)
+    {
+      DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
+    }
+  if ((err = Pa_OpenDefaultStream( &audio->stream, channels, 0, paInt16, rate,
+                                   paFramesPerBufferUnspecified, NULL,
+                                   NULL)) != paNoError)
+    {
+      DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
+    }
+
+  if ((err = Pa_StartStream(audio->stream)) != paNoError)
+    {
+      DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
+    }
+
+  audio->framesize = Pa_GetSampleSize( paInt16 ) * channels;
+
 #else

   audio->dsp = open (fdsp, O_RDONLY);
@@ -202,6 +244,8 @@ lingot_audio_destroy (LingotAudio * audio)
 {
 #ifdef ALSA
   snd_pcm_close (audio->capture_handle);
+#elif defined PORTAUDIO
+  Pa_Terminate();
 #else
   close (audio->dsp);
   free (audio);
@@ -213,6 +257,13 @@ lingot_audio_read (LingotAudio * audio, void *buffer, int size)
 {
 #ifdef ALSA
   return snd_pcm_readi (audio->capture_handle, buffer, size);
+#elif defined PORTAUDIO
+  PaError err;
+  if (((err = Pa_ReadStream(audio->stream, buffer, size / audio->framesize)) != paNoError) && (err != paInputOverflowed))
+    {
+      DIE ("PortAudio error: %s\n", Pa_GetErrorText( err ) );
+    }
+  return size;
 #else
   return read (audio->dsp, buffer, size);
 #endif
diff --git a/src/lingot-audio.h b/src/lingot-audio.h
index 996c822..49fc4ea 100644
--- a/src/lingot-audio.h
+++ b/src/lingot-audio.h
@@ -31,18 +31,25 @@
 #include <alsa/asoundlib.h>
 #endif

+#ifdef PORTAUDIO
+#include <portaudio.h>
+#endif
+
 typedef struct _LingotAudio LingotAudio;

 struct _LingotAudio
   {
   	#ifdef ALSA
   	snd_pcm_t *capture_handle;
+	#elif defined PORTAUDIO
+	PaStream *stream;
+	unsigned char framesize;
   	#else
     int dsp; // file handler.
     #endif
   };

-LingotAudio* lingot_audio_new(int channels, int rate, int format, char* fdsp);
+LingotAudio* lingot_audio_new(int channels, int rate, char* fdsp);
 void lingot_audio_destroy(LingotAudio* audio);
 int lingot_audio_read(LingotAudio* audio, void* buffer, int size);

diff --git a/src/lingot-core.c b/src/lingot-core.c
index d0cdaec..ef101ae 100644
--- a/src/lingot-core.c
+++ b/src/lingot-core.c
@@ -23,7 +23,6 @@

 #include <stdio.h>
 #include <math.h>
-#include <sys/soundcard.h>
 #include <string.h>
 #include <errno.h>

@@ -54,7 +53,7 @@ LingotCore* lingot_core_new(LingotConfig* conf) {
 #  endif
 # else
 	// creates an audio handler.
-	core->audio = lingot_audio_new(1, core->conf->sample_rate, SAMPLE_FORMAT,
+	core->audio = lingot_audio_new(1, core->conf->sample_rate,
 			core->conf->audio_dev);
 # endif

diff --git a/src/lingot-defs.h b/src/lingot-defs.h
index 09625fa..388c1f6 100644
--- a/src/lingot-defs.h
+++ b/src/lingot-defs.h
@@ -31,7 +31,6 @@
 #define FLT                  double

 #define SAMPLE_TYPE          int16_t
-#define SAMPLE_FORMAT        AFMT_S16_LE

 #define CONFIG_DIR_NAME           ".lingot/"
 #define DEFAULT_CONFIG_FILE_NAME  "lingot.conf"
ViewGit