2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
27 typedef struct snd_format_s
31 unsigned short channels;
34 typedef struct snd_buffer_s
37 unsigned int nbframes; // current size, in sample frames
38 unsigned int maxframes; // max size (buffer size), in sample frames
39 unsigned char samples[4]; // variable sized
42 typedef struct snd_ringbuffer_s
46 unsigned int maxframes; // max size (buffer size), in sample frames
47 unsigned int startframe; // index of the first frame in the buffer
48 // if startframe == endframe, the bufffer is empty
49 unsigned int endframe; // index of the first EMPTY frame in the "ring" buffer
50 // may be smaller than startframe if the "ring" buffer has wrapped
54 #define SFXFLAG_NONE 0
55 #define SFXFLAG_FILEMISSING (1 << 0) // wasn't able to load the associated sound file
56 #define SFXFLAG_LEVELSOUND (1 << 1) // the sfx is part of the server or client precache list for this level
57 #define SFXFLAG_STREAMED (1 << 2) // informative only. You shouldn't need to know that
58 #define SFXFLAG_MENUSOUND (1 << 3) // not freed during level change (menu sounds, music, etc)
60 typedef struct snd_fetcher_s snd_fetcher_t;
65 size_t memsize; // total memory used (including sfx_t and fetcher data)
67 snd_format_t format; // format describing the audio data that fetcher->getsamplesfloat will return
68 unsigned int flags; // cf SFXFLAG_* defines
69 unsigned int loopstart; // in sample frames. equals total_length if not looped
70 unsigned int total_length; // in sample frames
71 const snd_fetcher_t *fetcher;
72 void *fetcher_data; // Per-sfx data for the sound fetching functions
74 float volume_mult; // for replay gain (multiplier to apply)
75 float volume_peak; // for replay gain (highest peak); if set to 0, ReplayGain isn't supported
78 // maximum supported speakers constant
79 #define SND_LISTENERS 8
81 typedef struct channel_s
83 // provided sound information
84 sfx_t *sfx; // pointer to sound sample being used
85 float basevolume; // 0-1 master volume
86 unsigned int flags; // cf CHANNELFLAG_* defines
87 int entnum; // makes sound follow entity origin (allows replacing interrupting existing sound on same id)
88 int entchannel; // which channel id on the entity
89 vec3_t origin; // origin of sound effect
90 vec_t distfade; // distance multiplier (attenuation/clipK)
91 void *fetcher_data; // Per-channel data for the sound fetching function
92 int prologic_invert;// whether a sound is played on the surround channels in prologic
93 float basespeed; // playback rate multiplier for pitch variation
95 // these are often updated while mixer is running, glitching should be minimized (mismatched channel volumes from spatialization is okay)
96 // spatialized playback speed (speed * doppler ratio)
98 // spatialized volume per speaker (mastervol * distanceattenuation * channelvolume cvars)
99 float volume[SND_LISTENERS];
101 // updated ONLY by mixer
102 // position in sfx, starts at 0, loops or stops at sfx->total_length
106 // Sound fetching functions
107 // "start" is both an input and output parameter: it returns the actual start time of the sound buffer
108 typedef void (*snd_fetcher_getsamplesfloat_t) (channel_t *ch, sfx_t *sfx, int firstsampleframe, int numsampleframes, float *outsamplesfloat);
109 typedef void (*snd_fetcher_stopchannel_t) (channel_t *ch);
110 typedef void (*snd_fetcher_freesfx_t) (sfx_t *sfx);
113 snd_fetcher_getsamplesfloat_t getsamplesfloat;
114 snd_fetcher_stopchannel_t stopchannel;
115 snd_fetcher_freesfx_t freesfx;
118 extern unsigned int total_channels;
119 extern channel_t channels[MAX_CHANNELS];
121 extern snd_ringbuffer_t *snd_renderbuffer;
122 extern qboolean snd_threaded; // enables use of snd_usethreadedmixing, provided that no sound hacks are in effect (like timedemo)
123 extern qboolean snd_usethreadedmixing; // if true, the main thread does not mix sound, soundtime does not advance, and neither does snd_renderbuffer->endframe, instead the audio thread will call S_MixToBuffer as needed
125 extern cvar_t _snd_mixahead;
126 extern cvar_t snd_swapstereo;
127 extern cvar_t snd_streaming;
128 extern cvar_t snd_streaming_length;
130 #define SND_CHANNELLAYOUT_AUTO 0
131 #define SND_CHANNELLAYOUT_STANDARD 1
132 #define SND_CHANNELLAYOUT_ALSA 2
133 extern cvar_t snd_channellayout;
135 extern int snd_blocked; // counter. When > 0, we stop submitting sound to the audio device
137 extern mempool_t *snd_mempool;
139 // If simsound is true, the sound card is not initialized and no sound is submitted to it.
140 // More generally, all arch-dependent operations are skipped or emulated.
141 // Used for isolating performance in the renderer.
142 extern qboolean simsound;
145 #define STREAM_BUFFERSIZE 16384 // in sampleframes
148 // ====================================================================
149 // Architecture-independent functions
150 // ====================================================================
152 void S_MixToBuffer(void *stream, unsigned int frames);
154 qboolean S_LoadSound (sfx_t *sfx, qboolean complain);
156 // If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
157 // (if "sampleframes" is 0, the function chooses the size).
158 snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer);
161 // ====================================================================
162 // Architecture-dependent functions
163 // ====================================================================
165 // Create "snd_renderbuffer", attempting to use the chosen sound format, but accepting if the driver wants to change it (e.g. 7.1 to stereo or lowering the speed)
166 // Note: SDL automatically converts all formats, so this only fails if there is no audio
167 qboolean SndSys_Init (snd_format_t* fmt);
169 // Stop the sound card, delete "snd_renderbuffer" and free its other resources
170 void SndSys_Shutdown (void);
172 // Submit the contents of "snd_renderbuffer" to the sound card
173 void SndSys_Submit (void);
175 // Returns the number of sample frames consumed since the sound started
176 unsigned int SndSys_GetSoundTime (void);
178 // Get the exclusive lock on "snd_renderbuffer"
179 qboolean SndSys_LockRenderBuffer (void);
181 // Release the exclusive lock on "snd_renderbuffer"
182 void SndSys_UnlockRenderBuffer (void);
184 // if the sound system can generate events, send them
185 void SndSys_SendKeyEvents(void);
187 // exported for capturevideo so ogg can see all channels
188 typedef struct portable_samplepair_s
190 float sample[SND_LISTENERS];
191 } portable_sampleframe_t;
193 typedef struct listener_s
195 int channel_unswapped; // for un-swapping
202 typedef struct speakerlayout_s
205 unsigned int channels;
206 listener_t listeners[SND_LISTENERS];