-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsound.cpp
More file actions
144 lines (123 loc) · 4.17 KB
/
Copy pathsound.cpp
File metadata and controls
144 lines (123 loc) · 4.17 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
/*
* sound.c
* Allenroids
*
* Provides sound playback via SDL.
*
*/
#include "sound.h"
#include "Allenroids.h"
boolean audioOn;
SoundSample channels[NUMBER_CHANNELS];
char *soundName[]=
{ //soundBuffer Index:
"Sounds/Pop.wav", // 0
"Sounds/PopAlien.wav", // 1
"Sounds/Shields.wav", // 2
"Sounds/Thrust.wav", // 3
"Sounds/StopThrusting.wav", // 4
"Sounds/ExplosionSmall.wav", // 5
"Sounds/ExplosionMedium.wav", // 6
"Sounds/ExplosionLarge.wav", // 7
"Sounds/ExplosionHuge.wav", // 8
"Sounds/WarningPlayer1.wav", // 9
"Sounds/WarningPlayer2.wav", // 10
"Sounds/ReleaseCrystal.wav", // 11
"Sounds/RetrieveCrystal.wav", // 12
"Sounds/CrystalSplatter.wav", // 13
"Sounds/WarningAlien.wav", // 14
"Sounds/ShotTunk.wav" // 15
};
SDL_AudioCVT soundBuffer[NUMBER_SOUNDS];
/****************************************************
* soundInit
* set up SDL sound
***************************************************/
void soundInit(){
SDL_AudioSpec soundFormat;
/* Set 16-bit stereo audio at 22Khz */
soundFormat.freq = 22050;
// soundFormat.freq = 11127;
soundFormat.format = AUDIO_S16;
soundFormat.channels = 2; // 1 = mono, 2 = stereo
soundFormat.samples = 1024; // A good value for games
soundFormat.callback = mixaudio; //a function!
soundFormat.userdata = NULL;
if( SDL_OpenAudio(&soundFormat, NULL) < 0) {
//printf("Warning: %s\n", SDL_GetError());
audioOn = false;
return;
}
else
audioOn = true;
loadSounds(soundName, soundBuffer, NUMBER_SOUNDS);
SDL_PauseAudio(false); //activate sound
//load all the sounds here
}
/****************************************************
* loadSounds
* copies the specified sound names to memory
***************************************************/
void loadSounds(char *nameBuffer[], SDL_AudioCVT soundBuffer[], unsigned int numberSounds){
unsigned int counter;
SDL_AudioSpec wave;
Uint8 *data;
Uint32 dataLength;
for(counter = 0; counter < numberSounds; counter++){
// Load the sound file and convert it to 16-bit stereo at 22kHz
if ( SDL_LoadWAV(nameBuffer[counter], &wave, &data, &dataLength) == NULL ) {
//fprintf(stderr, "Couldn't load %s: %s\n", nameBuffer[counter], SDL_GetError());
return;
}
SDL_BuildAudioCVT(&soundBuffer[counter],
wave.format, wave.channels, wave.freq,
AUDIO_S16, 2, 22050);
soundBuffer[counter].buf = (Uint8 *) malloc(dataLength * soundBuffer[counter].len_mult);
memcpy(soundBuffer[counter].buf, data, dataLength);
soundBuffer[counter].len = dataLength;
SDL_ConvertAudio(&soundBuffer[counter]);
SDL_FreeWAV(data);
}//end for
}
/****************************************************
* mixAudio
* plays all the sounds on the docket at once.
***************************************************/
void mixaudio(void *unused, Uint8 *stream, int length)
{
unsigned int counter;
//Uint32 amount; //as suggested by tutorial code; creates signed/unsigned complaints
int amount;
for ( counter = 0; counter < NUMBER_CHANNELS; counter++ ){
amount = (channels[counter].dataLength - channels[counter].dataPosition);
if ( amount > length ){
amount = length;
}
SDL_MixAudio(stream, &channels[counter].data[channels[counter].dataPosition], amount, SDL_MIX_MAXVOLUME);
channels[counter].dataPosition += amount;
}
}
/****************************************************
* playSound
* run a sound
* adapted from http://www.libsdl.org/intro/usingsound.html
***************************************************/
void playSound(SDL_AudioCVT soundList[], int idToPlay){
int index;
// Look for an empty (or finished) sound slot
for ( index = 0; index < NUMBER_CHANNELS; ++index ) {
if ( channels[index].dataPosition == channels[index].dataLength ) {
//a finished sound has been found (position = length)
break;
}
}
if ( index == NUMBER_CHANNELS )
return; //no slot found
// Put the sound data in the slot (it starts playing immediately)
SDL_LockAudio();
channels[index].data = soundList[idToPlay].buf;
channels[index].dataLength = soundList[idToPlay].len_cvt;
channels[index].dataPosition = 0;
channels[index].soundID = idToPlay;
SDL_UnlockAudio();
}