int Tuning_table_get_nearest_note_index(const Tuning_table* tt, double cents) { rassert(tt != NULL); rassert(tt->note_count > 0); rassert(tt->pitch_map != NULL); rassert(isfinite(cents)); pitch_index* key = &(pitch_index){ .cents = cents }; pitch_index* pi_upper = AAtree_get_at_least(tt->pitch_map, key); pitch_index* pi_lower = AAtree_get_at_most(tt->pitch_map, key); pitch_index* pi = NULL; rassert(pi_upper != NULL || pi_lower != NULL); if (pi_lower == NULL) pi = pi_upper; else if (pi_upper == NULL) pi = pi_lower; else if (fabs(pi_upper->cents - cents) < fabs(pi_lower->cents - cents)) pi = pi_upper; else pi = pi_lower; return pi->note; }
const Sample_entry* Hit_map_get_entry( const Hit_map* map, int hit_index, double force, Random* random) { rassert(map != NULL); rassert(hit_index >= 0); rassert(hit_index < KQT_HITS_MAX); rassert(isfinite(force) || force == -INFINITY); rassert(random != NULL); AAtree* forces = map->hits[hit_index]; if (forces == NULL) { fprintf(stderr, "no forces\n"); return NULL; } Random_list* key = &(Random_list){ .force = force }; Random_list* greater = AAtree_get_at_least(forces, key); Random_list* smaller = AAtree_get_at_most(forces, key); Random_list* list = NULL; if (greater == NULL) list = smaller; else if (smaller == NULL) list = greater; else if (fabs(greater->force - force) < fabs(smaller->force - force)) list = greater; else list = smaller; if (list == NULL) return NULL; if (list->entry_count == 0) return NULL; const int index = Random_get_index(random, list->entry_count); return &list->entries[index]; }