aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
blob: 1ec810890cfaa271dbd375ad46d63d4a987da180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * audio.c
 *
 * Moo like a cow
 *
 * (c) 2008-2009 Thomas White <taw@bitwiz.org.uk>
 *
 * 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/>.
 *
 */

/* SDL's headers don't like this. Grr. */
//#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 <assert.h>

#include "types.h"
#include "audio.h"

static AudioContext *audio_context = NULL;

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;
		} else {
			a->mootex = 0;
		}

	}

	a->aplay_fallback = 0;
}

void audio_trigger_moo()
{
	AudioContext *a = audio_context;

	if ( a->mootex != 0 ) {
		printf("Mootex says 'no'\n");
		return;
	}

	if ( a == NULL ) {
		/* Try to open the audio again */
		printf("Trying to open the audio again...\n");
		audio_setup();
		a = audio_context;
		if ( a == NULL ) return;
	}
	printf("Moo!\n");
	a->mootex = 1;

	if ( a->aplay_fallback ) {

		pid_t pid;
		int status;

		printf("Using aplay fallback\n");
		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 */
		waitpid(pid, &status, 0);
		a->mootex = 0;

	} else if ( a->moo_pos == a->moo_len ) {
		a->moo_pos = 0;
	}
}

/* SDL audio initial setup */
void audio_setup()
{
	AudioContext *a;
	SDL_AudioSpec fmt;
	SDL_AudioSpec wave;
	Uint8 *data;
	Uint32 dlen;
	SDL_AudioCVT cvt;

	/* Create audio context */
	a = malloc(sizeof(AudioContext));
	assert(a != NULL);
	a->mootex = 1;		/* Not ready yet */
	audio_context = a;

	/* 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());
		goto out; /* and use fallback */
	}

	if ( SDL_LoadWAV(DATADIR"/openmoocow/moo.wav", &wave, &data, &dlen)
								== NULL ) {
		fprintf(stderr, "Couldn't load moo sound: %s\n",
				SDL_GetError());
		goto out; /* and use fallback */
	}
	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");
		goto out; /* and use fallback */
	}
	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;
	a->aplay_fallback = 1;

	SDL_PauseAudio(0);

out:
	a->mootex = 0;		/* Ready now */
}

void audio_shutdown()
{
	AudioContext *a = audio_context;

	if ( a == NULL ) return;

	SDL_CloseAudio();

	/* Now this can be freed */
	free(a);
}