-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio.cpp
More file actions
208 lines (185 loc) · 4.71 KB
/
audio.cpp
File metadata and controls
208 lines (185 loc) · 4.71 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <limits.h>
#include <assert.h>
using namespace std;
#include <SDL.h>
SDL_AudioSpec audio;
static int audio_initialized = 0;
#define SOUND_LOOP 1
#define SOUND_TERMINATE 2
#define SOUND_STEREO 4
struct Sound {
Uint32 pos;
Uint32 vol;
Uint32 pan;
Uint8 *data;
Uint32 length;
Uint32 flags;
Sound *next;
};
Sound SFX[128];
int num_sounds = 0;
Sound *free_blocks = NULL;
Sound *play_blocks = NULL;
void audio_callback(void *userdata, Uint8 *stream, int len) {
if(play_blocks == NULL) {
memset(stream, 0, len);
return;
}
for(int ctr=0; ctr<len; ctr+=4) {
long buf_l = 0, buf_r = 0;
Sound **sptr = &play_blocks;
while((*sptr) != NULL) {
if((*sptr)->flags & SOUND_TERMINATE) {
Sound *it = (*sptr);
(*sptr) = (*sptr)->next;
it->next = free_blocks;
free_blocks = it;
continue;
}
if((*sptr)->vol > 0) {
if((*sptr)->flags & SOUND_STEREO) { // Stereo Source
if((*sptr)->pos+ctr >= (*sptr)->length) {
sptr = &((*sptr)->next);
continue;
}
long val_l = Sint8((*sptr)->data[(*sptr)->pos+ctr+1]);
val_l <<= 8;
val_l |= (*sptr)->data[(*sptr)->pos+ctr+0] & 0xFF;
buf_l += val_l * (*sptr)->vol * 16;
long val_r = Sint8((*sptr)->data[(*sptr)->pos+ctr+3]);
val_r <<= 8;
val_r |= (*sptr)->data[(*sptr)->pos+ctr+2] & 0xFF;
buf_r += val_r * (*sptr)->vol * 16;
}
else { // Mono Source with software panning
if((*sptr)->pos+(ctr>>1) >= (*sptr)->length) {
sptr = &((*sptr)->next);
continue;
}
long val = Sint8((*sptr)->data[(*sptr)->pos+(ctr>>1)+1]);
val <<= 8;
val |= (*sptr)->data[(*sptr)->pos+(ctr>>1)+0] & 0xFF;
buf_l += val * (*sptr)->vol * (16-(*sptr)->pan);
buf_r += val * (*sptr)->vol * (16+(*sptr)->pan);
}
}
sptr = &((*sptr)->next);
}
if(buf_l > SHRT_MAX<<8) buf_l = SHRT_MAX<<8;
if(buf_l < SHRT_MIN<<8) buf_l = SHRT_MIN<<8;
if(buf_r > SHRT_MAX<<8) buf_r = SHRT_MAX<<8;
if(buf_r < SHRT_MIN<<8) buf_r = SHRT_MIN<<8;
stream[ctr+0] = (buf_l >> 8);
stream[ctr+1] = (buf_l >> 16);
stream[ctr+2] = (buf_r >> 8);
stream[ctr+3] = (buf_r >> 16);
}
Sound **sptr = &play_blocks;
while((*sptr) != NULL) {
if((*sptr)->flags & SOUND_STEREO) (*sptr)->pos += len;
else (*sptr)->pos += len>>1;
if((*sptr)->pos >= (*sptr)->length) {
if((*sptr)->flags & SOUND_LOOP) (*sptr)->pos = 0;
else {
Sound *it = (*sptr);
(*sptr) = (*sptr)->next;
it->next = free_blocks;
free_blocks = it;
continue;
}
}
sptr = &((*sptr)->next);
}
}
int audio_buildsound(const unsigned char *data, unsigned long len) {
if(!audio_initialized) return 0;
SFX[num_sounds].data = (unsigned char *)data;
SFX[num_sounds].length = len;
++num_sounds;
return num_sounds;
}
int audio_loadsound(const char *fn) {
if(!audio_initialized) return 0;
SDL_AudioSpec spec;
if(!SDL_LoadWAV(fn, &spec, &SFX[num_sounds].data, &SFX[num_sounds].length)) {
fprintf(stderr, "Warning: Can't load \"%s\"!\n", fn);
return 0;
}
++num_sounds;
return num_sounds;
}
int audio_loadmusic(const char *fn) {
int ret = audio_loadsound(fn);
SFX[ret-1].flags |= SOUND_STEREO;
return ret;
}
Sound *get_block() {
Sound *s;
if(free_blocks) { s = free_blocks; free_blocks = s->next; }
else { s = new Sound; }
return s;
}
void audio_play(int snd, int vol, int pan) {
if((!audio_initialized) || snd < 1) return;
Sound *s = get_block();
*s = SFX[snd-1];
s->pos = 0;
s->vol = vol;
s->pan = pan;
s->flags = 0;
SDL_LockAudio();
s->next = play_blocks;
play_blocks = s;
SDL_UnlockAudio();
}
Sound *audio_loop(int snd, int vol, int pan) {
if((!audio_initialized) || snd < 1) return NULL;
Sound *s = get_block();
*s = SFX[snd-1];
s->pos = 0;
s->vol = vol;
s->pan = pan;
s->flags |= SOUND_LOOP;
SDL_LockAudio();
s->next = play_blocks;
play_blocks = s;
SDL_UnlockAudio();
return s;
}
void audio_setvol(Sound *s, int v) {
if(!audio_initialized) return;
SDL_LockAudio();
if(s) s->vol = v;
SDL_UnlockAudio();
}
void audio_setpan(Sound *s, int p) {
if(!audio_initialized) return;
SDL_LockAudio();
if(s) s->pan = p;
SDL_UnlockAudio();
}
void audio_stop(Sound *s) {
if(!audio_initialized) return;
SDL_LockAudio();
if(s) s->flags |= SOUND_TERMINATE;
SDL_UnlockAudio();
}
void audio_init() {
audio.freq = 44100;
audio.format = AUDIO_S16;
audio.samples = 2048;
audio.channels = 2;
audio.callback = audio_callback;
audio.userdata = NULL;
if(SDL_OpenAudio(&audio, NULL) < 0) {
fprintf(stderr, "Error initializing SDL Audio!\n");
fprintf(stderr, "Continuing without sound.\n");
return;
}
SDL_PauseAudio(0);
audio_initialized = 1;
}