Debian Codegraph @

music/music.h

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/Lib/music.h $
21 * $Revision: 21 $
22 * $Date: 3/18/99 10:13a $
23 * $Author: Samir $
24 *
25 * Event driven music system
26 *
27 * $Log: /DescentIII/Main/Lib/music.h $
28 *
29 * 21 3/18/99 10:13a Samir
30 * msuic update.
31 *
32 * 20 3/03/99 5:08p Samir
33 * added slew of debug code: hopefully the library will be revamped after
34 * OEM.
35 *
36 * 19 2/27/99 8:23p Samir
37 * fixes to music system to act nicely to sudden and frequent region
38 * changes.
39 *
40 * 18 2/27/99 6:52p Samir
41 * added function to get current loop count
42 *
43 * 17 2/27/99 4:37p Samir
44 * return name of loop currently playing.
45 *
46 * 16 2/26/99 5:26p Samir
47 * fixes to streaming audio to reflect fix in direct sound mixer.
48 *
49 * 15 2/22/99 3:23p Luke
50 * upped size of music buffers
51 *
52 * 14 1/28/99 2:22p Samir
53 * simplified music system for D3.
54 *
55 * 13 12/15/98 10:22a Samir
56 * upped max labels.
57 *
58 * 12 12/11/98 4:03p Samir
59 * made stream public for oms_stream.
60 *
61 * 11 12/10/98 10:12a Samir
62 * uses newer streaming audio library
63 *
64 * 10 12/07/98 11:44a Samir
65 * added new themes.
66 *
67 * 9 12/03/98 12:51p Samir
68 * changed WasStopped to HasStopped.
69 *
70 * 8 11/20/98 5:21p Samir
71 * added SEND_STRM_NEXT
72 *
73 * 7 11/13/98 2:29p Samir
74 * added oms_tracklist and newer music code.
75 *
76 * 6 8/14/98 7:37p Matt
77 * Since PSFILENAME_LEN is now considered to be the max length of the
78 * string itself (not counting the terminating null), buffers for file
79 * name need to be PSFILENAME_LEN+1 bytes long.
80 *
81 * 5 8/10/98 5:53p Samir
82 * improved switching between streams and basic background/combat music.
83 *
84 * 4 7/28/98 5:43p Samir
85 * reorg of music system.
86 *
87 * 3 7/24/98 5:20p Samir
88 * first real music checkin.
89 *
90 * 2 7/08/98 6:26p Samir
91 * took out obsolete code.
92 *
93 * 2 6/09/97 4:16p Samir
94 * RCS check in
95 *
96 * $NoKeywords: $
97 */
98
99 #ifndef MUSIC_H
100 #define MUSIC_H
101
102 #include "pstypes.h"
103 #include "pserror.h"
104 #include "psclass.h"
105 #include "streamaudio.h"
106
107 ///////////////////////////////////////////////////////////////////////////////
108 // These are internal constants for the music sequencer
109
110 #define OMS_NUM_STRM 2
111 #define OMS_STRM_QSIZE 8
112
113 // OMS Theme Types
114 #define OMS_THEME_TYPES 8
115 #define OMS_THEME_TYPE_NONE 0
116 #define OMS_THEME_TYPE_INTRO 1
117 #define OMS_THEME_TYPE_IDLE 2
118 #define OMS_THEME_TYPE_COMBAT 3
119 #define OMS_THEME_TYPE_DEATH 4
120 #define OMS_THEME_TYPE_IDLE_TO_COMBAT 5
121 #define OMS_THEME_TYPE_COMBAT_TO_IDLE 6
122
123 // OMS Stream commands
124 #define OMS_STRM_FREE 255 // indicates free stream.
125 #define OMS_STRM_STOP 0 // stop current stream.
126 #define OMS_STRM_PLAY 1 // play current stream.
127 #define OMS_STRM_FADEOUT 2 // fades out current stream in (parm secs).
128 #define OMS_STRM_FADEIN 3 // plays current stream, fade it in. at (parm secs)
129 #define OMS_STRM_DELAY 4 // standard delay (parm = time)
130 #define OMS_STRM_LOAD 5 // loads song (parm = song type).
131 #define OMS_STRM_PARM 6 // extra parameter for last stream command (parm = parameter)
132 #define OMS_STRM_SWITCH 7 // switches on next measure of this calling stream to the dominant one.
133 #define OMS_STRM_NEXT 8 // specifies setting a 'next' request on an opened stream.
134
135 // ////////////////////////////////////////////////////////////////////////////
136 // generic event item for the queues.
137 struct oms_q_evt {
138 uint8_t cmd;
139 union {
140 float f;
141 int i;
142 void *p;
143 } parm;
144 };
145
146 class OutrageMusicSeq;
147
148 // internal class to the sequencer, handles command-based streams.
149 class oms_stream {
150 friend class OutrageMusicSeq;
151
152 OutrageMusicSeq *m_seq; // parent sequencer.
153 tQueue<oms_q_evt, OMS_STRM_QSIZE> m_q; // queue of stream commands.
154 float m_timer, m_timer_init; // timer managed by user.
155 float m_maxvol; // sustained max volume of stream (used in fading).
156 oms_q_evt m_status; // current command status (operation q item)
157 int m_stopcount; // number of times stopped.
158
159 union {
160 int i;
161 float f;
162 void *p;
163 } m_data; // data for internal ops.
164 union {
165 int i;
166 float f;
167 void *p;
168 } m_data2; // data for internal ops.
169
170 private:
171 inline void STREAM_COMMANDP(uint8_t cmd, void *p = NULL) {
172 m_status.cmd = cmd;
173 m_status.parm.p = p;
174 }
175 inline void STREAM_COMMANDF(uint8_t cmd, float cmdparm = 0) {
176 m_status.cmd = cmd;
177 m_status.parm.f = cmdparm;
178 };
179 inline void STREAM_COMMANDI(uint8_t cmd, int cmdparm = 0) {
180 m_status.cmd = cmd;
181 m_status.parm.f = cmdparm;
182 };
183 inline void STREAM_TIMER(float time_init) {
184 m_timer = time_init;
185 m_timer_init = time_init;
186 };
187
188 bool processCommand(); // processes current command on stream
189 void processQLoad(const char *fname); // processes load song q event.
190
191 public:
192 oms_stream();
193 ~oms_stream();
194
195 AudioStream m_stream; // current audio stream playing
196
197 public:
198 void Process(float frmtime); // processes a stream's events.
199 void Send(oms_q_evt *evt); // sends an event to the stream.
200 void Reset(OutrageMusicSeq *seq = NULL); // reset stream.
201 void SetVolume(float vol); // volume.
202 int GetPlayRequestCount(); // tells how many play events send are pending processing.
203
204 // event senders
205 public:
206 void SEND_STRM_LOAD(const char *fname);
207 void SEND_STRM_FADEOUT(float time);
208 void SEND_STRM_FADEIN(float time);
209 void SEND_STRM_STOP();
210 void SEND_STRM_PLAY(float vol, uint8_t count);
211 void SEND_STRM_FREE();
212 void SEND_STRM_SWITCH(bool *switch_flag); // when stream is done, changes value of flag.
213 void SEND_STRM_NEXT(const char *fname, float vol, uint8_t count,
214 bool *switch_flag); // specifies next stream to play in current stream.
215
216 // return values (after process is called, this is updated.)
217 public:
218 bool m_valid_result; // is this a valid returned result?
219 oms_q_evt m_result; // result event returned from process.
220 };
221
222 class oms_tracklist {
223 char **m_fnamelist; // filename list
224 char **m_symlist; // symbolic list
225 int16_t m_numtracks; // number of tracks
226 int16_t m_maxtracks;
227 bool m_init; // determines if initialized
228
229 public:
230 oms_tracklist();
231 ~oms_tracklist();
232
233 void init(int16_t maxtracks); // initializes track list system
234 void free(); // frees track list system
235 void reset(); // resets track list to 0
236
237 bool add(const char *fname, const char *sym);
238 const char *get(const char *sym); // returns a track filename.
239 };
240
241 // ////////////////////////////////////////////////////////////////////////////
242 // The Outrage Music Sequencer v1.0
243 // playback songs with special FX.
244 // this is an intermediate between song playback and event driven music.
245 // the calling application must act as the 'brain' in most cases.
246
247 typedef int16_t tMusicVal; // used for paramaters
248
249 inline float NORMALIZE_MUSICVAL(tMusicVal val) { return ((float)val) / 255.0f; }
250 inline tMusicVal QUANTIZE_MUSICVAL(float fval) { return (fval * 255); }
251
252 #define MAX_MUSIC_INSTRUCTIONS 1280
253 #define MAX_MUSIC_STRLEN 2048
254 #define N_MUSIC_REGS 16
255
256 // OMS Events
257 #define OMSEVT_MAKE(_evt, _priority) (((_evt) << 2) + (_priority & 0x03))
258 #define OMSEVT_PRIORITY(_evt) ((_evt) & 0x3)
259
260 #define OMS_EVT_NONE OMSEVT_MAKE(0, 0)
261 #define OMS_EVT_SONGENDED OMSEVT_MAKE(32, 0) // music completed (parm tells us what theme)
262 #define OMS_EVT_LOOPENDING OMSEVT_MAKE(31, 0) // a loop is ending.
263
264 // class
265 class OutrageMusicSeq {
266 friend class oms_stream; // stream should know about data here.
267
268 // music scripting system.
269 struct music_ins {
270 uint8_t cmd; // OMFCMD_
271 union {
272 tMusicVal num;
273 char *str;
274 } opr; // operand is either numeric or a string
275 };
276
277 struct music_stream {
278 oms_stream *strm; // attached stream.
279 oms_stream *old_strm; // previous stream.
280 const char *loop_name;
281 const char *pending_loop_name; // loop name preparing to play.
282 int16_t region; // region id.
283 int16_t type;
284 music_ins *ins; // intro, combat, etc. section
285 int16_t last_ip; // last ip value
286 int16_t ip;
287 tMusicVal b_reg; // branch register
288 tMusicVal c_reg; // compare regs
289 tMusicVal p_reg; // play count register.
290 tMusicVal i_reg; // iteration register
291 const char *ln_reg; // loopname register
292 bool request_stop; // we're requesting to stop at end of song.
293 bool ifi_block; // are we executing an ifi block?
294 bool immediate_switch; // immediate switch to new song.
295 bool stream_idle; // are we in a next switch?
296 bool error;
297 uint8_t pad[3];
298 };
299
300 // data
301 tMusicVal m_registers[N_MUSIC_REGS]; // registers
302 music_stream *m_active_song, *m_pending_song, *m_playing_song;
303 oms_stream m_strm[OMS_NUM_STRM]; // stream attached to it.
304
305 bool m_sequencer_run; // do we run the sequencer.
306 bool m_sequencer_init; // is sequencer initialized.
307 int m_dominant_strm; // which stream is dominant.
308 float m_timer; // master timer.
309 float m_mastervol; // master volume.
310
311 oms_tracklist m_tracklist; // sequencer track list.
312 tList<music_stream> m_music_list; // list of tracks.
313
314 int16_t m_curregion; // current region.
315
316 private:
317 // Music list management
318 music_stream *AddToList(int16_t region, int16_t theme_type, int n_inst, const music_ins *ins);
319 void FreeList();
320 music_stream *GetSong(int16_t region, int16_t theme_type);
321 bool LoadTheme(const char *file); // takes a filename containing oms data.
322
323 void ExecScript(music_stream *strm);
324
325 inline int DOMINANT_STRM_ADJUST() {
326 int nstrm = m_dominant_strm + 1;
327 if (nstrm == OMS_NUM_STRM)
328 nstrm = 0;
329 return nstrm;
330 };
331
332 private:
333 music_ins *m_ins_buffer; // music instruction buffer
334 music_ins *m_ins_curptr; // current pointer to free instruction buffer.
335 char *m_str_buffer; // string buffer.
336 char *m_str_curptr; // current pointer to free string
337
338 music_ins *alloc_and_copy_ins(int len, const music_ins *ins);
339 char *alloc_and_copy_str(const char *str);
340
341 public:
342 OutrageMusicSeq();
343 ~OutrageMusicSeq();
344
345 // takes a OMS theme file.
346 bool Init(const char *theme_file);
347 void Shutdown();
348
349 bool IsSequencerRunning() const { return m_sequencer_init && m_sequencer_run; }
350
351 // starts the sequencer (usually at the beginning of a level)
352 void Start();
353
354 // stops the sequencer, flushes events
355 void Stop();
356
357 // do a frame
358 void Frame(float frame_time);
359
360 // set volume of sequencer
361 void SetVolume(float vol);
362
363 // start a song, stopping the old either cleanly (on measure) or abruptly.
364 void StartSong(int song, bool clean_switch);
365
366 // stops song, cold.
367 void StopSong();
368
369 // pauses
370 void Pause();
371
372 // resumes
373 void Resume();
374
375 // get and set user defined (theme file) parameters
376 void SetRegister(int parm, tMusicVal val);
377 tMusicVal GetRegister(int parm);
378
379 // region setting
380 void SetCurrentRegion(int16_t region);
381 int16_t GetCurrentRegion() const;
382
383 // gets current region PLAYING, not PENDING like above.
384 int16_t GetPlayingRegion() const;
385
386 // get current loop playing
387 const char *GetCurrentLoopName(int *loop_count);
388
389 public:
390 tQueue<oms_q_evt, 4> m_output_q; // output events
391 };
392
393 #endif