]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_main.h
fix a warning
[divverent/darkplaces.git] / snd_main.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
21 #ifndef SND_MAIN_H
22 #define SND_MAIN_H
23
24 #include "sound.h"
25
26
27 typedef struct snd_format_s
28 {
29         unsigned int    speed;
30         unsigned short  width;
31         unsigned short  channels;
32 } snd_format_t;
33
34 typedef struct snd_buffer_s
35 {
36         snd_format_t            format;
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
40 } snd_buffer_t;
41
42 typedef struct snd_ringbuffer_s
43 {
44         snd_format_t            format;
45         unsigned char*          ring;
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
51 } snd_ringbuffer_t;
52
53 // sfx_t flags
54 #define SFXFLAG_NONE                    0
55 #define SFXFLAG_FILEMISSING             (1 << 0) // wasn't able to load the associated sound file
56 #define SFXFLAG_SERVERSOUND             (1 << 1) // the sfx is part of the server precache list
57 #define SFXFLAG_STREAMED                (1 << 2) // informative only. You shouldn't need to know that
58 #define SFXFLAG_PERMANENTLOCK   (1 << 3) // can never be freed (ex: used by the client code)
59
60 typedef struct snd_fetcher_s snd_fetcher_t;
61 struct sfx_s
62 {
63         char                            name[MAX_QPATH];
64         sfx_t                           *next;
65         size_t                          memsize;                // total memory used (including sfx_t and fetcher data)
66
67                                                                                 // One lock is automatically granted while the sfx is
68                                                                                 // playing (and removed when stopped). Locks can also be
69         int                                     locks;                  // added by S_PrecacheSound.
70                                                                                 // A SFX with no lock, no SFXFLAG_PERMANENTLOCK, and not precached after a level change is freed
71
72         unsigned int            flags;                  // cf SFXFLAG_* defines
73         unsigned int            loopstart;              // in sample frames. equals total_length if not looped
74         unsigned int            total_length;   // in sample frames
75         const snd_fetcher_t     *fetcher;
76         void                            *fetcher_data;  // Per-sfx data for the sound fetching functions
77
78         float                           volume_mult;    // for replay gain (multiplier to apply)
79         float                           volume_peak;    // for replay gain (highest peak); if set to 0, ReplayGain isn't supported
80 };
81
82 // maximum supported speakers constant
83 #define SND_LISTENERS 8
84
85 typedef struct channel_s
86 {
87         int                     listener_volume [SND_LISTENERS];        // 0-65536 volume per speaker
88         int                             master_vol;             // 0-65536 master volume
89         sfx_t                   *sfx;                   // sfx number
90         unsigned int    flags;                  // cf CHANNELFLAG_* defines
91         int                             pos;                    // sample position in sfx, negative values delay the start of the sound playback
92         int                             entnum;                 // to allow overriding a specific sound
93         int                             entchannel;
94         vec3_t                  origin;                 // origin of sound effect
95         vec_t                   dist_mult;              // distance multiplier (attenuation/clipK)
96         void                    *fetcher_data;  // Per-channel data for the sound fetching function
97         int                             prologic_invert;// whether a sound is played on the surround channels in prologic
98 } channel_t;
99
100 // Sound fetching functions
101 // "start" is both an input and output parameter: it returns the actual start time of the sound buffer
102 typedef const snd_buffer_t* (*snd_fetcher_getsb_t) (void *sfxfetcher, void **chfetcherpointer, unsigned int *start, unsigned int nbsampleframes);
103 typedef void (*snd_fetcher_endsb_t) (void *chfetcherdata);
104 typedef void (*snd_fetcher_free_t) (void *sfxfetcherdata);
105 typedef const snd_format_t* (*snd_fetcher_getfmt_t) (sfx_t* sfx);
106 struct snd_fetcher_s
107 {
108         snd_fetcher_getsb_t             getsb;
109         snd_fetcher_endsb_t             endsb;
110         snd_fetcher_free_t              free;
111         snd_fetcher_getfmt_t    getfmt;
112 };
113
114 extern unsigned int total_channels;
115 extern channel_t channels[MAX_CHANNELS];
116
117 extern snd_ringbuffer_t *snd_renderbuffer;
118 extern qboolean snd_threaded; // enables use of snd_usethreadedmixing, provided that no sound hacks are in effect (like timedemo)
119 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
120
121 extern cvar_t _snd_mixahead;
122 extern cvar_t snd_swapstereo;
123 extern cvar_t snd_streaming;
124
125 #define SND_CHANNELLAYOUT_AUTO          0
126 #define SND_CHANNELLAYOUT_STANDARD      1
127 #define SND_CHANNELLAYOUT_ALSA          2
128 extern cvar_t snd_channellayout;
129
130 extern int snd_blocked;         // counter. When > 0, we stop submitting sound to the audio device
131
132 extern mempool_t *snd_mempool;
133
134 // If simsound is true, the sound card is not initialized and no sound is submitted to it.
135 // More generally, all arch-dependent operations are skipped or emulated.
136 // Used for isolating performance in the renderer.
137 extern qboolean simsound;
138
139
140 #define STREAM_BUFFER_DURATION 0.3f // in seconds
141 #define STREAM_BUFFER_FILL 0.2f // in seconds
142 #define STREAM_BUFFER_SIZE(format_ptr) ((int)ceil (STREAM_BUFFER_DURATION * (format_ptr)->speed) * (format_ptr)->width * (format_ptr)->channels)
143
144 // We work with 1 sec sequences, so this buffer must be able to contain
145 // 1 sec of sound of the highest quality (48 KHz, 16 bit samples, stereo)
146 extern unsigned char resampling_buffer [48000 * 2 * 2];
147
148
149 // ====================================================================
150 //         Architecture-independent functions
151 // ====================================================================
152
153 void S_MixToBuffer(void *stream, unsigned int frames);
154
155 qboolean S_LoadSound (sfx_t *sfx, qboolean complain);
156
157 void S_LockSfx (sfx_t *sfx);
158 void S_UnlockSfx (sfx_t *sfx);
159
160 snd_buffer_t *Snd_CreateSndBuffer (const unsigned char *samples, unsigned int sampleframes, const snd_format_t* in_format, unsigned int sb_speed);
161 qboolean Snd_AppendToSndBuffer (snd_buffer_t* sb, const unsigned char *samples, unsigned int sampleframes, const snd_format_t* format);
162
163 // If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
164 // (if "sampleframes" is 0, the function chooses the size).
165 snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer);
166
167
168 // ====================================================================
169 //         Architecture-dependent functions
170 // ====================================================================
171
172 // Create "snd_renderbuffer" with the proper sound format if the call is successful
173 // May return a suggested format if the requested format isn't available
174 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested);
175
176 // Stop the sound card, delete "snd_renderbuffer" and free its other resources
177 void SndSys_Shutdown (void);
178
179 // Submit the contents of "snd_renderbuffer" to the sound card
180 void SndSys_Submit (void);
181
182 // Returns the number of sample frames consumed since the sound started
183 unsigned int SndSys_GetSoundTime (void);
184
185 // Get the exclusive lock on "snd_renderbuffer"
186 qboolean SndSys_LockRenderBuffer (void);
187
188 // Release the exclusive lock on "snd_renderbuffer"
189 void SndSys_UnlockRenderBuffer (void);
190
191 // if the sound system can generate events, send them
192 void SndSys_SendKeyEvents(void);
193
194 // exported for capturevideo so ogg can see all channels
195 typedef struct portable_samplepair_s
196 {
197         int sample[SND_LISTENERS];
198 } portable_sampleframe_t;
199
200 typedef struct listener_s
201 {
202         int channel_unswapped; // for un-swapping
203         float yawangle;
204         float dotscale;
205         float dotbias;
206         float ambientvolume;
207 }
208 listener_t;
209 typedef struct speakerlayout_s
210 {
211         const char *name;
212         unsigned int channels;
213         listener_t listeners[SND_LISTENERS];
214 }
215 speakerlayout_t;
216
217 #endif