void SetupWAVInfo (ubyte* buffer, int nLength) { tWAVInfo* infoP = reinterpret_cast<tWAVInfo*> (buffer); memcpy (infoP->header.chunkID, "RIFF", 4); infoP->header.chunkSize = nLength + sizeof (tWAVInfo) - 8; memcpy (infoP->header.riffType, "WAVE", 4); memcpy (infoP->format.chunkID, "fmt ", 4); infoP->format.chunkSize = sizeof (tWAVFormat) - sizeof (infoP->format.chunkID) - sizeof (infoP->format.chunkSize); infoP->format.format = 1; //PCM infoP->format.channels = 2; infoP->format.sampleRate = SAMPLE_RATE_22K; infoP->format.bitsPerSample = (audio.Format () == AUDIO_U8) ? 8 : 16; infoP->format.blockAlign = infoP->format.channels * (infoP->format.bitsPerSample / 8); infoP->format.avgBytesPerSec = infoP->format.sampleRate * infoP->format.blockAlign; memcpy (infoP->data.chunkID, "data", 4); infoP->data.chunkSize = nLength; }
int CAudioChannel::Resample (CSoundSample *soundP, int bD1Sound, int bMP3) { int h, i, k, l, nFormat = audio.Format (); float fFade; ushort* ps, * ph, nSound, nPrevSound; ubyte* dataP = soundP->data [soundP->bCustom].Buffer (); #if DBG if (soundP->bCustom) soundP->bCustom = soundP->bCustom; #endif h = i = soundP->nLength [soundP->bCustom]; l = 2 * i; if (bD1Sound) { if (gameOpts->sound.bUseSDLMixer) l *= 2; else bD1Sound = 0; } if (bMP3) l = (l * 32) / 11; //sample up to approx. 32 kHz else if (nFormat == AUDIO_S16LSB) l *= 2; if (!m_info.sample.Create (l + WAVINFO_SIZE)) return -1; m_info.bResampled = 1; ps = reinterpret_cast<ushort*> (m_info.sample.Buffer () + WAVINFO_SIZE); ph = reinterpret_cast<ushort*> (m_info.sample.Buffer () + WAVINFO_SIZE + l); ; for (i = k = 0; i < h; i++) { nSound = ushort (dataP [i]); if (bMP3) { //get as close to 32.000 Hz as possible if (k < 700) nSound <<= k / 100; else if (i < 700) nSound <<= i / 100; else nSound = (nSound - 1) << 8; *ps++ = nSound; if (ps >= ph) break; *ps++ = nSound; if (ps >= ph) break; if (++k % 11) { *ps++ = nSound; if (ps >= ph) break; } } else { if (nFormat == AUDIO_S16LSB) { fFade = float (i) / 500.0f; if (fFade > 1) fFade = float (h - i) / 500.0f; if (fFade > 1) fFade = 1.0f; nSound = ushort (32767.0f / 255.0f * float (nSound) * fFade); #if 1 // interpolate every 2nd sample *ps = nSound; if (i) *(ps - 1) = ushort ((uint (nSound) + uint (nPrevSound)) / 2); nPrevSound = nSound; ps += 2; #else *ps++ = nSound; *ps++ = nSound; #endif } else { nSound |= (nSound << 8); *ps++ = nSound; } } if (bD1Sound) { if (bMP3) { *ps++ = nSound; if (ps >= ph) break; *ps++ = nSound; if (ps >= ph) break; if (k % 11) { *ps++ = nSound; if (ps >= ph) break; } } else { *ps++ = nSound; if (nFormat == AUDIO_S16LSB) *ps++ = nSound; } } } Assert (ps == ph); #if MAKE_WAV SetupWAVInfo (m_info.sample.Buffer (), l); #endif return m_info.nLength = l; }