bool AudioReader::readWav(const void* ptr, SoundBuffer& soundBuf,uint64 size) { drwav wavData; if (!drwav_init_memory(&wavData, ptr, static_cast<size_t>(size))) return false; soundBuf.m_info.format = SoundBuffer::AudioFormat::wav; soundBuf.m_info.channelCount = wavData.channels; soundBuf.m_info.sampleCount = wavData.totalSampleCount; soundBuf.m_info.firstSample = wavData.memoryStream.currentReadPos; soundBuf.m_info.sampleRate = wavData.sampleRate; soundBuf.m_duration = static_cast<float>(soundBuf.m_info.sampleCount / soundBuf.m_info.sampleRate / soundBuf.m_info.channelCount); soundBuf.m_samples.reserve(wavData.memoryStream.dataSize - wavData.memoryStream.currentReadPos); for (size_t it = wavData.memoryStream.currentReadPos; it < wavData.memoryStream.dataSize; ++it) soundBuf.m_samples.push_back(wavData.memoryStream.data[it]); return true; }
result Wav::loadwav(MemoryFile *aReader) { drwav decoder; if (!drwav_init_memory(&decoder, aReader->getMemPtr(), aReader->length())) { return FILE_LOAD_FAILED; } drwav_uint64 samples = decoder.totalSampleCount / decoder.channels; if (!samples) { drwav_uninit(&decoder); return FILE_LOAD_FAILED; } mData = new float[(unsigned int)(samples * decoder.channels)]; mBaseSamplerate = (float)decoder.sampleRate; mSampleCount = (unsigned int)samples; mChannels = decoder.channels; unsigned int i, j, k; for (i = 0; i < mSampleCount; i += 512) { float tmp[512 * MAX_CHANNELS]; unsigned int blockSize = (mSampleCount - i) > 512 ? 512 : mSampleCount - i; drwav_read_pcm_frames_f32(&decoder, blockSize, tmp); for (j = 0; j < blockSize; j++) { for (k = 0; k < decoder.channels; k++) { mData[k * mSampleCount + i + j] = tmp[j * decoder.channels + k]; } } } drwav_uninit(&decoder); return SO_NO_ERROR; }