File: StutterControl.cpp

package info (click to toggle)
bespokesynth 1.3.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,716 kB
  • sloc: cpp: 117,136; ansic: 18,752; python: 593; xml: 74; makefile: 4
file content (323 lines) | stat: -rw-r--r-- 10,098 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
    bespoke synth, a software modular synthesizer
    Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/
//
//  StutterControl.cpp
//  Bespoke
//
//  Created by Ryan Challinor on 2/25/15.
//
//

#include "StutterControl.h"
#include "SynthGlobals.h"
#include "ModularSynth.h"
#include "Profiler.h"

StutterControl::StutterControl()
: IAudioProcessor(gBufferSize)
{
   for (int i = 0; i < kNumStutterTypes; ++i)
      mStutter[i] = false;
}

void StutterControl::CreateUIControls()
{
   IDrawableModule::CreateUIControls();

   mGridControlTarget = new GridControlTarget(this, "grid", 5, 145);

   mStutterCheckboxes[kHalf] = new Checkbox(this, "half note", 4, 3, &mStutter[kHalf]);
   mStutterCheckboxes[kQuarter] = new Checkbox(this, "quarter", mStutterCheckboxes[kHalf], kAnchor_Below, &mStutter[kQuarter]);
   mStutterCheckboxes[k8th] = new Checkbox(this, "8th", mStutterCheckboxes[kQuarter], kAnchor_Below, &mStutter[k8th]);
   mStutterCheckboxes[k16th] = new Checkbox(this, "16th", mStutterCheckboxes[k8th], kAnchor_Below, &mStutter[k16th]);
   mStutterCheckboxes[k32nd] = new Checkbox(this, "32nd", mStutterCheckboxes[k16th], kAnchor_Below, &mStutter[k32nd]);
   mStutterCheckboxes[k64th] = new Checkbox(this, "64th", mStutterCheckboxes[k32nd], kAnchor_Below, &mStutter[k64th]);
   mStutterCheckboxes[kReverse] = new Checkbox(this, "reverse", mStutterCheckboxes[k64th], kAnchor_Below, &mStutter[kReverse]);
   mStutterCheckboxes[kRampIn] = new Checkbox(this, "ramp in", mStutterCheckboxes[kReverse], kAnchor_Below, &mStutter[kRampIn]);
   mStutterCheckboxes[kRampOut] = new Checkbox(this, "ramp out", mStutterCheckboxes[kHalf], kAnchor_Right, &mStutter[kRampOut]);
   mStutterCheckboxes[kTumbleUp] = new Checkbox(this, "tumble up", mStutterCheckboxes[kRampOut], kAnchor_Below, &mStutter[kTumbleUp]);
   mStutterCheckboxes[kTumbleDown] = new Checkbox(this, "tumble down", mStutterCheckboxes[kTumbleUp], kAnchor_Below, &mStutter[kTumbleDown]);
   mStutterCheckboxes[kHalfSpeed] = new Checkbox(this, "half speed", mStutterCheckboxes[kTumbleDown], kAnchor_Below, &mStutter[kHalfSpeed]);
   mStutterCheckboxes[kDoubleSpeed] = new Checkbox(this, "double speed", mStutterCheckboxes[kHalfSpeed], kAnchor_Below, &mStutter[kDoubleSpeed]);
   mStutterCheckboxes[kDotted8th] = new Checkbox(this, "dotted eighth", mStutterCheckboxes[kDoubleSpeed], kAnchor_Below, &mStutter[kDotted8th]);
   mStutterCheckboxes[kQuarterTriplets] = new Checkbox(this, "triplets", mStutterCheckboxes[kDotted8th], kAnchor_Below, &mStutter[kQuarterTriplets]);
   mStutterCheckboxes[kFree] = new Checkbox(this, "free", mStutterCheckboxes[kQuarterTriplets], kAnchor_Below, &mStutter[kFree]);
   mFreeLengthSlider = new FloatSlider(this, "free length", mStutterCheckboxes[kFree], kAnchor_Below, 102, 15, &mStutterProcessor.mFreeStutterLength, .005f, .25f);
   mFreeSpeedSlider = new FloatSlider(this, "free speed", mFreeLengthSlider, kAnchor_Below, 102, 15, &mStutterProcessor.mFreeStutterSpeed, 0, 2);
}

void StutterControl::Init()
{
   IDrawableModule::Init();

   mStutterProcessor.Init();
}

StutterControl::~StutterControl()
{
}

void StutterControl::Process(double time)
{
   PROFILER(StutterControl);

   if (!mEnabled)
      return;

   ComputeSliders(0);
   SyncBuffers();

   IAudioReceiver* target = GetTarget();

   if (target)
   {
      mStutterProcessor.ProcessAudio(time, GetBuffer());

      ChannelBuffer* out = target->GetBuffer();
      for (int ch = 0; ch < GetBuffer()->NumActiveChannels(); ++ch)
      {
         Add(out->GetChannel(ch), GetBuffer()->GetChannel(ch), out->BufferSize());
         GetVizBuffer()->WriteChunk(GetBuffer()->GetChannel(ch), GetBuffer()->BufferSize(), ch);
      }
   }

   GetBuffer()->Reset();
}

void StutterControl::DrawModule()
{
   if (Minimized() || IsVisible() == false)
      return;

   mStutterProcessor.DrawStutterBuffer(4, 3, 90, 35);

   for (int i = 0; i < kNumStutterTypes; ++i)
      mStutterCheckboxes[i]->Draw();
   mFreeLengthSlider->Draw();
   mFreeSpeedSlider->Draw();
   mGridControlTarget->Draw();
}

void StutterControl::SendStutter(double time, StutterParams stutter, bool on)
{
   if (on)
      mStutterProcessor.StartStutter(time, stutter);
   else
      mStutterProcessor.EndStutter(time, stutter);
   UpdateGridLights();
}

StutterControl::StutterType StutterControl::GetStutterFromKey(int key)
{
   if (key == 'q')
      return kHalf;
   if (key == 'w')
      return kQuarter;
   if (key == 'e')
      return k8th;
   if (key == 'r')
      return k16th;
   if (key == 't')
      return k32nd;
   if (key == 'y')
      return k64th;
   if (key == 'u')
      return kReverse;
   if (key == 'a')
      return kRampIn;
   if (key == 's')
      return kRampOut;
   if (key == 'd')
      return kTumbleUp;
   if (key == 'f')
      return kTumbleDown;
   if (key == 'g')
      return kHalfSpeed;
   if (key == 'h')
      return kDoubleSpeed;
   if (key == 'j')
      return kDotted8th;
   if (key == 'k')
      return kQuarterTriplets;
   if (key == 'l')
      return kFree;
   return kNumStutterTypes;
}

void StutterControl::GetModuleDimensions(float& width, float& height)
{
   width = 175;
   height = 172;
}

StutterParams StutterControl::GetStutter(StutterControl::StutterType type)
{
   switch (type)
   {
      case kHalf:
         return StutterParams(kInterval_2n, 1);
      case kQuarter:
         return StutterParams(kInterval_4n, 1);
      case k8th:
         return StutterParams(kInterval_8n, 1);
      case k16th:
         return StutterParams(kInterval_16n, 1);
      case k32nd:
         return StutterParams(kInterval_32n, 1);
      case k64th:
         return StutterParams(kInterval_64n, 1);
      case kReverse:
         return StutterParams(kInterval_2n, -1);
      case kRampIn:
         return StutterParams(kInterval_8n, 0, 1, 500);
      case kRampOut:
         return StutterParams(kInterval_2n, 1, 0, 700);
      case kTumbleUp:
         return StutterParams(kInterval_8n, 1, 10, 10000);
      case kTumbleDown:
         return StutterParams(kInterval_32n, 1, 0, 2000);
      case kHalfSpeed:
         return StutterParams(kInterval_8n, .5f);
      case kDoubleSpeed:
         return StutterParams(kInterval_8n, 2);
      case kDotted8th:
         return StutterParams(kInterval_8nd, 1);
      case kQuarterTriplets:
         return StutterParams(kInterval_4nt, 1);
      case kFree:
         return StutterParams(kInterval_None, 1);
      case kNumStutterTypes:
         break;
   }
   assert(false);
   return StutterParams(kInterval_None, 1);
}

void StutterControl::CheckboxUpdated(Checkbox* checkbox, double time)
{
   if (checkbox == mEnabledCheckbox)
      mStutterProcessor.SetEnabled(time, IsEnabled());

   for (int i = 0; i < kNumStutterTypes; ++i)
   {
      if (checkbox == mStutterCheckboxes[i])
      {
         SendStutter(time, GetStutter((StutterType)i), mStutter[i]);
      }
   }
}

void StutterControl::FloatSliderUpdated(FloatSlider* slider, float oldVal, double time)
{
}

void StutterControl::OnControllerPageSelected()
{
   UpdateGridLights();
}

void StutterControl::OnGridButton(int x, int y, float velocity, IGridController* grid)
{
   int index = x + y * grid->NumCols();
   double time = NextBufferTime(false);
   if (index < kNumStutterTypes)
   {
      mStutter[index] = velocity > 0;
      SendStutter(time, GetStutter((StutterType)index), mStutter[index]);
   }
}

void StutterControl::PlayNote(double time, int pitch, int velocity, int voiceIdx, ModulationParameters modulation)
{
   int index = pitch % kNumStutterTypes;
   mStutter[index] = velocity > 0;
   SendStutter(time, GetStutter((StutterType)index), mStutter[index]);
}

void StutterControl::UpdateGridLights()
{
   if (mGridControlTarget == nullptr || mGridControlTarget->GetGridController() == nullptr)
      return;

   for (int i = 0; i < kNumStutterTypes; ++i)
   {
      mGridControlTarget->GetGridController()->SetLight(i % mGridControlTarget->GetGridController()->NumCols(), i / mGridControlTarget->GetGridController()->NumCols(), mStutter[i] ? kGridColor1Bright : kGridColor1Dim);
   }
}

bool StutterControl::OnPush2Control(Push2Control* push2, MidiMessageType type, int controlIndex, float midiValue)
{
   if (type == kMidiMessage_Note)
   {
      if (controlIndex >= 36 && controlIndex <= 99)
      {
         int gridIndex = controlIndex - 36;
         int x = gridIndex % 8;
         int y = 7 - gridIndex / 8;

         if (y < 2)
         {
            int index = x + y * 8;
            mStutter[index] = midiValue > 0;
            SendStutter(gTime, GetStutter((StutterType)index), mStutter[index]);
         }

         return true;
      }
   }

   return false;
}

void StutterControl::UpdatePush2Leds(Push2Control* push2)
{
   for (int x = 0; x < 8; ++x)
   {
      for (int y = 0; y < 8; ++y)
      {
         int pushColor;

         if (y < 2)
         {
            int index = x + y * 8;
            if (mStutter[index])
               pushColor = 2;
            else
               pushColor = 1;
         }
         else
         {
            pushColor = 0;
         }

         push2->SetLed(kMidiMessage_Note, x + (7 - y) * 8 + 36, pushColor);
      }
   }
}

void StutterControl::LoadLayout(const ofxJSONElement& moduleInfo)
{
   mModuleSaveData.LoadString("target", moduleInfo);

   SetUpFromSaveData();
}

void StutterControl::SetUpFromSaveData()
{
   SetTarget(TheSynth->FindModule(mModuleSaveData.GetString("target")));
}