]> git.xonotic.org Git - xonotic/darkplaces.git/blob - sound.h
Added a CHANNELFLAG_LOCALSOUND flag for channels playing a client-side sound, such...
[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         size_t  length;
31         size_t  offset;
32         qbyte   data[4];        // variable sized
33 } sfxbuffer_t;
34
35 typedef struct
36 {
37         unsigned int    speed;
38         unsigned int    width;
39         unsigned int    channels;
40 } snd_format_t;
41
42 // sfx_t flags
43 #define SFXFLAG_NONE                    0
44 #define SFXFLAG_SILENTLYMISSING (1 << 0) // if the sfx is missing and loaded with complain = false
45 #define SFXFLAG_USED                    (1 << 1)
46
47 typedef struct snd_fetcher_s snd_fetcher_t;
48 typedef struct sfx_s
49 {
50         char                            name[MAX_QPATH];
51         mempool_t                       *mempool;
52         unsigned int            flags;                  // cf SFXFLAG_* defines
53         snd_format_t            format;
54         int                                     loopstart;
55         size_t                          total_length;
56         const snd_fetcher_t     *fetcher;
57         void                            *fetcher_data;  // Per-sfx data for the sound fetching functions
58 } sfx_t;
59
60 typedef struct
61 {
62         snd_format_t    format;
63         int                             samples;                // mono samples in buffer
64         int                             samplepos;              // in mono samples
65         unsigned char   *buffer;
66         int                             bufferlength;   // used only by certain drivers
67 } dma_t;
68
69 // channel_t flags
70 #define CHANNELFLAG_NONE                0
71 #define CHANNELFLAG_FORCELOOP   (1 << 0) // force looping even if the sound is not looped
72 #define CHANNELFLAG_LOCALSOUND  (1 << 1) // non-game sound (ex: menu sound)
73 #define CHANNELFLAG_PAUSED              (1 << 2)
74
75 typedef struct
76 {
77         sfx_t                   *sfx;                   // sfx number
78         unsigned int    flags;                  // cf CHANNELFLAG_* defines
79         int                             leftvol;                // 0-255 volume
80         int                             rightvol;               // 0-255 volume
81         int                             end;                    // end time in global paintsamples
82         int                             lastptime;              // last time this channel was painted
83         int                             pos;                    // sample position in sfx
84         int                             looping;                // where to loop, -1 = no looping
85         int                             entnum;                 // to allow overriding a specific sound
86         int                             entchannel;
87         vec3_t                  origin;                 // origin of sound effect
88         vec_t                   dist_mult;              // distance multiplier (attenuation/clipK)
89         int                             master_vol;             // 0-255 master volume
90         void                    *fetcher_data;  // Per-channel data for the sound fetching function
91 } channel_t;
92
93 typedef const sfxbuffer_t* (*snd_fetcher_getsb_t) (channel_t* ch, unsigned int start, unsigned int nbsamples);
94 typedef void (*snd_fetcher_end_t) (channel_t* ch);
95 struct snd_fetcher_s
96 {
97         snd_fetcher_getsb_t     getsb;
98         snd_fetcher_end_t       end;
99 };
100
101 void S_Init (void);
102 void S_Startup (void);
103 void S_Shutdown (void);
104 int S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol,  float attenuation);
105 void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation);
106 void S_StopSound (int entnum, int entchannel);
107 void S_StopAllSounds(qboolean clear);
108 void S_PauseGameSounds (void);
109 void S_ResumeGameSounds (void);
110 void S_ClearBuffer (void);
111 void S_Update(vec3_t origin, vec3_t forward, vec3_t left, vec3_t up);
112 void S_ExtraUpdate (void);
113
114 sfx_t *S_GetCached(const char *name);
115 sfx_t *S_PrecacheSound (char *sample, int complain);
116 void S_TouchSound (char *sample);
117 void S_ClearUsed (void);
118 void S_PurgeUnused (void);
119 void S_PaintChannels(int endtime);
120 void S_InitPaintChannels (void);
121
122 // initializes cycling through a DMA buffer and returns information on it
123 qboolean SNDDMA_Init(void);
124
125 // gets the current DMA position
126 int SNDDMA_GetDMAPos(void);
127
128 // shutdown the DMA xfer.
129 void SNDDMA_Shutdown(void);
130
131 extern size_t ResampleSfx (const qbyte *in_data, size_t in_length, const snd_format_t* in_format, qbyte *out_data, const char* sfxname);
132
133 // ====================================================================
134 // User-setable variables
135 // ====================================================================
136
137 // LordHavoc: increased from 128 to 516 (4 for NUM_AMBIENTS)
138 #define MAX_CHANNELS                    516
139 // LordHavoc: increased maximum sound channels from 8 to 128
140 #define MAX_DYNAMIC_CHANNELS    128
141
142
143 extern channel_t channels[MAX_CHANNELS];
144 // 0 to MAX_DYNAMIC_CHANNELS-1  = normal entity sounds
145 // MAX_DYNAMIC_CHANNELS to MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS -1 = water, etc
146 // MAX_DYNAMIC_CHANNELS + NUM_AMBIENTS to total_channels = static sounds
147
148 extern unsigned int total_channels;
149
150 //
151 // Fake dma is a synchronous faking of the DMA progress used for
152 // isolating performance in the renderer.  The fakedma_updates is
153 // number of times S_Update() is called per second.
154 //
155
156 extern qboolean fakedma;
157 extern int fakedma_updates;
158 extern int paintedtime;
159 extern int soundtime;
160 extern vec3_t listener_vieworigin;
161 extern vec3_t listener_viewforward;
162 extern vec3_t listener_viewleft;
163 extern vec3_t listener_viewup;
164 extern volatile dma_t *shm;
165 extern volatile dma_t sn;
166 extern vec_t sound_nominal_clip_dist;
167
168 extern cvar_t loadas8bit;
169 extern cvar_t bgmvolume;
170 extern cvar_t volume;
171 extern cvar_t snd_swapstereo;
172
173 extern cvar_t cdaudioinitialized;
174 extern cvar_t snd_initialized;
175 extern cvar_t snd_streaming;
176
177 extern int snd_blocked;
178
179 void S_LocalSound (char *s);
180 qboolean S_LoadSound (sfx_t *s, int complain);
181 void S_UnloadSound(sfx_t *s);
182
183 void SND_InitScaletable (void);
184 void SNDDMA_Submit(void);
185
186 void S_AmbientOff (void);
187 void S_AmbientOn (void);
188
189 void *S_LockBuffer(void);
190 void S_UnlockBuffer(void);
191
192 // add some data to the tail of the rawsamples queue
193 void S_RawSamples_Enqueue(short *samples, unsigned int length);
194 // read and remove some data from the head of the rawsamples queue
195 void S_RawSamples_Dequeue(int *samples, unsigned int length);
196 // empty the rawsamples queue
197 void S_RawSamples_ClearQueue(void);
198 // returns how much more data the queue wants, or 0 if it is already full enough
199 int S_RawSamples_QueueWantsMore(void);
200
201 // resamples one sound buffer into another, while changing the length
202 void S_ResampleBuffer16Stereo(short *input, int inputlength, short *output, int outputlength);
203
204 // returns the rate that the rawsamples system is running at
205 int S_RawSamples_SampleRate(void);
206
207 #endif
208