]> icculus.org git repositories - divverent/darkplaces.git/blob - snd_mem.c
cleaned up scoreboard printing loops a bit (they don't need to check if the name...
[divverent/darkplaces.git] / snd_mem.c
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
22 #include "quakedef.h"
23
24 #include "snd_main.h"
25 #include "snd_ogg.h"
26 #include "snd_wav.h"
27
28
29 /*
30 ================
31 ResampleSfx
32 ================
33 */
34 size_t ResampleSfx (const qbyte *in_data, size_t in_length, const snd_format_t* in_format, qbyte *out_data, const char* sfxname)
35 {
36         size_t srclength, outcount, i;
37
38         srclength = in_length * in_format->channels;
39         outcount = (double)in_length * shm->format.speed / in_format->speed;
40
41         Con_DPrintf("ResampleSfx(%s): %d samples @ %dHz -> %d samples @ %dHz\n",
42                                 sfxname, in_length, in_format->speed, outcount, shm->format.speed);
43
44         // Trivial case (direct transfer)
45         if (in_format->speed == shm->format.speed)
46         {
47                 if (in_format->width == 1)
48                 {
49                         for (i = 0; i < srclength; i++)
50                                 ((signed char*)out_data)[i] = in_data[i] - 128;
51                 }
52                 else  // if (in_format->width == 2)
53                         memcpy (out_data, in_data, srclength * in_format->width);
54         }
55
56         // General case (linear interpolation with a fixed-point fractional
57         // step, 18-bit integer part and 14-bit fractional part)
58         // Can handle up to 2^18 (262144) samples per second (> 96KHz stereo)
59         #define FRACTIONAL_BITS 14
60         #define FRACTIONAL_MASK ((1 << FRACTIONAL_BITS) - 1)
61         #define INTEGER_BITS (sizeof(samplefrac)*8 - FRACTIONAL_BITS)
62         else
63         {
64                 const unsigned int fracstep = (double)in_format->speed / shm->format.speed * (1 << FRACTIONAL_BITS);
65                 size_t remain_in = srclength, total_out = 0;
66                 unsigned int samplefrac;
67                 const qbyte *in_ptr = in_data;
68                 qbyte *out_ptr = out_data;
69
70                 // Check that we can handle one second of that sound
71                 if (in_format->speed * in_format->channels > (1 << INTEGER_BITS))
72                         Sys_Error ("ResampleSfx: sound quality too high for resampling (%uHz, %u channel(s))",
73                                            in_format->speed, in_format->channels);
74
75                 // We work 1 sec at a time to make sure we don't accumulate any
76                 // significant error when adding "fracstep" over several seconds, and
77                 // also to be able to handle very long sounds.
78                 while (total_out < outcount)
79                 {
80                         size_t tmpcount;
81
82                         samplefrac = 0;
83
84                         // If more than 1 sec of sound remains to be converted
85                         if (outcount - total_out > shm->format.speed)
86                                 tmpcount = shm->format.speed;
87                         else
88                                 tmpcount = outcount - total_out;
89
90                         // Convert up to 1 sec of sound
91                         for (i = 0; i < tmpcount; i++)
92                         {
93                                 unsigned int j = 0;
94                                 unsigned int srcsample = (samplefrac >> FRACTIONAL_BITS) * in_format->channels;
95                                 int a, b;
96
97                                 // 16 bit samples
98                                 if (in_format->width == 2)
99                                 {
100                                         for (j = 0; j < in_format->channels; j++, srcsample++)
101                                         {
102                                                 // No value to interpolate with?
103                                                 if (srcsample + in_format->channels < remain_in)
104                                                 {
105                                                         a = ((const short*)in_ptr)[srcsample];
106                                                         b = ((const short*)in_ptr)[srcsample + in_format->channels];
107                                                         *((short*)out_ptr) = (((b - a) * (samplefrac & FRACTIONAL_MASK)) >> FRACTIONAL_BITS) + a;
108                                                 }
109                                                 else
110                                                         *((short*)out_ptr) = ((const short*)in_ptr)[srcsample];
111
112                                                 out_ptr += sizeof (short);
113                                         }
114                                 }
115                                 // 8 bit samples
116                                 else  // if (in_format->width == 1)
117                                 {
118                                         for (j = 0; j < in_format->channels; j++, srcsample++)
119                                         {
120                                                 // No more value to interpolate with?
121                                                 if (srcsample + in_format->channels < remain_in)
122                                                 {
123                                                         a = ((const qbyte*)in_ptr)[srcsample] - 128;
124                                                         b = ((const qbyte*)in_ptr)[srcsample + in_format->channels] - 128;
125                                                         *((signed char*)out_ptr) = (((b - a) * (samplefrac & FRACTIONAL_MASK)) >> FRACTIONAL_BITS) + a;
126                                                 }
127                                                 else
128                                                         *((signed char*)out_ptr) = ((const qbyte*)in_ptr)[srcsample] - 128;
129
130                                                 out_ptr += sizeof (signed char);
131                                         }
132                                 }
133
134                                 samplefrac += fracstep;
135                         }
136
137                         // Update the counters and the buffer position
138                         remain_in -= in_format->speed * in_format->channels;
139                         in_ptr += in_format->speed * in_format->channels * in_format->width;
140                         total_out += tmpcount;
141                 }
142         }
143
144         return outcount;
145 }
146
147 //=============================================================================
148
149 /*
150 ==============
151 S_LoadSound
152 ==============
153 */
154 qboolean S_LoadSound (sfx_t *s, qboolean complain)
155 {
156         char namebuffer[MAX_QPATH];
157         size_t len;
158         qboolean modified_name = false;
159
160         if (!shm || !shm->format.speed)
161                 return false;
162
163         // If we weren't able to load it previously, no need to retry
164         if (s->flags & SFXFLAG_FILEMISSING)
165                 return false;
166
167         // See if in memory
168         if (s->fetcher != NULL)
169         {
170                 if (s->format.speed != shm->format.speed)
171                         Sys_Error ("S_LoadSound: sound %s hasn't been resampled (%uHz instead of %uHz)", s->name);
172                 return true;
173         }
174
175         len = strlcpy (namebuffer, s->name, sizeof (namebuffer));
176         if (len >= sizeof (namebuffer))
177                 return false;
178
179         // Try to load it as a WAV file
180         if (S_LoadWavFile (namebuffer, s))
181                 return true;
182
183         // Else, try to load it as an Ogg Vorbis file
184         if (!strcasecmp (namebuffer + len - 4, ".wav"))
185         {
186                 strcpy (namebuffer + len - 3, "ogg");
187                 modified_name = true;
188         }
189         if (OGG_LoadVorbisFile (namebuffer, s))
190                 return true;
191
192         // Can't load the sound!
193         s->flags |= SFXFLAG_FILEMISSING;
194         if (complain)
195         {
196                 if (modified_name)
197                         strcpy (namebuffer + len - 3, "wav");
198                 Con_Printf("Couldn't load %s\n", namebuffer);
199         }
200         return false;
201 }
202
203 void S_UnloadSound (sfx_t *s)
204 {
205         if (s->fetcher != NULL)
206         {
207                 unsigned int i;
208
209                 // Stop all channels that use this sound
210                 for (i = 0; i < total_channels ; i++)
211                         if (channels[i].sfx == s)
212                                 S_StopChannel (i);
213
214                 s->fetcher = NULL;
215                 s->fetcher_data = NULL;
216                 Mem_FreePool(&s->mempool);
217         }
218 }