Hi Everyone!
I’ll start off by saying I’m quite new to the Daisy platform (and DSP in general), but am very excited by the applications that it offers. I’ve dabbled with some of the examples for Seed, and taken some inspiration from posts here on the forum, but my latest endeavor is trying to make the “Looper” example for the Petal work on my Seed board, in a breadboard with I/O and power broken out. I’ve had success compiling and uploading my edited example code, but the functionality of the looper isn’t there… (I get audio, and my volume control seems to work, however). I’m sure I’m making some GLARING errors to more experienced programmers, so any and all advice/help would be greatly appreciated.
Many thanks!
#include "DaisyDuino.h"
#define MAX_SIZE (48000 * 60 * 5) // 5 mins of floats at 48kHz
static DaisyHardware hw;
Switch button;
float volume;
bool first = true; //first loop (sets length)
bool rec = false; //currently recording
bool play = false; //currently playing
int pos = 0;
float DSY_SDRAM_BSS buf[MAX_SIZE];
int mod = MAX_SIZE;
int len = 0; //Length
float dry = 0;
float wet = 0;
bool res = false;
void ResetBuffer();
void Controls();
void NextSamples(float &output, float *in, size_t i);
static void AudioCallback(float *in, float *out, size_t size) {
float output = 0;
Controls();
for (size_t i = 0; i < size; i ++) {
NextSamples(output, in, i);
output = output * volume;
out[i] = out[i + 1] = output;
}
}
void setup() {
// INIT
hw = DAISY.init(DAISY_PETAL, AUDIO_SR_48K);
ResetBuffer();
button.Init(1000, true, 28, 0);
rec = false;
play = false;
DAISY.begin(AudioCallback);
}
void loop() {
}
// Resets the buffer
void ResetBuffer() {
play = false;
rec = false;
first = true;
pos = 0;
len = 0;
for (int i = 0; i < mod; i++) {
buf[i] = 0;
}
mod = MAX_SIZE;
}
void UpdateButtons() {
// switch1 pressed
if (button.Pressed()) {
if (first && rec) {
first = false;
mod = len;
len = 0;
}
res = true;
play = true;
rec = !rec;
}
// switch1 held
if (button.TimeHeldMs() >= 1000 && res) {
ResetBuffer();
res = false;
}
// switch1 pressed and not empty buffer
if (button.Pressed() && !(!rec && first)) {
play = !play;
rec = false;
}
}
void Controls() {
button.Debounce();
volume = (map(analogRead(A0), 0.0, 1023.0, 0.0, 100.0) / 100.00);
UpdateButtons();
}
void WriteBuffer(float *in, size_t i) {
buf[pos] = buf[pos] * 0.5 + in[i] * 0.5;
if (first) {
len++;
}
}
void NextSamples(float &output, float *in, size_t i) {
if (rec) {
WriteBuffer(in, i);
}
output = buf[pos];
// automatic looptime
if (len >= MAX_SIZE) {
first = false;
mod = MAX_SIZE;
len = 0;
}
if (play) {
pos++;
pos %= mod;
}
if (!rec) {
output = output * 0.5 + in[i] * (1 - 0.5);
}
}