-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsdl_audio.cpp
More file actions
95 lines (66 loc) · 1.37 KB
/
sdl_audio.cpp
File metadata and controls
95 lines (66 loc) · 1.37 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
#include <bits/stdc++.h>
#include <SDL2/SDL.h>
using namespace std;
struct AudioData
{
Uint8 *idx;
Uint32 len;
};
void callbackfun(void *data, Uint8 *buffer, int buffer_len)
{
AudioData *audio = (AudioData*)data;
if( audio->len == 0)
{
return;
}
Uint32 length = (Uint32)buffer_len;
length = (length > audio->len ? audio->len : length);
SDL_memcpy(buffer, audio->idx, length);
audio->idx += length;
audio->len -= length;
}
int main()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_AudioSpec aud_wav_spec;
Uint8 *wav_start;
Uint32 wav_len;
if(SDL_LoadWAV("sample_30_sec.wav", &aud_wav_spec, &wav_start, &wav_len) == NULL)
{
cout << "error 1" << endl;
return 1;
}
else
{
cout << "file loaded" << endl;
}
AudioData audio;
audio.idx = wav_start;
audio.len = wav_len;
cout << "calling callback function" << endl;
aud_wav_spec.callback = callbackfun;
aud_wav_spec.userdata = &audio;
SDL_AudioDeviceID device = SDL_OpenAudioDevice(NULL,0, &aud_wav_spec, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
if(device == 0)
{
cout << "error 2" << endl;
return 1;
}
else
{
cout << "opening audio was success" << endl;
}
SDL_PauseAudioDevice(device,0);
cout << ":::playing audio:::" << endl;
while(audio.len > 0)
{
SDL_Delay(100);
//cout << "Looping \n";
}
SDL_CloseAudioDevice(device);
cout << "closing the audio device" << endl;
SDL_FreeWAV(wav_start);
cout << "free wav" << endl;
SDL_Quit();
return 0;
}