/* 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/AudioMath.h" #include "audiosynth/AudioSampler_generalSynth.h" #include "audiosynth/AudioSequence.h" #include "audiosynth/AudioSequenceItemCache.h" #define stemobject_implementation AudioSequence stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); AudioSequence * AudioSequence_create(struct AudioSequence_item * items, unsigned int itemCount, bool copy, bool takeOwnership) { stemobject_create_implementation(init, items, itemCount, copy, takeOwnership) } bool AudioSequence_init(AudioSequence * self, struct AudioSequence_item * items, unsigned int itemCount, bool copy, bool takeOwnership) { call_super(init, self); self->itemCount = itemCount; if (copy) { self->items = malloc(itemCount * sizeof(*self->items)); for (unsigned int itemIndex = 0; itemIndex < itemCount; itemIndex++) { self->items[itemIndex] = items[itemIndex]; self->items[itemIndex].parameters = call_virtual(copy, items[itemIndex].parameters); self->items[itemIndex].parametersOwned = true; } } else { self->items = items; } self->private_ivar(itemsOwned) = takeOwnership; return true; } void AudioSequence_dispose(AudioSequence * self) { if (self->private_ivar(itemsOwned)) { for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { if (self->items[itemIndex].parametersOwned) { call_virtual(dispose, self->items[itemIndex].parameters); } } free(self->items); } call_super_virtual(dispose, self); } AudioSequence * AudioSequence_copy(AudioSequence * self) { stemobject_copy_implementation(initCopy) } void AudioSequence_initCopy(AudioSequence * self, AudioSequence * original) { call_super(init, self); self->itemCount = original->itemCount; if (self->itemCount == 0) { self->items = NULL; } else { self->items = malloc(self->itemCount * sizeof(*self->items)); for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { self->items[itemIndex].time = original->items[itemIndex].time; self->items[itemIndex].leftMultiplier = original->items[itemIndex].leftMultiplier; self->items[itemIndex].rightMultiplier = original->items[itemIndex].rightMultiplier; self->items[itemIndex].parameters = call_virtual(copy, original->items[itemIndex].parameters); self->items[itemIndex].parametersOwned = true; } } self->private_ivar(itemsOwned) = true; } void AudioSequence_addItem(AudioSequence * self, struct AudioSequence_item item) { self->items = realloc(self->items, (self->itemCount + 1) * sizeof(*self->items)); self->items[self->itemCount] = item; self->itemCount++; } struct AudioSequence_cacheItem { AudioSamplerParameters * parameters; PCMAudio * audio; }; PCMAudio * AudioSequence_composite(AudioSequence * self, unsigned int bytesPerSample, unsigned int channelCount, unsigned int sampleRate, float amplitude, bool useCache) { float totalLength = 0.0f; float channelMultipliers[channelCount]; for (unsigned int channelIndex = 0; channelIndex < channelCount; channelIndex++) { channelMultipliers[channelIndex] = 1.0f; } if (useCache) { AudioSequenceItemCache * itemCache = AudioSequenceItemCache_create(); AudioSequenceItemCache_cacheAllAudioSequenceItems(itemCache, self, bytesPerSample, channelCount, sampleRate, amplitude); float framesToSeconds = 1.0f / sampleRate; for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { AudioSequenceItemCache_item * cacheItem = AudioSequenceItemCache_getCacheItem(itemCache, self->items[itemIndex].parameters); float length = cacheItem->audio->frameCount * framesToSeconds; if (length + self->items[itemIndex].time > totalLength) { totalLength = length + self->items[itemIndex].time; } } AudioFrameIndex frameCount = totalLength * sampleRate; PCMAudio * audio = PCMAudio_create(bytesPerSample, channelCount, sampleRate, frameCount, calloc(frameCount, bytesPerSample * channelCount), false); for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { AudioSequenceItemCache_item * cacheItem = AudioSequenceItemCache_getCacheItem(itemCache, self->items[itemIndex].parameters); AudioFrameIndex frameOffset = self->items[itemIndex].time * sampleRate; channelMultipliers[0] = self->items[itemIndex].leftMultiplier; if (channelCount > 1) { channelMultipliers[1] = self->items[itemIndex].rightMultiplier; } mixAudioSamples(cacheItem->audio->samples, cacheItem->audio->channelCount, cacheItem->audio->sampleRate, cacheItem->audio->bytesPerSample, audio->samples + frameOffset * bytesPerSample * channelCount, audio->channelCount, audio->sampleRate, audio->bytesPerSample, channelMultipliers, cacheItem->audio->frameCount, audio->frameCount - frameOffset, NULL, NULL, NULL); } AudioSequenceItemCache_dispose(itemCache); return audio; } for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { float length = AudioSampler_generalSynth_getLength(self->items[itemIndex].parameters); if (length + self->items[itemIndex].time > totalLength) { totalLength = length + self->items[itemIndex].time; } } AudioFrameIndex frameCount = totalLength * sampleRate; PCMAudio * audio = PCMAudio_create(bytesPerSample, channelCount, sampleRate, frameCount, calloc(frameCount, bytesPerSample * channelCount), false); for (unsigned int itemIndex = 0; itemIndex < self->itemCount; itemIndex++) { channelMultipliers[0] = self->items[itemIndex].leftMultiplier; if (channelCount > 1) { channelMultipliers[1] = self->items[itemIndex].rightMultiplier; } AudioSampler_generalSynth_renderIntoMix(self->items[itemIndex].parameters, channelMultipliers, audio, self->items[itemIndex].time * sampleRate); } return audio; }