bool Voice_pool_resize(Voice_pool* pool, uint16_t size) { assert(pool != NULL); //assert(size >= 0); uint16_t new_size = size; if (new_size == pool->size) return true; // Remove excess voices if any for (uint16_t i = new_size; i < pool->size; ++i) { del_Voice(pool->voices[i]); pool->voices[i] = NULL; } if (new_size < pool->size) pool->size = new_size; // Handle 0 voices if (new_size == 0) { memory_free(pool->voices); pool->voices = NULL; return true; } // Resize voice array Voice** new_voices = memory_realloc_items(Voice*, new_size, pool->voices); if (new_voices == NULL) return false; pool->voices = new_voices; // Sanitise new fields if any for (uint16_t i = pool->size; i < new_size; ++i) pool->voices[i] = NULL; // Allocate new voices for (uint16_t i = pool->size; i < new_size; ++i) { pool->voices[i] = new_Voice(); if (pool->voices[i] == NULL || !Voice_reserve_state_space(pool->voices[i], pool->state_size)) { for (--i; i >= pool->size; --i) { del_Voice(pool->voices[i]); pool->voices[i] = NULL; } return false; } } pool->size = new_size; return true; }
Voice* new_Voice(void) { Voice* voice = memory_alloc_item(Voice); if (voice == NULL) return NULL; voice->id = 0; voice->prio = VOICE_PRIO_INACTIVE; voice->gen = NULL; voice->state_size = 0; voice->state = NULL; voice->state_size = sizeof(Voice_state); voice->state = memory_alloc_item(Voice_state); voice->rand_p = new_Random(); voice->rand_s = new_Random(); if (voice->state == NULL || voice->rand_p == NULL || voice->rand_s == NULL) { del_Voice(voice); return NULL; } Random_set_context(voice->rand_p, "vp"); Random_set_context(voice->rand_s, "vs"); Voice_state_clear(voice->state); return voice; }
void del_Voice_pool(Voice_pool* pool) { if (pool == NULL) return; for (uint16_t i = 0; i < pool->size; ++i) { del_Voice(pool->voices[i]); pool->voices[i] = NULL; } memory_free(pool->voices); memory_free(pool); return; }
Voice_pool* new_Voice_pool(uint16_t size) { //assert(size >= 0); Voice_pool* pool = memory_alloc_item(Voice_pool); if (pool == NULL) return NULL; pool->size = size; pool->state_size = 0; pool->new_group_id = 0; pool->voices = NULL; pool->group_iter_offset = 0; pool->group_iter = *VOICE_GROUP_AUTO; if (size > 0) { pool->voices = memory_alloc_items(Voice, size); if (pool->voices == NULL) { memory_free(pool); return NULL; } } for (int i = 0; i < size; ++i) { pool->voices[i] = new_Voice(); if (pool->voices[i] == NULL) { for (--i; i >= 0; --i) { del_Voice(pool->voices[i]); } memory_free(pool->voices); memory_free(pool); return NULL; } } return pool; }