aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/.gitignore6
-rw-r--r--src/Makefile.am11
-rw-r--r--src/accelerometers.c145
-rw-r--r--src/accelerometers.h38
-rw-r--r--src/audio.c159
-rw-r--r--src/audio.h39
-rw-r--r--src/main.c50
-rw-r--r--src/mainwindow.c90
-rw-r--r--src/mainwindow.h37
-rw-r--r--src/moosynth.c140
-rw-r--r--src/physics.c57
-rw-r--r--src/physics.h39
-rw-r--r--src/types.h97
13 files changed, 908 insertions, 0 deletions
diff --git a/src/.gitignore b/src/.gitignore
new file mode 100644
index 0000000..b851e33
--- /dev/null
+++ b/src/.gitignore
@@ -0,0 +1,6 @@
+.deps
+Makefile
+Makefile.in
+*.o
+openmoocow
+moosynth
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..e125111
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,11 @@
+bin_PROGRAMS = openmoocow
+
+openmoocow_SOURCES = main.c mainwindow.c accelerometers.c audio.c physics.c
+openmoocow_LDADD = @LIBS@
+
+moosynth_SOURCES = moosynth.c
+moosynth_LDADD = @LIBS@ -lfftw3
+
+AM_CFLAGS = -Wall -g @CFLAGS@
+AM_CPPFLAGS = -DDATADIR=\""$(datadir)"\" -DPIXMAPDIR=\""$(prefix)/share/pixmaps/openmoocow"\"
+
diff --git a/src/accelerometers.c b/src/accelerometers.c
new file mode 100644
index 0000000..7d8b0f2
--- /dev/null
+++ b/src/accelerometers.c
@@ -0,0 +1,145 @@
+/*
+ * accelerometers.c
+ *
+ * Accelerometer stuff
+ *
+ * (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 <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include "types.h"
+
+struct input_event {
+ struct timeval time;
+ uint16_t type;
+ uint16_t code;
+ int32_t value;
+};
+#define EV_SYN (0x00)
+#define EV_REL (0x02)
+#define SYN_REPORT (0x00)
+#define REL_X (0x00)
+#define REL_Y (0x01)
+#define REL_Z (0x02)
+
+AccelHandle *accelerometer_open() {
+
+ AccelHandle *accel;
+
+ accel = malloc(sizeof(AccelHandle));
+ if ( accel == NULL ) return NULL;
+
+ accel->fh1 = fopen("/dev/input/event2", "rb");
+ accel->fh2 = NULL;
+// accel->fh2 = fopen("/dev/input/event3", "rb");
+ accel->ax = 0;
+ accel->ay = 0;
+ accel->az = 0;
+ accel->bx = 0;
+ accel->by = 0;
+ accel->bz = 0;
+
+ return accel;
+
+}
+
+void accelerometer_update(AccelHandle *accel) {
+
+ if ( accel->fh1 != NULL ) {
+
+ int i;
+
+ for ( i=1; i<=4; i++ ) {
+
+ struct input_event ev;
+ size_t rval;
+
+ rval = fread(&ev, sizeof(struct input_event), 1, accel->fh1);
+ if ( rval != 1 ) {
+ fprintf(stderr, "Couldn't read accelerometer data");
+ return;
+ }
+
+ if ( ev.type == EV_REL ) {
+ if ( ev.code == REL_X ) {
+ accel->lax = ev.value;
+ }
+ if ( ev.code == REL_Y ) {
+ accel->lay = ev.value;
+ }
+ if ( ev.code == REL_Z ) {
+ accel->laz = ev.value;
+ }
+ }
+
+ if ( ev.type == EV_SYN ) {
+ if ( ev.code == SYN_REPORT ) {
+ accel->ax = accel->lax;
+ accel->ay = accel->lay;
+ accel->az = accel->laz;
+ accel->lval = accel->ax + accel->ay;
+ }
+ }
+
+ }
+
+ }
+
+ if ( accel->fh2 != NULL ) {
+
+ struct input_event ev;
+ size_t rval;
+
+ rval = fread(&ev, sizeof(struct input_event), 1, accel->fh2);
+ if ( rval != 1 ) {
+ fprintf(stderr, "Couldn't read accelerometer data");
+ return;
+ }
+
+ if ( ev.type == EV_REL ) {
+ if ( ev.code == REL_X ) {
+ accel->lbx = ev.value;
+ }
+ if ( ev.code == REL_Y ) {
+ accel->lby = ev.value;
+ }
+ if ( ev.code == REL_Z ) {
+ accel->lbz = ev.value;
+ }
+ }
+
+ if ( ev.type == EV_SYN ) {
+ if ( ev.code == SYN_REPORT ) {
+ accel->bx = accel->lbx;
+ accel->by = accel->lby;
+ accel->bz = accel->lbz;
+ }
+ }
+ }
+
+
+}
+
diff --git a/src/accelerometers.h b/src/accelerometers.h
new file mode 100644
index 0000000..f83149a
--- /dev/null
+++ b/src/accelerometers.h
@@ -0,0 +1,38 @@
+/*
+ * accelerometers.h
+ *
+ * Accelerometer stuff
+ *
+ * (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 ACCELEROMETERS_H
+#define ACCELEROMETERS_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "types.h"
+
+extern AccelHandle *accelerometer_open(void);
+extern void accelerometer_update(AccelHandle *accel);
+
+#endif /* ACCELEROMETERS_H */
+
diff --git a/src/audio.c b/src/audio.c
new file mode 100644
index 0000000..ed37a89
--- /dev/null
+++ b/src/audio.c
@@ -0,0 +1,159 @@
+/*
+ * audio.c
+ *
+ * Moo like a cow
+ *
+ * (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 <stdarg.h>
+#include <stdio.h>
+#include <SDL.h>
+#include <SDL_audio.h>
+#include <math.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#include "types.h"
+
+static void audio_mix(void *data, Uint8 *stream8, int len) {
+
+ AudioContext *a = data;
+ int j;
+ Sint16 *stream = (Sint16 *)stream8;
+ Sint16 samp;
+
+ len /= 2; /* Number of samples to write */
+
+ for ( j=0; j<len; j++ ) {
+
+ stream[j] = 0;
+
+ if ( a->moo_pos < a->moo_len ) {
+ samp = a->moo_buf[a->moo_pos++];
+ stream[j] += samp;
+ }
+
+ }
+
+}
+
+void audio_trigger_moo(AudioContext *a) {
+
+ if ( a->aplay_fallback ) {
+
+ pid_t pid;
+ int status;
+
+ pid = fork();
+ if ( !( (pid != 0) && (pid != -1) ) ) {
+ if ( pid == -1 ) {
+ fprintf(stderr, "fork() failed.\n");
+ return;
+ } else {
+ /* Forked successfully, child process */
+ 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);
+
+ } else {
+ if ( a->moo_pos == a->moo_len ) {
+ a->moo_pos = 0;
+ }
+ }
+
+}
+
+/* SDL audio initial setup */
+AudioContext *audio_setup() {
+
+ AudioContext *a;
+ SDL_AudioSpec fmt;
+ SDL_AudioSpec wave;
+ Uint8 *data;
+ Uint32 dlen;
+ SDL_AudioCVT cvt;
+
+ /* Create audio context */
+ a = malloc(sizeof(AudioContext));
+ if ( a == NULL ) return NULL;
+
+ /* 16-bit mono audio at 44.1 kHz */
+ fmt.freq = 44100;
+ fmt.format = AUDIO_S16;
+ fmt.channels = 1;
+ fmt.samples = 512;
+ fmt.callback = audio_mix;
+ fmt.userdata = a;
+ fmt.silence = 0;
+
+ if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
+ fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
+ a->aplay_fallback = 1;
+ return a;
+ }
+ 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;
+ }
+ 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;
+ }
+ memcpy(cvt.buf, data, dlen);
+ cvt.len = dlen;
+ SDL_ConvertAudio(&cvt);
+ SDL_FreeWAV(data);
+
+ a->moo_len = cvt.len_cvt/2 - 2; /* Convert bytes to samples */
+ a->moo_pos = a->moo_len; /* Play nothing to start with */
+ a->moo_buf = (Sint16 *)cvt.buf;
+
+ SDL_PauseAudio(0);
+
+ return a;
+
+}
+
+void audio_shutdown(AudioContext *a) {
+
+ if ( a == NULL ) return;
+
+ if ( !a->aplay_fallback ) {
+ SDL_CloseAudio();
+ }
+
+ /* Now this can be freed */
+ free(a);
+
+}
+
diff --git a/src/audio.h b/src/audio.h
new file mode 100644
index 0000000..fc4e9f6
--- /dev/null
+++ b/src/audio.h
@@ -0,0 +1,39 @@
+/*
+ * audio.h
+ *
+ * Moo like a cow
+ *
+ * (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 AUDIOH
+#define AUDIO_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "types.h"
+
+extern AudioContext *audio_setup(void);
+extern void audio_trigger_moo(AudioContext *a);
+extern void audio_shutdown(AudioContext *a);
+
+#endif /* AUDIO_H */
+
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..1d74b7b
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,50 @@
+/*
+ * main.c
+ *
+ * The Top Level Source File
+ *
+ * (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 <gtk/gtk.h>
+#include <stdlib.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#include "types.h"
+#include "mainwindow.h"
+
+int main(int argc, char *argv[]) {
+
+ MainWindow *mw;
+
+ gtk_init(&argc, &argv);
+ mw = mainwindow_open();
+ if ( mw != NULL ) {
+ gtk_main();
+ }
+
+ return 0;
+
+}
+
diff --git a/src/mainwindow.c b/src/mainwindow.c
new file mode 100644
index 0000000..8c2b256
--- /dev/null
+++ b/src/mainwindow.c
@@ -0,0 +1,90 @@
+/*
+ * mainwindow.c
+ *
+ * Main window
+ *
+ * (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 <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;
+}
+
+MainWindow *mainwindow_open(void) {
+
+ MainWindow *mw;
+ GtkWidget *label;
+
+ mw = malloc(sizeof(*mw));
+ if ( mw == NULL ) return NULL;
+
+ mw->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+ gtk_window_set_default_size(GTK_WINDOW(mw->window), 240, 320);
+ gtk_window_set_title(GTK_WINDOW(mw->window), "OpenMooCow");
+
+ 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();
+
+ g_signal_connect(G_OBJECT(mw->window), "destroy", G_CALLBACK(mainwindow_closed), mw);
+ gtk_widget_show_all(mw->window);
+
+ return mw;
+
+}
+
diff --git a/src/mainwindow.h b/src/mainwindow.h
new file mode 100644
index 0000000..4f994ec
--- /dev/null
+++ b/src/mainwindow.h
@@ -0,0 +1,37 @@
+/*
+ * mainwindow.h
+ *
+ * Main window
+ *
+ * (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 MAINWINDOW_H
+#define MAINWINDOW_H
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "types.h"
+
+MainWindow *mainwindow_open(void);
+
+#endif /* MAINWINDOW_H */
+
diff --git a/src/moosynth.c b/src/moosynth.c
new file mode 100644
index 0000000..6d78415
--- /dev/null
+++ b/src/moosynth.c
@@ -0,0 +1,140 @@
+/*
+ * moosynth.c
+ *
+ * Work out how to synthesize a cow
+ *
+ * (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 <stdarg.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fftw3.h>
+#include <math.h>
+
+int main(int argc, char *argv[]) {
+
+ FILE *fh;
+ double *data;
+ fftw_complex *ft;
+ struct stat statbuf;
+ fftw_plan plan;
+ int samples;
+ int16_t *sdata;
+ int i, max_fq, c;
+ double max_am, next_max_am, max_ph;
+ const int ncomp = 20;
+ int components[ncomp];
+
+ if ( argc != 2 ) {
+ fprintf(stderr, "Syntax: %s <file.pcm>\n", argv[0]);
+ return 1;
+ }
+
+ /* Determine size of data */
+ if ( stat(argv[1], &statbuf) == -1 ) {
+ fprintf(stderr, "Couldn't stat file '%s'\n", argv[1]);
+ return 1;
+ }
+ samples = statbuf.st_size/2; /* Two bytes per sample, one channel */
+ printf("File size: %lli bytes\n", (long long int)statbuf.st_size);
+
+ /* Read the data in */
+ fh = fopen(argv[1], "rb");
+ if ( fh == NULL ) {
+ fprintf(stderr, "Couldn't open file '%s'\n", argv[1]);
+ return 1;
+ }
+ sdata = malloc(samples*2);
+ fread(sdata, 2, samples, fh);
+ fclose(fh);
+
+ data = fftw_malloc(samples*sizeof(double));
+ ft = fftw_malloc(samples*sizeof(fftw_complex));
+
+ for ( i=0; i<samples; i++ ) {
+ data[i] = sdata[i];
+ }
+
+ plan = fftw_plan_dft_r2c_1d(samples, data, ft, FFTW_ESTIMATE);
+ fftw_execute(plan);
+
+ printf("DC component: %f %f\n", ft[0][0], ft[0][1]);
+ next_max_am = +1000000000.0;
+ for ( c=0; c<ncomp-1; c++ ) {
+
+ max_am = 0.0;
+ max_ph = 0.0;
+ max_fq = 0;
+ for ( i=1; i<samples/2; i++ ) {
+
+ double re, im, am, ph;
+
+ re = ft[i][0];
+ im = ft[i][1];
+ am = sqrt(re*re + im*im);
+ ph = atan2(im, re);
+ // printf("Frequency: %i Hz : %f %f\n", (44100/samples)*i, am, 180*(ph/M_PI));
+
+ if ( (am > max_am) && (am < next_max_am) ) {
+ max_am = am;
+ max_fq = i;
+ max_ph = ph;
+ }
+
+ }
+ printf("%2i %6i Hz %14.2f %+7.1f\n", c+1, (44100/samples)*max_fq, max_am, 180*(max_ph/M_PI));
+ components[c] = max_fq;
+ next_max_am = max_am;
+
+ }
+
+ for ( i=0; i<samples/2; i++ ) {
+ int c;
+ int found = 0;
+ for ( c=0; c<ncomp-1; c++ ) {
+ if ( components[c] == i ) found = 1;
+ }
+ if ( !found ) {
+ ft[i][0] = 0.0;
+ ft[i][1] = 0.0;
+ }
+ }
+
+ plan = fftw_plan_dft_c2r_1d(samples, ft, data, FFTW_ESTIMATE);
+ fftw_execute(plan);
+
+ for ( i=0; i<samples; i++ ) {
+ sdata[i] = (data[i])/(samples*2);
+ }
+ fh = fopen("filtered.pcm", "wb");
+ fwrite(sdata, 2, samples, fh);
+ fclose(fh);
+
+ return 0;
+
+}
+
diff --git a/src/physics.c b/src/physics.c
new file mode 100644
index 0000000..40aca38
--- /dev/null
+++ b/src/physics.c
@@ -0,0 +1,57 @@
+/*
+ * 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
new file mode 100644
index 0000000..07b89aa
--- /dev/null
+++ b/src/physics.h
@@ -0,0 +1,39 @@
+/*
+ * 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
new file mode 100644
index 0000000..d4a2f62
--- /dev/null
+++ b/src/types.h
@@ -0,0 +1,97 @@
+/*
+ * types.h
+ *
+ * Data types
+ *
+ * (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 TYPES_H
+#define TYPES_H
+
+#include <gtk/gtk.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <SDL.h>
+
+typedef struct {
+
+ FILE *fh1;
+ FILE *fh2;
+
+ int ax;
+ int ay;
+ int az;
+ int bx;
+ int by;
+ int bz;
+
+ int lval; /* The "mooing" contribution */
+
+ int lax;
+ int lay;
+ int laz;
+ int lbx;
+ int lby;
+ int lbz;
+
+} AccelHandle;
+
+typedef struct {
+
+ long moo_len;
+ long moo_pos;
+ Sint16 *moo_buf;
+
+ int aplay_fallback;
+
+} AudioContext;
+
+typedef struct {
+
+ int pos; /* Slider position, 0=bottom, 1000=top */
+ int moo;
+
+} PhysicsContext;
+
+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 */
+