Skip to content

Commit

Permalink
break code into different files
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed Apr 2, 2021
1 parent 87b620c commit 06ed251
Show file tree
Hide file tree
Showing 14 changed files with 872 additions and 755 deletions.
44 changes: 39 additions & 5 deletions CMakeLists.txt
Expand Up @@ -3,19 +3,53 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project(bleepbloopmachine CXX)
add_executable(bleepbloopmachine src/main.cc)
add_library(controls src/controls.h src/controls.cc)

add_library(AudioSampleC src/kit/AudioSampleC.h src/kit/AudioSampleC.cpp)
add_library(AudioSampleK src/kit/AudioSampleK.h src/kit/AudioSampleK.cpp)
add_library(AudioSampleS src/kit/AudioSampleS.h src/kit/AudioSampleS.cpp)
add_library(AudioSampleH src/kit/AudioSampleH.h src/kit/AudioSampleH.cpp)
add_library(AudioSampleO src/kit/AudioSampleO.h src/kit/AudioSampleO.cpp)
target_link_arduino_libraries(bleepbloopmachine AUTO_PUBLIC)
target_link_arduino_libraries(controls AUTO_PUBLIC)

target_link_arduino_libraries(AudioSampleK AUTO_PUBLIC)
target_link_arduino_libraries(AudioSampleC AUTO_PUBLIC)
target_link_arduino_libraries(AudioSampleS AUTO_PUBLIC)
target_link_arduino_libraries(AudioSampleH AUTO_PUBLIC)
target_link_arduino_libraries(AudioSampleO AUTO_PUBLIC)
target_link_libraries(bleepbloopmachine PRIVATE controls AudioSampleK AudioSampleC AudioSampleS AudioSampleH AudioSampleO)

add_executable(bleepbloopmachine src/main.h src/main.cc)
target_link_arduino_libraries(bleepbloopmachine AUTO_PUBLIC)

add_library(controls src/controls.h src/controls.cc)
target_link_arduino_libraries(controls AUTO_PUBLIC)

add_library(display src/display.h src/display.cc)
target_link_arduino_libraries(display AUTO_PUBLIC)

add_library(track src/track.h src/track.cc)
target_link_arduino_libraries(track AUTO_PUBLIC)

add_library(block src/block.h src/block.cc)
target_link_arduino_libraries(block AUTO_PUBLIC)

add_library(weirdmath src/weirdmath.h src/weirdmath.cc)
target_link_arduino_libraries(weirdmath AUTO_PUBLIC)

add_library(freq src/freq.h src/freq.cc)
target_link_arduino_libraries(freq AUTO_PUBLIC)

target_link_libraries(track PUBLIC
AudioSampleK
AudioSampleC
AudioSampleS
AudioSampleH
AudioSampleO)

target_link_libraries(bleepbloopmachine PUBLIC
controls
track
block
weirdmath
freq
)

target_enable_arduino_upload(bleepbloopmachine)
140 changes: 140 additions & 0 deletions src/block.cc
@@ -0,0 +1,140 @@
#include "block.h"

float freq[12][9] = {
{
16.3516,
32.7032,
65.40639,
130.8128,
261.6256,
523.2511,
1046.502,
2093.005,
4186.009,
},
{
17.32391,
34.64783,
69.29566,
138.5913,
277.1826,
554.3653,
1108.731,
2217.461,
4434.922,
},
{
18.35405,
36.7081,
73.41619,
146.8324,
293.6648,
587.3295,
1174.659,
2349.318,
4698.636,
},
{
19.44544,
38.89087,
77.78175,
155.5635,
311.127,
622.254,
1244.508,
2489.016,
4978.032,
},
{
20.60172,
41.20344,
82.40689,
164.8138,
329.6276,
659.2551,
1318.51,
2637.02,
5274.041,
},
{
21.82676,
43.65353,
87.30706,
174.6141,
349.2282,
698.4565,
1396.913,
2793.826,
5587.652,
},
{
23.12465,
46.2493,
92.49861,
184.9972,
369.9944,
739.9888,
1479.978,
2959.955,
5919.911,
},
{
24.49971,
48.99943,
97.99886,
195.9977,
391.9954,
783.9909,
1567.982,
3135.963,
6271.927,
},
{
25.95654,
51.91309,
103.8262,
207.6523,
415.3047,
830.6094,
1661.219,
3322.438,
6644.875,
},
{
27.5,
55.0,
110.0,
220.0,
440.0,
880.0,
1760.0,
3520.0,
7040.0,
},
{
29.13524,
58.27047,
116.5409,
233.0819,
466.1638,
932.3275,
1864.655,
3729.31,
7458.62,
},
{
30.86771,
61.73541,
123.4708,
246.9417,
493.8833,
987.7666,
1975.533,
3951.066,
7902.133,
},
};

float SoundBlock::frequency() {
return freq[note][octave];
}
91 changes: 91 additions & 0 deletions src/block.h
@@ -0,0 +1,91 @@
#pragma once
#include "weirdmath.h"

class Block {
public:
bool active = false;
void activate() { active = true; }
void deactivate() { active = false; }
};

class KitBlock : public Block {
public:
int sample;
void sampleUp() { sample = wrapping_add(sample, 4); }
void sampleDown() { sample = wrapping_sub(sample, 4); }
};

#define MAX_ATTACK 800.0
#define MAX_DECAY 800.0

class SoundBlock : public Block {
public:
enum Filter { low };
int note = 0;
int octave = 3;
float attack = 10.0;
float decay = 40.0;
float delay = 0.0;
float pan = 0.0;
float amp = 1.0;
float sustain = 0.0;
float filterType = Filter::low;
float filterQ = 0.618;
float filterFreq = 8000.0;
int filterStage = 0;
float pulseWidth = 0.5;
float frequency();
void activate(int n, int o) {
note = n;
octave = o;
active = true;
}
void filterDown() {
filterFreq = saturating_sub(filterFreq, 10000.0, 100.0, 40.0);
}
void filterUp() {
filterFreq = saturating_add(filterFreq, 10000.0, 100.0, 40.0);
}
void filterQDown() {
filterFreq = saturating_sub(filterFreq, 1.0, 0.0234, 0);
}
void filterQUp() { filterFreq = saturating_add(filterFreq, 1.0, 0.0234, 0); }
void noteUp() { note = wrapping_add(note, 11); }
void noteDown() { note = wrapping_sub(note, 11); }
void octaveUp() { octave = wrapping_add(octave, 8); }
void octaveDown() { octave = wrapping_sub(octave, 8); }
void decayUp() { decay = saturating_add_curve(decay, MAX_DECAY, 10, 0); }
void decayDown() { decay = saturating_sub_curve(decay, MAX_DECAY, 10, 0); }
void delayUp() { delay = saturating_add(delay, 100.0, 10.0, 0); }
void delayDown() { delay = saturating_sub(delay, 100.0, 10.0, 0); }
void ampUp() { amp = saturating_add(amp, 1.0, 0.1, 0); }
void ampDown() { amp = saturating_sub(amp, 1.0, 0.1, 0); }
void pulseWidthUp() { pulseWidth = saturating_add(pulseWidth, 1.0, 0.1, 0); }
void pulseWidthDown() {
pulseWidth = saturating_sub(pulseWidth, 1.0, 0.1, 0);
}
void panLeft() { pan = saturating_sub(pan, 1.0); }
void panRight() { pan = saturating_add(pan, 1.0); }
void attackUp() { attack = saturating_add_curve(attack, MAX_ATTACK, 10, 0); }
void attackDown() { attack = saturating_sub_curve(attack, MAX_ATTACK, 10, 0); }
void sustainUp() { sustain = saturating_add(sustain, 1.0, 0.137, 0); }
void sustainDown() { sustain = saturating_sub(sustain, 1.0, 0.137, 0); }
SoundBlock cut() {
active = false;
SoundBlock s;
s.note = note;
s.octave = octave;
s.active = true;
s.attack = attack;
s.decay = decay;
s.delay = delay;
s.amp = amp;
s.pan = pan;
s.sustain = sustain;
s.filterFreq = filterFreq;
s.filterQ = filterQ;
s.filterType = filterType;
s.pulseWidth = pulseWidth;
return s;
}
};
17 changes: 17 additions & 0 deletions src/controls.cc
Expand Up @@ -94,3 +94,20 @@ uint32_t Controls::readButtons(void) {
uint32_t Controls::justPressed() { return justpressed_buttons; }

uint32_t Controls::justReleased() { return justreleased_buttons; }

Menu1Control &operator++(Menu1Control &s) {
return s = Menu1Control{wrapping_add((int)s, 3)};
}

Menu1Control &operator--(Menu1Control &s) {
return s = Menu1Control{wrapping_sub((int)s, 3)};
}


Menu2Control &operator++(Menu2Control &s) {
return s = Menu2Control{wrapping_add((int)s, 3)};
}

Menu2Control &operator--(Menu2Control &s) {
return s = Menu2Control{wrapping_sub((int)s, 3)};
}
25 changes: 25 additions & 0 deletions src/controls.h
@@ -1,5 +1,7 @@
#pragma once
#include <Arduino.h>
#include "weirdmath.h"

#define BUTTON_CLOCK 48
#define BUTTON_DATA 49
#define BUTTON_LATCH 50
Expand All @@ -23,6 +25,15 @@
#define PAD_LEFT 0x40
#define PAD_RIGHT 0x80

#define KEYLAYER_0 0
#define KEYLAYER_1 1
#define KEYLAYER_2 2
#define KEYLAYER_3 3
#define KEYLAYER_4 4
#define KEYLAYER_5 5

enum class Keymod { a, b, ab, select, start, none };

class Controls {
public:
void begin();
Expand All @@ -38,3 +49,17 @@ class Controls {
uint32_t justpressed_buttons;
uint32_t justreleased_buttons;
};

enum class MenuMode { live, menu1, menu2 };

enum class Menu1Control { env, mod, filter, delay };

enum class Menu2Control { bpm, chain, wave, file };

Menu1Control &operator++(Menu1Control &s);

Menu1Control &operator--(Menu1Control &s);

Menu2Control &operator++(Menu2Control &s);

Menu2Control &operator--(Menu2Control &s);
10 changes: 10 additions & 0 deletions src/display.cc
@@ -0,0 +1,10 @@
#include "display.h"

Point getCoord(int idx) {
int x = idx % 4;
int y = idx / 4;
return Point {
x : GRID_OFFSET_X + (x * (BLOCK_SIZE + GRID_GAP)),
y : GRID_OFFSET_Y + (y * (BLOCK_SIZE + GRID_GAP))
};
}
28 changes: 28 additions & 0 deletions src/display.h
@@ -0,0 +1,28 @@
#pragma once

#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>

#define NEOPIXEL_PIN 8
#define NEOPIXEL_LENGTH 5

#define DISPLAY_CS 44
#define DISPLAY_RST 46
#define DISPLAY_DC 45
#define DISPLAY_BACKLIGHT 47
#define DISPLAY_HEIGHT 128
#define DISPLAY_WIDTH 160

#define BLOCK_SIZE 20
#define GRID_OFFSET_X 36
#define GRID_OFFSET_Y 20
#define GRID_GAP 2
#define STANDARD_BLOCK_COLOR 0xfebb
#define TICK_BLOCK_COLOR 0xfc53

struct Point {
int x;
int y;
};

Point getCoord(int idx);
Empty file added src/freq.cc
Empty file.

0 comments on commit 06ed251

Please sign in to comment.