Debian Codegraph @

music/sequencer.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/sequencer.cpp $
21 * $Revision: 35 $
22 * $Date: 4/09/99 12:04p $
23 * $Author: Samir $
24 *
25 * Event driven music system
26 *
27 * $Log: /DescentIII/Main/music/sequencer.cpp $
28 *
29 * 35 4/09/99 12:04p Samir
30 * take out error messages (mprintfs) for stream load failures.
31 *
32 * 34 4/01/99 4:50p Matt
33 * Took out Warning() function, chaning those calls to mprintf()
34 *
35 * 33 3/18/99 10:13a Samir
36 * msuic update.
37 *
38 * 32 3/03/99 5:08p Samir
39 * added slew of debug code: hopefully the library will be revamped after
40 * OEM.
41 *
42 * 31 3/03/99 3:43a Samir
43 * moved mprintfs for debugging.
44 *
45 * 30 3/02/99 1:18p Samir
46 * fixes to prevent bad filenames from crashing system.
47 *
48 * 29 3/01/99 5:01a Samir
49 * fixed bug when no pending or active song, it would still try to start a
50 * pending song.
51 *
52 * 28 2/28/99 6:35p Samir
53 * fixed streaming bugs for very small samples.
54 *
55 * 27 2/27/99 8:23p Samir
56 * fixes to music system to act nicely to sudden and frequent region
57 * changes.
58 *
59 * 26 2/27/99 6:51p Samir
60 * added code for music tester to display current stream and loop/region.
61 *
62 * 25 2/27/99 5:13p Nate
63 * Samir: fixes
64 *
65 * 24 2/27/99 4:37p Samir
66 * return name of loop currently playing.
67 *
68 * 23 2/26/99 5:26p Samir
69 * fixes to streaming audio to reflect fix in direct sound mixer.
70 *
71 * 22 2/22/99 3:23p Luke
72 * upped size of music buffers
73 *
74 * 21 2/16/99 12:09p Samir
75 * immediate switches open new stream object.
76 *
77 * 20 1/28/99 2:22p Samir
78 * simplified music system for D3.
79 *
80 * 19 12/13/98 6:51p Samir
81 * enhancements.
82 *
83 * 18 12/11/98 6:05p Samir
84 * fixed looping bug (didn't set loop count!)
85 *
86 * 17 12/11/98 4:02p Samir
87 * clean theme changes.
88 *
89 * 16 12/10/98 10:12a Samir
90 * uses newer streaming audio library
91 *
92 * 15 12/07/98 11:45a Samir
93 * hacks to get music system mostly working in DS_8. Will change this
94 * system so it's a lot cleaner after music demo.
95 *
96 * 14 12/03/98 12:48p Samir
97 * speed up sequencer so that gap between samples isn't too big.
98 *
99 * 13 11/20/98 5:20p Samir
100 * added a bunch of high level scripting commands.
101 *
102 * 12 11/16/98 4:15p Samir
103 * fadeouts.
104 *
105 * 11 11/13/98 2:27p Samir
106 * new music system.
107 *
108 * 10 8/10/98 5:53p Samir
109 * improved switching between streams and basic background/combat music.
110 *
111 * 9 7/30/98 7:33p Jeff
112 * fixed bug passing NULL to reset.
113 *
114 * 8 7/28/98 5:43p Samir
115 * reorg of music system.
116 *
117 * 7 7/24/98 5:20p Samir
118 * first real music checkin.
119 *
120 * 6 7/08/98 6:26p Samir
121 * took out obsolete code.
122 *
123 * 5 2/15/98 7:12p Chris
124 * More improvements to the sound lib
125 *
126 * 4 1/05/98 3:54p Chris
127 * Added ambient and explosion sounds
128 *
129 * 3 12/31/97 2:58a Chris
130 * Another major revision to the SoundLib. This cleaned up the code after
131 * removing the direct sound secondary buffers.
132 *
133 * 2 12/10/97 4:47p Chris
134 * Revision 1.0 of new sound library (no hardware -- uses primary buffer
135 * streaming)
136 *
137 * 3 5/29/97 3:58p Samir
138 * Works with some very small pauses.
139 *
140 * 2 5/23/97 4:34p Samir
141 *
142 * $NoKeywords: $
143 */
144
145 #include <cstring>
146 #include <cstdlib>
147
148 #include "ddio.h"
149 #include "inffile.h"
150 #include "mem.h"
151 #include "music.h"
152 #include "musiclib.h"
153 #include "pserror.h"
154 #include "streamaudio.h"
155
156 #define LOGFILE(_s)
157
158 OutrageMusicSeq::OutrageMusicSeq() {
159 m_sequencer_run = false;
160 m_sequencer_init = false;
161 m_str_buffer = NULL;
162 m_ins_buffer = NULL;
163 m_curregion = -1;
164 m_pending_song = m_active_song = m_playing_song = NULL;
165 }
166
167 OutrageMusicSeq::~OutrageMusicSeq() { OutrageMusicSeq::Shutdown(); }
168
169 // takes a OMS theme file.
170 bool OutrageMusicSeq::Init(const char *theme_file) {
171 // reset streams
172 m_dominant_strm = 0;
173 m_timer = 0.0f;
174
175 if (theme_file[0] == 0) {
176 return false;
177 }
178
179 Stop();
180
181 // initialize memory buffers
182 m_ins_buffer = (music_ins *)mem_malloc(sizeof(music_ins) * MAX_MUSIC_INSTRUCTIONS);
183 m_str_buffer = (char *)mem_malloc(MAX_MUSIC_STRLEN);
184 m_ins_curptr = m_ins_buffer;
185 m_str_curptr = m_str_buffer;
186 m_str_curptr[0] = 0;
187
188 // initialize music lists
189 m_music_list.free();
190 m_tracklist.init(64);
191 m_pending_song = m_active_song = m_playing_song = NULL;
192 m_output_q.flush();
193 m_curregion = -1;
194
195 OutrageMusicSeq::SetVolume(1.0f);
196 m_sequencer_init = true;
197
198 return LoadTheme(theme_file);
199 }
200
201 void OutrageMusicSeq::Shutdown() {
202 if (!m_sequencer_init)
203 return;
204
205 Stop();
206
207 // free music lists.
208 FreeList();
209 m_tracklist.free();
210
211 // free allocated buffers
212 mem_free(m_ins_buffer);
213 mem_free(m_str_buffer);
214
215 // free streams
216 for (int i = 0; i < OMS_NUM_STRM; i++)
217 m_strm[i].Reset(this);
218
219 m_playing_song = NULL;
220 m_sequencer_init = false;
221 }
222
223 // starts the sequencer (usually at the beginning of a level)
224 void OutrageMusicSeq::Start() {
225 if (IsSequencerRunning() || !m_sequencer_init)
226 return;
227
228 int i;
229
230 m_sequencer_run = true;
231 m_dominant_strm = 0;
232
233 m_timer = 0.0f;
234
235 for (i = 0; i < N_MUSIC_REGS; i++) {
236 m_registers[i] = 0;
237 }
238
239 mprintf(0, "Music system on.\n");
240 }
241
242 // stops the sequencer, flushes events
243 void OutrageMusicSeq::Stop() {
244 if (!IsSequencerRunning())
245 return;
246
247 int i;
248
249 mprintf(0, "Music system off.\n");
250 m_sequencer_run = false;
251
252 for (i = 0; i < OMS_NUM_STRM; i++) {
253 m_strm[i].Reset(this);
254 }
255 }
256
257 // set volume of sequencer
258 void OutrageMusicSeq::SetVolume(float vol) {
259 int i;
260
261 ASSERT(vol >= 0.0f && vol <= 1.0f);
262 m_mastervol = vol;
263
264 for (i = 0; i < OMS_NUM_STRM; i++)
265 m_strm[i].SetVolume(m_mastervol);
266 }
267
268 #define START_PENDING_SONG() \
269 do { \
270 ASSERT(m_pending_song); \
271 if (m_active_song) { \
272 m_pending_song->strm = m_active_song->strm; \
273 } \
274 m_active_song = m_pending_song; \
275 if (m_active_song) { \
276 m_active_song->stream_idle = false; \
277 m_active_song->request_stop = false; \
278 } \
279 m_pending_song = NULL; \
280 } while (0)
281
282 // runs a frame of input.
283 void OutrageMusicSeq::Frame(float frame_time) {
284 bool start_pending_song = false;
285 int i;
286
287 // run check.
288 if (!m_sequencer_run)
289 return;
290
291 // close all streams that aren't the dominant one and have stopped playing
292 for (i = 0; i < OMS_NUM_STRM; i++) {
293 if ((&m_strm[i]) != (&m_strm[m_dominant_strm])) {
294 if (m_strm[i].m_stream.State() == STRM_STOPPED) {
295 m_strm[i].m_stream.Close();
296 }
297 }
298 }
299
300 // determine when to start song if a song is pending.
301 if (!m_active_song && m_pending_song) {
302 start_pending_song = true;
303 }
304
305 if (m_pending_song) {
306 if (m_pending_song->immediate_switch == true) {
307 start_pending_song = true;
308 } else if (m_active_song) {
309 if (m_active_song->stream_idle) {
310 // this will transfer the current song's steam to the pending one, so the
311 // ExecScript function can manage these streams.
312 start_pending_song = true;
313 } else if (!m_active_song->request_stop) {
314 m_active_song->request_stop = true;
315 }
316 }
317 }
318
319 if (start_pending_song) {
320 mprintf(0, "MUSIC:Starting pending song.\n");
321 LOGFILE((_logfp, "MUSIC:Starting pending song.\n"));
322 START_PENDING_SONG();
323 }
324
325 // execute song code
326 ExecScript(m_active_song);
327
328 // process streams, starting at dominant stream.
329 // this is vital to ensure that the dominant stream's commands are executed before any
330 // other streams.
331 // i = m_dominant_strm;
332 // do
333 // {
334 // m_strm[i].Process(frame_time);
335 // i++;
336 // if (i == OMS_NUM_STRM) i = 0;
337 // }
338 // while (i != m_dominant_strm);
339
340 m_timer += frame_time;
341 }
342
343 // music kernal.
344 void OutrageMusicSeq::ExecScript(music_stream *strm) {
345 if (!strm)
346 return;
347
348 // get command
349 next_ins:
350 music_ins *cur_ins = &strm->ins[strm->ip];
351 uint8_t cmd = cur_ins->cmd;
352 tMusicVal val = cur_ins->opr.num;
353 char *str = cur_ins->opr.str;
354 const char *name;
355
356 // goto next instruction.
357 strm->ip++;
358
359 // mprintf(0, "%d ", cmd);
360 AudioStream *stream = &strm->strm->m_stream;
361
362 // execute opcodes.
363 // if (strm->last_ip != strm->ip) {
364 // LOGFILE((_logfp, "ins=%d\n", cmd));
365 // }
366 strm->last_ip = strm->ip;
367
368 switch (cmd) {
369 case OMFCMD_PLAY:
370 name = m_tracklist.get(strm->ln_reg);
371 if (name) {
372 // close this song's stream.
373 bool err = false;
374
375 strm->old_strm = strm->strm;
376
377 strm->error = false;
378
379 if (strm->immediate_switch) {
380 // we start playing the current stream immediately.
381 stream->Close();
382 m_dominant_strm = DOMINANT_STRM_ADJUST();
383 // mprintf(0, "MUSIC: Starting stream with %s on channel %d.\n", name, m_dominant_strm);
384 LOGFILE((_logfp, "MUSIC: Starting stream with %s on channel %d.\n", name, m_dominant_strm));
385 strm->strm = &m_strm[m_dominant_strm];
386 stream = &strm->strm->m_stream;
387 err = stream->Open(name);
388 } else {
389 m_dominant_strm = DOMINANT_STRM_ADJUST();
390 // mprintf(0, "MUSIC: Preparing stream with %s on channel %d.\n", name, m_dominant_strm);
391 LOGFILE((_logfp, "MUSIC: Preparing stream with %s on channel %d.\n", name, m_dominant_strm));
392 strm->strm = &m_strm[m_dominant_strm];
393 stream = &strm->strm->m_stream;
394 err = stream->Open(name);
395 // stream->Open(name, STRM_OPNF_GRADUAL);
396 }
397
398 // skip instructions until error is cleared.
399 if (!err) {
400 // mprintf(0, "MUSIC: Error opening stream %s on channel %d.\n", name, m_dominant_strm);
401 LOGFILE((_logfp, "MUSIC: Error opening stream %s on channel %d.\n", name, m_dominant_strm));
402 strm->error = true;
403 }
404
405 stream->SetLoopCount(strm->p_reg);
406 stream->SetVolume(m_mastervol);
407 strm->stream_idle = false;
408 } else {
409 mprintf(0, "OMS: Stream was not found in track list.\n");
410 }
411
412 strm->pending_loop_name = name;
413 break;
414
415 case OMFCMD_MPLAY:
416 if (strm->immediate_switch || strm->error) {
417 strm->immediate_switch = false;
418 } else if (!stream->IsReady()) {
419 strm->ip--;
420 break;
421 } else if (strm->old_strm) {
422 if (strm->old_strm->m_stream.State() != STRM_STOPPED && strm->old_strm->m_stream.State() != STRM_INVALID) {
423 strm->ip--;
424 break;
425 }
426 }
427 strm->loop_name = strm->pending_loop_name;
428 m_playing_song = strm;
429
430 if (!strm->error) {
431 strm->error = !(stream->Play());
432 }
433
434 if (!strm->error) {
435 // mprintf(0, "MUSIC: playing %s.\n", strm->loop_name);
436 LOGFILE((_logfp, "MUSIC: playing %s.\n", strm->loop_name));
437 LOGFILE((_logfp, "MUSIC: state of played stream is %d.\n", strm->loop_name, stream->State()));
438 } else {
439 // mprintf(0, "MUSIC: Error playing %s.\n", strm->loop_name);
440 LOGFILE((_logfp, "MUSIC: Error playing %s.\n", strm->loop_name));
441 }
442 break;
443
444 case OMFCMD_WAIT:
445 Int3(); // flaky, shouldn't be used anymore.
446 strm->ip--;
447 if (val == 1) {
448 if (strm->strm->m_valid_result && strm->strm->m_result.cmd == OMS_STRM_STOP) {
449 // wait till stream has finished to continue.
450 strm->ip++;
451 }
452 }
453 break;
454
455 case OMFCMD_LCMP:
456 strm->c_reg = val;
457 break;
458
459 case OMFCMD_LLPT:
460 strm->ln_reg = str;
461 break;
462
463 case OMFCMD_LPLC:
464 strm->p_reg = val;
465 break;
466
467 case OMFCMD_SETI:
468 strm->i_reg = val;
469 break;
470
471 case OMFCMD_INCI:
472 strm->i_reg++;
473 break;
474
475 case OMFCMD_IFI:
476 strm->ifi_block = (strm->i_reg == val) ? true : false;
477 if (!strm->ifi_block) {
478 // skip ifi block.
479 while (strm->ins[strm->ip].cmd != OMFCMD_ENDIFI)
480 strm->ip++;
481 strm->ip++;
482 }
483 break;
484
485 case OMFCMD_ENDIFI:
486 strm->ifi_block = false;
487 break;
488
489 case OMFCMD_COMPARE: {
490 tMusicVal reg1 = strm->c_reg;
491 tMusicVal reg2 = m_registers[val];
492 strm->b_reg = (reg2 - reg1);
493 break;
494 }
495
496 case OMFCMD_GOTO:
497 // simply set IP.
498 strm->ip = val;
499 break;
500
501 case OMFCMD_BLT:
502 if (strm->b_reg < 0) {
503 strm->ip = val;
504 }
505 break;
506
507 case OMFCMD_BGT:
508 if (strm->b_reg > 0) {
509 strm->ip = val;
510 }
511 break;
512
513 case OMFCMD_BEQ:
514 if (strm->b_reg == 0) {
515 strm->ip = val;
516 }
517 break;
518
519 case OMFCMD_BNIF:
520 if (stream->ReadAheadFinishedLoop() || strm->error) {
521 oms_q_evt evt;
522 evt.cmd = OMS_EVT_LOOPENDING;
523 evt.parm.i = strm->type;
524 m_output_q.send(evt);
525 if (!strm->error) {
526 strm->ip--; // repeat this instruction again
527 }
528 } else if (!strm->request_stop) {
529 if (stream->ReadAhead()) {
530 strm->ip = val;
531 } else if (stream->State() != STRM_STOPPING && stream->State() != STRM_STOPPED) {
532 strm->ip = val;
533 }
534 } else {
535 stream->Stop(true); // soft stop on end of stream.
536 if (strm->request_stop) {
537 strm->stream_idle = true;
538 strm->request_stop = false;
539 mprintf(0, "MUSIC: Processed stop request.\n");
540 LOGFILE((_logfp, "MUSIC: Processed stop request.\n"));
541 }
542 }
543 break;
544
545 case OMFCMD_ENDSECTION: {
546 strm->ip--;
547 if (stream->State() == STRM_STOPPED) {
548 oms_q_evt evt;
549 strm->ip++;
550 evt.cmd = OMS_EVT_SONGENDED;
551 evt.parm.i = strm->type;
552 m_output_q.send(evt);
553 strm->stream_idle = true;
554 m_active_song = NULL;
555 m_playing_song = NULL;
556 }
557 break;
558 }
559 }
560
561 // okay, ifi_block is only really useful to determine if we're going to execute consecutive instructions
562 // note that even if we turn off ifi_block, this doesn't stop instructions from running inside the ifi_block
563 // but it does stop consecutive instructions.
564 // the actual decision to run the instructions in an ifi_block depends on the initial check.
565 if (cmd != OMFCMD_ENDSECTION && cmd != OMFCMD_PLAY && cmd != OMFCMD_MPLAY && cmd < OMFCMD_BRANCHING) {
566 goto next_ins;
567 } else {
568 strm->ifi_block = false;
569 }
570 }
571
572 // start a song, stopping the old either cleanly (on measure) or abruptly.
573 void OutrageMusicSeq::StartSong(int song, bool clean_switch) {
574 if (m_curregion >= 0) {
575 m_pending_song = GetSong(m_curregion, (int16_t)song);
576 } else {
577 m_pending_song = NULL;
578 }
579
580 if (m_pending_song) {
581 m_pending_song->request_stop = false;
582 m_pending_song->immediate_switch = !clean_switch;
583 m_pending_song->strm = &m_strm[m_dominant_strm];
584 }
585 }
586
587 // stops song, cold.
588 void OutrageMusicSeq::StopSong() {
589 if (m_active_song) {
590 m_active_song = NULL;
591 m_playing_song = NULL;
592 }
593 m_strm[m_dominant_strm].m_stream.Close();
594 }
595
596 // pauses
597 void OutrageMusicSeq::Pause() {
598 int i;
599
600 for (i = 0; i < OMS_NUM_STRM; i++) {
601 m_strm[i].m_stream.Pause();
602 }
603 }
604
605 void OutrageMusicSeq::Resume() {
606 int i;
607
608 for (i = 0; i < OMS_NUM_STRM; i++) {
609 m_strm[i].m_stream.Resume();
610 }
611 }
612
613 // get and set user defined (theme file) parameters
614 void OutrageMusicSeq::SetRegister(int parm, tMusicVal val) {
615 ASSERT(parm >= 0 && parm < N_MUSIC_REGS);
616 m_registers[parm] = val;
617 }
618
619 tMusicVal OutrageMusicSeq::GetRegister(int parm) {
620 ASSERT(parm >= 0 && parm < N_MUSIC_REGS);
621 return m_registers[parm];
622 }
623
624 // memory management
625 OutrageMusicSeq::music_ins *OutrageMusicSeq::alloc_and_copy_ins(int len, const music_ins *ins) {
626 // copy instructions to static buffer and return a pointer to it
627 if ((m_ins_curptr + len) > (m_ins_buffer + MAX_MUSIC_INSTRUCTIONS)) {
628 Int3();
629 return NULL;
630 } else {
631 memcpy(m_ins_curptr, ins, len * sizeof(music_ins));
632 m_ins_curptr += len;
633 return (m_ins_curptr - len);
634 }
635 }
636
637 char *OutrageMusicSeq::alloc_and_copy_str(const char *str) {
638 // copy string to static buffer and return a pointer to it
639 int slen = strlen(str) + 1;
640 if ((m_str_curptr + slen) > (m_str_buffer + MAX_MUSIC_STRLEN)) {
641 Int3();
642 return NULL;
643 } else {
644 strcpy(m_str_curptr, str);
645 m_str_curptr += slen;
646 return (m_str_curptr - slen);
647 }
648 }
649
650 // Music list management
651 OutrageMusicSeq::music_stream *OutrageMusicSeq::AddToList(int16_t region, int16_t theme_type, int len,
652 const music_ins *ins) {
653 tListNode<music_stream> *node = new tListNode<OutrageMusicSeq::music_stream>;
654
655 // create list item
656 node->t.region = region;
657 node->t.type = theme_type;
658 node->t.ins = alloc_and_copy_ins(len, ins);
659 if (!node->t.ins) {
660 delete node;
661 return NULL;
662 }
663
664 // link it in.
665 m_music_list.link(node);
666
667 return &node->t;
668 }
669
670 void OutrageMusicSeq::FreeList() {
671 tListNode<music_stream> *node;
672
673 while ((node = m_music_list.start())) {
674 node = m_music_list.unlink();
675 delete[] node;
676 }
677
678 m_music_list.free();
679 }
680
681 OutrageMusicSeq::music_stream *OutrageMusicSeq::GetSong(int16_t region, int16_t theme_type) {
682 tListNode<music_stream> *node = m_music_list.start();
683 music_stream *strm = NULL;
684 int i;
685
686 for (i = 0; i < m_music_list.length(); node = m_music_list.next(), i++) {
687 if (region == node->t.region) {
688 if (theme_type == node->t.type) {
689 strm = &node->t;
690 }
691
692 if (strm) {
693 strm->request_stop = false;
694 strm->immediate_switch = false;
695 strm->ip = 0;
696 strm->b_reg = 0;
697 strm->c_reg = 0;
698 strm->ln_reg = 0;
699 strm->p_reg = 0;
700 strm->i_reg = 0;
701 strm->ifi_block = false;
702 strm->stream_idle = true;
703 strm->strm = NULL;
704 strm->old_strm = NULL;
705 strm->loop_name = NULL;
706 strm->pending_loop_name = NULL;
707 strm->last_ip = -1;
708 strm->error = false;
709 break;
710 }
711 }
712 }
713
714 return strm;
715 }
716
717 //@@void OutrageMusicSeq::seqSilence()
718 //@@{
719 //@@// silence all streams.
720 //@@ int init_strm = m_dominant_strm;
721 //@@ do
722 //@@ {
723 //@@ // request switch to dominant stream when measure is complete.
724 //@@ m_strm[m_dominant_strm].SEND_STRM_FADEOUT(4.0f);
725 //@@ m_strm[m_dominant_strm].SEND_STRM_STOP();
726 //@@ m_strm[m_dominant_strm].SEND_STRM_FREE();
727 //@@ m_dominant_strm = DOMINANT_STRM_ADJUST();
728 //@@ }
729 //@@ while (init_strm != m_dominant_strm);
730 //@@}
731
732 void OutrageMusicSeq::SetCurrentRegion(int16_t region) { m_curregion = region; }
733
734 int16_t OutrageMusicSeq::GetCurrentRegion() const { return m_curregion; }
735
736 // get current loop playing
737 const char *OutrageMusicSeq::GetCurrentLoopName(int *count) {
738 if (m_playing_song) {
739 *count = m_playing_song->strm->m_stream.GetLoopCount();
740 return m_playing_song->loop_name;
741 }
742
743 *count = 0;
744 return NULL;
745 }
746
747 // gets current region PLAYING, not PENDING like above.
748 int16_t OutrageMusicSeq::GetPlayingRegion() const {
749 if (m_playing_song) {
750 return m_playing_song->region;
751 }
752
753 return -1;
754 }