Lots of minor fixes and improvements to the sound engine, plus a few more important...
[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
28 {
29         size_t  length;
30         size_t  offset;
31         qbyte   data[4];        // variable sized
32 } sfxbuffer_t;
33
34 typedef struct
35 {
36         unsigned int    speed;
37         unsigned int    width;
38         unsigned int    channels;
39 } snd_format_t;
40
41 // sfx_t flags
42 #define SFXFLAG_NONE                    0
43 #define SFXFLAG_FILEMISSING             (1 << 0) // wasn't able to load the associated sound file
44 #define SFXFLAG_SERVERSOUND             (1 << 1) // the sfx is part of the server precache list
45 #define SFXFLAG_STREAMED                (1 << 2) // informative only. You shouldn't need to know that
46
47 typedef struct snd_fetcher_s snd_fetcher_t;
48 struct sfx_s
49 {
50         char                            name[MAX_QPATH];
51         sfx_t                           *next;
52         mempool_t                       *mempool;
53         unsigned int            locks;                  // A locked sfx_t must not be freed.
54                                                                                 // Locks are added by S_PrecacheSound and S_ServerSounds.
55                                                                                 // SFX can be freed by S_UnlockSfx or S_ServerSounds.
56         unsigned int            flags;                  // cf SFXFLAG_* defines
57         snd_format_t            format;
58         int                                     loopstart;
59         size_t                          total_length;
60         const snd_fetcher_t     *fetcher;
61         void                            *fetcher_data;  // Per-sfx data for the sound fetching functions
62 };
63
64 typedef struct
65 {
66         snd_format_t    format;
67         int                             samples;                // mono samples in buffer
68         int                             samplepos;              // in mono samples
69         unsigned char   *buffer;
70         int                             bufferlength;   // used only by certain drivers
71 } dma_t;
72
73 typedef struct
74 {
75         sfx_t                   *sfx;                   // sfx number
76         unsigned int    flags;                  // cf CHANNELFLAG_* defines
77         int                             master_vol;             // 0-255 master volume
78         int                             leftvol;                // 0-255 volume
79         int                             rightvol;               // 0-255 volume
80         int                             end;                    // end time in global paintsamples
81         int                             lastptime;              // last time this channel was painted
82         int                             pos;                    // sample position in sfx
83         int                             entnum;                 // to allow overriding a specific sound
84         int                             entchannel;
85         vec3_t                  origin;                 // origin of sound effect
86         vec_t                   dist_mult;              // distance multiplier (attenuation/clipK)
87         void                    *fetcher_data;  // Per-channel data for the sound fetching function
88 } channel_t;
89
90 typedef const sfxbuffer_t* (*snd_fetcher_getsb_t) (channel_t* ch, unsigned int start, unsigned int nbsamples);
91 typedef void (*snd_fetcher_end_t) (channel_t* ch);
92 struct snd_fetcher_s
93 {
94         snd_fetcher_getsb_t             getsb;
95         snd_fetcher_end_t               end;
96 };
97
98 void S_PaintChannels(int endtime);
99
100 // initializes cycling through a DMA buffer and returns information on it
101 qboolean SNDDMA_Init(void);
102
103 // gets the current DMA position
104 int SNDDMA_GetDMAPos(void);
105
106 void SNDDMA_Submit(void);
107
108 // shutdown the DMA xfer.
109 void SNDDMA_Shutdown(void);
110
111 qboolean S_LoadSound (sfx_t *s, qboolean complain);
112 void S_UnloadSound(sfx_t *s);
113
114 void *S_LockBuffer(void);
115 void S_UnlockBuffer(void);
116
117 extern size_t ResampleSfx (const qbyte *in_data, size_t in_length, const snd_format_t* in_format, qbyte *out_data, const char* sfxname);
118
119 // ====================================================================
120 // User-setable variables
121 // ====================================================================
122
123 // 0 to NUM_AMBIENTS - 1 = water, etc
124 // NUM_AMBIENTS to NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS - 1 = normal entity sounds
125 // NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS to total_channels = static sounds
126 #define MAX_CHANNELS                    516
127 #define MAX_DYNAMIC_CHANNELS    128
128
129 extern channel_t channels[MAX_CHANNELS];
130
131 extern unsigned int total_channels;
132
133 extern int paintedtime;
134 extern int soundtime;
135 extern volatile dma_t *shm;
136
137 extern cvar_t snd_swapstereo;
138 extern cvar_t snd_streaming;
139
140 extern int snd_blocked;
141
142
143 #endif