/* 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 "audiosynth/AudioSampler_generalSynth.h" #include "audiosynth/AudioSequenceItemCache.h" #include "audiosynth/HashTableAudioSamplerParametersKey.h" #define stemobject_implementation AudioSequenceItemCache v_begin(); v_func(dispose); v_end(); AudioSequenceItemCache * AudioSequenceItemCache_create(void) { stemobject_create_implementation(init) } bool AudioSequenceItemCache_init(AudioSequenceItemCache * self) { call_super(init, self); self->hashTable = HashTable_create(sizeof(AudioSequenceItemCache_item)); return true; } static bool disposeCacheItem(HashTable * hashTable, HashTable_key key, void * value, void * context) { AudioSequenceItemCache_item * item = value; PCMAudio_dispose(item->audio); return true; } void AudioSequenceItemCache_dispose(AudioSequenceItemCache * self) { HashTable_foreach(self->hashTable, disposeCacheItem, NULL); HashTable_dispose(self->hashTable); call_super_virtual(dispose, self); } AudioSequenceItemCache_item * AudioSequenceItemCache_getCacheItem(AudioSequenceItemCache * self, AudioSamplerParameters * parameters) { HashTableAudioSamplerParametersKey key; stemobject_assign_vtable(key, HashTableAudioSamplerParametersKey); HashTableAudioSamplerParametersKey_init(&key, parameters, false); return HashTable_get(self->hashTable, HashTable_objectKey(&key)); } void AudioSequenceItemCache_cacheAllAudioSequenceItems(AudioSequenceItemCache * self, AudioSequence * audioSequence, unsigned int bytesPerSample, unsigned int channelCount, unsigned int sampleRate, float amplitude) { HashTableAudioSamplerParametersKey key; stemobject_assign_vtable(key, HashTableAudioSamplerParametersKey); for (unsigned int itemIndex = 0; itemIndex < audioSequence->itemCount; itemIndex++) { HashTableAudioSamplerParametersKey_init(&key, audioSequence->items[itemIndex].parameters, false); if (HashTable_get(self->hashTable, HashTable_objectKey(&key)) == NULL) { AudioSequenceItemCache_item item; item.parameters = audioSequence->items[itemIndex].parameters; item.audio = AudioSampler_generalSynth_createPCMAudio(audioSequence->items[itemIndex].parameters, amplitude, bytesPerSample, channelCount, sampleRate); HashTable_set(self->hashTable, HashTable_objectKey(&key), &item); } } }