Skip to content

Commit

Permalink
reorder rotaries + add sampler features + looping arrow
Browse files Browse the repository at this point in the history
  • Loading branch information
topisani committed Jul 23, 2017
1 parent 1c48fbb commit 79b2728
Show file tree
Hide file tree
Showing 11 changed files with 160 additions and 62 deletions.
2 changes: 1 addition & 1 deletion install.sh
Expand Up @@ -7,6 +7,6 @@ kbd() {
}

cmake . &&
make -j4 &&
make -j4
kbd &
bin/top-1
24 changes: 12 additions & 12 deletions src/modules/mixer.cpp
Expand Up @@ -60,26 +60,26 @@ bool MixerScreen::keypress(ui::Key key) {
using namespace ui;
bool shift = GLOB.ui.keys[K_SHIFT];
switch (key) {
case K_RED_UP:
case K_BLUE_UP:
if (shift) module->data.track[0].pan++;
else module->data.track[0].level++;
return true;
case K_RED_DOWN:
case K_BLUE_DOWN:
if (shift) module->data.track[0].pan--;
else module->data.track[0].level--;
return true;
case K_RED_CLICK:
case K_BLUE_CLICK:
module->data.track[0].muted.toggle();
return true;
case K_BLUE_UP:
case K_GREEN_UP:
if (shift) module->data.track[1].pan++;
else module->data.track[1].level++;
return true;
case K_BLUE_DOWN:
case K_GREEN_DOWN:
if (shift) module->data.track[1].pan--;
else module->data.track[1].level--;
return true;
case K_BLUE_CLICK:
case K_GREEN_CLICK:
module->data.track[1].muted.toggle();
return true;
case K_WHITE_UP:
Expand All @@ -93,15 +93,15 @@ bool MixerScreen::keypress(ui::Key key) {
case K_WHITE_CLICK:
module->data.track[2].muted.toggle();
return true;
case K_GREEN_UP:
case K_RED_UP:
if (shift) module->data.track[3].pan++;
else module->data.track[3].level++;
return true;
case K_GREEN_DOWN:
case K_RED_DOWN:
if (shift) module->data.track[3].pan--;
else module->data.track[3].level--;
return true;
case K_GREEN_CLICK:
case K_RED_CLICK:
module->data.track[3].muted.toggle();
return true;
}
Expand All @@ -121,10 +121,10 @@ void MixerScreen::drawMixerSegment(

Colour trackCol;
switch (track) {
case 1: trackCol = Colours::Red; break;
case 2: trackCol = Colours::Blue; break;
case 1: trackCol = Colours::Blue; break;
case 2: trackCol = Colours::Green; break;
case 3: trackCol = Colours::White; break;
case 4: trackCol = Colours::Green; break;
case 4: trackCol = Colours::Red; break;
}
Colour muteCol = (module->data.track[track-1].muted) ? Colours::Red : Colours::Gray60;
float mix = module->data.track[track-1].level;
Expand Down
83 changes: 52 additions & 31 deletions src/modules/sampler.cpp
Expand Up @@ -18,7 +18,8 @@ Sampler::Sampler() :
editScreen (new SampleEditScreen(this)) {

GLOB.events.samplerateChanged.add([&] (uint sr) {
maxSampleSize = 6 * sr;
maxSampleSize = 16 * sr;
sampleSpeed = sampleSampleRate / float(sr);
});

}
Expand All @@ -35,13 +36,15 @@ void Sampler::process(uint nframes) {

for (auto &&voice : data.voiceData) {

float playSpeed = voice.speed * sampleSpeed;

// Process audio
if (voice.playProgress >= 0) {
if (voice.playProgress >= 0 && playSpeed > 0) {
if (voice.fwd()) {
if (voice.loop() && voice.trigger) {
for(int i = 0; i < nframes; ++i) {
GLOB.audioData.proc[i] += sampleData[voice.in + voice.playProgress];
voice.playProgress += 1;
voice.playProgress += playSpeed;
if (voice.playProgress >= voice.length()) {
voice.playProgress = 0;
}
Expand All @@ -50,7 +53,7 @@ void Sampler::process(uint nframes) {
int frms = std::min<int>(nframes, voice.length() - voice.playProgress);
for(int i = 0; i < frms; ++i) {
GLOB.audioData.proc[i] += sampleData[voice.in + voice.playProgress];
voice.playProgress += 1;
voice.playProgress += playSpeed;
}
if (voice.playProgress >= voice.length()) {
voice.playProgress = -1;
Expand All @@ -60,7 +63,7 @@ void Sampler::process(uint nframes) {
if (voice.loop() && voice.trigger) {
for(int i = 0; i < nframes; ++i) {
GLOB.audioData.proc[i] += sampleData[voice.in + voice.playProgress];
voice.playProgress -= 1;
voice.playProgress -= playSpeed;
if (voice.playProgress < 0) {
voice.playProgress = voice.length() -1;
}
Expand All @@ -69,7 +72,7 @@ void Sampler::process(uint nframes) {
int frms = std::min<int>(nframes, voice.playProgress);
for(int i = 0; i < frms; ++i) {
GLOB.audioData.proc[i] += sampleData[voice.in + voice.playProgress];
voice.playProgress -= 1;
voice.playProgress -= playSpeed;
}
}
}
Expand Down Expand Up @@ -99,13 +102,22 @@ void Sampler::load() {
sampleData.resize(rs);
sf.read(sampleData.data(), rs);

// Auto assign voices
// for (uint i = 0; i < nVoices; ++i) {
// auto &&vd = data.voiceData[i];
sampleSampleRate = sf.samplerate;
sampleSpeed = sampleSampleRate / float(GLOB.samplerate);

// vd.in = i * (sampleData.size() / nVoices);
// vd.out = (i + 1) * sampleData.size() / nVoices;
// }
for (auto &&v : data.voiceData) {
v.in.max = rs;
v.out.max = rs;
}

// Auto assign voices
for (uint i = 0; i < nVoices; ++i) {
auto &&vd = data.voiceData[i];
if (vd.in < 0 || vd.out >= rs) {
vd.in = i * (rs / nVoices);
vd.out = (i + 1) * rs / nVoices;
}
}

auto &mwf = editScreen->mainWF;
mwf->clear();
Expand All @@ -120,11 +132,6 @@ void Sampler::load() {
}
editScreen->topWFW.viewRange = {0, wf->size() - 1};

for (auto &&v : data.voiceData) {
v.in.max = sf.size();
v.out.max = sf.size();
}

if (sf.size() == 0) LOGD << "Empty sample file";
sf.close();
}
Expand All @@ -142,21 +149,26 @@ bool module::SampleEditScreen::keypress(ui::Key key) {
using namespace ui;
auto& voice = module->data.voiceData[module->currentVoiceIdx];
switch (key) {
case K_GREEN_UP: voice.in.inc(); return true;
case K_GREEN_DOWN: voice.in.dec(); return true;
case K_BLUE_UP: voice.out.inc(); return true;
case K_BLUE_DOWN: voice.out.dec(); return true;
case K_BLUE_UP: voice.in.inc(); return true;
case K_BLUE_DOWN: voice.in.dec(); return true;
case K_GREEN_UP: voice.out.inc(); return true;
case K_GREEN_DOWN: voice.out.dec(); return true;
case K_WHITE_UP: voice.speed.inc(); return true;
case K_WHITE_DOWN: voice.speed.dec(); return true;
case K_WHITE_CLICK: voice.speed.reset(); return true;
case K_RED_UP: voice.mode.inc(); return true;
case K_RED_DOWN: voice.mode.dec(); return true;
}
}

namespace drawing {

const static drawing::Size topWFsize = {240, 20};
const static drawing::Point topWFpos = {20, 20};
const static drawing::Size arrowSize = {30, 20};
const static drawing::Point arrowPos = {270, 20};
const static drawing::Size topWFsize = {210, 20};
const static drawing::Point topWFpos = {60, 20};
const static drawing::Size arrowSize = {20, 20};
const static drawing::Point arrowPos = {280, 20};
const static drawing::Size pitchSize = {30, 20};
const static drawing::Point pitchPos = {20, 40};
const static drawing::Size mainWFsize = {280, 170};
const static drawing::Point mainWFpos = {20, 50};

Expand All @@ -181,6 +193,8 @@ module::SampleEditScreen::SampleEditScreen(Sampler *m) :
void module::SampleEditScreen::draw(drawing::Canvas &ctx) {
using namespace drawing;

Colour colourCurrent;

ctx.callAt(topWFpos, [&] () {
topWFW.drawRange(ctx, topWFW.viewRange, Colours::TopWF);
for (uint i = 0; i < Sampler::nVoices; ++i) {
Expand Down Expand Up @@ -209,11 +223,11 @@ void module::SampleEditScreen::draw(drawing::Canvas &ctx) {
if (mix < 0) mix = 1;
if (voice.fwd()) mix = 1 - mix; //voice is not reversed

Colour colour = baseColour.mix(Colours::TopWFActive, mix);
colourCurrent = baseColour.mix(Colours::TopWFActive, mix);
topWFW.drawRange(ctx, {
std::size_t(std::round(voice.in / topWF->ratio)),
std::size_t(std::round(voice.out / topWF->ratio))
}, colour);
}, colourCurrent);
}
});

Expand All @@ -239,16 +253,23 @@ void module::SampleEditScreen::draw(drawing::Canvas &ctx) {
icon.colour = Colours::Red;
ctx.drawAt(arrowPos, icon);

ctx.callAt(mainWFpos, [&] () {
ctx.beginPath();
ctx.fillStyle(Colours::White);
ctx.font(FONT_NORM);
ctx.font(15);
ctx.textAlign(TextAlign::Left, TextAlign::Baseline);
ctx.fillText(fmt::format("×{:.2F}", voice.speed.get()), pitchPos);

ctx.callAt(
mainWFpos, [&] () {

mainWFW.lineCol = colourCurrent;
mainWFW.minPx = 5;
mainWFW.viewRange = {
std::size_t(std::round(voice.in / mainWF->ratio)),
std::size_t(std::round(voice.out / mainWF->ratio))};

ctx.beginPath();
ctx.roundedCurve(mainWFW.begin(), mainWFW.end(), -1);
ctx.stroke(Colours::TopWFCur);
mainWFW.draw(ctx);

ctx.beginPath();
ctx.circle(mainWFW.point(mainWFW.viewRange.in), 2);
Expand Down
5 changes: 4 additions & 1 deletion src/modules/sampler.h
Expand Up @@ -19,6 +19,8 @@ class Sampler : public module::SynthModule {

size_t maxSampleSize = 0;
top1::DynArray<float> sampleData;
int sampleSampleRate = 44100;
float sampleSpeed = 1;

std::shared_ptr<SampleEditScreen> editScreen;

Expand All @@ -34,14 +36,15 @@ class Sampler : public module::SynthModule {
};
module::Opt<int> in = {this, "in", 0, 0, -1, 100};
module::Opt<int> out = {this, "out", 0, 0, -1, 100};
module::Opt<float> speed = {this, "speed", 1, 0, 5, 0.01};
module::WrapOpt<int> mode = {this, "mode", 0, -3, 2, 1};

bool fwd() const {return mode >= 0;}
bool bwd() const {return !fwd();}
bool stop() const {return mode == FwdStop || mode == BwdStop;}
bool loop() const {return mode == FwdLoop || mode == BwdLoop;}

int playProgress = -1;
float playProgress = -1;
bool trigger;
int length() const {
return out - in;
Expand Down
1 change: 1 addition & 0 deletions src/ui/canvas.h
Expand Up @@ -263,6 +263,7 @@ class Canvas : public NanoCanvas::Canvas {
return *this;
}

using Super::textAlign;

using Super::fillStyle;
Canvas& fillStyle(const Colour& color) {
Expand Down
27 changes: 13 additions & 14 deletions src/ui/glfw-impl.cpp
@@ -1,5 +1,4 @@
#define GLFW_INCLUDE_ES3
#define GLFW_INCLUDE_GLEXT
#include <GLFW/glfw3.h>
#include <nanocanvas/NanoCanvas.h>
#define NANOVG_GLES3_IMPLEMENTATION
Expand All @@ -25,29 +24,29 @@ static ui::Key keyboardKey(int xKey, int mods) {

// Rotaries
case GLFW_KEY_Q:
if (mods & GLFW_MOD_CONTROL) return K_RED_CLICK;
return K_RED_UP;
case GLFW_KEY_A:
if (mods & GLFW_MOD_CONTROL) return K_RED_CLICK;
return K_RED_DOWN;
case GLFW_KEY_W:
if (mods & GLFW_MOD_CONTROL) return K_BLUE_CLICK;
return K_BLUE_UP;
case GLFW_KEY_S:
case GLFW_KEY_A:
if (mods & GLFW_MOD_CONTROL) return K_BLUE_CLICK;
return K_BLUE_DOWN;
case GLFW_KEY_W:
if (mods & GLFW_MOD_CONTROL) return K_GREEN_CLICK;
return K_GREEN_UP;
case GLFW_KEY_S:
if (mods & GLFW_MOD_CONTROL) return K_GREEN_CLICK;
return K_GREEN_DOWN;
case GLFW_KEY_E:
if (mods & GLFW_MOD_CONTROL) return K_WHITE_CLICK;
return K_WHITE_UP;
case GLFW_KEY_D:
if (mods & GLFW_MOD_CONTROL) return K_WHITE_CLICK;
return K_WHITE_DOWN;
case GLFW_KEY_R:
if (mods & GLFW_MOD_CONTROL) return K_GREEN_CLICK;
return K_GREEN_UP;
if (mods & GLFW_MOD_CONTROL) return K_RED_CLICK;
return K_RED_UP;
case GLFW_KEY_F:
if (mods & GLFW_MOD_CONTROL) return K_GREEN_CLICK;
return K_GREEN_DOWN;
if (mods & GLFW_MOD_CONTROL) return K_RED_CLICK;
return K_RED_DOWN;

case GLFW_KEY_LEFT: return K_LEFT;
case GLFW_KEY_RIGHT: return K_RIGHT;
Expand Down Expand Up @@ -145,8 +144,8 @@ void MainUI::mainRoutine() {
return;
}

glfwSetWindowAspectRatio(window, 4, 3);
glfwSetWindowSizeLimits(window, 320, 240, GLFW_DONT_CARE, GLFW_DONT_CARE);
// glfwSetWindowAspectRatio(window, 4, 3);
// glfwSetWindowSizeLimits(window, 320, 240, GLFW_DONT_CARE, GLFW_DONT_CARE);

glfwSetKeyCallback(window, key);

Expand Down

0 comments on commit 79b2728

Please sign in to comment.