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 unsigned int flags; // cf SFXFLAG_* defines
68 unsigned int loopstart; // in sample frames. equals total_length if not looped
69 unsigned int total_length; // in sample frames
70 const snd_fetcher_t *fetcher;
71 void *fetcher_data; // Per-sfx data for the sound fetching functions
73 float volume_mult; // for replay gain (multiplier to apply)
74 float volume_peak; // for replay gain (highest peak); if set to 0, ReplayGain isn't supported
77 // maximum supported speakers constant
78 #define SND_LISTENERS 8
80 typedef struct channel_s
82 int listener_volume [SND_LISTENERS]; // 0-65536 volume per speaker
83 int master_vol; // 0-65536 master volume
84 sfx_t *sfx; // pointer to sound sample being used
85 unsigned int flags; // cf CHANNELFLAG_* defines
86 int pos; // sample position in sfx, negative values delay the start of the sound playback
87 int entnum; // to allow overriding a specific sound
89 vec3_t origin; // origin of sound effect
90 vec_t dist_mult; // 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
95 // Sound fetching functions
96 // "start" is both an input and output parameter: it returns the actual start time of the sound buffer
97 typedef const snd_buffer_t* (*snd_fetcher_getsb_t) (void *sfxfetcher, void **chfetcherpointer, unsigned int *start, unsigned int nbsampleframes);
98 typedef void (*snd_fetcher_endsb_t) (void *chfetcherdata);
99 typedef void (*snd_fetcher_free_t) (void *sfxfetcherdata);
100 typedef const snd_format_t* (*snd_fetcher_getfmt_t) (sfx_t* sfx);
103 snd_fetcher_getsb_t getsb;
104 snd_fetcher_endsb_t endsb;
105 snd_fetcher_free_t free;
106 snd_fetcher_getfmt_t getfmt;
109 extern unsigned int total_channels;
110 extern channel_t channels[MAX_CHANNELS];
112 extern snd_ringbuffer_t *snd_renderbuffer;
113 extern qboolean snd_threaded; // enables use of snd_usethreadedmixing, provided that no sound hacks are in effect (like timedemo)
114 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
116 extern cvar_t _snd_mixahead;
117 extern cvar_t snd_swapstereo;
118 extern cvar_t snd_streaming;
120 #define SND_CHANNELLAYOUT_AUTO 0
121 #define SND_CHANNELLAYOUT_STANDARD 1
122 #define SND_CHANNELLAYOUT_ALSA 2
123 extern cvar_t snd_channellayout;
125 extern int snd_blocked; // counter. When > 0, we stop submitting sound to the audio device
127 extern mempool_t *snd_mempool;
129 // If simsound is true, the sound card is not initialized and no sound is submitted to it.
130 // More generally, all arch-dependent operations are skipped or emulated.
131 // Used for isolating performance in the renderer.
132 extern qboolean simsound;
135 #define STREAM_BUFFER_DURATION 0.3f // in seconds
136 #define STREAM_BUFFER_FILL 0.2f // in seconds
137 #define STREAM_BUFFER_SIZE(format_ptr) ((int)ceil (STREAM_BUFFER_DURATION * (format_ptr)->speed) * (format_ptr)->width * (format_ptr)->channels)
139 // We work with 1 sec sequences, so this buffer must be able to contain
140 // 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
141 extern unsigned char resampling_buffer [48000 * 2 * 2];
144 // ====================================================================
145 // Architecture-independent functions
146 // ====================================================================
148 void S_MixToBuffer(void *stream, unsigned int frames);
150 qboolean S_LoadSound (sfx_t *sfx, qboolean complain);
152 snd_buffer_t *Snd_CreateSndBuffer (const unsigned char *samples, unsigned int sampleframes, const snd_format_t* in_format, unsigned int sb_speed);
153 qboolean Snd_AppendToSndBuffer (snd_buffer_t* sb, const unsigned char *samples, unsigned int sampleframes, const snd_format_t* format);
155 // If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
156 // (if "sampleframes" is 0, the function chooses the size).
157 snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer);
160 // ====================================================================
161 // Architecture-dependent functions
162 // ====================================================================
164 // Create "snd_renderbuffer" with the proper sound format if the call is successful
165 // May return a suggested format if the requested format isn't available
166 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested);
168 // Stop the sound card, delete "snd_renderbuffer" and free its other resources
169 void SndSys_Shutdown (void);
171 // Submit the contents of "snd_renderbuffer" to the sound card
172 void SndSys_Submit (void);
174 // Returns the number of sample frames consumed since the sound started
175 unsigned int SndSys_GetSoundTime (void);
177 // Get the exclusive lock on "snd_renderbuffer"
178 qboolean SndSys_LockRenderBuffer (void);
180 // Release the exclusive lock on "snd_renderbuffer"
181 void SndSys_UnlockRenderBuffer (void);
183 // if the sound system can generate events, send them
184 void SndSys_SendKeyEvents(void);
186 // exported for capturevideo so ogg can see all channels
187 typedef struct portable_samplepair_s
189 int sample[SND_LISTENERS];
190 } portable_sampleframe_t;
192 typedef struct listener_s
194 int channel_unswapped; // for un-swapping
201 typedef struct speakerlayout_s
204 unsigned int channels;
205 listener_t listeners[SND_LISTENERS];