aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas White <taw27@cam.ac.uk>2008-11-23 20:21:49 +0000
committerThomas White <taw27@cam.ac.uk>2008-11-23 20:21:49 +0000
commit0cb293213f711eb3321eb255c947d66664a604cb (patch)
treea517e4253802d228e40231e4313a3015577c9044
parentec010b19cac1d5aea5393a687a91431f2e541101 (diff)
parent5c62580a9d3c16165d0436152963fd92a597cb23 (diff)
Merge branch 'audio-thread'
-rw-r--r--Makefile.am2
-rw-r--r--configure.ac2
-rw-r--r--openmoocow.control2
-rw-r--r--src/Makefile.am2
-rw-r--r--src/accelerometers.c41
-rw-r--r--src/accelerometers.h1
-rw-r--r--src/audio.c32
-rw-r--r--src/audio.h4
-rw-r--r--src/main.c10
-rw-r--r--src/mainwindow.c42
-rw-r--r--src/physics.c57
-rw-r--r--src/physics.h39
-rw-r--r--src/types.h25
13 files changed, 92 insertions, 167 deletions
diff --git a/Makefile.am b/Makefile.am
index 12f6b12..22c7450 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,4 +1,4 @@
-EXTRA_DIST = configure src/mainwindow.h src/types.h src/accelerometers.h src/audio.h src/physics.h data/moo.wav \
+EXTRA_DIST = configure src/mainwindow.h src/types.h src/accelerometers.h src/physics.h data/moo.wav \
openmoocow.control data/cow.png data/icon.png data/openmoocow.desktop
SUBDIRS = src data
diff --git a/configure.ac b/configure.ac
index a32c9c1..67820d3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -41,7 +41,7 @@ else
fi
CFLAGS="$CFLAGS $GTK_CFLAGS $LIBSDL_CFLAGS"
-LIBS="$LIBS $GTK_LIBS $LIBSDL_LIBS"
+LIBS="$LIBS $GTK_LIBS $LIBSDL_LIBS -lgthread-2.0"
AC_OUTPUT(Makefile src/Makefile data/Makefile)
diff --git a/openmoocow.control b/openmoocow.control
index 1c74c88..ab600e8 100644
--- a/openmoocow.control
+++ b/openmoocow.control
@@ -6,6 +6,6 @@ Priority: optional
Maintainer: $USER
Architecture: armv4t
Homepage: http://www.srcf.ucam.org/~taw27/
-Depends:
+Depends: libsdl-1.2-0
Source: ${SRC}
diff --git a/src/Makefile.am b/src/Makefile.am
index e125111..f631315 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,6 +1,6 @@
bin_PROGRAMS = openmoocow
-openmoocow_SOURCES = main.c mainwindow.c accelerometers.c audio.c physics.c
+openmoocow_SOURCES = main.c mainwindow.c accelerometers.c audio.c
openmoocow_LDADD = @LIBS@
moosynth_SOURCES = moosynth.c
diff --git a/src/accelerometers.c b/src/accelerometers.c
index 7d8b0f2..464134b 100644
--- a/src/accelerometers.c
+++ b/src/accelerometers.c
@@ -29,8 +29,10 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
+#include <unistd.h>
#include "types.h"
+#include "audio.h"
struct input_event {
struct timeval time;
@@ -53,8 +55,7 @@ AccelHandle *accelerometer_open() {
if ( accel == NULL ) return NULL;
accel->fh1 = fopen("/dev/input/event2", "rb");
- accel->fh2 = NULL;
-// accel->fh2 = fopen("/dev/input/event3", "rb");
+ accel->fh2 = fopen("/dev/input/event3", "rb");
accel->ax = 0;
accel->ay = 0;
accel->az = 0;
@@ -143,3 +144,39 @@ void accelerometer_update(AccelHandle *accel) {
}
+/* The accelerometer work thread */
+static void *accel_work(void *data) {
+
+ AccelHandle *accel;
+ int pos = 0;
+
+ accel = accelerometer_open();
+ audio_setup();
+
+ while ( 1 ) {
+
+ accelerometer_update(accel);
+
+ if ( accel->lval > 1000 ) pos = 1000;
+ if ( (accel->lval < -1000) && (pos == 1000) ) {
+ pos = 0;
+ audio_trigger_moo();
+ }
+
+ usleep(25000);
+ }
+
+ audio_shutdown();
+
+ return NULL;
+
+}
+
+void accelerometer_start() {
+
+ GThread *work_thread;
+
+ work_thread = g_thread_create(accel_work, NULL, TRUE, NULL);
+
+}
+
diff --git a/src/accelerometers.h b/src/accelerometers.h
index f83149a..602dd1e 100644
--- a/src/accelerometers.h
+++ b/src/accelerometers.h
@@ -33,6 +33,7 @@
extern AccelHandle *accelerometer_open(void);
extern void accelerometer_update(AccelHandle *accel);
+extern void accelerometer_start(void);
#endif /* ACCELEROMETERS_H */
diff --git a/src/audio.c b/src/audio.c
index ed37a89..765ca77 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -22,6 +22,7 @@
*
*/
+/* SDL's headers don't like this. Grr. */
//#ifdef HAVE_CONFIG_H
//#include <config.h>
//#endif
@@ -35,9 +36,12 @@
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
+#include <assert.h>
#include "types.h"
+static AudioContext *audio_context = NULL;
+
static void audio_mix(void *data, Uint8 *stream8, int len) {
AudioContext *a = data;
@@ -54,14 +58,23 @@ static void audio_mix(void *data, Uint8 *stream8, int len) {
if ( a->moo_pos < a->moo_len ) {
samp = a->moo_buf[a->moo_pos++];
stream[j] += samp;
+ } else {
+ a->mootex = 0;
}
}
}
-void audio_trigger_moo(AudioContext *a) {
+void audio_trigger_moo() {
+ AudioContext *a = audio_context;
+
+ if ( a->mootex != 0 ) return;
+
+ printf("Moo!\n");
+ a->mootex = 1;
+
if ( a->aplay_fallback ) {
pid_t pid;
@@ -77,9 +90,8 @@ void audio_trigger_moo(AudioContext *a) {
execlp("aplay", "aplay", DATADIR"/openmoocow/moo.wav", NULL);
}
} /* else forked successfully, parent process */
- printf("Waiting...\n"); fflush(stdout);
waitpid(pid, &status, 0);
- printf("Done mooing\n"); fflush(stdout);
+ a->mootex = 0;
} else {
if ( a->moo_pos == a->moo_len ) {
@@ -90,7 +102,7 @@ void audio_trigger_moo(AudioContext *a) {
}
/* SDL audio initial setup */
-AudioContext *audio_setup() {
+void audio_setup() {
AudioContext *a;
SDL_AudioSpec fmt;
@@ -101,7 +113,9 @@ AudioContext *audio_setup() {
/* Create audio context */
a = malloc(sizeof(AudioContext));
- if ( a == NULL ) return NULL;
+ assert(a != NULL);
+
+ a->mootex = 0;
/* 16-bit mono audio at 44.1 kHz */
fmt.freq = 44100;
@@ -115,19 +129,19 @@ AudioContext *audio_setup() {
if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
a->aplay_fallback = 1;
- return a;
+ return;
}
a->aplay_fallback = 0;
if ( SDL_LoadWAV(DATADIR"/openmoocow/moo.wav", &wave, &data, &dlen) == NULL ) {
fprintf(stderr, "Couldn't load moo sound: %s\n", SDL_GetError());
- return a;
+ return;
}
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 1, 44100);
cvt.buf = malloc(dlen*cvt.len_mult);
if ( cvt.buf == NULL ) {
fprintf(stderr, "Not enough memory to convert audio \n");
- return a;
+ return;
}
memcpy(cvt.buf, data, dlen);
cvt.len = dlen;
@@ -140,7 +154,7 @@ AudioContext *audio_setup() {
SDL_PauseAudio(0);
- return a;
+ audio_context = a;
}
diff --git a/src/audio.h b/src/audio.h
index fc4e9f6..a8f0032 100644
--- a/src/audio.h
+++ b/src/audio.h
@@ -32,8 +32,8 @@
#include "types.h"
extern AudioContext *audio_setup(void);
-extern void audio_trigger_moo(AudioContext *a);
-extern void audio_shutdown(AudioContext *a);
+extern void audio_trigger_moo(void);
+extern void audio_shutdown(void);
#endif /* AUDIO_H */
diff --git a/src/main.c b/src/main.c
index 1d74b7b..f07e829 100644
--- a/src/main.c
+++ b/src/main.c
@@ -30,16 +30,26 @@
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
+#include <glib.h>
#include "types.h"
#include "mainwindow.h"
+#include "accelerometers.h"
int main(int argc, char *argv[]) {
MainWindow *mw;
gtk_init(&argc, &argv);
+ g_thread_init(NULL);
+
+ /* Start the accelerometer thread */
+ accelerometer_start();
+
+ /* Open the window */
mw = mainwindow_open();
+
+ /* Wait "forever" */
if ( mw != NULL ) {
gtk_main();
}
diff --git a/src/mainwindow.c b/src/mainwindow.c
index 8c2b256..93a1fd9 100644
--- a/src/mainwindow.c
+++ b/src/mainwindow.c
@@ -28,41 +28,24 @@
#include <gtk/gtk.h>
#include <stdlib.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <math.h>
#include "types.h"
-#include "accelerometers.h"
#include "audio.h"
-#include "physics.h"
-
-static gboolean mainwindow_timeout(gpointer data) {
-
- MainWindow *mw = data;
-
- accelerometer_update(mw->accel);
-
- physics_update(mw->physics, mw->accel->lval);
- if ( mw->physics->moo ) {
- audio_trigger_moo(mw->audio);
- mw->physics->moo = 0;
- }
-
- return TRUE; /* Call back again */
-
-}
static gint mainwindow_closed(GtkWidget *widget, MainWindow *mw) {
- audio_shutdown(mw->audio);
gtk_exit(0);
return 0;
}
+static gint mainwindow_clicked(GtkWidget *widget, GdkEventButton *event, gpointer data) {
+ audio_trigger_moo();
+ return 0;
+}
+
MainWindow *mainwindow_open(void) {
MainWindow *mw;
- GtkWidget *label;
+ GtkWidget *eb;
mw = malloc(sizeof(*mw));
if ( mw == NULL ) return NULL;
@@ -71,16 +54,13 @@ MainWindow *mainwindow_open(void) {
gtk_window_set_default_size(GTK_WINDOW(mw->window), 240, 320);
gtk_window_set_title(GTK_WINDOW(mw->window), "OpenMooCow");
+ eb = gtk_event_box_new();
mw->cow = gtk_image_new_from_file(PIXMAPDIR"/cow.png");
- gtk_container_add(GTK_CONTAINER(mw->window), mw->cow);
-
- mw->accel = accelerometer_open();
- mw->accel_timeout = g_timeout_add(10, mainwindow_timeout, mw);
- accelerometer_update(mw->accel);
-
- mw->audio = audio_setup();
- mw->physics = physics_setup();
+ gtk_container_add(GTK_CONTAINER(eb), mw->cow);
+ gtk_container_add(GTK_CONTAINER(mw->window), eb);
+ gtk_widget_add_events(GTK_WIDGET(eb), GDK_BUTTON_PRESS_MASK);
+ g_signal_connect(G_OBJECT(eb), "button_press_event", G_CALLBACK(mainwindow_clicked), mw);
g_signal_connect(G_OBJECT(mw->window), "destroy", G_CALLBACK(mainwindow_closed), mw);
gtk_widget_show_all(mw->window);
diff --git a/src/physics.c b/src/physics.c
deleted file mode 100644
index 40aca38..0000000
--- a/src/physics.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * physics.c
- *
- * Moobox physics simulation
- *
- * (c) 2008 Thomas White <taw27@srcf.ucam.org>
- *
- * This file is part of OpenMooCow - accelerometer moobox simulator
- *
- * OpenMooCow 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 3 of the License, or
- * (at your option) any later version.
- *
- * OpenMooCow 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 OpenMooCow. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdlib.h>
-
-#include "types.h"
-
-PhysicsContext *physics_setup() {
-
- PhysicsContext *ctx;
-
- ctx = malloc(sizeof(PhysicsContext));
- if ( ctx == NULL ) return NULL;
-
- ctx->pos = 0;
- ctx->moo = 0;
-
- return ctx;
-
-}
-
-/* lval = acceleration upwards in cm/s/s */
-void physics_update(PhysicsContext *ctx, int lval) {
-
- if ( lval > 1000 ) ctx->pos = 1000;
- if ( (lval < -1000) && (ctx->pos == 1000) ) {
- ctx->pos = 0;
- ctx->moo = 1;
- }
-
-}
-
diff --git a/src/physics.h b/src/physics.h
deleted file mode 100644
index 07b89aa..0000000
--- a/src/physics.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * physics.h
- *
- * Moobox physics simulation
- *
- * (c) 2008 Thomas White <taw27@srcf.ucam.org>
- *
- * This file is part of OpenMooCow - accelerometer moobox simulator
- *
- * OpenMooCow 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 3 of the License, or
- * (at your option) any later version.
- *
- * OpenMooCow 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 OpenMooCow. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-
-#ifndef PHYSICS_H
-#define PHYSICS_H
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include "types.h"
-
-extern PhysicsContext *physics_setup(void);
-extern void physics_update(PhysicsContext *ctx, int lval);
-
-
-#endif /* PHYSICS_H */
-
diff --git a/src/types.h b/src/types.h
index d4a2f62..7837ff2 100644
--- a/src/types.h
+++ b/src/types.h
@@ -61,36 +61,15 @@ typedef struct {
int aplay_fallback;
-} AudioContext;
-
-typedef struct {
-
- int pos; /* Slider position, 0=bottom, 1000=top */
- int moo;
+ unsigned int mootex;
-} PhysicsContext;
+} AudioContext;
typedef struct {
GtkWidget *window;
- GtkWidget *notebook;
- GtkWidget *acceldata;
GtkWidget *cow;
- GtkWidget *ax;
- GtkWidget *ay;
- GtkWidget *az;
- GtkWidget *bx;
- GtkWidget *by;
- GtkWidget *bz;
- GtkWidget *moo;
-
- AccelHandle *accel;
- guint accel_timeout;
-
- AudioContext *audio;
- PhysicsContext *physics;
-
} MainWindow;
#endif /* TYPES_H */