Debian Codegraph @

sound.cpp

raw symbols SCIP
1 /* ************************************************************************* *
2 SDL-Ball - DX-Ball/Breakout remake with openGL and SDL for Linux
3 Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * ************************************************************************* */
18
19 #include <vector>
20
21 struct sampleQueuedItem {
22 int s,p,num;
23 };
24
25 class soundClass {
26 private:
27 #ifndef NOSOUND
28 //The actual sound samples
29 Mix_Chunk *sample[SNDSAMPLES];
30 //List of queued samples
31 vector<struct sampleQueuedItem> q;
32 void loadSample(const char *SampleName, int sampleNum);
33
34 int m; //Secret undocumented variable
35
36 #endif
37
38 public:
39 bool init();
40 void play();
41 void add(int i, GLfloat x);
42 void loadsounds();
43 ~soundClass();
44 };
45
46 bool soundClass::init() {
47 #ifndef NOSOUND
48 m=0;
49 int audio_rate = 44100;
50 Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
51 int audio_channels = 2;
52 int audio_buffers = 1024;
53 if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
54 cout << "Error: Could not open audio device."<<endl<<"Sound have been disabled."<<endl;
55 setting.sound=0;
56 return(0);
57 }
58 Mix_AllocateChannels(MIX_CHANNELS);
59
60 if(setting.sound)
61 loadsounds();
62 #endif
63 return(1);
64 }
65
66 #ifndef NOSOUND
67 void soundClass::loadSample(const char *SampleName, int sampleNum)
68 {
69 string F="snd/";
70 F += SampleName;
71
72 sample[sampleNum] = Mix_LoadWAV(useTheme(F,setting.sndTheme).data());
73 if(!sample[sampleNum])
74 {
75 cout << "SoundManager '"<< F <<"' : " << Mix_GetError() << endl;
76 }
77 }
78 #endif
79
80
81 /* This function puts a sample in queue for playing */
82 void soundClass::add(int i, GLfloat x)
83 {
84 #ifndef NOSOUND
85 if(setting.sound==0)
86 return;
87
88 int p = 0;
89 if(setting.stereo)
90 {
91 p=(255.0/3.2)*(x+1.6);
92 }
93
94 struct sampleQueuedItem qt;
95 qt.s=i;
96 qt.p=p;
97 q.push_back( qt );
98
99 #endif
100 }
101
102 void soundClass::loadsounds()
103 {
104
105 #ifndef NOSOUND
106 loadSample("start.ogg", SND_START);
107 loadSample("ball-hit-border.ogg", SND_BALL_HIT_BORDER);
108 loadSample("ball-hit-paddle.ogg", SND_BALL_HIT_PADDLE);
109 loadSample("norm-brick-breaka.ogg", SND_NORM_BRICK_BREAK); //breaka
110
111 loadSample("norm-brick-breakb.ogg", SND_NORM_BRICK_BREAKB);
112 loadSample("norm-brick-breakc.ogg", SND_NORM_BRICK_BREAKC);
113 loadSample("norm-brick-breakd.ogg", SND_NORM_BRICK_BREAKD);
114 loadSample("norm-brick-breake.ogg", SND_NORM_BRICK_BREAKE);
115
116
117 loadSample("expl-brick-break.ogg", SND_EXPL_BRICK_BREAK);
118 loadSample("glass-brick-hit.ogg", SND_GLASS_BRICK_HIT);
119 loadSample("glass-brick-break.ogg", SND_GLASS_BRICK_BREAK);
120 loadSample("invisible-brick-appear.ogg", SND_INVISIBLE_BRICK_APPEAR);
121 loadSample("cement-brick-hit.ogg", SND_CEMENT_BRICK_HIT);
122 loadSample("doom-brick-break.ogg", SND_DOOM_BRICK_BREAK);
123 loadSample("po-hit-border.ogg", SND_PO_HIT_BORDER);
124 loadSample("good-po-hit-paddle.ogg", SND_GOOD_PO_HIT_PADDLE);
125 loadSample("evil-po-hit-paddle.ogg", SND_EVIL_PO_HIT_PADDLE);
126 loadSample("shot.ogg", SND_SHOT);
127 loadSample("die.ogg", SND_DIE);
128 loadSample("nextlevel.ogg", SND_NEXTLEVEL);
129 loadSample("gameover.ogg", SND_GAMEOVER);
130 loadSample("highscore.ogg", SND_HIGHSCORE);
131 loadSample("menuclick.ogg", SND_MENUCLICK);
132 loadSample("glue-ball-hit-paddle.ogg", SND_GLUE_BALL_HIT_PADDLE);
133 loadSample("buy-powerup.ogg", SND_BUY_POWERUP);
134 #endif
135 }
136
137 /* This function is called only when drawing a frame, and plays the queue of samples,
138 It will average the x/stereo position of a sample if it is queued more than once */
139 void soundClass::play()
140 {
141 #ifndef NOSOUND
142 if(setting.sound==0)
143 return;
144
145 //Playlist (lol, imagination for the win..)
146 vector<struct sampleQueuedItem> pl;
147 vector<struct sampleQueuedItem>::iterator plIt;
148 bool same=0;
149 int freeChannel = -1; //The channel we will use for this sample
150
151 //Loop through queue and find samples thare are the same, average their position and put in a new vector
152 for(vector<struct sampleQueuedItem>::iterator it = q.begin(); it != q.end(); ++it)
153 {
154 //Loop thrugh the playlist to see find out if this allready exist
155 same=0;
156 for(plIt=pl.begin(); plIt != pl.end(); ++plIt)
157 {
158 if(plIt->s == it->s)
159 {
160 same=1;
161 plIt->num++;
162 plIt->p += it->p;
163 }
164 }
165
166 //this sample is not yet in the playlist
167 if(!same)
168 {
169 pl.push_back( *it );
170 plIt = pl.end();
171 --plIt;
172 plIt->num=1;
173 }
174 }
175 q.clear();
176
177 //Play the actual samples :)
178 for(plIt = pl.begin(); plIt != pl.end(); ++plIt)
179 {
180 //Find a free channel:
181 for(int i=0; i < MIX_CHANNELS; i++)
182 {
183 if(!Mix_Playing(i))
184 {
185 freeChannel=i;
186 break;
187 }
188 }
189 if(setting.stereo)
190 {
191 plIt->p /=plIt->num;
192 Mix_SetPanning(freeChannel, 255-plIt->p, plIt->p);
193 }
194
195 if(plIt->s == SND_NORM_BRICK_BREAK)
196 {
197 switch(m)
198 {
199 //Case 0 = SND_NORM_BREAK(a)
200 case 1:
201 plIt->s = SND_NORM_BRICK_BREAKB;
202 break;
203 case 2:
204 plIt->s = SND_NORM_BRICK_BREAKC;
205 break;
206 case 3:
207 plIt->s = SND_NORM_BRICK_BREAKD;
208 break;
209 case 4:
210 plIt->s = SND_NORM_BRICK_BREAKE;
211 break;
212 }
213
214 m++;
215 if(m==5)
216 m=0;
217
218 }
219
220 if(Mix_PlayChannel(freeChannel, sample[plIt->s], 0) == -1)
221 {
222 printf("Sample %i: %s\n",plIt->s, Mix_GetError());
223 }
224 }
225 #endif
226 }
227
228 soundClass::~soundClass()
229 {
230 #ifndef NOSOUND
231 for(int i=0; i < SNDSAMPLES; i++)
232 {
233 Mix_FreeChunk(sample[i]);
234 }
235 Mix_CloseAudio();
236 #endif
237 }