1 /*
2 ** i_sound.h
3 **
4 **---------------------------------------------------------------------------
5 ** Copyright 1998-2006 Randy Heit
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions
10 ** are met:
11 **
12 ** 1. Redistributions of source code must retain the above copyright
13 **    notice, this list of conditions and the following disclaimer.
14 ** 2. Redistributions in binary form must reproduce the above copyright
15 **    notice, this list of conditions and the following disclaimer in the
16 **    documentation and/or other materials provided with the distribution.
17 ** 3. The name of the author may not be used to endorse or promote products
18 **    derived from this software without specific prior written permission.
19 **
20 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 ** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 **---------------------------------------------------------------------------
31 **
32 */
33 
34 
35 #ifndef __I_SOUND__
36 #define __I_SOUND__
37 
38 #include "doomtype.h"
39 #include "i_soundinternal.h"
40 
41 class FileReader;
42 
43 enum ECodecType
44 {
45 	CODEC_Unknown,
46 	CODEC_Vorbis,
47 };
48 
49 enum EStartSoundFlags
50 {
51 	SNDF_LOOP=1,
52 	SNDF_NOPAUSE=2,
53 	SNDF_AREA=4,
54 	SNDF_ABSTIME=8,
55 	SNDF_NOREVERB=16,
56 };
57 
58 class SoundStream
59 {
60 public:
61 	virtual ~SoundStream ();
62 
63 	enum
64 	{	// For CreateStream
65 		Mono = 1,
66 		Bits8 = 2,
67 		Bits32 = 4,
68 		Float = 8,
69 
70 		// For OpenStream
71 		Loop = 16
72 	};
73 
74 	virtual bool Play(bool looping, float volume) = 0;
75 	virtual void Stop() = 0;
76 	virtual void SetVolume(float volume) = 0;
77 	virtual bool SetPaused(bool paused) = 0;
78 	virtual unsigned int GetPosition() = 0;
79 	virtual bool IsEnded() = 0;
80 	virtual bool SetPosition(unsigned int pos);
81 	virtual bool SetOrder(int order);
82 	virtual FString GetStats();
83 };
84 
85 typedef bool (*SoundStreamCallback)(SoundStream *stream, void *buff, int len, void *userdata);
86 
87 struct SoundDecoder;
88 
89 class SoundRenderer
90 {
91 public:
92 	SoundRenderer ();
93 	virtual ~SoundRenderer ();
94 
IsNull()95 	virtual bool IsNull() { return false; }
96 	virtual void SetSfxVolume (float volume) = 0;
97 	virtual void SetMusicVolume (float volume) = 0;
98 	virtual SoundHandle LoadSound(BYTE *sfxdata, int length) = 0;
99 	SoundHandle LoadSoundVoc(BYTE *sfxdata, int length);
100 	virtual SoundHandle LoadSoundRaw(BYTE *sfxdata, int length, int frequency, int channels, int bits, int loopstart, int loopend = -1) = 0;
101 	virtual void UnloadSound (SoundHandle sfx) = 0;	// unloads a sound from memory
102 	virtual unsigned int GetMSLength(SoundHandle sfx) = 0;	// Gets the length of a sound at its default frequency
103 	virtual unsigned int GetSampleLength(SoundHandle sfx) = 0;	// Gets the length of a sound at its default frequency
104 	virtual float GetOutputRate() = 0;
105 
106 	// Streaming sounds.
107 	virtual SoundStream *CreateStream (SoundStreamCallback callback, int buffbytes, int flags, int samplerate, void *userdata) = 0;
108     virtual SoundStream *OpenStream (FileReader *reader, int flags) = 0;
109 	virtual SoundStream *OpenStream (const char *url, int flags);
110 
111 	// Starts a sound.
112 	virtual FISoundChannel *StartSound (SoundHandle sfx, float vol, int pitch, int chanflags, FISoundChannel *reuse_chan) = 0;
113 	virtual FISoundChannel *StartSound3D (SoundHandle sfx, SoundListener *listener, float vol, FRolloffInfo *rolloff, float distscale, int pitch, int priority, const FVector3 &pos, const FVector3 &vel, int channum, int chanflags, FISoundChannel *reuse_chan) = 0;
114 
115 	// Stops a sound channel.
116 	virtual void StopChannel (FISoundChannel *chan) = 0;
117 
118 	// Changes a channel's volume.
119 	virtual void ChannelVolume (FISoundChannel *chan, float volume) = 0;
120 
121 	// Marks a channel's start time without actually playing it.
122 	virtual void MarkStartTime (FISoundChannel *chan) = 0;
123 
124 	// Returns position of sound on this channel, in samples.
125 	virtual unsigned int GetPosition(FISoundChannel *chan) = 0;
126 
127 	// Gets a channel's audibility (real volume).
128 	virtual float GetAudibility(FISoundChannel *chan) = 0;
129 
130 	// Synchronizes following sound startups.
131 	virtual void Sync (bool sync) = 0;
132 
133 	// Pauses or resumes all sound effect channels.
134 	virtual void SetSfxPaused (bool paused, int slot) = 0;
135 
136 	// Pauses or resumes *every* channel, including environmental reverb.
137 	enum EInactiveState
138 	{
139 		INACTIVE_Active,		// sound is active
140 		INACTIVE_Complete,		// sound is completely paused
141 		INACTIVE_Mute			// sound is only muted
142 	};
143 	virtual void SetInactive(EInactiveState inactive) = 0;
144 
145 	// Updates the volume, separation, and pitch of a sound channel.
146 	virtual void UpdateSoundParams3D (SoundListener *listener, FISoundChannel *chan, bool areasound, const FVector3 &pos, const FVector3 &vel) = 0;
147 
148 	virtual void UpdateListener (SoundListener *) = 0;
149 	virtual void UpdateSounds () = 0;
UpdateMusic()150 	virtual void UpdateMusic() {}
151 
152 	virtual bool IsValid () = 0;
153 	virtual void PrintStatus () = 0;
154 	virtual void PrintDriversList () = 0;
155 	virtual FString GatherStats ();
156 	virtual short *DecodeSample(int outlen, const void *coded, int sizebytes, ECodecType type);
157 
158 	virtual void DrawWaveDebug(int mode);
159 
160 protected:
161     virtual SoundDecoder *CreateDecoder(FileReader *reader);
162 };
163 
164 extern SoundRenderer *GSnd;
165 extern bool nosfx;
166 extern bool nosound;
167 
168 void I_InitSound ();
169 void I_ShutdownSound ();
170 
171 void S_ChannelEnded(FISoundChannel *schan);
172 void S_ChannelVirtualChanged(FISoundChannel *schan, bool is_virtual);
173 float S_GetRolloff(FRolloffInfo *rolloff, float distance, bool logarithmic);
174 FISoundChannel *S_GetChannel(void *syschan);
175 
176 extern ReverbContainer *DefaultEnvironments[26];
177 
178 bool IsFModExPresent();
179 bool IsOpenALPresent();
180 
181 #endif
182