aboutsummaryrefslogtreecommitdiff
path: root/src/audio.c
blob: 25e751a78bddc0e31c8836932a9099e7a726824c (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
/*
 * audio.c
 *
 * Sound stuff
 *
 * (c) 2008 Thomas White <taw27@cam.ac.uk>
 *
 *  thrust3d - a silly game
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <SDL.h>
#include <SDL_audio.h>

#include "types.h"

static void audio_mix(void *data, Uint8 *stream8, int len) {

	AudioContext *a = data;
	int i, j, clip_count;
	Sint16 *stream = (Sint16 *)stream8;
	
	len /= 2;	/* Samples */
	
	/* Zero the buffer */
	for ( j=0; j<len; j++ ) {
		stream[j] = 0;
	}
	
	/* For each currently playing sound... */
	clip_count = 0;
	for ( i=0; i<AUDIO_MAX_SOUNDS; i++ ) {
		
		/* Playing? */
		if ( !a->sounds[i].inuse ) continue;
		
		for ( j=0; j<len; j++ ) {
		
			Sint16 samp;
			Sint32 test;
			
			samp = a->sounds[i].data[a->sounds[i].dpos++];
			test = stream[j] + samp*a->sounds[i].volume;
			if ( test > 32767 ) {
				if ( stream[j] != 32767 ) {
					clip_count++;
				}
				stream[j] = 32767;
			} else {
				stream[j] += samp * a->sounds[i].volume;
			}
			
			if ( a->sounds[i].dpos == a->sounds[i].dlen ) {
				if ( a->sounds[i].repeat ) {
					a->sounds[i].dpos = 0;
					if ( a->debug ) printf("AU: Looping sound %i at buffer offset %i/%i\n", i, j, len);
				} else {
					a->sounds[i].inuse = 0;
					free(a->sounds[i].data);
					if ( a->debug ) printf("AU: End of sound %i\n", i);
					break;
				}
			}
		
		}
		
	}
	
	/* Fast ramp-up of volume when starting program, avoids 'click' */
	if ( a->startup ) {
		for ( j=0; j<len; j++ ) {
			stream[j] *= a->startup_volume;
			a->startup_volume += 0.001;
			if ( a->startup_volume > 1.0 ) {
				if ( a->startup ) printf("AU: Finished startup ramp.\n");
				a->startup = 0;
				a->startup_volume = 1.0;
			}
		}
		
	}
	
	if ( a->debug && (clip_count > 0) ) printf("AU: Clipped %i samples.\n", clip_count);
	
}

void audio_play(AudioContext *a, char *file, float volume, int repeat) {

	int idx;
	SDL_AudioSpec wave;
	Uint8 *data;
	Uint32 dlen;
	SDL_AudioCVT cvt;
	char filename[128];
	
	/* Look for an empty sound slot */
	for ( idx=0; idx<AUDIO_MAX_SOUNDS; idx++ ) {
		if ( !a->sounds[idx].inuse ) break;
	}
	if ( a->debug ) printf("AU: Selected channel %i for sound '%s'\n", idx, file);
	if ( idx == AUDIO_MAX_SOUNDS ) {
		fprintf(stderr, "Not enough audio channels to play '%s'\n", file);
		return;
	}
	
	snprintf(filename, 127, "%s%s.wav", DATADIR"/sound/", file);
	
	/* Load the sound file and convert it to 16-bit stereo at 22kHz */
	if ( SDL_LoadWAV(filename, &wave, &data, &dlen) == NULL ) {
		fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
		return;
	}
	SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq, AUDIO_S16, 2, 44100);
	cvt.buf = malloc(dlen*cvt.len_mult);
	memcpy(cvt.buf, data, dlen);
	cvt.len = dlen;
	SDL_ConvertAudio(&cvt);
	SDL_FreeWAV(data);
	
	/* Put the sound data in the slot */
	SDL_LockAudio();
	a->sounds[idx].data = (Sint16 *)cvt.buf;
	a->sounds[idx].dlen = cvt.len_cvt / 2;
	a->sounds[idx].dpos = 0;
	a->sounds[idx].inuse = 1;
	a->sounds[idx].repeat = repeat;
	a->sounds[idx].volume = volume;
	SDL_UnlockAudio();
	
}

/* SDL audio initial setup */
AudioContext *audio_setup(int debug) {
	
	AudioContext *a;
	SDL_AudioSpec fmt;
	int i;
	
	/* Create audio context */
	a = malloc(sizeof(AudioContext));
	if ( a == NULL ) return NULL;
	
	/* Initialise audio context */
	a->debug = debug;
	a->startup = 1;
	a->startup_volume = 0.0;
	for ( i=0; i<AUDIO_MAX_SOUNDS; i++ ) {
		a->sounds[i].inuse = 0;
	}
	
	/* 16-bit stereo audio at 44.1 kHz */
	fmt.freq = 44100;
	fmt.format = AUDIO_S16;
	fmt.channels = 2;
	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());
		free(a);
		return NULL;
	}
	SDL_PauseAudio(0);
	
	audio_play(a, "moan", 0.4, 1);
	
	return a;
	
}

void audio_shutdown(AudioContext *ctx) {
	SDL_CloseAudio();
	free(ctx);
}