Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
MusicTrack.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: MusicTrack.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Music Director class to manage selection, setup, and playback
13  of background music tracks for both menu and game modes
14 */
15 
16 
17 #include "MemDebug.h"
18 #include "MusicTrack.h"
19 #include "MusicDirector.h"
20 #include "Starshatter.h"
21 #include "AudioConfig.h"
22 
23 #include "Game.h"
24 #include "Sound.h"
25 
26 // +-------------------------------------------------------------------+
27 
28 const double FADE_TIME = 1.5;
29 const double SILENCE = -5000;
30 
31 // +-------------------------------------------------------------------+
32 
33 MusicTrack::MusicTrack(const Text& txt, int m, int n)
34 : name(txt), sound(0), state(NONE), mode(m), index(n),
35 fade(0), fade_time(FADE_TIME)
36 {
37  long max_vol = 0;
38 
40  max_vol = AudioConfig::GameMusic();
41  else
42  max_vol = AudioConfig::MenuMusic();
43 
44  if (max_vol <= AudioConfig::Silence())
45  return;
46 
47  name.setSensitive(false);
48 
49  if (name.contains(".ogg")) {
51 
52  if (name.contains("-loop")) {
55  Sound::LOOP |
57  }
58 
59  else {
63  }
64 
65  sound->SetVolume((long) SILENCE);
66  }
67 }
68 
70 {
71  if (sound) {
72  sound->Stop();
73  sound->Release();
74  }
75 }
76 
77 // +--------------------------------------------------------------------+
78 
79 void
81 {
82  bool music_pause = false;
83 
85  if (stars) {
86  music_pause = (stars->GetGameMode() == Starshatter::PLAY_MODE) &&
87  Game::Paused();
88  }
89 
90  if (sound && !music_pause) {
91  double fvol = 1;
92  long volume = 0;
93 
94  switch (state) {
95  case PLAY:
96  if (sound->IsReady())
97  sound->Play();
98  SetVolume(volume);
99  break;
100 
101  case FADE_IN:
102  if (sound->IsReady())
103  sound->Play();
104 
105  if (fade > 0) {
106  fvol = fade/fade_time;
107  volume = (long) (fvol * SILENCE);
108  SetVolume(volume);
109  }
110 
111  if (fade < 0.01)
112  state = PLAY;
113  break;
114 
115  case FADE_OUT:
116  if (sound->IsReady())
117  sound->Play();
118 
119  if (fade > 0) {
120  fvol = 1 - fade/fade_time;
121  volume = (long) (fvol * SILENCE);
122  SetVolume(volume);
123  }
124 
125  if (fade < 0.01)
126  state = STOP;
127  break;
128 
129  case STOP:
130  if (sound->IsPlaying()) {
131  sound->Stop();
132  sound->Release();
133  sound = 0;
134  }
135  break;
136  }
137 
138  if (fade > 0)
139  fade -= Game::GUITime();
140 
141  if (fade < 0)
142  fade = 0;
143  }
144 }
145 
146 // +--------------------------------------------------------------------+
147 
148 void
150 {
151  state = PLAY;
152  fade = 0;
153 }
154 
155 void
157 {
158  state = STOP;
159  fade = 0;
160 }
161 
162 void
164 {
165  if (state != FADE_IN && state != PLAY) {
166  state = FADE_IN;
167  fade = fade_time;
168  }
169 }
170 
171 void
173 {
174  if (state != FADE_OUT && state != STOP) {
175  state = FADE_OUT;
176  fade = fade_time;
177  }
178 }
179 
180 // +--------------------------------------------------------------------+
181 
182 int
184 {
185  if (sound)
186  return sound->IsReady();
187 
188  return false;
189 }
190 
191 int
193 {
194  if (sound)
195  return sound->IsPlaying();
196 
197  return false;
198 }
199 
200 int
202 {
203  if (sound)
204  return sound->IsDone() || sound->LoopCount() >= 5;
205 
206  return true;
207 }
208 
209 int
211 {
212  if (sound)
213  return sound->IsDone() || sound->LoopCount() >= 4;
214 
215  return true;
216 }
217 
218 // +--------------------------------------------------------------------+
219 
220 long
222 {
223  if (sound)
224  return sound->GetVolume();
225 
226  return 0;
227 }
228 
229 void
231 {
232  if (sound) {
233  long max_vol = 0;
234 
236  max_vol = AudioConfig::GameMusic();
237  else
238  max_vol = AudioConfig::MenuMusic();
239 
240  if (v > max_vol)
241  v = max_vol;
242 
243  sound->SetVolume(v);
244  }
245 }
246 
247 double
249 {
250  if (sound)
251  return sound->GetTotalTime();
252 
253  return 0;
254 }
255 
256 double
258 {
259  if (sound)
260  return sound->GetTimeRemaining();
261 
262  return 0;
263 }
264 
265 double
267 {
268  if (sound)
269  return sound->GetTimeElapsed();
270 
271  return 0;
272 }
273