- add portaudio

Piotr Pawlow [2014-03-14 00:04:56]
- add portaudio
Filename
configure.in
src/Makefile.am
src/glade/lingot-config-dialog.glade
src/lingot-audio-portaudio.c
src/lingot-audio-portaudio.h
src/lingot-audio.c
src/lingot-audio.h
src/lingot-config.c
src/lingot-config.h
diff --git a/configure.in b/configure.in
index 8d3ae94..978c905 100644
--- a/configure.in
+++ b/configure.in
@@ -86,6 +86,24 @@ if test "x$useoss" = "xyes"; then
 	CFLAGS="$CFLAGS -DOSS"
 fi

+useportaudio=yes
+
+AC_ARG_ENABLE(
+  portaudio,
+  AC_HELP_STRING([--enable-portaudio], [use PORTAUDIO @<:@default=yes@:>@]),
+  [
+    if test "x$enableval" = "xno"; then
+      useportaudio=no
+    fi
+  ])
+
+if test "x$useportaudio" = "xyes"; then
+	PKG_CHECK_MODULES([PORTAUDIO], [portaudio-2.0])
+	AC_SUBST(PORTAUDIO_CFLAGS)
+	AC_SUBST(PORTAUDIO_LIBS)
+	CFLAGS="$CFLAGS -DPORTAUDIO"
+fi
+
 PKG_CHECK_MODULES([GLADE], [libglade-2.0])
 AC_SUBST(GLADE_CFLAGS)
 AC_SUBST(GLADE_LIBS)
diff --git a/src/Makefile.am b/src/Makefile.am
index 03b32d1..44f4439 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,7 +1,7 @@
 SUBDIRS = glade

 INCLUDES = \
-	$(PACKAGE_CFLAGS) $(GTK_CFLAGS) $(ALSA_CFLAGS) $(JACK_CFLAGS) $(GLADE_CFLAGS)\
+	$(PACKAGE_CFLAGS) $(GTK_CFLAGS) $(ALSA_CFLAGS) $(JACK_CFLAGS) $(PORTAUDIO_CFLAGS) $(GLADE_CFLAGS)\
 	-DLINGOT_GLADEDIR=\""$(datadir)/lingot/glade/"\" \
 	-DLINGOT_LOCALEDIR=\""$(datadir)/locale"\"

@@ -21,6 +21,8 @@ lingot_SOURCES = \
 	lingot-audio-alsa.h\
 	lingot-audio-jack.c\
 	lingot-audio-jack.h\
+	lingot-audio-portaudio.c\
+	lingot-audio-portaudio.h\
 	lingot-complex.h\
 	lingot-complex.c\
 	lingot-config.c\
@@ -50,6 +52,6 @@ lingot_SOURCES = \
 lingot_LDFLAGS =

 lingot_LDADD =  \
-	$(PACKAGE_LIBS) $(GTK_LIBS) $(ALSA_LIBS) $(JACK_LIBS) $(GLADE_LIBS)\
+	$(PACKAGE_LIBS) $(GTK_LIBS) $(ALSA_LIBS) $(JACK_LIBS) $(PORTAUDIO_LIBS) $(GLADE_LIBS)\
 	 -lpthread

diff --git a/src/glade/lingot-config-dialog.glade b/src/glade/lingot-config-dialog.glade
index faf1c21..b49871a 100644
--- a/src/glade/lingot-config-dialog.glade
+++ b/src/glade/lingot-config-dialog.glade
@@ -44,7 +44,8 @@
                         <property name="tooltip" translatable="yes">Here you can pick the sound system.</property>
                         <property name="items">OSS
 ALSA
-JACK</property>
+JACK
+PORTAUDIO</property>
                         <signal name="changed" handler="on_combobox_input_architecture_changed"/>
                       </widget>
                       <packing>
diff --git a/src/lingot-audio-portaudio.c b/src/lingot-audio-portaudio.c
new file mode 100644
index 0000000..d002672
--- /dev/null
+++ b/src/lingot-audio-portaudio.c
@@ -0,0 +1,155 @@
+/*
+ * lingot, a musical instrument tuner.
+ *
+ * Copyright (C) 2004-2011  Ibán Cereijo Graña, Jairo Chapela Martínez.
+ *
+ * This file is part of lingot.
+ *
+ * lingot is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * lingot is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with lingot; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <stdlib.h>
+
+#include "lingot-defs.h"
+#include "lingot-audio-portaudio.h"
+#include "lingot-i18n.h"
+#include "lingot-msg.h"
+#include <string.h>
+
+LingotAudioHandler* lingot_audio_portaudio_new(char* device, int sample_rate) {
+
+	LingotAudioHandler* audio = NULL;
+
+#	ifdef PORTAUDIO
+	char error_message[1000];
+	PaError err;
+	const char* exception;
+	int channels = 1;
+	const PaStreamInfo* s_info;
+	PaDeviceIndex dev_idx_default;
+	const PaDeviceInfo* dev_info;
+
+	audio = malloc(sizeof(LingotAudioHandler));
+	audio->read_buffer = NULL;
+	audio->audio_system = AUDIO_SYSTEM_PORTAUDIO;
+	audio->read_buffer_size = 128;
+
+	try {
+		if ((err = Pa_Initialize()) != paNoError)
+		{
+			sprintf(error_message, "Pa_Initialize error: %s\n",
+				Pa_GetErrorText(err));
+			throw(error_message);
+		}
+
+		dev_idx_default = Pa_GetDefaultInputDevice();
+		dev_info = Pa_GetDeviceInfo(dev_idx_default);
+		strcpy(audio->device, dev_info->name);
+
+		if ((err = Pa_OpenDefaultStream( &audio->portaudio_stream, channels, 0, paInt16, sample_rate,
+				audio->read_buffer_size, NULL,
+				NULL)) != paNoError)
+		{
+			sprintf(error_message, "Pa_OpenDefaultStream error: %s\n",
+				Pa_GetErrorText(err));
+			throw(error_message);
+		}
+		if ((err = Pa_StartStream(audio->portaudio_stream)) != paNoError)
+		{
+			sprintf(error_message, "Pa_StartStream error: %s\n",
+				Pa_GetErrorText(err));
+			throw(error_message);
+		}
+
+		s_info = Pa_GetStreamInfo(audio->portaudio_stream);
+		audio->real_sample_rate = s_info->sampleRate;
+
+		audio->read_buffer = malloc(channels * audio->read_buffer_size
+				* Pa_GetSampleSize(paInt16));
+		memset(audio->read_buffer, 0, audio->read_buffer_size
+				* Pa_GetSampleSize(paInt16));
+
+	} catch {
+		if (audio->portaudio_stream != NULL)
+			Pa_CloseStream(audio->portaudio_stream);
+		free(audio);
+		audio = NULL;
+		lingot_msg_add_error(exception);
+	}
+
+#	else
+	lingot_msg_add_error(
+			_("The application has not been built with PORTAUDIO support"));
+#	endif
+
+	return audio;
+}
+
+void lingot_audio_portaudio_destroy(LingotAudioHandler* audio) {
+#	ifdef PORTAUDIO
+	if (audio != NULL) {
+		Pa_CloseStream(audio->portaudio_stream);
+		free(audio->read_buffer);
+	}
+#	endif
+}
+
+int lingot_audio_portaudio_read(LingotAudioHandler* audio) {
+#	ifdef PORTAUDIO
+	PaError err;
+	int i;
+
+	if (((err = Pa_ReadStream(audio->portaudio_stream, audio->read_buffer,
+			audio->read_buffer_size)) != paNoError) && (err != paInputOverflowed))
+	{
+		char buff[100];
+		sprintf(buff, _("Read from audio interface failed.\n%s."),
+				Pa_GetErrorText(err));
+		lingot_msg_add_error(buff);
+		return -1;
+	}
+
+	// float point conversion
+	for (i = 0; i < audio->read_buffer_size; i++) {
+		audio->flt_read_buffer[i] = audio->read_buffer[i];
+	}
+#	endif
+
+	return 0;
+}
+
+LingotAudioSystemProperties* lingot_audio_portaudio_get_audio_system_properties(
+		audio_system_t audio_system) {
+
+	LingotAudioSystemProperties* result =
+			(LingotAudioSystemProperties*) malloc(1
+					* sizeof(LingotAudioSystemProperties));
+
+	// TODO
+	result->forced_sample_rate = 0;
+	result->n_devices = 0;
+	result->devices = NULL;
+
+	result->n_sample_rates = 5;
+	result->sample_rates = malloc(result->n_sample_rates * sizeof(int));
+	result->sample_rates[0] = 8000;
+	result->sample_rates[1] = 11025;
+	result->sample_rates[2] = 22050;
+	result->sample_rates[3] = 44100;
+	result->sample_rates[4] = 48000;
+
+	return result;
+}
+
diff --git a/src/lingot-audio-portaudio.h b/src/lingot-audio-portaudio.h
new file mode 100644
index 0000000..615ae7c
--- /dev/null
+++ b/src/lingot-audio-portaudio.h
@@ -0,0 +1,34 @@
+/*
+ * lingot, a musical instrument tuner.
+ *
+ * Copyright (C) 2004-2011  Ibán Cereijo Graña, Jairo Chapela Martínez.
+ *
+ * This file is part of lingot.
+ *
+ * lingot is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * lingot is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with lingot; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef __LINGOT_AUDIO_PORTAUDIO_H__
+#define __LINGOT_AUDIO_PORTAUDIO_H__
+
+#include "lingot-audio.h"
+
+LingotAudioHandler* lingot_audio_portaudio_new(char* device, int sample_rate);
+void lingot_audio_portaudio_destroy(LingotAudioHandler*);
+int lingot_audio_portaudio_read(LingotAudioHandler*);
+LingotAudioSystemProperties* lingot_audio_portaudio_get_audio_system_properties(
+		audio_system_t);
+
+#endif
diff --git a/src/lingot-audio.c b/src/lingot-audio.c
index 7e81ab1..cf9f15a 100644
--- a/src/lingot-audio.c
+++ b/src/lingot-audio.c
@@ -48,6 +48,9 @@ LingotAudioHandler* lingot_audio_new(audio_system_t audio_system, char* device,
 	case AUDIO_SYSTEM_JACK:
 		result = lingot_audio_jack_new(device, sample_rate);
 		break;
+	case AUDIO_SYSTEM_PORTAUDIO:
+		result = lingot_audio_portaudio_new(device, sample_rate);
+		break;
 	}

 	if (result != NULL) {
@@ -80,6 +83,9 @@ void lingot_audio_destroy(LingotAudioHandler* audio) {
 		case AUDIO_SYSTEM_JACK:
 			lingot_audio_jack_destroy(audio);
 			break;
+		case AUDIO_SYSTEM_PORTAUDIO:
+			lingot_audio_portaudio_destroy(audio);
+			break;
 		default:
 			perror("unknown audio system\n");
 			break;
@@ -100,6 +106,9 @@ int lingot_audio_read(LingotAudioHandler* audio) {
 		case AUDIO_SYSTEM_ALSA:
 			result = lingot_audio_alsa_read(audio);
 			break;
+		case AUDIO_SYSTEM_PORTAUDIO:
+			result = lingot_audio_portaudio_read(audio);
+			break;
 			//		case AUDIO_SYSTEM_JACK:
 			//			result = lingot_audio_jack_read(audio);
 			//			break;
@@ -126,6 +135,9 @@ LingotAudioSystemProperties* lingot_audio_get_audio_system_properties(
 	case AUDIO_SYSTEM_JACK:
 		result = lingot_audio_jack_get_audio_system_properties(audio_system);
 		break;
+	case AUDIO_SYSTEM_PORTAUDIO:
+		result = lingot_audio_portaudio_get_audio_system_properties(audio_system);
+		break;
 	default:
 		perror("unknown audio system\n");
 		result = NULL;
diff --git a/src/lingot-audio.h b/src/lingot-audio.h
index ea7a041..1976b1a 100644
--- a/src/lingot-audio.h
+++ b/src/lingot-audio.h
@@ -31,6 +31,10 @@
 #include <jack/jack.h>
 #endif

+#ifdef PORTAUDIO
+#include <portaudio.h>
+#endif
+
 #include <stdint.h>
 #include <pthread.h>
 #include "lingot-config.h"
@@ -63,6 +67,10 @@ struct _LingotAudioHandler {
 	int nframes;
 #	endif

+#	ifdef PORTAUDIO
+	PaStream *portaudio_stream;
+#	endif
+
 	//	char error_message[100];
 	unsigned int real_sample_rate;

diff --git a/src/lingot-config.c b/src/lingot-config.c
index e0a1343..48213da 100644
--- a/src/lingot-config.c
+++ b/src/lingot-config.c
@@ -57,7 +57,7 @@ const char* audio_system_t_to_str(audio_system_t audio_system) {
 // converts a string to an audio_system_t
 audio_system_t str_to_audio_system_t(char* audio_system) {
 	audio_system_t result = -1;
-	const char* values[] = { "OSS", "ALSA", "JACK", NULL };
+	const char* values[] = { "OSS", "ALSA", "JACK", "PORTAUDIO", NULL };
 	int i;
 	for (i = 0; values[i] != NULL; i++) {
 		if (!strcmp(audio_system, values[i])) {
diff --git a/src/lingot-config.h b/src/lingot-config.h
index 157baa6..6324a6a 100644
--- a/src/lingot-config.h
+++ b/src/lingot-config.h
@@ -27,7 +27,7 @@
 #include "lingot-config-scale.h"

 typedef enum audio_system_t {
-	AUDIO_SYSTEM_OSS = 0, AUDIO_SYSTEM_ALSA = 1, AUDIO_SYSTEM_JACK = 2
+	AUDIO_SYSTEM_OSS = 0, AUDIO_SYSTEM_ALSA = 1, AUDIO_SYSTEM_JACK = 2, AUDIO_SYSTEM_PORTAUDIO = 3
 } audio_system_t;

 typedef enum window_type_t {
ViewGit