]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_main.h
improved r_texturestats output to count compressed textures as only 1
[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 and S_ServerSounds.
70                                                                                 // A SFX with no lock and no SFXFLAG_PERMANENTLOCK is
71                                                                                 // freed at level change by S_ServerSounds.
72
73         unsigned int            flags;                  // cf SFXFLAG_* defines
74         unsigned int            loopstart;              // in sample frames. equals total_length if not looped
75         unsigned int            total_length;   // in sample frames
76         const snd_fetcher_t     *fetcher;
77         void                            *fetcher_data;  // Per-sfx data for the sound fetching functions
78 };
79
80 // maximum supported speakers constant
81 #define SND_LISTENERS 8
82
83 typedef struct channel_s
84 {
85         short                   listener_volume [SND_LISTENERS];        // 0-255 volume per speaker
86         int                             master_vol;             // 0-255 master volume
87         sfx_t                   *sfx;                   // sfx number
88         unsigned int    flags;                  // cf CHANNELFLAG_* defines
89         int                             pos;                    // sample position in sfx, negative values delay the start of the sound playback
90         int                             entnum;                 // to allow overriding a specific sound
91         int                             entchannel;
92         vec3_t                  origin;                 // origin of sound effect
93         vec_t                   dist_mult;              // distance multiplier (attenuation/clipK)
94         void                    *fetcher_data;  // Per-channel data for the sound fetching function
95 } channel_t;
96
97 // Sound fetching functions
98 // "start" is both an input and output parameter: it returns the actual start time of the sound buffer
99 typedef const snd_buffer_t* (*snd_fetcher_getsb_t) (channel_t* ch, unsigned int *start, unsigned int nbsampleframes);
100 typedef void (*snd_fetcher_endsb_t) (channel_t* ch);
101 typedef void (*snd_fetcher_free_t) (sfx_t* sfx);
102 typedef const snd_format_t* (*snd_fetcher_getfmt_t) (sfx_t* sfx);
103 struct snd_fetcher_s
104 {
105         snd_fetcher_getsb_t             getsb;
106         snd_fetcher_endsb_t             endsb;
107         snd_fetcher_free_t              free;
108         snd_fetcher_getfmt_t    getfmt;
109 };
110
111 // 0 to NUM_AMBIENTS - 1 = water, etc
112 // NUM_AMBIENTS to NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS - 1 = normal entity sounds
113 // NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS to total_channels = static sounds
114 #define MAX_DYNAMIC_CHANNELS    512
115 #define MAX_CHANNELS                    1028
116
117 extern unsigned int total_channels;
118 extern channel_t channels[MAX_CHANNELS];
119
120 extern snd_ringbuffer_t *snd_renderbuffer;
121 extern unsigned int soundtime;  // WARNING: sound modules must NOT use it
122
123 extern cvar_t _snd_mixahead;
124 extern cvar_t snd_swapstereo;
125 extern cvar_t snd_streaming;
126
127 #define SND_CHANNELLAYOUT_AUTO          0
128 #define SND_CHANNELLAYOUT_STANDARD      1
129 #define SND_CHANNELLAYOUT_ALSA          2
130 extern cvar_t snd_channellayout;
131
132
133 extern int snd_blocked;         // counter. When > 0, we stop submitting sound to the audio device
134
135 extern mempool_t *snd_mempool;
136
137 // If simsound is true, the sound card is not initialized and no sound is submitted to it.
138 // More generally, all arch-dependent operations are skipped or emulated.
139 // Used for isolating performance in the renderer.
140 extern qboolean simsound;
141
142
143 // ====================================================================
144 //         Architecture-independent functions
145 // ====================================================================
146
147 void S_PaintChannels (snd_ringbuffer_t* rb, unsigned int starttime, unsigned int endtime);
148
149 qboolean S_LoadSound (sfx_t *sfx, qboolean complain);
150
151 void S_LockSfx (sfx_t *sfx);
152 void S_UnlockSfx (sfx_t *sfx);
153
154 snd_buffer_t *Snd_CreateSndBuffer (const unsigned char *samples, unsigned int sampleframes, const snd_format_t* in_format, unsigned int sb_speed);
155 qboolean Snd_AppendToSndBuffer (snd_buffer_t* sb, const unsigned char *samples, unsigned int sampleframes, const snd_format_t* format);
156
157 // If "buffer" is NULL, the function allocates one buffer of "sampleframes" sample frames itself
158 // (if "sampleframes" is 0, the function chooses the size).
159 snd_ringbuffer_t *Snd_CreateRingBuffer (const snd_format_t* format, unsigned int sampleframes, void* buffer);
160
161
162 // ====================================================================
163 //         Architecture-dependent functions
164 // ====================================================================
165
166 // Create "snd_renderbuffer" with the proper sound format if the call is successful
167 // May return a suggested format if the requested format isn't available
168 qboolean SndSys_Init (const snd_format_t* requested, snd_format_t* suggested);
169
170 // Stop the sound card, delete "snd_renderbuffer" and free its other resources
171 void SndSys_Shutdown (void);
172
173 // Submit the contents of "snd_renderbuffer" to the sound card
174 void SndSys_Submit (void);
175
176 // Returns the number of sample frames consumed since the sound started
177 unsigned int SndSys_GetSoundTime (void);
178
179 // Get the exclusive lock on "snd_renderbuffer"
180 qboolean SndSys_LockRenderBuffer (void);
181
182 // Release the exclusive lock on "snd_renderbuffer"
183 void SndSys_UnlockRenderBuffer (void);
184
185
186 #endif