1 /* B.Shapr 2 * Beat / envelope shaper LV2 plugin 3 * 4 * Copyright (C) 2019 by Sven Jähnichen 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 3, or (at your option) 9 * any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software Foundation, 18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 19 */ 20 21 #ifndef BSHAPR_HPP_ 22 #define BSHAPR_HPP_ 23 24 #include <cmath> 25 #include <array> 26 #include <vector> 27 #include <lv2/lv2plug.in/ns/lv2core/lv2.h> 28 #include <lv2/lv2plug.in/ns/ext/atom/atom.h> 29 #include <lv2/lv2plug.in/ns/ext/atom/util.h> 30 #include <lv2/lv2plug.in/ns/ext/atom/forge.h> 31 #include <lv2/lv2plug.in/ns/ext/urid/urid.h> 32 #include <lv2/lv2plug.in/ns/ext/time/time.h> 33 #include <lv2/lv2plug.in/ns/ext/state/state.h> 34 #include "Globals.hpp" 35 #include "Urids.hpp" 36 #include "BUtilities/Point.hpp" 37 #include "Node.hpp" 38 #include "Shape.hpp" 39 #include "BShaprNotifications.hpp" 40 41 42 #define MAX_F_ORDER 12 43 #define P_ORDER 6 44 #define PITCHBUFFERTIME 20 45 #define PITCHFADERTIME 2 46 #define DELAYBUFFERTIME 20 47 #define MINOPTIONVALUE -20000 48 #define MAXOPTIONVALUE 20000 49 50 struct AudioBuffer 51 { 52 AudioBuffer (); 53 AudioBuffer (const uint32_t size); 54 ~AudioBuffer (); 55 float* frames; 56 double wPtr1, wPtr2, rPtr1, rPtr2; 57 uint32_t size; 58 void resize (const uint32_t size); 59 void reset (); 60 }; 61 62 class Message 63 { 64 public: 65 Message (); 66 void clearMessages (); 67 void setMessage (MessageNr messageNr); 68 void deleteMessage (MessageNr messageNr); 69 bool isMessage (MessageNr messageNr); 70 MessageNr loadMessage (); 71 bool isScheduled (); 72 private: 73 uint32_t messageBits; 74 bool scheduled; 75 }; 76 77 class Fader 78 { 79 public: 80 Fader (); 81 Fader (const float value, const float speed); 82 void setTarget (const float target); 83 void setSpeed (const float speed); 84 float proceed (); 85 float getValue () const; 86 87 protected: 88 float value; 89 float target; 90 float speed; 91 }; 92 93 class BShapr 94 { 95 public: 96 BShapr (double samplerate, const LV2_Feature* const* features); 97 ~BShapr(); 98 void connect_port (uint32_t port, void *data); 99 void run (uint32_t n_samples); 100 LV2_State_Status state_save(LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features); 101 LV2_State_Status state_restore(LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const* features); 102 103 LV2_URID_Map* map; 104 105 private: 106 void fillFilterBuffer (float filterBuffer[MAXSHAPES] [MAX_F_ORDER / 2], const float value); 107 bool isAudioOutputConnected (int shapeNr); 108 void audioLevel (const float input1, const float input2, float* output1, float* output2, const float amp); 109 void stereoBalance (const float input1, const float input2, float* output1, float* output2, const float balance); 110 void stereoWidth (const float input1, const float input2, float* output1, float* output2, const float width); 111 void lowPassFilter (const float input1, const float input2, float* output1, float* output2, const float cutoffFreq, const int shape); 112 void highPassFilter (const float input1, const float input2, float* output1, float* output2, const float cutoffFreq, const int shape); 113 void pitch (const float input1, const float input2, float* output1, float* output2, const float semitone, const int shape); 114 void delay (const float input1, const float input2, float* output1, float* output2, const float delaytime, const int shape); 115 void doppler (const float input1, const float input2, float* output1, float* output2, const float delaytime, const int shape); 116 void decimate (const float input1, const float input2, float* output1, float* output2, const float hz, const int shape); 117 void distortion (const float input1, const float input2, float* output1, float* output2, const int mode, const float drive, const float limit); 118 void bitcrush (const float input1, const float input2, float* output1, float* output2, const float bitNr); 119 120 #ifdef SUPPORTS_CV 121 void sendCv (const float input1, const float input2, float* output1, float* output2, float* cv, const float amp); 122 #else 123 void sendMidi (const float input1, const float input2, float* output1, float* output2, const uint8_t midiCh, const uint8_t midiCC, const float amp, const uint32_t frames, const int shape); 124 #endif 125 126 void play(uint32_t start, uint32_t end); 127 void notifyMonitorToGui (); 128 void notifyShapeToGui (int shapeNr); 129 void notifyMessageToGui (); 130 void notifyStatusToGui (); 131 double getPositionFromBeats (double beats); 132 double getPositionFromFrames (uint64_t frames); 133 double getPositionFromSeconds (double seconds); 134 135 double rate; 136 float bpm; 137 float speed; 138 uint64_t bar; 139 float barBeat; 140 float beatsPerBar; 141 uint32_t beatUnit; 142 143 double position; 144 double offset; 145 uint64_t refFrame; 146 147 // Audio buffers 148 float* audioInput1; 149 float* audioInput2; 150 float* audioOutput1; 151 float* audioOutput2; 152 AudioBuffer audioBuffer1 [MAXSHAPES]; 153 AudioBuffer audioBuffer2 [MAXSHAPES]; 154 float filter1Buffer1 [MAXSHAPES] [MAX_F_ORDER / 2]; 155 float filter1Buffer2 [MAXSHAPES] [MAX_F_ORDER / 2]; 156 float filter2Buffer1 [MAXSHAPES] [MAX_F_ORDER / 2]; 157 float filter2Buffer2 [MAXSHAPES] [MAX_F_ORDER / 2]; 158 float decimateBuffer1 [MAXSHAPES]; 159 float decimateBuffer2 [MAXSHAPES]; 160 double decimateCounter [MAXSHAPES]; 161 uint8_t sendValue [MAXSHAPES]; 162 163 Fader factors[MAXSHAPES]; 164 165 // Controllers 166 float* new_controllers[NR_CONTROLLERS]; 167 float controllers [NR_CONTROLLERS]; 168 169 // Nodes and Maps 170 Shape<MAXNODES> shapes[MAXSHAPES]; 171 StaticArrayList<Node, MAXNODES> tempNodes[MAXSHAPES]; 172 173 // Atom port 174 BShaprURIDs urids; 175 176 LV2_Atom_Sequence* controlPort; 177 LV2_Atom_Sequence* notifyPort; 178 179 #ifdef SUPPORTS_CV 180 float* cvOutputs[MAXSHAPES]; 181 #endif 182 183 LV2_Atom_Forge forge; 184 LV2_Atom_Forge_Frame notify_frame; 185 186 // Data buffers 187 float nodeBuffer[7]; 188 float shapeBuffer[MAXNODES * 7]; 189 190 // MIDI 191 uint8_t key; 192 193 // Internals 194 bool ui_on; 195 Message message; 196 int monitorPos; 197 unsigned int notificationsCount; 198 float stepCount; 199 std::array<BShaprNotifications, NOTIFYBUFFERSIZE> notifications; 200 bool scheduleNotifyShapes[MAXSHAPES]; 201 bool scheduleNotifyStatus; 202 203 }; 204 205 #endif /* BSHAPR_HPP_ */ 206