-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
417 lines (323 loc) · 10.8 KB
/
test.c
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
#include <libavutil/avstring.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <time.h>
#include <assert.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_thread.h>
/* SDL_AudioFormat of files, such as s16 little endian */
#define AUDIO_FORMAT AUDIO_S16LSB
/* Frequency of the file */
#define AUDIO_FREQUENCY 48000
/* 1 mono, 2 stereo, 4 quad, 6 (5.1) */
#define AUDIO_CHANNELS 2
/* Specifies a unit of audio data to be used at a time. Must be a power of 2 */
#define AUDIO_SAMPLES 4096
/* Max number of sounds that can be in the audio queue at anytime, stops too much mixing */
#define AUDIO_MAX_SOUNDS 25
void intHandler(int dummy) {
exit(1);
}
#define MUS_PATH "aaa.wav"
// prototype for our audio callback
// see the implementation for more information
void my_audio_callback(void *userdata, Uint8 *stream, int len);
// variable declarations
static Uint8 *audio_pos; // global pointer to the audio buffer to be played
static Uint32 audio_len; // remaining length of the sample we have to play
int main(int argc, char* argv[]){
// Initialize SDL.
if (SDL_Init(SDL_INIT_AUDIO) < 0)
return 1;
// local variables
static Uint32 wav_length; // length of our sample
static Uint8 *wav_buffer; // buffer containing our audio file
static SDL_AudioSpec wav_spec; // the specs of our piece of music
// the specs, length and buffer of our wav are filled
if( SDL_LoadWAV(MUS_PATH, &wav_spec, &wav_buffer, &wav_length) == NULL ){
return 1;
}
// set the callback function
wav_spec.callback = my_audio_callback;
wav_spec.userdata = NULL;
wav_spec.silence = 0;
// set our global static variables
audio_pos = wav_buffer; // copy sound buffer
audio_len = wav_length; // copy file length
const char* d = SDL_GetAudioDeviceName(0, 1);
printf("Capture: %s", d);
const char* e = SDL_GetAudioDeviceName(0, 0);
printf("Device: %s", e);
// Returns a valid device ID that is > 0 on success or 0 on failure
//int audioDeviceId = SDL_OpenAudioDevice(d, 1, &wanted_spec, &spec, 0);
int audioDeviceId = SDL_OpenAudioDevice(NULL, 0, &wav_spec, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
printf("[stream_component_open] Device id: %d.", audioDeviceId);
if (audioDeviceId == 0) {
printf("[stream_component_open] Failed to open audio: %s.", SDL_GetError());
return -1;
} else {
// Start audio playing
SDL_PauseAudioDevice(audioDeviceId, 0);
}
/* Open the audio device */
/*if ( SDL_OpenAudio(&wav_spec, NULL) < 0 ){
fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
exit(-1);
}*/
SDL_PauseAudio(0);
// wait until we're don't playing
while ( audio_len > 0 ) {
SDL_Delay(100);
}
// shut everything down
SDL_CloseAudio();
SDL_FreeWAV(wav_buffer);
}
// audio callback function
// here you have to copy the data of your audio buffer into the
// requesting audio buffer (stream)
// you should only copy as much as the requested length (len)
void my_audio_callback(void *userdata, Uint8 *stream, int len) {
SDL_memset(stream, 0, len);
printf("Play...\n");
if (audio_len ==0)
return;
len = ( len > audio_len ? audio_len : len );
//SDL_memcpy (stream, audio_pos, len); // simply copy from one buffer into the other
SDL_MixAudio(stream, audio_pos, len, SDL_MIX_MAXVOLUME);// mix from one buffer into another
//SDL_MixAudioFormat(stream, audio_pos, AUDIO_FORMAT, len, SDL_MIX_MAXVOLUME);
printf("Pos... %d\n", audio_pos);
audio_pos += len;
audio_len -= len;
}
/*
* Native WAVE format
*
* On some GNU/Linux you can identify a files properties using:
* mplayer -identify music.wav
*
* On some GNU/Linux to convert any music to this or another specified format use:
* ffmpeg -i in.mp3 -acodec pcm_s16le -ac 2 -ar 48000 out.wav
*/
/* Flags OR'd together, which specify how SDL should behave when a device cannot offer a specific feature
* If flag is set, SDL will change the format in the actual audio file structure (as opposed to gDevice->want)
*
* Note: If you're having issues with Emscripten / EMCC play around with these flags
*
* 0 Allow no changes
* SDL_AUDIO_ALLOW_FREQUENCY_CHANGE Allow frequency changes (e.g. AUDIO_FREQUENCY is 48k, but allow files to play at 44.1k
* SDL_AUDIO_ALLOW_FORMAT_CHANGE Allow Format change (e.g. AUDIO_FORMAT may be S32LSB, but allow wave files of S16LSB to play)
* SDL_AUDIO_ALLOW_CHANNELS_CHANGE Allow any number of channels (e.g. AUDIO_CHANNELS being 2, allow actual 1)
* SDL_AUDIO_ALLOW_ANY_CHANGE Allow all changes above
*/
#define SDL_AUDIO_ALLOW_CHANGES SDL_AUDIO_ALLOW_ANY_CHANGE
typedef struct sound
{
uint32_t length;
uint32_t lengthTrue;
uint8_t * bufferTrue;
uint8_t * buffer;
uint8_t loop;
uint8_t fade;
uint8_t free;
uint8_t volume;
SDL_AudioSpec audio;
struct sound * next;
} Audio;
/*
* Definition for the game global sound device
*
*/
typedef struct privateAudioDevice
{
SDL_AudioDeviceID device;
SDL_AudioSpec want;
uint8_t audioEnabled;
} PrivateAudioDevice;
/* File scope variables to persist data */
static PrivateAudioDevice * gDevice;
static uint32_t gSoundCount;
/*
* Add a music to the queue, addAudio wrapper for music due to fade
*
* @param new New Audio to add
*
*/
static void addMusic(Audio * root, Audio * new);
/*
* Wrapper function for playMusic, playSound, playMusicFromMemory, playSoundFromMemory
*
* @param filename Provide a filename to load WAV from, or NULL if using FromMemory
* @param audio Provide an Audio object if copying from memory, or NULL if using a filename
* @param sound 1 if looping (music), 0 otherwise (sound)
* @param volume See playSound for explanation
*
*/
static inline void playAudio(const char * filename, Audio * audio, uint8_t loop, int volume);
/*
* Add a sound to the end of the queue
*
* @param root Root of queue
* @param new New Audio to add
*
*/
static void addAudio(Audio * root, Audio * new);
/*
* Audio callback function for OpenAudioDevice
*
* @param userdata Points to linked list of sounds to play, first being a placeholder
* @param stream Stream to mix sound into
* @param len Length of sound to play
*
*/
void unpauseAudio()
{
if(gDevice->audioEnabled)
{
SDL_PauseAudioDevice(gDevice->device, 0);
}
}
static inline void audioCallback(void * userdata, uint8_t * stream, int len);
void initAudio(void)
{
Audio * global;
gDevice = calloc(1, sizeof(PrivateAudioDevice));
gSoundCount = 0;
if(gDevice == NULL)
{
fprintf(stderr, "[%s: %d]Fatal Error: Memory c-allocation error\n", __FILE__, __LINE__);
return;
}
gDevice->audioEnabled = 0;
if(!(SDL_WasInit(SDL_INIT_AUDIO) & SDL_INIT_AUDIO))
{
fprintf(stderr, "[%s: %d]Error: SDL_INIT_AUDIO not initialized\n", __FILE__, __LINE__);
return;
}
SDL_memset(&(gDevice->want), 0, sizeof(gDevice->want));
(gDevice->want).freq = AUDIO_FREQUENCY;
(gDevice->want).format = AUDIO_FORMAT;
(gDevice->want).channels = AUDIO_CHANNELS;
(gDevice->want).samples = AUDIO_SAMPLES;
(gDevice->want).callback = audioCallback;
(gDevice->want).userdata = calloc(1, sizeof(Audio));
global = (gDevice->want).userdata;
if(global == NULL)
{
fprintf(stderr, "[%s: %d]Error: Memory allocation error\n", __FILE__, __LINE__);
return;
}
global->buffer = NULL;
global->next = NULL;
/* want.userdata = new; */
if((gDevice->device = SDL_OpenAudioDevice(NULL, 0, &(gDevice->want), NULL, SDL_AUDIO_ALLOW_CHANGES)) == 0)
{
fprintf(stderr, "[%s: %d]Warning: failed to open audio device: %s\n", __FILE__, __LINE__, SDL_GetError());
}
else
{
/* Set audio device enabled global flag */
gDevice->audioEnabled = 1;
/* Unpause active audio stream */
unpauseAudio();
}
}
Audio * createAudio(const char * filename, uint8_t loop, int volume)
{
Audio * new = calloc(1, sizeof(Audio));
if(new == NULL)
{
fprintf(stderr, "[%s: %d]Error: Memory allocation error\n", __FILE__, __LINE__);
return NULL;
}
if(filename == NULL)
{
fprintf(stderr, "[%s: %d]Warning: filename NULL: %s\n", __FILE__, __LINE__, filename);
return NULL;
}
new->next = NULL;
new->loop = loop;
new->fade = 0;
new->free = 1;
new->volume = volume;
if(SDL_LoadWAV(filename, &(new->audio), &(new->bufferTrue), &(new->lengthTrue)) == NULL)
{
fprintf(stderr, "[%s: %d]Warning: failed to open wave file: %s error: %s\n", __FILE__, __LINE__, filename, SDL_GetError());
free(new);
return NULL;
}
new->buffer = new->bufferTrue;
new->length = new->lengthTrue;
(new->audio).callback = NULL;
(new->audio).userdata = NULL;
return new;
}
static inline void playAudio(const char * filename, Audio * audio, uint8_t loop, int volume)
{
Audio * new;
/* Load from filename or from Memory */
if(filename != NULL)
{
/* Create new music sound with loop */
new = createAudio(filename, loop, volume);
}
((Audio *) (gDevice->want).userdata)->next = new;
}
static inline void audioCallback(void * userdata, uint8_t * stream, int len)
{
printf("Callback\n");
Audio * audio = (Audio *) userdata;
Audio * previous = audio;
int tempLength;
uint8_t music = 0;
/* Silence the main buffer */
SDL_memset(stream, 0, len);
/* First one is place holder */
audio = audio->next;
while(audio != NULL)
{
if(audio->length > 0)
{
if(music && audio->loop == 1 && audio->fade == 0)
{
tempLength = 0;
}
else
{
tempLength = ((uint32_t) len > audio->length) ? audio->length : (uint32_t) len;
}
SDL_MixAudioFormat(stream, audio->buffer, AUDIO_FORMAT, tempLength, audio->volume);
audio->buffer += tempLength;
audio->length -= tempLength;
previous = audio;
audio = audio->next;
}
}
}
static void addAudio(Audio * root, Audio * new)
{
root->next = new;
}
/*void main() {
if(SDL_Init(SDL_INIT_AUDIO) < 0)
{
return;
}
initAudio();
playAudio("aaa.wav", NULL, 1, SDL_MIX_MAXVOLUME);
SDL_Delay(3000);
}*/