1 /* 2 This file is part of Warzone 2100. 3 Copyright (C) 1999-2004 Eidos Interactive 4 Copyright (C) 2005-2020 Warzone 2100 Project 5 6 Warzone 2100 is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 Warzone 2100 is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with Warzone 2100; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21 #ifndef __INCLUDED_LIB_SOUND_TRACK_H__ 22 #define __INCLUDED_LIB_SOUND_TRACK_H__ 23 24 #include "lib/framework/frame.h" 25 #include <physfs.h> 26 #include "sounddefs.h" 27 28 #include <AL/al.h> 29 #include <functional> 30 31 #define ATTENUATION_FACTOR 0.0003f 32 33 #define SAMPLE_NOT_ALLOCATED -1 34 #define SAMPLE_NOT_FOUND -3 35 #define SAMPLE_COORD_INVALID -5 36 37 #define AUDIO_VOL_MAX 100L 38 39 /* typedefs 40 */ 41 42 typedef bool (* AUDIO_CALLBACK)(void *psObj); 43 struct AUDIO_STREAM; 44 45 /* structs */ 46 47 struct SIMPLE_OBJECT; 48 49 struct AUDIO_SAMPLE 50 { 51 SDWORD iTrack; // ID number identifying a specific sound; currently (r1182) mapped in audio_id.c 52 ALuint iSample; // OpenAL name of the sound source 53 #ifdef DEBUG // only used for debugging 54 ALboolean isLooping; // if sample loops 55 ALboolean is3d; // if sample is 3d (as opposed to 2d) 56 char filename[256]; // actual filename of sample 57 #endif 58 SDWORD x, y, z; 59 float fVol; // computed volume of sample 60 bool bFinishedPlaying; 61 AUDIO_CALLBACK pCallback; 62 SIMPLE_OBJECT *psObj; 63 AUDIO_SAMPLE *psPrev; 64 AUDIO_SAMPLE *psNext; 65 }; 66 67 struct TRACK 68 { 69 bool bLoop; 70 SDWORD iVol; 71 SDWORD iAudibleRadius; 72 SDWORD iTime; // duration in milliseconds 73 UDWORD iTimeLastFinished; // time last finished in ms 74 UDWORD iNumPlaying; 75 ALuint iBufferName; // OpenAL name of the buffer 76 const char *fileName; 77 }; 78 79 /* functions 80 */ 81 82 bool sound_Init(HRTFMode hrtf); 83 bool sound_Shutdown(); 84 85 TRACK *sound_LoadTrackFromFile(const char *fileName); 86 unsigned int sound_SetTrackVals(const char *fileName, bool loop, unsigned int volume, unsigned int audibleRadius); 87 void sound_ReleaseTrack(TRACK *psTrack); 88 89 void sound_StopTrack(AUDIO_SAMPLE *psSample); 90 void sound_PauseTrack(AUDIO_SAMPLE *psSample); 91 void sound_UpdateSample(AUDIO_SAMPLE *psSample); 92 void sound_CheckAllUnloaded(); 93 void sound_RemoveActiveSample(AUDIO_SAMPLE *psSample); 94 bool sound_CheckTrack(SDWORD iTrack); 95 96 SDWORD sound_GetTrackTime(SDWORD iTrack); 97 SDWORD sound_GetTrackAudibleRadius(SDWORD iTrack); 98 SDWORD sound_GetTrackVolume(SDWORD iTrack); 99 const char *sound_GetTrackName(SDWORD iTrack); 100 101 bool sound_TrackLooped(SDWORD iTrack); 102 void sound_SetCallbackFunction(void *fn); 103 104 bool sound_Play2DTrack(AUDIO_SAMPLE *psSample, bool bQueued); 105 bool sound_Play3DTrack(AUDIO_SAMPLE *psSample); 106 void sound_PlayWithCallback(AUDIO_SAMPLE *psSample, SDWORD iCurTime, AUDIO_CALLBACK pDoneFunc); 107 void sound_FinishedCallback(AUDIO_SAMPLE *psSample); 108 109 bool sound_GetSystemActive(); 110 SDWORD sound_GetTrackID(TRACK *psTrack); 111 SDWORD sound_GetAvailableID(); 112 SDWORD sound_GetNumPlaying(SDWORD iTrack); 113 114 SDWORD sound_GetGlobalVolume(); 115 void sound_SetGlobalVolume(SDWORD iVol); 116 117 void sound_SetStoppedCallback(AUDIO_CALLBACK pStopTrackCallback); 118 119 UDWORD sound_GetTrackTimeLastFinished(SDWORD iTrack); 120 void sound_SetTrackTimeLastFinished(SDWORD iTrack, UDWORD iTime); 121 122 bool sound_isStreamPlaying(AUDIO_STREAM *stream); 123 void sound_StopStream(AUDIO_STREAM *stream); 124 void sound_PauseStream(AUDIO_STREAM *stream); 125 void sound_ResumeStream(AUDIO_STREAM *stream); 126 AUDIO_STREAM *sound_PlayStreamWithBuf(PHYSFS_file *fileHandle, float volume, const std::function<void (const void *)>& onFinished, const void *user_data, size_t streamBufferSize, unsigned int buffer_count, bool allowSeeking = false); 127 float sound_GetStreamVolume(const AUDIO_STREAM *stream); 128 void sound_SetStreamVolume(AUDIO_STREAM *stream, float volume); 129 double sound_GetStreamTotalTime(AUDIO_STREAM *stream); 130 131 #endif // __INCLUDED_LIB_SOUND_TRACK_H__ 132