|
| 1 | +/*============================================================================== |
| 2 | +
|
| 3 | + ofxVisualProgramming: A visual programming patching environment for OF |
| 4 | +
|
| 5 | + Copyright (c) 2023 Emanuele Mazza aka n3m3da <[email protected]> |
| 6 | +
|
| 7 | + ofxVisualProgramming is distributed under the MIT License. |
| 8 | + This gives everyone the freedoms to use ofxVisualProgramming in any context: |
| 9 | + commercial or non-commercial, public or private, open or closed source. |
| 10 | +
|
| 11 | + Permission is hereby granted, free of charge, to any person obtaining a |
| 12 | + copy of this software and associated documentation files (the "Software"), |
| 13 | + to deal in the Software without restriction, including without limitation |
| 14 | + the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 15 | + and/or sell copies of the Software, and to permit persons to whom the |
| 16 | + Software is furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | + The above copyright notice and this permission notice shall be included |
| 19 | + in all copies or substantial portions of the Software. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 22 | + OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 26 | + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 27 | + DEALINGS IN THE SOFTWARE. |
| 28 | +
|
| 29 | + See https://github.com/d3cod3/ofxVisualProgramming for documentation |
| 30 | +
|
| 31 | +==============================================================================*/ |
| 32 | + |
| 33 | +#ifndef OFXVP_BUILD_WITH_MINIMAL_OBJECTS |
| 34 | + |
| 35 | +#include "FrequencyToNote.h" |
| 36 | + |
| 37 | +string notation_2[NOTES] = {"C1","C#1","D1","D#1","E1","F1","F#1","G1","G#1","A1","A#1","B1", |
| 38 | + "C2","C#2","D2","D#2","E2","F2","F#2","G2","G#2","A2","A#2","B2", |
| 39 | + "C3","C#3","D3","D#3","E3","F3","F#3","G3","G#3","A3","A#3","B3", |
| 40 | + "C4","C#4","D4","D#4","E4","F4","F#4","G4","G#4","A4","A#4","B4", |
| 41 | + "C5","C#5","D5","D#5","E5","F5","F#5","G5","G#5","A5","A#5","B5", |
| 42 | + "C6","C#6","D6","D#6","E6","F6","F#6","G6","G#6","A6","A#6","B6", |
| 43 | + "C7","C#7","D7","D#7","E7","F7","F#7","G7","G#7","A7","A#7","B7", |
| 44 | + "C8","C#8","D8","D#8","E8","F8","F#8","G8","G#8","A8","A#8","B8", |
| 45 | + "C9","C#9","D9","D#9","E9","F9","F#9","G9","G#9","A9","A#9","B9", |
| 46 | + "C10","C#10","D10","D#10","E10","F10","F#10","G10","G#10","A10","A#10","B10", |
| 47 | + "C11","C#11","D11","D#11","E11","F11","F#11","G11"}; |
| 48 | + |
| 49 | + |
| 50 | +//-------------------------------------------------------------- |
| 51 | +FrequencyToNote::FrequencyToNote() : PatchObject("frequency to note"){ |
| 52 | + |
| 53 | + this->numInlets = 1; |
| 54 | + this->numOutlets = 1; |
| 55 | + |
| 56 | + _inletParams[0] = new float(); // frequency |
| 57 | + *(float *)&_inletParams[0] = 0.0f; |
| 58 | + |
| 59 | + _outletParams[0] = new float(); // midi [0 - 127] |
| 60 | + *(float *)&_outletParams[0] = 0.0f; |
| 61 | + |
| 62 | + this->initInletsState(); |
| 63 | + |
| 64 | + frequency = 440.0f; |
| 65 | + lastNote = frequencyToPitch(frequency); |
| 66 | + |
| 67 | + loaded = false; |
| 68 | + |
| 69 | + this->width *= 1.4f; |
| 70 | +} |
| 71 | + |
| 72 | +//-------------------------------------------------------------- |
| 73 | +void FrequencyToNote::newObject(){ |
| 74 | + PatchObject::setName( this->objectName ); |
| 75 | + |
| 76 | + this->addInlet(VP_LINK_NUMERIC,"frequency"); |
| 77 | + |
| 78 | + this->addOutlet(VP_LINK_NUMERIC,"midi note"); |
| 79 | + |
| 80 | +} |
| 81 | + |
| 82 | +//-------------------------------------------------------------- |
| 83 | +void FrequencyToNote::setupObjectContent(shared_ptr<ofAppGLFWWindow> &mainWindow){ |
| 84 | + unusedArgs(mainWindow); |
| 85 | +} |
| 86 | + |
| 87 | +//-------------------------------------------------------------- |
| 88 | +void FrequencyToNote::updateObjectContent(map<int,shared_ptr<PatchObject>> &patchObjects){ |
| 89 | + unusedArgs(patchObjects); |
| 90 | + |
| 91 | + if(this->inletsConnected[0]){ |
| 92 | + frequency = ofClamp(*(float *)&_inletParams[0],1.0f,13289.0f); |
| 93 | + } |
| 94 | + |
| 95 | + *(float *)&_outletParams[0] = frequencyToPitch(frequency); |
| 96 | + lastNote = static_cast<int>(*(float *)&_outletParams[0]); |
| 97 | + |
| 98 | + if(!loaded){ |
| 99 | + loaded = true; |
| 100 | + lastNote = static_cast<int>(floor(this->getCustomVar("MIDI_NOTE"))); |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +//-------------------------------------------------------------- |
| 106 | +void FrequencyToNote::drawObjectContent(ofTrueTypeFont *font, shared_ptr<ofBaseGLRenderer>& glRenderer){ |
| 107 | + unusedArgs(font,glRenderer); |
| 108 | + |
| 109 | + ofSetColor(255); |
| 110 | +} |
| 111 | + |
| 112 | +//-------------------------------------------------------------- |
| 113 | +void FrequencyToNote::drawObjectNodeGui( ImGuiEx::NodeCanvas& _nodeCanvas ){ |
| 114 | + |
| 115 | + // CONFIG GUI inside Menu |
| 116 | + if(_nodeCanvas.BeginNodeMenu()){ |
| 117 | + |
| 118 | + ImGui::Separator(); |
| 119 | + ImGui::Separator(); |
| 120 | + ImGui::Separator(); |
| 121 | + |
| 122 | + if (ImGui::BeginMenu("CONFIG")) |
| 123 | + { |
| 124 | + |
| 125 | + drawObjectNodeConfig(); this->configMenuWidth = ImGui::GetWindowWidth(); |
| 126 | + |
| 127 | + ImGui::EndMenu(); |
| 128 | + } |
| 129 | + |
| 130 | + _nodeCanvas.EndNodeMenu(); |
| 131 | + } |
| 132 | + |
| 133 | + // Visualize (Object main view) |
| 134 | + if( _nodeCanvas.BeginNodeContent(ImGuiExNodeView_Visualise) ){ |
| 135 | + |
| 136 | + ImGui::Dummy(ImVec2(-1,IMGUI_EX_NODE_CONTENT_PADDING*scaleFactor)); |
| 137 | + |
| 138 | + ImGui::PushItemWidth(90*scaleFactor); |
| 139 | + ImGui::InputFloat("frequency",&frequency); |
| 140 | + ImGui::PopItemWidth(); |
| 141 | + ImGui::Spacing(); |
| 142 | + ImGui::Spacing(); |
| 143 | + ImGui::Spacing(); |
| 144 | + ImGui::Text("Midi note: %s", ofToString(lastNote).c_str()); |
| 145 | + ImGui::Spacing(); |
| 146 | + ImGui::Text("Notation: %s", notation_2[static_cast<int>(lastNote)].c_str()); |
| 147 | + |
| 148 | + _nodeCanvas.EndNodeContent(); |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +//-------------------------------------------------------------- |
| 154 | +void FrequencyToNote::drawObjectNodeConfig(){ |
| 155 | + ImGuiEx::ObjectInfo( |
| 156 | + "Convert a frequency in Hz to his correspondent quantized note", |
| 157 | + "https://mosaic.d3cod3.org/reference.php?r=frequency-to-note", scaleFactor); |
| 158 | +} |
| 159 | + |
| 160 | +//-------------------------------------------------------------- |
| 161 | +void FrequencyToNote::removeObjectContent(bool removeFileFromData){ |
| 162 | + unusedArgs(removeFileFromData); |
| 163 | +} |
| 164 | + |
| 165 | +//-------------------------------------------------- |
| 166 | +float FrequencyToNote::frequencyToPitch(float freq){ |
| 167 | + return pdsp::f2p(freq); |
| 168 | +} |
| 169 | + |
| 170 | +OBJECT_REGISTER( FrequencyToNote, "frequency to note", OFXVP_OBJECT_CAT_SOUND) |
| 171 | + |
| 172 | +#endif |
0 commit comments