implemented 7.1 audio, only works with SDL (attempted ALSA support but ALSA doesn...
[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 sfxbuffer_s
28 {
29         unsigned int    length;
30         unsigned int    offset;
31         unsigned char   data[4];        // variable sized
32 } sfxbuffer_t;
33
34 typedef struct snd_format_s
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 #define SFXFLAG_PERMANENTLOCK   (1 << 3) // can never be freed (ex: used by the client code)
47
48 typedef struct snd_fetcher_s snd_fetcher_t;
49 struct sfx_s
50 {
51         char                            name[MAX_QPATH];
52         sfx_t                           *next;
53         size_t                          memsize;                // total memory used (including sfx_t and fetcher data)
54         int                                     locks;                  // One lock is automatically granted while the sfx is
55                                                                                 // playing (and removed when stopped). Locks can also be
56                                                                                 // added by S_PrecacheSound and S_ServerSounds.
57                                                                                 // A SFX with no lock and no SFXFLAG_PERMANENTLOCK is
58                                                                                 // freed at level change by S_ServerSounds.
59         unsigned int            flags;                  // cf SFXFLAG_* defines
60         snd_format_t            format;
61         int                                     loopstart;
62         unsigned int            total_length;
63         const snd_fetcher_t     *fetcher;
64         void                            *fetcher_data;  // Per-sfx data for the sound fetching functions
65 };
66
67 typedef struct dma_s
68 {
69         snd_format_t    format;
70         int                             sampleframes;   // frames in buffer (frame = samples for all speakers)
71         int                             samples;                // mono samples in buffer
72         int                             samplepos;              // in mono samples
73         unsigned char   *buffer;
74         int                             bufferlength;   // used only by certain drivers
75 } dma_t;
76
77 // maximum supported speakers constant
78 #define SND_LISTENERS 8
79
80 typedef struct channel_s
81 {
82         int pad[8];
83         sfx_t                   *sfx;                   // sfx number
84         int pad2[8];
85         unsigned int    flags;                  // cf CHANNELFLAG_* defines
86         int                             master_vol;             // 0-255 master volume
87         short                   listener_volume[SND_LISTENERS];         // 0-255 volume per speaker
88         int                             end;                    // end time in global paintsamples
89         int                             lastptime;              // last time this channel was painted
90         int                             pos;                    // sample position in sfx
91         int                             entnum;                 // to allow overriding a specific sound
92         int                             entchannel;
93         vec3_t                  origin;                 // origin of sound effect
94         vec_t                   dist_mult;              // distance multiplier (attenuation/clipK)
95         void                    *fetcher_data;  // Per-channel data for the sound fetching function
96 } channel_t;
97
98 typedef const sfxbuffer_t* (*snd_fetcher_getsb_t) (channel_t* ch, unsigned int start, unsigned int nbsamples);
99 typedef void (*snd_fetcher_endsb_t) (channel_t* ch);
100 typedef void (*snd_fetcher_free_t) (sfx_t* sfx);
101 struct snd_fetcher_s
102 {
103         snd_fetcher_getsb_t             getsb;
104         snd_fetcher_endsb_t             endsb;
105         snd_fetcher_free_t              free;
106 };
107
108 void S_PaintChannels(int endtime);
109
110 // initializes cycling through a DMA buffer and returns information on it
111 qboolean SNDDMA_Init(void);
112
113 // gets the current DMA position
114 int SNDDMA_GetDMAPos(void);
115
116 void SNDDMA_Submit(void);
117
118 // shutdown the DMA xfer.
119 void SNDDMA_Shutdown(void);
120
121 qboolean S_LoadSound (sfx_t *s, qboolean complain);
122
123 void S_LockSfx (sfx_t *sfx);
124 void S_UnlockSfx (sfx_t *sfx);
125
126 void *S_LockBuffer(void);
127 void S_UnlockBuffer(void);
128
129 extern size_t ResampleSfx (const unsigned char *in_data, size_t in_length, const snd_format_t* in_format, unsigned char *out_data, const char* sfxname);
130
131 // ====================================================================
132
133 // 0 to NUM_AMBIENTS - 1 = water, etc
134 // NUM_AMBIENTS to NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS - 1 = normal entity sounds
135 // NUM_AMBIENTS + MAX_DYNAMIC_CHANNELS to total_channels = static sounds
136 #define MAX_CHANNELS                    516
137 #define MAX_DYNAMIC_CHANNELS    128
138
139 extern channel_t channels[MAX_CHANNELS];
140
141 extern unsigned int total_channels;
142
143 extern int paintedtime;
144 extern int soundtime;
145 extern volatile dma_t *shm;
146
147 extern cvar_t snd_swapstereo;
148 extern cvar_t snd_streaming;
149
150 extern int snd_blocked;
151
152 extern mempool_t *snd_mempool;
153
154
155 #endif