-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsound.h
240 lines (221 loc) · 4.66 KB
/
sound.h
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
bool SOUND = true;
#define NSONSM 26 //max number of sounds to load in memory
#define NSONS 32 //number of sounds that can be simultaneously played
typedef struct {
Uint8 *data;
Uint32 size;
}Sound;
Sound LoadSound (const char *file)
{
SDL_AudioSpec wav_spec;
Uint32 wav_length;
Uint8 *wav_buff;
SDL_AudioCVT wav_cvt;
SDL_AudioSpec des;
des.freq = 11050;
des.format = AUDIO_U8;
//des.samples = 256;
//des.callback = NULL;
des.channels = 1;
//des.userdata = NULL;
if(SDL_LoadWAV(file, &wav_spec, &wav_buff, &wav_length) == NULL)
{
fprintf(stderr, "Unable to open %s : %s\n", file, SDL_GetError());
exit(EXIT_FAILURE);
}
Sound s;
if (wav_spec.freq == 5525)
{
//conversion
Uint8 *data = (Uint8 *)malloc(sizeof(Uint8)*wav_length*2);
if (!data)
{
fprintf(stderr,"Out of memory !!!\n");
}
Uint32 i;
Uint8 *from = (Uint8 *)wav_buff;
Uint8 *dest = data;
Uint8 rf =0;
for (i=0;i<wav_length;i++)
{
*dest = (*from+2*rf)/3;
dest++;
*dest = (*from*2+rf)/3;
rf = *from;
dest++;
from++;
}
SDL_FreeWAV(wav_buff);
wav_buff = (Uint8 *)data;
wav_length *= 2;
}
/*
Uint8 *data = (Uint8 *)malloc(wav_length);
memcpy(data, wav_buff, wav_length);
// We can delete to original WAV data now
SDL_FreeWAV(wav_buff);
wav_buff = data;
*/
s.data = wav_buff; //wav_cvt.buf;
if (!s.data)
{
fprintf(stderr,"Error : No sound after conversion !!!\n");
}
s.size = wav_length; //wav_cvt.len_cvt;
//printf("Size of sound : avant %d, apres %d\n",wav_cvt.len,s.size);
//printf("Size of sound : %d\n",wav_length);
return s;
}
typedef struct
{
Uint8 *ptr; //la ou on en est (NULL si son pas lu)
Uint32 i; //sound index
}RefSound;
void mixaudio(void *unused, Uint8 *stream, int len);
struct Sounds
{
Sound s[NSONSM];
RefSound r[NSONS];
//
void Init (); //initializes everything (and load sounds)
void Load (); //load all sounds
void Load (const char *file, Uint16 i); //Load the i-th sound (needs to be .wav)
void init (); //initializes the structure
void Start (); //starts the sound system
void Stop (); //stops the sound system
void Play (Uint16 i); //plays the i-th sound
};
void Sounds::Init ()
{
init(); //
Start(); //
Load(); //
}
void Sounds::Load (const char *file, Uint16 i)
{
s[i] = LoadSound(file);
}
void Sounds::Load ()
{
int i;
char file[256];
char tamp[256];
MakeName("son00.wav", file, SOUNDS_DIRECTORY, 256);
for (i=1;i<NSONSM;i++)
{
file[strlen(SOUNDS_DIRECTORY)+3] = '0'+i/10;
file[strlen(SOUNDS_DIRECTORY)+4] = '0'+i%10;
MakeName(file, tamp, RESOURCES, 256);
Load(tamp, i);
}
}
void Sounds::init ()
{
int i;
for (i=0;i<NSONSM;i++)
{
s[i].data = NULL;
s[i].size = 0;
}
for (i=0;i<NSONS;i++)
{
r[i].ptr = NULL;
r[i].i = 0;
}
}
void Sounds::Start ()
{
init();
SDL_AudioSpec des,obt;
des.freq = 11050;
des.format = AUDIO_U8;
des.samples = 256;
des.callback = mixaudio;
des.channels = 1;
des.userdata = this;
if (SDL_OpenAudio(&des,&obt) < 0)
{
printf("Unable to open Audio : %s\n",SDL_GetError());
exit(EXIT_FAILURE);
}
if (obt.freq != des.freq)
{
printf("Frequency not reached !!!!\n");
}
/*
printf("Audio obtenu:\n");
printf("freq : %d\n",obt.freq);
printf("samples : %d\n",obt.samples);
printf("channels : %d\n",obt.channels);
printf("format : %d\n",obt.format);
printf("Liste des formats:\n");
printf("AUDIO_U8 = %d\n",AUDIO_U8);
printf("AUDIO_S8 = %d\n",AUDIO_S8);
printf("AUDIO_U16 = %d\n",AUDIO_U16);
printf("AUDIO_S16 = %d\n",AUDIO_S16);
printf("AUDIO_U16MSB = %d\n",AUDIO_U16MSB);
printf("AUDIO_S16MSB = %d\n",AUDIO_S16MSB);
printf("AUDIO_U16SYS = %d\n",AUDIO_U16SYS);
printf("AUDIO_S16SYS = %d\n",AUDIO_S16SYS);
*/
//lancement du son
SDL_PauseAudio(0);
}
void Sounds::Stop ()
{
SDL_CloseAudio();
}
void Sounds::Play (Uint16 i) //plays the i-th sound
{
#if HSON
if (!SOUND)
return;
#else
return;
#endif
if (i == 0)
return;
if (i >= NSONSM)
{
fprintf(stderr, "Sound file doesn't exist !!!\n");
return;
}
//looking for a free slot
///SDL_LockAudio();
int j;
for (j=0;j<NSONS;j++)
{
if (r[j].ptr == NULL)
{ //starts sounds
//printf("Launch of %d in %d ...\n",i,j);
r[j].ptr = s[i].data;
r[j].i = i;
break;
}
}
///SDL_UnlockAudio();
}
void mixaudio(void *userdata, Uint8 *stream, int len)
{
int i;
Uint32 amount;
Sounds *s = (Sounds *)userdata;
for (i=0;i<NSONS;i++)
{
if (s->r[i].ptr != NULL)
{
//printf("son %d...\n",i);
amount = s->s[s->r[i].i].size - (s->r[i].ptr - s->s[s->r[i].i].data); //amount of remaining to play
if ( amount > len )
{
amount = len;
}
SDL_MixAudio(stream, s->r[i].ptr, amount, SDL_MIX_MAXVOLUME);
s->r[i].ptr += amount;
if (amount < len)
{
s->r[i].ptr = NULL;
}
}
}
}