]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sound.h
Ogg vorbis streaming support; the code decides whether it will cache or stream the...
[xonotic/darkplaces.git] / sound.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // sound.h -- client sound i/o functions
21
22 #ifndef SOUND_H
23 #define SOUND_H
24
25 #define DEFAULT_SOUND_PACKET_VOLUME 255
26 #define DEFAULT_SOUND_PACKET_ATTENUATION 1.0
27
28 typedef struct
29 {
30         int left;
31         int right;
32 } portable_samplepair_t;
33
34 typedef struct
35 {
36         size_t  length;
37         size_t  offset;
38         qbyte   data[4];        // variable sized
39 } sfxbuffer_t;
40
41 typedef struct
42 {
43         unsigned int    speed;
44         unsigned int    width;
45         unsigned int    channels;
46 } snd_format_t;
47
48 // sfx_t flags
49 #define SFXFLAG_SILENTLYMISSING (1 << 0) // if the sfx is missing and loaded with complain = false
50 #define SFXFLAG_USED                    (1 << 1)
51
52 typedef struct snd_fetcher_s snd_fetcher_t;
53 typedef struct sfx_s
54 {
55         char                            name[MAX_QPATH];
56         mempool_t                       *mempool;
57         unsigned int            flags;                  // cf SFXFLAG_* defines
58         snd_format_t            format;
59         int                                     loopstart;
60         size_t                          total_length;
61         const snd_fetcher_t     *fetcher;
62         void                            *fetcher_data;  // Per-sfx data for the sound fetching functions
63 } sfx_t;
64
65 typedef struct
66 {
67         snd_format_t    format;
68         int                             samples;                // mono samples in buffer
69         int                             samplepos;              // in mono samples
70         unsigned char   *buffer;
71         int                             bufferlength;   // used only by certain drivers
72 } dma_t;
73
74 typedef struct
75 {
76         sfx_t   *sfx;                   // sfx number
77         int             forceloop;              // force looping even if the sound is not looped
78         int             leftvol;                // 0-255 volume
79         int             rightvol;               // 0-255 volume
80         int             end;                    // end time in global paintsamples
81         int     pos;                    // sample position in sfx
82         int             looping;                // where to loop, -1 = no looping
83         int             entnum;                 // to allow overriding a specific sound
84         int             entchannel;
85         vec3_t  origin;                 // origin of sound effect
86         vec_t   dist_mult;              // distance multiplier (attenuation/clipK)
87         int             master_vol;             // 0-255 master volume
88         void    *fetcher_data;  // Per-channel data for the sound fetching function
89 } channel_t;
90
91 typedef const sfxbuffer_t* (*snd_fetcher_getsb_t) (channel_t* ch, unsigned int start, unsigned int nbsamples);
92 typedef void (*snd_fetcher_end_t) (channel_t* ch);
93 struct snd_fetcher_s
94 {
95         snd_fetcher_getsb_t     getsb;
96         snd_fetcher_end_t       end;
97 };
98
99 typedef struct
100 {
101         int             rate;
102         int             width;
103         int             channels;
104         int             loopstart;
105         int             samples;
106         int             dataofs;                // chunk starts this many bytes from file start
107 } wavinfo_t;
108
109 void S_Init (void);
110 void S_Startup (void);
111 void S_Shutdown (void);
112 void S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol,  float attenuation);
113 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation);
114 void S_StopSound (int entnum, int entchannel);
115 void S_StopAllSounds(qboolean clear);
116 void S_ClearBuffer (void);
117 void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up);
118 void S_ExtraUpdate (void);
119
120 sfx_t *S_GetCached(const char *name);
121 sfx_t *S_PrecacheSound (char *sample, int complain);
122 void S_TouchSound (char *sample);
123 void S_ClearUsed (void);
124 void S_PurgeUnused (void);
125 void S_PaintChannels(int endtime);
126 void S_InitPaintChannels (void);
127
128 // picks a channel based on priorities, empty slots, number of channels
129 channel_t *SND_PickChannel(int entnum, int entchannel);
130
131 // spatializes a channel
132 void SND_Spatialize(channel_t *ch, int isstatic);
133
134 // initializes cycling through a DMA buffer and returns information on it
135 qboolean SNDDMA_Init(void);
136
137 // gets the current DMA position
138 int SNDDMA_GetDMAPos(void);
139
140 // shutdown the DMA xfer.
141 void SNDDMA_Shutdown(void);
142
143 extern size_t ResampleSfx (const qbyte *in_data, size_t in_length, const snd_format_t* in_format, qbyte *out_data, const char* sfxname);
144
145 // ====================================================================
146 // User-setable variables
147 // ====================================================================
148
149 // LordHavoc: increased from 128 to 516 (4 for NUM_AMBIENTS)
150 #define MAX_CHANNELS                    516
151 // LordHavoc: increased maximum sound channels from 8 to 128
152 #define MAX_DYNAMIC_CHANNELS    128
153
154
155 extern channel_t channels[MAX_CHANNELS];
156 // 0 to MAX_DYNAMIC_CHANNELS-1  = normal entity sounds
157 // MAX_DYNAMIC_CHANNELS to MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS -1 = water, etc
158 // MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS to total_channels = static sounds
159
160 extern unsigned int total_channels;
161
162 //
163 // Fake dma is a synchronous faking of the DMA progress used for
164 // isolating performance in the renderer.  The fakedma_updates is
165 // number of times S_Update() is called per second.
166 //
167
168 extern qboolean fakedma;
169 extern int fakedma_updates;
170 extern int paintedtime;
171 extern int soundtime;
172 extern vec3_t listener_vieworigin;
173 extern vec3_t listener_viewforward;
174 extern vec3_t listener_viewleft;
175 extern vec3_t listener_viewup;
176 extern volatile dma_t *shm;
177 extern volatile dma_t sn;
178 extern vec_t sound_nominal_clip_dist;
179
180 extern cvar_t loadas8bit;
181 extern cvar_t bgmvolume;
182 extern cvar_t volume;
183 extern cvar_t snd_swapstereo;
184
185 extern cvar_t cdaudioinitialized;
186 extern cvar_t snd_initialized;
187 extern cvar_t snd_streaming;
188
189 extern int snd_blocked;
190
191 void S_LocalSound (char *s);
192 qboolean S_LoadSound (sfx_t *s, int complain);
193 void S_UnloadSound(sfx_t *s);
194
195 void SND_InitScaletable (void);
196 void SNDDMA_Submit(void);
197
198 void S_AmbientOff (void);
199 void S_AmbientOn (void);
200
201 void *S_LockBuffer(void);
202 void S_UnlockBuffer(void);
203
204 // add some data to the tail of the rawsamples queue
205 void S_RawSamples_Enqueue(short *samples, unsigned int length);
206 // read and remove some data from the head of the rawsamples queue
207 void S_RawSamples_Dequeue(int *samples, unsigned int length);
208 // empty the rawsamples queue
209 void S_RawSamples_ClearQueue(void);
210 // returns how much more data the queue wants, or 0 if it is already full enough
211 int S_RawSamples_QueueWantsMore(void);
212
213 // resamples one sound buffer into another, while changing the length
214 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength);
215
216 // returns the rate that the rawsamples system is running at
217 int S_RawSamples_SampleRate(void);
218
219 #endif
220