/* Copyright (c) 2024 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 "audioplayer/AudioSequenceStream.h" #include #define stemobject_implementation AudioSequenceStream v_begin(); v_func(dispose); v_func(writeAudioFrames); v_func(seek); v_end(); struct AudioSequenceStream_itemInstance { AudioSequenceItemCache_item * item; AudioFrameIndex startFrameIndex; AudioFrameIndex frameCount; float leftMultiplier; float rightMultiplier; }; struct AudioSequenceStream_voice { unsigned int itemInstanceCount; unsigned int itemInstanceAllocatedCount; struct AudioSequenceStream_itemInstance * itemInstances; unsigned int currentItemInstanceIndex; }; AudioSequenceStream * AudioSequenceStream_create(AudioSequence * audioSequence, AudioSequenceItemCache * itemCache, AudioFrameIndex startFrameIndex, AudioFrameIndex loopFrameCount, unsigned int bytesPerSample, unsigned int channelCount, unsigned int sampleRate) { stemobject_create_implementation(init, audioSequence, itemCache, startFrameIndex, loopFrameCount, bytesPerSample, channelCount, sampleRate) } bool AudioSequenceStream_init(AudioSequenceStream * self, AudioSequence * audioSequence, AudioSequenceItemCache * itemCache, AudioFrameIndex startFrameIndex, AudioFrameIndex loopFrameCount, unsigned int bytesPerSample, unsigned int channelCount, unsigned int sampleRate) { call_super(init, self, startFrameIndex, loopFrameCount, bytesPerSample, channelCount, sampleRate); self->voiceCount = 0; unsigned int voiceAllocatedCount = 0; self->voices = NULL; for (unsigned int itemIndex = 0; itemIndex < audioSequence->itemCount; itemIndex++) { struct AudioSequenceStream_itemInstance itemInstance; itemInstance.item = AudioSequenceItemCache_getCacheItem(itemCache, audioSequence->items[itemIndex].parameters); itemInstance.startFrameIndex = audioSequence->items[itemIndex].time * sampleRate; itemInstance.frameCount = itemInstance.item->audio->frameCount; itemInstance.leftMultiplier = audioSequence->items[itemIndex].leftMultiplier; itemInstance.rightMultiplier = audioSequence->items[itemIndex].rightMultiplier; bool homeFound = false; for (unsigned int voiceIndex = 0; voiceIndex < self->voiceCount; voiceIndex++) { struct AudioSequenceStream_itemInstance * lastItemInstance = &self->voices[voiceIndex].itemInstances[self->voices[voiceIndex].itemInstanceCount - 1]; if (lastItemInstance->startFrameIndex + lastItemInstance->frameCount <= itemInstance.startFrameIndex) { if (itemInstance.startFrameIndex + itemInstance.frameCount > loopFrameCount && self->voices[voiceIndex].itemInstances[0].startFrameIndex < loopFrameCount - (itemInstance.startFrameIndex + itemInstance.frameCount)) { continue; } if (self->voices[voiceIndex].itemInstanceCount >= self->voices[voiceIndex].itemInstanceAllocatedCount) { self->voices[voiceIndex].itemInstanceAllocatedCount *= 2; self->voices[voiceIndex].itemInstances = realloc(self->voices[voiceIndex].itemInstances, self->voices[voiceIndex].itemInstanceAllocatedCount * sizeof(*self->voices[voiceIndex].itemInstances)); } self->voices[voiceIndex].itemInstances[self->voices[voiceIndex].itemInstanceCount++] = itemInstance; homeFound = true; break; } } if (!homeFound) { if (self->voiceCount >= voiceAllocatedCount) { voiceAllocatedCount = voiceAllocatedCount * 2 + (voiceAllocatedCount == 0) * 8; self->voices = realloc(self->voices, voiceAllocatedCount * sizeof(*self->voices)); } self->voices[self->voiceCount].itemInstanceCount = 1; self->voices[self->voiceCount].itemInstanceAllocatedCount = 16; self->voices[self->voiceCount].itemInstances = malloc(self->voices[self->voiceCount].itemInstanceAllocatedCount * sizeof(*self->voices[self->voiceCount].itemInstances)); self->voices[self->voiceCount].itemInstances[0] = itemInstance; self->voices[self->voiceCount].currentItemInstanceIndex = 0; self->voiceCount++; } } return true; } void AudioSequenceStream_dispose(AudioSequenceStream * self) { for (unsigned int voiceIndex = 0; voiceIndex < self->voiceCount; voiceIndex++) { free(self->voices[voiceIndex].itemInstances); } free(self->voices); call_super_virtual(dispose, self); } static void mixItemInstance(int16_t * outSamples, AudioFrameIndex outFrameIndexStart, struct AudioSequenceStream_itemInstance * itemInstance, AudioFrameIndex inFrameIndex, AudioFrameIndex frameCount, float rampStartVolume, float rampEndVolume, AudioFrameIndex rampFrameCountTotal, unsigned int channelCount) { AudioFrameIndex instanceStartFrameIndex = 0, instanceFrameCount = itemInstance->frameCount; if (inFrameIndex > itemInstance->startFrameIndex) { instanceStartFrameIndex = inFrameIndex - itemInstance->startFrameIndex; } else if (inFrameIndex < itemInstance->startFrameIndex) { rampStartVolume += (rampEndVolume - rampStartVolume) * instanceStartFrameIndex / frameCount; } if (inFrameIndex + frameCount < itemInstance->startFrameIndex + itemInstance->frameCount) { instanceFrameCount -= (inFrameIndex + frameCount) - (itemInstance->startFrameIndex + itemInstance->frameCount); } else if (inFrameIndex + frameCount > itemInstance->startFrameIndex + itemInstance->frameCount) { rampEndVolume = rampStartVolume + (rampEndVolume - rampStartVolume) * ((itemInstance->startFrameIndex + itemInstance->frameCount) - (inFrameIndex + frameCount)) / frameCount; } int16_t * inSamples = itemInstance->item->audio->samples; float channelMultipliers[channelCount]; for (unsigned int channelIndex = 0; channelIndex < channelCount; channelIndex++) { channelMultipliers[channelIndex] = 1.0f; } channelMultipliers[0] = itemInstance->leftMultiplier; if (channelCount > 1) { channelMultipliers[1] = itemInstance->rightMultiplier; } for (unsigned int frameIndex = instanceStartFrameIndex; frameIndex < instanceFrameCount; frameIndex++) { AudioFrameIndex outFrameIndex = outFrameIndexStart + frameIndex - instanceStartFrameIndex; float frameVolume = rampStartVolume + (rampEndVolume - rampStartVolume) * outFrameIndex / rampFrameCountTotal; if (frameVolume < 0.0f) { frameVolume = 0.0f; } else if (frameVolume > 1.0f) { frameVolume = 1.0f; } frameVolume *= frameVolume; for (unsigned int channelIndex = 0; channelIndex < channelCount; channelIndex++) { int32_t mixedSample = outSamples[outFrameIndex * channelCount + channelIndex] + inSamples[frameIndex * channelCount + channelIndex] * frameVolume * channelMultipliers[channelIndex]; if (mixedSample > INT16_MAX) { mixedSample = INT16_MAX; } else if (mixedSample < INT16_MIN) { mixedSample = INT16_MIN; } outSamples[outFrameIndex * channelCount + channelIndex] = mixedSample; } } } void AudioSequenceStream_writeAudioFrames(AudioSequenceStream * self, void * outBuffer, AudioFrameIndex frameCount, AudioFrameIndex * ioOutFrameIndex, float rampStartVolume, float rampEndVolume, AudioFrameIndex rampFrameCountTotal) { AudioFrameIndex inFrameIndex = self->frameIndex, outFrameIndex = *ioOutFrameIndex; memset(outBuffer, 0, frameCount * self->bytesPerSample * self->channelCount); for (unsigned int voiceIndex = 0; voiceIndex < self->voiceCount; voiceIndex++) { if (self->voices[voiceIndex].currentItemInstanceIndex >= self->voices[voiceIndex].itemInstanceCount) { self->voices[voiceIndex].currentItemInstanceIndex = 0; } struct AudioSequenceStream_itemInstance * itemInstance = &self->voices[voiceIndex].itemInstances[self->voices[voiceIndex].currentItemInstanceIndex]; while (itemInstance->startFrameIndex < inFrameIndex + frameCount && itemInstance->startFrameIndex + itemInstance->frameCount > inFrameIndex) { mixItemInstance(outBuffer, outFrameIndex, itemInstance, inFrameIndex, frameCount, rampStartVolume, rampEndVolume, rampFrameCountTotal, self->channelCount); if (itemInstance->startFrameIndex + itemInstance->frameCount <= inFrameIndex + frameCount) { self->voices[voiceIndex].currentItemInstanceIndex++; self->voices[voiceIndex].currentItemInstanceIndex %= self->voices[voiceIndex].itemInstanceCount; itemInstance = &self->voices[voiceIndex].itemInstances[self->voices[voiceIndex].currentItemInstanceIndex]; } } } } void AudioSequenceStream_seek(AudioSequenceStream * self, AudioFrameIndex offset, int whence) { call_super_virtual(seek, self, offset, whence); for (unsigned int voiceIndex = 0; voiceIndex < self->voiceCount; voiceIndex++) { self->voices[voiceIndex].currentItemInstanceIndex = 0; while (self->voices[voiceIndex].currentItemInstanceIndex < self->voices[voiceIndex].itemInstanceCount && self->voices[voiceIndex].itemInstances[self->voices[voiceIndex].currentItemInstanceIndex].startFrameIndex < self->frameIndex) { self->voices[voiceIndex].currentItemInstanceIndex++; } } }