| 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/streamaudio.h $ |
| 21 | * $Revision: 1.2 $ |
| 22 | * $Date: 2004/02/09 04:14:51 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Interface for streaming audio, compressed or not |
| 26 | * |
| 27 | * $Log: streamaudio.h,v $ |
| 28 | * Revision 1.2 2004/02/09 04:14:51 kevinb |
| 29 | * Added newlines to all headers to reduce number of warnings printed |
| 30 | * |
| 31 | * Made some small changes to get everything compiling. |
| 32 | * |
| 33 | * All Ready to merge the 1.5 tree. |
| 34 | * |
| 35 | * Revision 1.1.1.1 2000/04/18 00:00:38 icculus |
| 36 | * initial checkin |
| 37 | * |
| 38 | * |
| 39 | * 30 10/25/99 6:00p Jeff |
| 40 | * fixed adecode.h include for Linux |
| 41 | * |
| 42 | * 29 10/21/99 1:52p Kevin |
| 43 | * Mac Merge! |
| 44 | * |
| 45 | * 28 9/30/99 4:35p Jeff |
| 46 | * fixed linux typo |
| 47 | * |
| 48 | * 27 9/30/99 4:29p Jeff |
| 49 | * Linux compile |
| 50 | * |
| 51 | * 26 7/28/99 3:04p Kevin |
| 52 | * Macintosh! |
| 53 | * |
| 54 | * 25 5/17/99 1:50p Ardussi |
| 55 | * changed to compile on Mac |
| 56 | * |
| 57 | * 24 5/13/99 5:04p Ardussi |
| 58 | * changes for compiling on the Mac |
| 59 | * |
| 60 | * 23 5/10/99 9:22p Samir |
| 61 | * added code to allow for certain streams to be specified to start on |
| 62 | * next sound frame. |
| 63 | * |
| 64 | * 22 4/15/99 1:44a Jeff |
| 65 | * changes for linux compile |
| 66 | * |
| 67 | * 21 4/10/99 5:09p Samir |
| 68 | * beginning to add prioritization of sounds code. currently non |
| 69 | * functional, but allows me to change all calls to Play3dSound so that I |
| 70 | * can test later. |
| 71 | * |
| 72 | * 20 3/04/99 6:38p Samir |
| 73 | * Saved stream state per frame. |
| 74 | * |
| 75 | * 19 3/03/99 5:08p Samir |
| 76 | * added slew of debug code: hopefully the library will be revamped after |
| 77 | * OEM. |
| 78 | * |
| 79 | * 18 2/28/99 5:11p Nate |
| 80 | * added playcount. |
| 81 | * |
| 82 | * 17 2/27/99 6:52p Samir |
| 83 | * added function to get current loop count |
| 84 | * |
| 85 | * 16 2/26/99 5:26p Samir |
| 86 | * fixes to streaming audio to reflect fix in direct sound mixer. |
| 87 | * |
| 88 | * 15 1/13/99 6:48a Jeff |
| 89 | * made linux friendly with some #ifdef's |
| 90 | * |
| 91 | * 14 12/13/98 6:50p Samir |
| 92 | * fixed problems with looping and next.switching sync. |
| 93 | * |
| 94 | * 13 12/11/98 3:27p Samir |
| 95 | * add debug code. |
| 96 | * |
| 97 | * 12 12/10/98 7:12p Samir |
| 98 | * fixed some bugs in file/playback buffer sequencing with new system. |
| 99 | * |
| 100 | * 11 12/10/98 10:11a Samir |
| 101 | * newer streaming audio library. |
| 102 | * |
| 103 | * 8 11/20/98 5:20p Samir |
| 104 | * correcting a lot of errors in AudioStream::Next |
| 105 | * |
| 106 | * 7 11/13/98 2:25p Samir |
| 107 | * added 'next' stream processing. |
| 108 | * |
| 109 | * 7 11/13/98 10:48a Samir |
| 110 | * added 'next' stream capability. |
| 111 | * |
| 112 | * 6 10/23/98 7:05p Samir |
| 113 | * added pause and resume |
| 114 | * |
| 115 | * 5 8/10/98 5:54p Samir |
| 116 | * added looping streams and soft measure stopping. |
| 117 | * |
| 118 | * 4 7/24/98 5:19p Samir |
| 119 | * added osfarchive class, |
| 120 | * |
| 121 | * 3 7/09/98 8:36p Samir |
| 122 | * fully implemented redo of Stream interface. |
| 123 | * |
| 124 | * 2 7/08/98 6:27p Samir |
| 125 | * stream library integrated with highlevel sound system. |
| 126 | * |
| 127 | * 1 7/08/98 6:22p Samir |
| 128 | * moved from man. |
| 129 | * |
| 130 | * 3 6/19/98 6:07p Jeff |
| 131 | * added a function to return a handle to the sound |
| 132 | * |
| 133 | * 2 6/18/98 5:17p Jeff |
| 134 | * Initial creation, support should be pretty much complete |
| 135 | * |
| 136 | * $NoKeywords: $ |
| 137 | */ |
| 138 | #ifndef __STREAMAUDIO_H_ |
| 139 | #define __STREAMAUDIO_H_ |
| 140 | |
| 141 | #include "adecode.h" |
| 142 | #include "ssl_lib.h" |
| 143 | |
| 144 | void *AudioStreamCB(void *user_data, int handle, int *size); |
| 145 | int ADecodeFileRead(void *data, void *buf, uint32_t qty); |
| 146 | int StreamPlay(const char *filename, float volume, int flags); |
| 147 | void StreamStop(int handle); |
| 148 | int StreamGetSoundHandle(int handle); |
| 149 | |
| 150 | ////////////////////////////////////////////////////////////////////////////// |
| 151 | class llsSystem; |
| 152 | struct CFILE; |
| 153 | // stream file info |
| 154 | #define OSF_HDR_SIZE 128 |
| 155 | #define OSF_TAG "OSF1" |
| 156 | #define OSF_HDR_TITLE_OFS 64 |
| 157 | #define OSF_HDR_TITLE_LEN 32 |
| 158 | // stream type |
| 159 | #define OSF_DIGITAL_STRM 0 |
| 160 | // compression type |
| 161 | #define OSF_DIGIRAW_STRM 0 |
| 162 | #define OSF_DIGIACM_STRM 1 |
| 163 | // stream format |
| 164 | #define SAF_8BIT_MASK 0x00 |
| 165 | #define SAF_16BIT_MASK 0x01 |
| 166 | #define SAF_MONO_MASK 0x00 |
| 167 | #define SAF_STEREO_MASK 0x10 |
| 168 | |
| 169 | #define SAF_8BIT_M (SAF_8BIT_MASK | SAF_MONO_MASK) // 0x00 |
| 170 | #define SAF_8BIT_S (SAF_8BIT_MASK | SAF_STEREO_MASK) // 0x10 |
| 171 | #define SAF_16BIT_M (SAF_16BIT_MASK | SAF_MONO_MASK) // 0x01 |
| 172 | #define SAF_16BIT_S (SAF_16BIT_MASK | SAF_STEREO_MASK) // 0x11 |
| 173 | ////////////////////////////////////////////////////////////////////////////// |
| 174 | struct tOSFDigiHdr // this struct belongs to OSF_DIGITAL_STRM |
| 175 | { |
| 176 | uint32_t measure; |
| 177 | }; |
| 178 | class OSFArchive { |
| 179 | CFILE *m_fp; |
| 180 | uint32_t m_length; // file information stored such as length |
| 181 | uint8_t m_type; // stream type |
| 182 | uint8_t m_comp; // compression type |
| 183 | uint8_t m_flags; // format |
| 184 | uint32_t m_rate; // frequency |
| 185 | bool m_writemode; // are we in write mode? |
| 186 | union { |
| 187 | tOSFDigiHdr digi; |
| 188 | } m_hdr; |
| 189 | char m_name[OSF_HDR_TITLE_LEN]; // title string length. |
| 190 | int m_datastart; // pointer to start of data. |
| 191 | public: |
| 192 | OSFArchive(); |
| 193 | ~OSFArchive(); |
| 194 | bool Open(const char *filename, bool write = false); |
| 195 | void Close(); |
| 196 | bool Opened() const { return m_fp ? true : false; }; |
| 197 | void Rewind(); |
| 198 | // write out operations. |
| 199 | bool SaveHeader(uint8_t type, uint8_t comp, uint8_t flags, uint32_t rate, uint32_t length, void *hdr, const char *name); |
| 200 | bool WriteBlock(uint8_t *blk, int size); |
| 201 | // read in operations. |
| 202 | int Read(uint8_t *blk, int size); |
| 203 | // get archive info. |
| 204 | uint8_t StreamType() const { return m_type; }; |
| 205 | uint8_t StreamComp() const { return m_comp; }; |
| 206 | uint8_t StreamFormat() const { return m_flags; }; |
| 207 | uint32_t StreamRate() const { return m_rate; }; |
| 208 | uint32_t StreamLength() const { return m_length; }; |
| 209 | const char *StreamName() const { return (const char *)m_name; }; |
| 210 | // get header info. |
| 211 | const void *StreamHeader() const { return (void *)&m_hdr.digi; }; |
| 212 | }; |
| 213 | ////////////////////////////////////////////////////////////////////////////// |
| 214 | // streamaudio constants. |
| 215 | #define STRM_BUFCOUNT 4 |
| 216 | #define STRM_BUFSIZE STREAM_BUFFER_SIZE |
| 217 | #define STRM_LIMIT 4 |
| 218 | #define STRM_STOPPED 0x0 |
| 219 | #define STRM_PLAYING 0x1 |
| 220 | #define STRM_PAUSED 0x2 |
| 221 | #define STRM_STOPPING 0x4 |
| 222 | #define STRM_PENDING 0x8 // DAJ |
| 223 | #define STRM_INVALID 0xff |
| 224 | // flags used to open stream. |
| 225 | #define STRM_OPNF_ONETIME 0x1 |
| 226 | #define STRM_OPNF_GRADUAL 0x2 |
| 227 | /* This class will handle streams for the music system. |
| 228 | Including allowing an interface to dynamically change the stream. |
| 229 | */ |
| 230 | #define STRM_BUFF_USED 0x1 // allocated buffer with data |
| 231 | #define STRM_BUFF_TERMINAL 0x2 // terminates on this buffer |
| 232 | #define STRM_BUFF_LOOPEND 0x4 // marks last buffer in measure |
| 233 | class AudioStream { |
| 234 | OSFArchive m_archive; // audio stream archive object. |
| 235 | AudioDecoder::IAudioDecoder *m_decoder; // audio codec object |
| 236 | struct { // mixing buffers |
| 237 | uint8_t *data; |
| 238 | int nbytes; // number of bytes of valid data. |
| 239 | int flags; |
| 240 | int id; |
| 241 | } m_buffer[STRM_BUFCOUNT]; |
| 242 | int m_bufsize; // size of each buffer. |
| 243 | float m_measure_timer; // timer for measure checking. |
| 244 | float m_measure_time; // amount of time per measure. |
| 245 | float m_last_frametime; |
| 246 | uint8_t m_sbufidx; // stream position markers |
| 247 | uint8_t m_fbufidx; // file position markers |
| 248 | uint8_t m_curbufidx; // current buffer in measure index |
| 249 | uint8_t m_playcount; |
| 250 | bool m_readahead; // if stream is currently reading from disk |
| 251 | bool m_readahead_finished_loop; // if a loop's readahead has finished |
| 252 | int16_t m_nbufs; // number of buffers streamed so far. |
| 253 | play_information m_playinfo; // used by llsSystem |
| 254 | float m_volume; // volume of stream. |
| 255 | int16_t m_state, m_laststate; // current state of stream playing |
| 256 | int m_llshandle; // internal sound handle. |
| 257 | int m_flags; // stream playing options. |
| 258 | int16_t m_streamindex; // index into active stream table. |
| 259 | int16_t m_loopcount; // loop counter. |
| 260 | int m_bytesleft; // number of bytes left in file |
| 261 | int m_curmeasure; // current measure. |
| 262 | int m_playbytesleft, m_playbytestotal; |
| 263 | int m_curid; // stream's id # |
| 264 | int *m_stopflag; // location of stop flag used in stop function |
| 265 | bool m_loop; // are we looping? |
| 266 | bool m_stopnextmeasure; // stop on next measure. |
| 267 | bool m_start_on_frame; // we will play this stream on the next ::Frame call. |
| 268 | bool m_start_on_frame_looped; // the stream that will play on next frame is looped. |
| 269 | private: |
| 270 | friend void *AudioStreamCB(void *user_data, int handle, int *size); |
| 271 | friend int ADecodeFileRead(void *data, void *buf, uint32_t qty); |
| 272 | void *StreamCallback(int *size); // invoked by omsStreamCB. |
| 273 | int ReadFileData(int buf, int len); // reads in decompressed raw data. |
| 274 | int ReadFileDirect(char *buf, int len); // reads in decompressed raw data. |
| 275 | void UpdateData(); // updates file buffers |
| 276 | void End(); // cleans up after a stop. |
| 277 | void Reset(); // resets to start of stream. |
| 278 | bool OpenDigitalStream(); // opens and prepares a digital stream |
| 279 | bool ReopenDigitalStream(uint8_t fbufidx, int nbufs); |
| 280 | |
| 281 | private: |
| 282 | // attach a low level sound system to all streams. |
| 283 | static llsSystem *m_ll_sndsys; |
| 284 | // list of all currently played streams |
| 285 | static AudioStream *m_streams[STRM_LIMIT]; |
| 286 | static int m_thisid; |
| 287 | // allocates a stream slot for a stream |
| 288 | bool ActivateStream(AudioStream *stream); |
| 289 | void DeactivateStream(AudioStream *stream); |
| 290 | |
| 291 | public: |
| 292 | // sets the low-level sound object |
| 293 | static void InitSystem(llsSystem *sndsys); |
| 294 | // shutdsown |
| 295 | static void Shutdown(); |
| 296 | // called by application to allow stream playback |
| 297 | static void Frame(); |
| 298 | // called to pause all streams. |
| 299 | static void PauseAll(); |
| 300 | static void ResumeAll(); |
| 301 | public: |
| 302 | AudioStream(); |
| 303 | ~AudioStream(); |
| 304 | // simple operations |
| 305 | // flags specify what type of stream you want. |
| 306 | bool Open(const char *filename, int open_flags = 0); |
| 307 | // specifies next stream to be opened when current one ends. |
| 308 | void Close(); |
| 309 | // simple requests |
| 310 | bool Play(bool start_on_frame = false); // plays a stream |
| 311 | void Stop(bool on_measure = false, |
| 312 | int *stop_flag = |
| 313 | NULL); // causes a rewind to start of stream, if on_measure is true, stop occurs when measure ends |
| 314 | void SetVolume(float vol); // sets volume |
| 315 | float GetVolume(); // returns volume |
| 316 | // misc requests |
| 317 | int GetLoopCount() const; // 0 = infinite. |
| 318 | void SetLoopCount(int loop_count); // sets the number of times this stream will loop (0 = infinite!) |
| 319 | void SetFlags(int flags); // sets flags for playback (STRM_OPNF_XXX) |
| 320 | void Pause(); // pauses a stream |
| 321 | void Resume(); // resumes a paused stream. |
| 322 | // state. |
| 323 | bool ReadAhead(); // are we still reading from disk? |
| 324 | bool ReadAheadFinishedLoop(); // has stream finished with its readahead of current loop? |
| 325 | bool IsReady(); // is this stopped stream ready to play? |
| 326 | int State() const { // returns current state |
| 327 | return m_state; |
| 328 | }; |
| 329 | // information |
| 330 | int GetLength() const { return m_archive.StreamLength(); }; |
| 331 | int GetPos() const { return m_archive.StreamLength() - m_bytesleft; }; |
| 332 | int GetBufferSize() const { return m_bufsize; }; |
| 333 | int CurrentMeasure() const { // returns current measure. |
| 334 | return m_curmeasure; |
| 335 | }; |
| 336 | int TotalMeasures() const; // total measure count |
| 337 | int GetSoundHandle() const { // returns the sound library handle for this stream. |
| 338 | return m_llshandle; |
| 339 | }; |
| 340 | // these functions are the 'simplified' stream interface from Jeff (most of this code is from Jeff) |
| 341 | friend int StreamPlay(const char *filename, float volume, int flags); |
| 342 | friend void StreamStop(int handle); |
| 343 | friend int StreamGetSoundHandle(int handle); |
| 344 | }; |
| 345 | #endif |