2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
33 size_t ResampleSfx (const qbyte *in_data, size_t in_length, const snd_format_t* in_format, qbyte *out_data, const char* sfxname)
35 size_t srclength, outcount, i;
37 srclength = in_length * in_format->channels;
38 outcount = (double)in_length * shm->format.speed / in_format->speed;
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);
43 // Trivial case (direct transfer)
44 if (in_format->speed == shm->format.speed)
46 if (in_format->width == 1)
48 for (i = 0; i < srclength; i++)
49 ((signed char*)out_data)[i] = in_data[i] - 128;
51 else // if (in_format->width == 2)
52 memcpy (out_data, in_data, srclength * in_format->width);
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)
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;
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);
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)
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;
87 tmpcount = outcount - total_out;
89 // Convert up to 1 sec of sound
90 for (i = 0; i < tmpcount; i++)
93 unsigned int srcsample = (samplefrac >> FRACTIONAL_BITS) * in_format->channels;
97 if (in_format->width == 2)
99 for (j = 0; j < in_format->channels; j++, srcsample++)
101 // No value to interpolate with?
102 if (srcsample + in_format->channels < remain_in)
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;
109 *((short*)out_ptr) = ((const short*)in_ptr)[srcsample];
111 out_ptr += sizeof (short);
115 else // if (in_format->width == 1)
117 for (j = 0; j < in_format->channels; j++, srcsample++)
119 // No more value to interpolate with?
120 if (srcsample + in_format->channels < remain_in)
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;
127 *((signed char*)out_ptr) = ((const qbyte*)in_ptr)[srcsample] - 128;
129 out_ptr += sizeof (signed char);
133 samplefrac += fracstep;
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;
146 //=============================================================================
153 qboolean S_LoadSound (sfx_t *s, qboolean complain)
155 char namebuffer[MAX_QPATH];
157 qboolean modified_name = false;
159 // see if still in memory
160 if (!shm || !shm->format.speed)
162 if (s->fetcher != NULL)
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);
169 len = strlcpy (namebuffer, s->name, sizeof (namebuffer));
170 if (len >= sizeof (namebuffer))
173 // Try to load it as a WAV file
174 if (S_LoadWavFile (namebuffer, s))
177 // Else, try to load it as an Ogg Vorbis file
178 if (!strcasecmp (namebuffer + len - 4, ".wav"))
180 strcpy (namebuffer + len - 3, "ogg");
181 modified_name = true;
183 if (OGG_LoadVorbisFile (namebuffer, s))
186 // Can't load the sound!
188 s->flags |= SFXFLAG_SILENTLYMISSING;
190 s->flags &= ~SFXFLAG_SILENTLYMISSING;
194 strcpy (namebuffer + len - 3, "wav");
195 Con_Printf("Couldn't load %s\n", namebuffer);
200 void S_UnloadSound(sfx_t *s)
202 if (s->fetcher != NULL)
207 s->fetcher_data = NULL;
208 Mem_FreePool(&s->mempool);
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;