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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
/**
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/>.
**/
//
// Producer.cpp
// modularSynth
//
// Created by Ryan Challinor on 11/13/13.
//
//
#include "Producer.h"
#include "IAudioReceiver.h"
#include "Sample.h"
#include "SynthGlobals.h"
#include "ModularSynth.h"
#include "Profiler.h"
const float mBufferX = 5;
const float mBufferY = 80;
const float mBufferW = 900;
const float mBufferH = 300;
Producer::Producer()
{
mWriteBuffer = new float[gBufferSize];
Clear(mWriteBuffer, gBufferSize);
mSample = new Sample();
for (int i = 0; i < PRODUCER_NUM_BIQUADS; ++i)
{
AddChild(&mBiquad[i]);
mBiquad[i].SetPosition(150 + 100 * i, mBufferY + 10);
mBiquad[i].SetFilterType(kFilterType_Lowpass);
mBiquad[i].SetFilterParams(1600, sqrt(2) / 2);
}
}
void Producer::CreateUIControls()
{
IDrawableModule::CreateUIControls();
mVolumeSlider = new FloatSlider(this, "volume", 5, 20, 110, 15, &mVolume, 0, 2);
mPlayCheckbox = new Checkbox(this, "play", 5, 60, &mPlay);
mLoopCheckbox = new Checkbox(this, "loop", 55, 60, &mLoop);
mClipStartSlider = new FloatSlider(this, "start", mBufferX, mBufferY + mBufferH - 30, 900, 15, &mClipStart, 0, gSampleRate * 200);
mClipEndSlider = new FloatSlider(this, "end", mBufferX, mBufferY + mBufferH - 15, 900, 15, &mClipEnd, 0, gSampleRate * 200);
mZoomStartSlider = new FloatSlider(this, "zoomstart", mBufferX, mBufferY + mBufferH + 5, 900, 15, &mZoomStart, 0, gSampleRate * 200);
mZoomEndSlider = new FloatSlider(this, "zoomend", mBufferX, mBufferY + mBufferH + 20, 900, 15, &mZoomEnd, 0, gSampleRate * 200);
mNumBarsSlider = new IntSlider(this, "num bars", 215, 3, 220, 15, &mNumBars, 1, 16);
mOffsetSlider = new FloatSlider(this, "off", 215, 20, 110, 15, &mOffset, -1, 1, 4);
mWriteButton = new ClickButton(this, "write", 600, 50);
mDoubleLengthButton = new ClickButton(this, "double", 600, 10);
mHalveLengthButton = new ClickButton(this, "halve", 600, 28);
mTempoSlider = new FloatSlider(this, "tempo", 490, 10, 100, 15, &mTempo, 30, 200);
mStartOffsetSlider = new IntSlider(this, "start", 490, 28, 100, 15, &mStartOffset, 0, gSampleRate * 4);
mCalcTempoButton = new ClickButton(this, "calc tempo", 490, 46);
mRestartButton = new ClickButton(this, "restart", 100, 60);
for (int i = 0; i < PRODUCER_NUM_BIQUADS; ++i)
mBiquad[i].CreateUIControls();
}
Producer::~Producer()
{
delete[] mWriteBuffer;
delete mSample;
}
void Producer::Process(double time)
{
PROFILER(Producer);
IAudioReceiver* target = GetTarget();
if (!mEnabled || target == nullptr || mSample == nullptr || mPlay == false)
return;
ComputeSliders(0);
int bufferSize = target->GetBuffer()->BufferSize();
float* out = target->GetBuffer()->GetChannel(0);
assert(bufferSize == gBufferSize);
float volSq = mVolume * mVolume;
const float* data = mSample->Data()->GetChannel(0);
int numSamples = mSample->LengthInSamples();
for (int i = 0; i < bufferSize; ++i)
{
if (mPlayhead < numSamples)
{
out[i] += data[mPlayhead] * volSq;
}
else
{
mPlay = false;
out[i] = 0; //fill the rest with zero
}
mPlayhead += 1;
while (IsSkipMeasure(GetMeasureForSample(mPlayhead)))
{
mPlayhead += GetSamplesPerMeasure();
}
if (mLoop && mPlayhead > mClipEnd)
mPlayhead = mClipStart;
}
ChannelBuffer buff(out, bufferSize);
for (int i = 0; i < PRODUCER_NUM_BIQUADS; ++i)
mBiquad[i].ProcessAudio(gTime, &buff);
GetVizBuffer()->WriteChunk(out, bufferSize, 0);
}
void Producer::FilesDropped(std::vector<std::string> files, int x, int y)
{
mSample->Reset();
mSample->Read(files[0].c_str());
mClipStart = 0;
mClipEnd = mSample->LengthInSamples();
mZoomStart = 0;
mZoomEnd = mClipEnd;
mZoomStartSlider->SetExtents(0, mClipEnd);
mZoomEndSlider->SetExtents(0, mClipEnd);
UpdateZoomExtents();
}
void Producer::DropdownClicked(DropdownList* list)
{
}
void Producer::DropdownUpdated(DropdownList* list, int oldVal, double time)
{
}
void Producer::UpdateSample()
{
}
void Producer::ButtonClicked(ClickButton* button, double time)
{
if (button == mWriteButton)
{
DoWrite();
}
if (button == mDoubleLengthButton)
{
float newEnd = (mClipEnd - mClipStart) * 2 + mClipStart;
if (newEnd < mSample->LengthInSamples())
{
mClipEnd = newEnd;
mNumBars *= 2;
}
}
if (button == mHalveLengthButton)
{
if (mNumBars % 2 == 0)
{
float newEnd = (mClipEnd - mClipStart) / 2 + mClipStart;
mClipEnd = newEnd;
mNumBars /= 2;
}
}
if (button == mCalcTempoButton)
{
if (mClipStart < mClipEnd)
{
float samplesPerMeasure = mClipEnd - mClipStart;
float secondsPerMeasure = samplesPerMeasure / gSampleRate;
mTempo = 1 / secondsPerMeasure * 4 * 60 * mNumBars;
mStartOffset = ((mClipStart / samplesPerMeasure) - int(mClipStart / samplesPerMeasure)) * samplesPerMeasure;
}
}
if (button == mRestartButton)
mPlayhead = mClipStart;
}
void Producer::DoWrite()
{
if (mSample)
{
ChannelBuffer sample(mSample->LengthInSamples());
sample.CopyFrom(mSample->Data());
for (int i = 0; i < PRODUCER_NUM_BIQUADS; ++i)
mBiquad[i].ProcessAudio(gTime, &sample);
float* toWrite = new float[mSample->LengthInSamples()];
int pos = 0;
for (int i = 0; i < mSample->LengthInSamples(); ++i)
{
if (IsSkipMeasure(GetMeasureForSample(i)) == false)
{
toWrite[pos] = sample.GetChannel(0)[i];
++pos;
}
}
Sample::WriteDataToFile(ofGetTimestampString("producer/producer_%Y-%m-%d_%H-%M.wav").c_str(), &toWrite, pos);
mClipStart = 0;
mClipEnd = mSample->LengthInSamples();
mOffset = 0;
mZoomStart = 0;
mZoomEnd = mClipEnd;
mZoomStartSlider->SetExtents(0, mClipEnd);
mZoomEndSlider->SetExtents(0, mClipEnd);
UpdateZoomExtents();
}
}
void Producer::UpdateZoomExtents()
{
mClipStartSlider->SetExtents(mZoomStart, mZoomEnd);
mClipEndSlider->SetExtents(mZoomStart, mZoomEnd);
}
int Producer::GetMeasureSample(int measure)
{
return mStartOffset + measure * GetSamplesPerMeasure();
}
float Producer::GetBufferPos(int sample)
{
return (sample - mZoomStart) / (mZoomEnd - mZoomStart);
}
int Producer::GetMeasureForSample(int sample)
{
return (sample - mStartOffset) / GetSamplesPerMeasure();
}
int Producer::GetSamplesPerMeasure()
{
return gSampleRate / (mTempo / 60 / 4);
}
bool Producer::IsSkipMeasure(int measure)
{
return ListContains(measure, mSkipMeasures);
}
void Producer::DrawModule()
{
if (Minimized() || IsVisible() == false)
return;
mVolumeSlider->Draw();
mPlayCheckbox->Draw();
mLoopCheckbox->Draw();
if (mSample)
{
ofPushMatrix();
ofTranslate(mBufferX, mBufferY);
ofPushStyle();
mSample->LockDataMutex(true);
DrawAudioBuffer(mBufferW, mBufferH, mSample->Data(), mZoomStart, mZoomEnd, (int)mPlayhead);
mSample->LockDataMutex(false);
ofFill();
for (int measure = 0; GetMeasureSample(measure) < mZoomEnd; ++measure)
{
if (GetMeasureSample(measure) >= mZoomStart)
{
float pos = GetBufferPos(GetMeasureSample(measure));
ofSetColor(0, 0, 255);
ofRect(pos * mBufferW, 0, 1, mBufferH);
if (IsSkipMeasure(measure))
{
ofSetColor(255, 0, 0, 100);
ofRect(pos * mBufferW, 0, (GetBufferPos(GetMeasureSample(measure + 1)) - pos) * mBufferW, mBufferH);
}
}
}
/*int start = ofMap(mClipStart, 0, length, 0, width, true);
int end = ofMap(mClipEnd, 0, length, 0, width, true);
for (int i = 0; i < mNumBars; i++)
{
float barSpacing = float(end-start)/mNumBars;
int x = barSpacing * i + start;
x += barSpacing * -mOffset;
ofSetColor(255,255,0);
ofLine(x, 0, x, height);
}
ofSetColor(255,0,0);
ofLine(start,0,start,height);
ofLine(end,0,end,height);
ofSetColor(0,255,0);
int position = ofMap(pos, 0, length, 0, width, true);
ofLine(position,0,position,height);*/
ofPopStyle();
ofPopMatrix();
mClipStartSlider->Draw();
mClipEndSlider->Draw();
mZoomStartSlider->Draw();
mZoomEndSlider->Draw();
mNumBarsSlider->Draw();
mOffsetSlider->Draw();
mWriteButton->Draw();
mDoubleLengthButton->Draw();
mHalveLengthButton->Draw();
mTempoSlider->Draw();
mStartOffsetSlider->Draw();
mCalcTempoButton->Draw();
mRestartButton->Draw();
if (mSample)
DrawTextNormal(ofToString(mSample->GetPlayPosition()), 335, 50);
}
for (int i = 0; i < PRODUCER_NUM_BIQUADS; ++i)
mBiquad[i].Draw();
}
void Producer::OnClicked(float x, float y, bool right)
{
IDrawableModule::OnClicked(x, y, right);
if (right)
return;
if (x >= mBufferX && y >= mBufferY + 100 && x < mBufferX + mBufferW && y < mBufferY + mBufferH)
{
if (IsKeyHeld('x'))
{
float pos = (x - mBufferX) / mBufferW;
float sample = pos * (mZoomEnd - mZoomStart) + mZoomStart;
int measure = GetMeasureForSample(sample);
if (IsSkipMeasure(measure))
mSkipMeasures.remove(measure);
else
mSkipMeasures.push_back(measure);
}
else
{
mPlayhead = ofMap(x, mBufferX, mBufferX + mBufferW, mZoomStart, mZoomEnd, true);
}
}
}
void Producer::CheckboxUpdated(Checkbox* checkbox, double time)
{
if (checkbox == mPlayCheckbox)
{
if (mSample)
mSample->Reset();
}
}
void Producer::GetModuleDimensions(float& width, float& height)
{
width = 910;
height = 430;
}
void Producer::FloatSliderUpdated(FloatSlider* slider, float oldVal, double time)
{
if (slider == mClipStartSlider)
{
if (mSample && mClipStart > mSample->LengthInSamples())
mClipStart = mSample->LengthInSamples();
}
if (slider == mClipEndSlider)
{
if (mSample && mClipEnd > mSample->LengthInSamples())
mClipEnd = mSample->LengthInSamples();
}
if (slider == mZoomStartSlider)
{
if (mSample && mZoomStart > mSample->LengthInSamples())
mZoomStart = mSample->LengthInSamples();
UpdateZoomExtents();
}
if (slider == mZoomEndSlider)
{
if (mSample && mZoomEnd > mSample->LengthInSamples())
mZoomEnd = mSample->LengthInSamples();
UpdateZoomExtents();
}
}
void Producer::IntSliderUpdated(IntSlider* slider, int oldVal, double time)
{
}
void Producer::PlayNote(double time, int pitch, int velocity, int voiceIdx, ModulationParameters modulation)
{
if (mSample)
{
mPlay = false;
if (pitch == 16)
{
mSample->Reset();
}
else if (pitch >= 0 && pitch < 16 && velocity > 0)
{
int slice = (pitch / 8) * 8 + 7 - (pitch % 8);
int barLength = (mClipEnd - mClipStart) / mNumBars;
int position = -mOffset * barLength + (barLength / 4) * slice + mClipStart;
mSample->Play(time, 1, position);
}
}
}
void Producer::LoadLayout(const ofxJSONElement& moduleInfo)
{
mModuleSaveData.LoadString("target", moduleInfo);
SetUpFromSaveData();
}
void Producer::SetUpFromSaveData()
{
SetTarget(TheSynth->FindModule(mModuleSaveData.GetString("target")));
}
|