Skip to content

Commit

Permalink
Cleaned wav loop loading
Browse files Browse the repository at this point in the history
  • Loading branch information
KiritoDv committed Dec 2, 2023
1 parent 8e8cb06 commit 1080a87
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions soh/soh/resource/importer/AudioSampleFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,37 @@ void LUS::AudioCustomSampleFactoryV0::ParseFileBinary(std::shared_ptr<BinaryRead

switch (format) {
case AudioFormat::WAV: {
drwav_uint32 channels;
drwav_uint32 sampleRate;
drwav_uint64 totalPcm;
bool hasLoop = false;
drwav wav;
drwav_init_memory_with_metadata(&wav, bytes, length, 0, nullptr);

if(wav.pMetadata != nullptr && wav.pMetadata->type == drwav_metadata_type_smpl) {
const auto metadata = wav.pMetadata->data.smpl;

if(metadata.sampleLoopCount > 0) {
audioSample->loop.start = metadata.pLoops[0].firstSampleByteOffset;
audioSample->loop.end = metadata.pLoops[0].lastSampleByteOffset;
audioSample->loop.count = 1;
audioSample->loopStateCount = 1;
hasLoop = true;
if(wav.pMetadata != nullptr) {
for(size_t id = 0; id < wav.metadataCount; id++) {
const auto metadata = wav.pMetadata[id];

if(metadata.type == drwav_metadata_type_smpl) {
const auto smpl = wav.pMetadata->data.smpl;
if(smpl.sampleLoopCount > 0) {
audioSample->loop.start = smpl.pLoops[0].firstSampleByteOffset;
audioSample->loop.end = smpl.pLoops[0].lastSampleByteOffset;
audioSample->loop.count = 1;
audioSample->loopStateCount = 1;
break;
}
}
}
} else {
audioSample->loop.count = 0;
audioSample->loop.start = 0;
audioSample->loop.end = wav.totalPCMFrameCount;
}

const auto pcmData = drwav__read_pcm_frames_and_close_s16(&wav, &channels, &sampleRate, &totalPcm);
const auto pcmData = drwav__read_pcm_frames_and_close_s16(&wav, nullptr, nullptr, nullptr);

audioSample->sample.size = totalPcm * channels * sizeof(int16_t);
audioSample->sample.size = wav.totalPCMFrameCount * wav.channels * sizeof(int16_t);
audioSample->sample.sampleAddr = reinterpret_cast<uint8_t *>(pcmData);
audioSample->sample.codec = CODEC_S16;

if(!hasLoop) {
audioSample->loop.count = 0;
audioSample->loop.start = 0;
audioSample->loop.end = totalPcm;
}

audioSample->sample.sampleRateMagicValue = 'RIFF';
audioSample->sample.sampleRate = sampleRate * pitch;
audioSample->sample.sampleRate = wav.sampleRate * pitch;
audioSample->sample.loop = &audioSample->loop;
break;
}
Expand Down

0 comments on commit 1080a87

Please sign in to comment.