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