This repository has been archived by the owner on Jul 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathAudio.cpp
223 lines (195 loc) · 5.63 KB
/
Audio.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (c) 2014, Wei Mingzhi <whistler_wmz@users.sf.net>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author and contributors may not be used to endorse
* or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
static int audio_len = 0;
static unsigned char *audio_pos = NULL;
static int audio_len2 = 0;
static unsigned char *audio_pos2 = NULL;
static SDL_mutex *mtx = NULL;
static SDL_AudioSpec audio_spec;
bool g_fAudioOpened = false;
// The audio function callback takes the following parameters:
// stream: A pointer to the audio buffer to be filled
// len: The length (in bytes) of the audio buffer
static void SOUND_FillAudio(void *, unsigned char *stream, int len)
{
memset(stream, 0, len);
int len2 = len;
unsigned char *mix_stream1 = NULL, *mix_stream2 = NULL;
int mix_len1 = 0, mix_len2 = 0;
SDL_mutexP(mtx);
// Mix as much data as possible
if (audio_len > 0)
{
len = (len > audio_len) ? audio_len : len;
mix_stream1 = audio_pos;
mix_len1 = len;
audio_pos += len;
audio_len -= len;
}
if (audio_len2 > 0)
{
len = (len2 > audio_len2) ? audio_len2 : len2;
mix_stream2 = audio_pos2;
mix_len2 = len;
audio_pos2 += len;
audio_len2 -= len;
}
SDL_mutexV(mtx);
if (mix_stream1 != NULL)
{
SDL_MixAudio(stream, mix_stream1, mix_len1, SDL_MIX_MAXVOLUME);
}
if (mix_stream2 != NULL)
{
SDL_MixAudio(stream, mix_stream2, mix_len2, SDL_MIX_MAXVOLUME);
}
}
int SOUND_OpenAudio(int freq, int channels, int samples)
{
if (g_fAudioOpened)
{
return 0;
}
mtx = SDL_CreateMutex();
// Set the audio format
audio_spec.freq = freq;
audio_spec.format = AUDIO_S16;
audio_spec.channels = channels; // 1 = mono, 2 = stereo
audio_spec.samples = samples;
audio_spec.callback = SOUND_FillAudio;
audio_spec.userdata = NULL;
// Initialize audio
if (!SDL_WasInit(SDL_INIT_AUDIO))
{
fprintf(stderr, "ERROR: SDL not initialized: %s.\n", SDL_GetError());
return -1;
}
// Open the audio device, forcing the desired format
if (SDL_OpenAudio(&audio_spec, NULL) < 0)
{
fprintf(stderr, "WARNING: Couldn't open audio: %s\n", SDL_GetError());
return -1;
}
else
{
g_fAudioOpened = true;
return 0;
}
}
void SOUND_CloseAudio()
{
if (g_fAudioOpened)
{
SDL_CloseAudio();
g_fAudioOpened = false;
}
SDL_DestroyMutex(mtx);
mtx = NULL;
}
void *SOUND_LoadWAV(const char *filename)
{
SDL_AudioCVT *wavecvt;
SDL_AudioSpec wavespec, *loaded;
unsigned char *buf;
unsigned int len;
if (!g_fAudioOpened) {
return NULL;
}
wavecvt = (SDL_AudioCVT *)malloc(sizeof(SDL_AudioCVT));
if (wavecvt == NULL)
{
return NULL;
}
loaded = SDL_LoadWAV_RW(SDL_RWFromFile(filename, "rb"), 1, &wavespec, &buf, &len);
if (loaded == NULL)
{
free(wavecvt);
return NULL;
}
// Build the audio converter and create conversion buffers
if (SDL_BuildAudioCVT(wavecvt, wavespec.format, wavespec.channels, wavespec.freq,
audio_spec.format, audio_spec.channels, audio_spec.freq) < 0)
{
SDL_FreeWAV(buf);
free(wavecvt);
return NULL;
}
int samplesize = ((wavespec.format & 0xFF) / 8) * wavespec.channels;
wavecvt->len = len & ~(samplesize - 1);
wavecvt->buf = (unsigned char *)malloc(wavecvt->len * wavecvt->len_mult);
if (wavecvt->buf == NULL)
{
SDL_FreeWAV(buf);
free(wavecvt);
return NULL;
}
memcpy(wavecvt->buf, buf, len);
SDL_FreeWAV(buf);
// Run the audio converter
if (SDL_ConvertAudio(wavecvt) < 0)
{
free(wavecvt->buf);
free(wavecvt);
return NULL;
}
return wavecvt;
}
void SOUND_FreeWAV(void *audio)
{
if (audio == NULL)
{
return;
}
free(((SDL_AudioCVT *)audio)->buf);
free(audio);
}
void SOUND_PlayWAV(int channel, void *audio)
{
if (audio == NULL)
{
return;
}
SDL_mutexP(mtx);
if (channel == 0)
{
audio_pos = ((SDL_AudioCVT *)audio)->buf;
audio_len = ((SDL_AudioCVT *)audio)->len * ((SDL_AudioCVT *)audio)->len_mult;
}
else
{
audio_pos2 = ((SDL_AudioCVT *)audio)->buf;
audio_len2 = ((SDL_AudioCVT *)audio)->len * ((SDL_AudioCVT *)audio)->len_mult;
}
SDL_mutexV(mtx);
SDL_PauseAudio(0);
}