/* Copyright (c) 2021 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "audiosynth/WaveSampler_pcmData.h" #include "utilities/lookup3.h" #include #define stemobject_implementation WaveSampler_pcmData stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_entry(copy); stemobject_vtable_entry(isEqual); stemobject_vtable_entry(hash); stemobject_vtable_entry(getLength); stemobject_vtable_entry(sample); stemobject_vtable_end(); WaveSampler_pcmData * WaveSampler_pcmData_create(PCMAudio * audioData, bool loop, size_t startFrameIndex, size_t frameCount) { stemobject_create_implementation(init, audioData, loop, startFrameIndex, frameCount) } bool WaveSampler_pcmData_init(WaveSampler_pcmData * self, PCMAudio * audioData, bool loop, size_t startFrameIndex, size_t frameCount) { call_super(init, self); self->audioData = PCMAudio_copy(audioData); self->audioReferenceCount = malloc(sizeof(*self->audioReferenceCount)); *self->audioReferenceCount = 1; self->loop = loop; self->startFrameIndex = startFrameIndex; self->frameCount = frameCount; return true; } static void dereferenceAudio(WaveSampler_pcmData * self) { --*self->audioReferenceCount; if (*self->audioReferenceCount == 0) { free(self->audioReferenceCount); PCMAudio_dispose(self->audioData); } } void WaveSampler_pcmData_dispose(WaveSampler_pcmData * self) { dereferenceAudio(self); call_super_virtual(dispose, self); } WaveSampler_pcmData * WaveSampler_pcmData_copy(WaveSampler_pcmData * self) { stemobject_copy_implementation(initCopy) } void WaveSampler_pcmData_initCopy(WaveSampler_pcmData * self, WaveSampler_pcmData * original) { call_super(init, self); self->audioData = original->audioData; self->audioReferenceCount = original->audioReferenceCount; ++*self->audioReferenceCount; self->loop = original->loop; self->startFrameIndex = original->startFrameIndex; self->frameCount = original->frameCount; } bool WaveSampler_pcmData_isEqual(WaveSampler_pcmData * self, compat_type(WaveSampler_pcmData *) compareUntyped) { WaveSampler_pcmData * compare = compareUntyped; if (!call_super_virtual(isEqual, self, compare)) { return false; } return (self->loop == compare->loop && self->startFrameIndex == compare->startFrameIndex && self->frameCount == compare->frameCount && PCMAudio_isEqual(self->audioData, compare->audioData)); } uint32_t WaveSampler_pcmData_hash(WaveSampler_pcmData * self, uint32_t initval) { initval = hashlittle(&self->loop, sizeof(self->loop), initval); initval = hashlittle(&self->startFrameIndex, sizeof(self->startFrameIndex), initval); initval = hashlittle(&self->frameCount, sizeof(self->frameCount), initval); initval = hashlittle(&self->audioData->bytesPerSample, sizeof(self->audioData->bytesPerSample), initval); initval = hashlittle(&self->audioData->channelCount, sizeof(self->audioData->channelCount), initval); initval = hashlittle(&self->audioData->sampleRate, sizeof(self->audioData->sampleRate), initval); initval = hashlittle(&self->audioData->frameCount, sizeof(self->audioData->frameCount), initval); initval = hashlittle(self->audioData->samples, self->audioData->frameCount * self->audioData->bytesPerSample * self->audioData->channelCount, initval); return initval; } float WaveSampler_pcmData_getLength(WaveSampler_pcmData * self) { if (self->frameCount == 0) { return (float) self->audioData->frameCount / self->audioData->sampleRate; } return (float) self->frameCount / self->audioData->sampleRate; } static float samplePCM(WaveSampler_pcmData * self, int frameIndex) { PCMAudio * audio = self->audioData; int frameMax = audio->frameCount; int loopFrameCount = frameMax; if (self->frameCount > 0) { loopFrameCount = self->frameCount; } if (self->loop) { frameIndex %= loopFrameCount; } frameIndex += self->startFrameIndex; if (frameIndex < 0 || (size_t) frameIndex >= audio->frameCount || (size_t) frameIndex > self->startFrameIndex + loopFrameCount) { return 0.0f; } if (audio->bytesPerSample == 4) { float * samples = audio->samples; return samples[frameIndex]; } if (audio->bytesPerSample == 2) { int16_t * samples = audio->samples; return samples[frameIndex] / (float) INT16_MAX; } int8_t * samples = audio->samples; return samples[frameIndex] / (float) INT8_MAX; } float WaveSampler_pcmData_sample(WaveSampler_pcmData * self, SamplerObject_state * state, float phase, float phaseDelta, float time, float timeDelta) { phase *= self->audioData->sampleRate / 220.0f; int frameIndex1 = floorf(phase); float sample1 = samplePCM(self, frameIndex1); int frameIndex2 = ceilf(phase); float sample2 = samplePCM(self, frameIndex2); return sample1 + (sample2 - sample1) * (phase - frameIndex1); } void WaveSampler_pcmData_setAudioData(WaveSampler_pcmData * self, PCMAudio * audioData, bool takeOwnership) { dereferenceAudio(self); if (takeOwnership) { self->audioData = audioData; } else { self->audioData = PCMAudio_copy(audioData); } self->audioReferenceCount = malloc(sizeof(*self->audioReferenceCount)); *self->audioReferenceCount = 1; }