/* 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 "audiolab/Globals.h" #include "audiolab/SynthControls_waveModifier_chord.h" #include "uitoolkit/UILabel.h" #include "utilities/IOUtilities.h" #include #define stemobject_implementation SynthControls_waveModifier_chord v_begin(); v_func(dispose); v_end(); SynthControls_waveModifier_chord * SynthControls_waveModifier_chord_create(Vector2f position, WaveModifier_chord * modifier, SynthControls_liveUpdateCallback liveUpdateCallback, SynthControls_updateCompleteCallback updateCompleteCallback, void * callbackContext, UIWindowRoot * windowRoot, UIAppearance appearance) { stemobject_create_implementation(init, position, modifier, liveUpdateCallback, updateCompleteCallback, callbackContext, windowRoot, appearance) } static void noteChangeCallback(PianoNoteControl * control, double referenceTime, void * context) { SynthControls_waveModifier_chord * self = context; self->modifier->notes[self->editingNoteIndex].note = control->selectedNote; self->liveUpdateCallback(self->callbackContext); self->updateCompleteCallback(self->callbackContext, "change chord note"); } defineSynthControlSliderCallback_extended(WaveModifier_chord, self->modifier, notes[self->editingNoteIndex].amplitude, noteAmplitude, "chord note amplitude", 0.05f, self->liveUpdateCallback, self->updateCompleteCallback, self->callbackContext); static void updateNoteDisplay(SynthControls_waveModifier_chord * self) { char text[15]; snprintf_safe(text, sizeof(text), "Note %u/%u", self->editingNoteIndex + 1, self->modifier->noteCount); UILabel_setText(self->noteLabel, STR(text)); UIFloatEditText_setValue(self->editText_noteAmplitude, self->modifier->notes[self->editingNoteIndex].amplitude); UISlider_setValue(self->slider_noteAmplitude, self->modifier->notes[self->editingNoteIndex].amplitude); PianoNoteControl_setSelectedNote(self->noteControl, self->modifier->notes[self->editingNoteIndex].note); self->removeNoteButton->visible = self->modifier->noteCount > 1; } static void previousNoteButtonCallback(UIButton * button, unsigned int modifiers, double referenceTime, void * context) { SynthControls_waveModifier_chord * self = context; self->editingNoteIndex = (self->editingNoteIndex + self->modifier->noteCount - 1) % self->modifier->noteCount; updateNoteDisplay(self); } static void nextNoteButtonCallback(UIButton * button, unsigned int modifiers, double referenceTime, void * context) { SynthControls_waveModifier_chord * self = context; self->editingNoteIndex = (self->editingNoteIndex + 1) % self->modifier->noteCount; updateNoteDisplay(self); } static void addNoteButtonCallback(UIButton * button, unsigned int modifiers, double referenceTime, void * context) { SynthControls_waveModifier_chord * self = context; self->modifier->notes = realloc(self->modifier->notes, (self->modifier->noteCount + 1) * sizeof(*self->modifier->notes)); self->editingNoteIndex++; for (unsigned int noteIndex = self->modifier->noteCount; noteIndex > self->editingNoteIndex; noteIndex--) { self->modifier->notes[noteIndex] = self->modifier->notes[noteIndex - 1]; } self->modifier->notes[self->editingNoteIndex].note = NOTE_VALUE(NOTE_A, 4); self->modifier->notes[self->editingNoteIndex].amplitude = 1.0f; self->modifier->noteCount++; self->liveUpdateCallback(self->callbackContext); self->updateCompleteCallback(self->callbackContext, "add chord note"); updateNoteDisplay(self); } static void removeNoteButtonCallback(UIButton * button, unsigned int modifiers, double referenceTime, void * context) { SynthControls_waveModifier_chord * self = context; if (self->modifier->noteCount > 1) { self->modifier->noteCount--; for (unsigned int noteIndex = self->editingNoteIndex; noteIndex < self->modifier->noteCount; noteIndex++) { self->modifier->notes[noteIndex] = self->modifier->notes[noteIndex + 1]; } if (self->editingNoteIndex >= self->modifier->noteCount) { self->editingNoteIndex--; } self->liveUpdateCallback(self->callbackContext); self->updateCompleteCallback(self->callbackContext, "remove chord note"); updateNoteDisplay(self); } } bool SynthControls_waveModifier_chord_init(SynthControls_waveModifier_chord * self, Vector2f position, WaveModifier_chord * modifier, SynthControls_liveUpdateCallback liveUpdateCallback, SynthControls_updateCompleteCallback updateCompleteCallback, void * callbackContext, UIWindowRoot * windowRoot, UIAppearance appearance) { call_super(init, self, position, liveUpdateCallback, updateCompleteCallback, callbackContext, windowRoot, appearance); self->editingNoteIndex = 0; self->modifier = modifier; float controlY = -CONTROL_OFFSET_Y / 2; call_virtual(addElement, self, UILabel_create(STRL("Note"), VECTOR2f(CONTROL_DEFAULT_X - CONTROL_SLIDER_LABEL_PADDING, controlY), VECTOR2f(0.0f, 0.0f), VECTOR2f(1.0f, 0.5f), ALIGN_RIGHT, 0, OVERFLOW_RESIZE, OVERFLOW_RESIZE, g_uiAppearance), true); call_virtual(addElement, self, self->noteControl = PianoNoteControl_create(VECTOR2f(CONTROL_DEFAULT_X, controlY), VECTOR2f(0.0f, 0.5f), noteChangeCallback, self, self->appearance), true); controlY -= CONTROL_OFFSET_Y; addSynthControlSlider_extended(self, CONTROL_DEFAULT_X, self->modifier->notes[self->editingNoteIndex].amplitude, "Amplitude", noteAmplitude, 0.0f, 1.0f, 1.0f); controlY -= 6; call_virtual(addElement, self, self->noteLabel = UILabel_create(STR_NULL, VECTOR2f(30, controlY), VECTOR2f(0.0f, 0.0f), VECTOR2f(0.0f, 0.5f), ALIGN_LEFT, 0, OVERFLOW_RESIZE, OVERFLOW_RESIZE, g_uiAppearance), true); call_virtual(addElement, self, UIButton_create(STRL("Prev"), VECTOR2f(115, controlY), VECTOR2f(0.0f, 0.5f), 0.0f, OVERFLOW_RESIZE, previousNoteButtonCallback, self, g_uiAppearance), true); call_virtual(addElement, self, UIButton_create(STRL("Next"), VECTOR2f(163, controlY), VECTOR2f(0.0f, 0.5f), 0.0f, OVERFLOW_RESIZE, nextNoteButtonCallback, self, g_uiAppearance), true); call_virtual(addElement, self, UIButton_create(STRL("Add"), VECTOR2f(211, controlY), VECTOR2f(0.0f, 0.5f), 0.0f, OVERFLOW_RESIZE, addNoteButtonCallback, self, g_uiAppearance), true); call_virtual(addElement, self, self->removeNoteButton = UIButton_create(STRL("Remove"), VECTOR2f(253, controlY), VECTOR2f(0.0f, 0.5f), 0.0f, OVERFLOW_RESIZE, removeNoteButtonCallback, self, g_uiAppearance), true); self->removeNoteButton->visible = self->modifier->noteCount > 0; controlY -= CONTROL_OFFSET_Y + 6; updateNoteDisplay(self); return true; } void SynthControls_waveModifier_chord_dispose(SynthControls_waveModifier_chord * self) { call_super_virtual(dispose, self); }