Debian Codegraph @

music/streamer.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/music/streamer.cpp $
21 * $Revision: 10 $
22 * $Date: 4/01/99 4:50p $
23 * $Author: Matt $
24 *
25 * Stream interface with sequencer.
26 *
27 * $Log: /DescentIII/Main/music/streamer.cpp $
28 *
29 * 10 4/01/99 4:50p Matt
30 * Took out Warning() function, chaning those calls to mprintf()
31 *
32 * 9 2/26/99 5:26p Samir
33 * fixes to streaming audio to reflect fix in direct sound mixer.
34 *
35 * 8 12/10/98 10:12a Samir
36 * uses newer streaming audio library
37 *
38 * 7 12/07/98 11:45a Samir
39 * hacks to get music system mostly working in DS_8. Will change this
40 * system so it's a lot cleaner after music demo.
41 *
42 * 6 12/03/98 12:48p Samir
43 * speed up sequencer so that gap between samples isn't too big.
44 *
45 * 5 11/20/98 5:21p Samir
46 * added SEND_STRM_NEXT
47 *
48 * 4 11/13/98 2:27p Samir
49 * new music system.
50 *
51 * 3 8/10/98 5:53p Samir
52 * improved switching between streams and basic background/combat music.
53 *
54 * 2 7/28/98 5:43p Samir
55 * reorg of music system.
56 *
57 * 1 7/28/98 12:47p Samir
58 * moved and revamped from sequencer.cpp
59 *
60 * $NoKeywords: $
61 */
62
63 #include "music.h"
64 #include "streamaudio.h"
65
66 oms_stream::oms_stream() {}
67
68 oms_stream::~oms_stream() {}
69
70 // processes a stream's events.
71 void oms_stream::Process(float frmtime) {
72 oms_q_evt evt, evt2 = {}; // event for use
73
74 if (m_timer > 0.0f)
75 m_timer -= frmtime;
76
77 // invalidate current process result.
78 m_valid_result = false;
79
80 if (!processCommand())
81 return;
82
83 // process queue events.
84 if (!m_q.recv(&evt))
85 return;
86
87 switch (evt.cmd) {
88 case OMS_STRM_LOAD:
89 processQLoad((const char *)evt.parm.p);
90 break;
91
92 case OMS_STRM_FADEOUT:
93 // reset timer for fadeout to 0 volume
94 STREAM_COMMANDI(OMS_STRM_FADEOUT);
95 STREAM_TIMER(evt.parm.f);
96 break;
97
98 case OMS_STRM_FADEIN:
99 // fade in to volume m_maxvol
100 STREAM_COMMANDI(OMS_STRM_FADEIN);
101 STREAM_TIMER(evt.parm.f);
102 break;
103
104 case OMS_STRM_STOP:
105 STREAM_COMMANDI(OMS_STRM_STOP);
106 m_stream.Stop();
107 break;
108
109 case OMS_STRM_FREE:
110 STREAM_COMMANDI(OMS_STRM_FREE);
111 m_stream.Close();
112 break;
113
114 case OMS_STRM_PLAY:
115 // process queue events.
116 if (!m_q.recv(&evt2)) {
117 Int3();
118 }
119 STREAM_COMMANDI(OMS_STRM_PLAY, evt2.parm.i);
120 m_stream.SetLoopCount(evt2.parm.i);
121 m_stream.SetVolume(evt.parm.f);
122 m_stream.Play();
123 break;
124
125 case OMS_STRM_SWITCH:
126 STREAM_COMMANDP(OMS_STRM_SWITCH, evt.parm.p);
127 m_stream.Stop(true, &m_data.i);
128 mprintf(0, "%d-%d ", m_data.i, m_stream.State());
129 break;
130
131 case OMS_STRM_NEXT:
132 // m_stream.Next((const char *)evt.parm.p);
133 if (!m_q.recv(&evt2)) {
134 Int3();
135 }
136 m_data.f = evt2.parm.f;
137 if (!m_q.recv(&evt2)) {
138 Int3();
139 }
140 m_data2.i = evt2.parm.i;
141 if (!m_q.recv(&evt2)) {
142 Int3();
143 }
144 STREAM_COMMANDP(OMS_STRM_NEXT, evt2.parm.p);
145 *(bool *)(evt2.parm.p) = true;
146 break;
147 }
148 }
149
150 // sends an event to the stream.
151 void oms_stream::Send(oms_q_evt *evt) { m_q.send(*evt); }
152
153 // reset stream.
154 void oms_stream::Reset(OutrageMusicSeq *seq) {
155 m_timer = 0.0f;
156 m_timer_init = 0.0f;
157 m_maxvol = 1.0f;
158 m_status.cmd = OMS_STRM_FREE;
159 m_seq = seq;
160 m_q.flush();
161 m_stream.Close();
162 }
163
164 // volume.
165 void oms_stream::SetVolume(float vol) {
166 m_maxvol = vol;
167 m_stream.SetVolume(vol);
168 }
169
170 // processes current command on stream, return false to end processing.
171 bool oms_stream::processCommand() {
172 switch (m_status.cmd) {
173 case OMS_STRM_FADEIN:
174 // timed fadein to dest volume
175 if (m_timer <= 0.0f) {
176 m_stream.SetVolume(m_maxvol);
177 STREAM_COMMANDI(OMS_STRM_PLAY);
178 } else {
179 m_stream.SetVolume(m_maxvol * (m_timer_init - m_timer) / m_timer_init);
180 }
181 return false;
182
183 case OMS_STRM_FADEOUT:
184 // timed fadeout to dest volume
185 if (m_timer <= 0.0f) {
186 m_stream.SetVolume(0.0f);
187 STREAM_COMMANDI(OMS_STRM_PLAY);
188 } else {
189 m_stream.SetVolume(m_maxvol * m_timer / m_timer_init);
190 }
191 return false;
192
193 case OMS_STRM_SWITCH:
194 // if current measure is greater than initial measure at switch, then we will stop
195 // this stream and send a play command to the current dominant stream.
196 // mprintf(0, "%d-%d ",m_data.i, m_stream.State());
197 if (m_data.i || m_stream.State() == STRM_STOPPED || m_stream.State() == STRM_INVALID) {
198 bool *still_playing = (bool *)m_status.parm.p;
199 *still_playing = false;
200 if (m_status.cmd != OMS_STRM_STOP) {
201 STREAM_COMMANDI(OMS_STRM_STOP);
202 m_valid_result = true;
203 m_result.cmd = OMS_STRM_STOP;
204 }
205 }
206 return false;
207
208 case OMS_STRM_PLAY:
209 // check if stream is still playing. if not, then set current stream status and signal
210 // a normal song end.
211 if (m_stream.State() == STRM_STOPPED) {
212 if (m_status.cmd != OMS_STRM_STOP) {
213 STREAM_COMMANDI(OMS_STRM_STOP);
214 m_valid_result = true;
215 m_result.cmd = OMS_STRM_STOP;
216 return false;
217 }
218 }
219 break;
220
221 case OMS_STRM_NEXT:
222 // if (m_stream.HasNextStarted()) {
223 // *(bool *)(m_status.parm.p) = false;
224 // m_stream.SetLoopCount(m_data2.i);
225 // m_stream.SetVolume(m_data.f);
226 // STREAM_COMMANDI(OMS_STRM_PLAY, m_data2.i);
227 // return false;
228 // }
229 // else {
230 // *(bool *)(m_status.parm.p) = true;
231 // }
232 break;
233 }
234
235 return true;
236 }
237
238 // processes load song q event.
239 void oms_stream::processQLoad(const char *fname) {
240 m_stream.Close();
241
242 if (m_stream.Open(fname)) {
243 STREAM_COMMANDP(OMS_STRM_LOAD, (void *)fname);
244 m_data.p = (void *)fname;
245 } else {
246 mprintf(0, "OMS: Couldn't load song %s.\n", fname);
247
248 STREAM_COMMANDI(OMS_STRM_FREE);
249 m_valid_result = true;
250 m_result.cmd = OMS_EVT_SONGENDED;
251 m_result.parm.p = (void *)fname;
252 }
253 }
254
255 // THESE FUNCTIONS SEND EVENTS TO THE STREAM EASILY
256
257 void oms_stream::SEND_STRM_LOAD(const char *fname) {
258 oms_q_evt evt;
259
260 evt.cmd = OMS_STRM_LOAD;
261 evt.parm.p = (void *)fname;
262 m_q.send(evt);
263 }
264
265 void oms_stream::SEND_STRM_FADEOUT(float time) {
266 oms_q_evt evt;
267
268 evt.cmd = OMS_STRM_FADEOUT;
269 evt.parm.f = time;
270 m_q.send(evt);
271 }
272
273 void oms_stream::SEND_STRM_FADEIN(float time) {
274 oms_q_evt evt;
275
276 evt.cmd = OMS_STRM_FADEIN;
277 evt.parm.f = time;
278 m_q.send(evt);
279 }
280
281 void oms_stream::SEND_STRM_STOP() {
282 oms_q_evt evt;
283
284 evt.cmd = OMS_STRM_STOP;
285 m_q.send(evt);
286 }
287
288 void oms_stream::SEND_STRM_PLAY(float vol, uint8_t count) {
289 oms_q_evt evt;
290
291 evt.cmd = OMS_STRM_PLAY;
292 evt.parm.f = vol;
293 m_q.send(evt);
294 evt.cmd = OMS_STRM_PARM;
295 evt.parm.i = (int)count;
296 m_q.send(evt);
297 }
298
299 void oms_stream::SEND_STRM_FREE() {
300 oms_q_evt evt;
301
302 evt.cmd = OMS_STRM_FREE;
303 m_q.send(evt);
304 }
305
306 void oms_stream::SEND_STRM_SWITCH(bool *switch_flag) {
307 oms_q_evt evt;
308 evt.cmd = OMS_STRM_SWITCH;
309 evt.parm.p = (void *)switch_flag;
310 m_q.send(evt);
311 }
312
313 void oms_stream::SEND_STRM_NEXT(const char *fname, float vol, uint8_t count, bool *switch_flag) {
314 oms_q_evt evt;
315
316 evt.cmd = OMS_STRM_NEXT;
317 evt.parm.p = (void *)fname;
318 m_q.send(evt);
319 evt.cmd = OMS_STRM_PARM;
320 evt.parm.f = vol;
321 m_q.send(evt);
322 evt.cmd = OMS_STRM_PARM;
323 evt.parm.i = (int)count;
324 m_q.send(evt);
325 evt.cmd = OMS_STRM_PARM;
326 evt.parm.p = (void *)switch_flag;
327 m_q.send(evt);
328 }