This repository was archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (110 loc) · 2.93 KB
/
Copy pathmain.cpp
File metadata and controls
127 lines (110 loc) · 2.93 KB
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
#include "daisy_pod.h"
#include "daisysp.h"
using namespace daisy;
using namespace daisysp;
DaisyPod hw;
Oscillator lfo;
// BPM range: 30 - 240
#define TTEMPO_MIN 30
#define TTEMPO_MAX 240
static uint32_t prev_ms = 0;
static float lfo_out;
static uint16_t tt_count = 0;
// Prototypes
float bpm_to_freq(uint32_t tempo);
uint32_t ms_to_bpm(uint32_t ms);
void HandleSystemRealTime(uint8_t srt_type);
void AudioCallback(AudioHandle::InputBuffer in,
AudioHandle::OutputBuffer out,
size_t size);
float bpm_to_freq(uint32_t tempo)
{
return tempo / 60.0f;
}
uint32_t ms_to_bpm(uint32_t ms)
{
return 60000 / ms;
}
void AudioCallback(AudioHandle::InputBuffer in,
AudioHandle::OutputBuffer out,
size_t size)
{
hw.ProcessAllControls();
if(hw.button1.RisingEdge())
{
uint32_t ms = System::GetNow();
uint32_t diff = ms - prev_ms;
uint32_t bpm = ms_to_bpm(diff);
#ifdef DEBUG
hw.seed.PrintLine("msec=%d, diff=%d, BPM=%d", ms, diff, bpm);
#endif
if(bpm >= TTEMPO_MIN && bpm <= TTEMPO_MAX)
{
// set BPM
lfo.SetFreq(bpm_to_freq(bpm));
}
prev_ms = ms;
}
for(size_t i = 0; i < size; i++)
{
lfo_out = lfo.Process();
out[0][i] = lfo_out * in[0][i];
out[1][i] = lfo_out * in[1][i];
}
}
void HandleSystemRealTime(uint8_t srt_type)
{
#ifdef DEBUG
default: hw.seed.PrintLine("MIDI SystemRealTime: %x", m.srt_type); break;
#endif
// MIDI Clock - 24 clicks per quarter note
if(srt_type == TimingClock)
{
tt_count++;
if(tt_count == 24)
{
uint32_t ms = System::GetNow();
uint32_t diff = ms - prev_ms;
uint32_t bpm = ms_to_bpm(diff);
#ifdef DEBUG
hw.seed.PrintLine("msec=%d, diff=%d, BPM=%d", ms, diff, bpm);
#endif
if(bpm >= TTEMPO_MIN && bpm <= TTEMPO_MAX)
{
lfo.SetFreq(bpm_to_freq(bpm));
}
prev_ms = ms;
tt_count = 0;
}
}
}
int main(void)
{
hw.Init();
hw.SetAudioBlockSize(4); // number of samples handled per callback
hw.SetAudioSampleRate(SaiHandle::Config::SampleRate::SAI_48KHZ);
// LFO Settings
lfo.Init(hw.AudioSampleRate());
lfo.SetWaveform(lfo.WAVE_SIN);
lfo.SetFreq(1.0f);
lfo.SetAmp(1.0f);
#ifdef DEBUG
hw.seed.StartLog(false);
System::Delay(250);
#endif
hw.StartAdc();
hw.StartAudio(AudioCallback);
hw.midi.StartReceive();
while(true)
{
hw.midi.Listen();
while(hw.midi.HasEvents())
{
MidiEvent m = hw.midi.PopEvent();
if(m.type == SystemRealTime)
{
HandleSystemRealTime(m.srt_type);
}
}
}
}