Can someone have a quick look at the script below and tell me if there is errors. I can’t seem to get it to run without compilation errors.
Being fully transparent I’m trying to utilize chat GPT to assist me in this, but I really think AI hasn’t come along as far as people might think…lol
#include "DaisyDuino.h"
// Create instances of the low-pass and high-pass filters using Svf
Svf lpFilter, hpFilter;
// Rotary encoder pins
#define ENC_A_PIN D2
#define ENC_B_PIN D3
// Variables for rotary encoder
Encoder enc;
int32_t encPos = 0;
int32_t prevEncPos = 0;
// Audio buffer size
const size_t blockSize = 48; // Adjust according to your needs
// Daisy Pod hardware
static DaisyHardware pod;
// Audio callback function
void AudioCallback(float **in, float **out, size_t size)
{
for (size_t i = 0; i < size; i++)
{
// Read input audio sample from ADC or other source
float input = in[0][i];
// Process audio samples through the filters
lpFilter.Process(input); // Update internal state of the low-pass filter
float lpOutput = lpFilter.Low(); // Get low-pass output
hpFilter.Process(lpOutput); // Update internal state of the high-pass filter
float bpOutput = hpFilter.High(); // Get high-pass output
// Store processed audio in output buffer or send to DAC or other destination
out[0][i] = bpOutput;
}
}
void setup()
{
// Initialize Daisy hardware as a Pod with a 48kHz sample rate
pod = DAISY.init(DAISY_POD, AUDIO_SR_48K);
// Initialize the rotary encoder
enc.Init(ENC_A_PIN, ENC_B_PIN, 0); // Pins A, B, and an optional click button pin (0 for
none)
// Set the audio block size for processing
DAISY.setAudioBlockSize(blockSize);
// Initialize the filters
lpFilter.Init(AUDIO_SR_48K);
hpFilter.Init(AUDIO_SR_48K);
// Set default cutoff frequencies
lpFilter.SetFreq(1000.0f); // 1000 Hz cutoff for low-pass filter
lpFilter.SetRes(0.707f); // Q = 0.707 for low-pass filter
hpFilter.SetFreq(100.0f); // 100 Hz cutoff for high-pass filter
hpFilter.SetRes(0.707f); // Q = 0.707 for high-pass filter
// Start audio processing
DAISY.begin(AudioCallback);
}
void loop()
{
// Read rotary encoder position
encPos += enc.Process();
if (encPos != prevEncPos)
{
// Map encoder position to cutoff frequency range
float newCutoffFreq = constrain(encPos * 10.0f + 1000.0f, 20.0f, 20000.0f); // Scale and
limit
// Update low-pass filter cutoff frequency
lpFilter.SetFreq(newCutoffFreq);
// Update high-pass filter cutoff frequency
hpFilter.SetFreq(newCutoffFreq * 0.1f); // Example: Scale HP cutoff lower
prevEncPos = encPos;
}
}
First thing to learn is how to post pre-formatted text, so somebody can easily grab your source code and try to compile it.
“can’t seem to get it to run without compilation errors.” - that’s not much information.
1 Like
Sorry, I was updating from my phone, so formatting wasn’t noticed, and again new this this specific project. I’ve spent many hours learning the ESP32 device and have some knowledge and have built projects for brain training into certain frequencies, but the Daisy seed is new to me and that’s why I’m here, to learn.
Learning proper formatting is one, how to setup the POD for using it’s functions, and also I’m thinking, breaking this out into steps for each function.
- Getting the input audio to filter on the output jack between 120Hz down to 20Hz.
- Using an OLED screen (if possible) to display the audio signal cutoff frequency on the screen when using the rotary encoder
- Having a type of FFT listen for specific frequencies on the input (ONLY above 18500Hz) for two dominant frequencies and then use PWM to “follow” that signals behaviour for use with Audio Strobe signals for hypnagogic light (brain training) used in many “Mind Machines” - I own the Dream Machine and the Kasina that work WONDERS for anxiety and depression, but are out of reach for many cost wise, and my goal is to help others and build something for ALL who may need it.
- Code in step 1. would be used for sub frequency transducers to ultimately HEAR, FEEL, and have LIGHT train or coach the brain into calming states with the use or support of the Daisy Seed.
Anyone willing to help guide to success, would be “partners” in this along the way, and I think you in advance for the support.
I may post some ESP code as well to maybe help understand my attempts for those interested.
Here is an attempt to convert an ESP32 code to the Daisy Seed for review to maybe get a better picture at what I’m trying to do somewhat.
#include "daisy_seed.h"
#include "daisysp.h"
#include <FFT.h> // Keep this library if you still plan on using FFT for frequency analysis
using namespace daisy;
using namespace daisysp;
DaisySeed hw; // Daisy Seed hardware object
FFT fft; // FFT object for frequency analysis
// Define LED pins (replace with actual GPIO mappings for Daisy Seed)
const dsy_gpio_pin LEFT_RED_PIN = {DSY_GPIOB, 2};
const dsy_gpio_pin LEFT_GREEN_PIN = {DSY_GPIOB, 4};
const dsy_gpio_pin LEFT_BLUE_PIN = {DSY_GPIOB, 15};
const dsy_gpio_pin RIGHT_RED_PIN = {DSY_GPIOB, 16};
const dsy_gpio_pin RIGHT_GREEN_PIN = {DSY_GPIOB, 17};
const dsy_gpio_pin RIGHT_BLUE_PIN = {DSY_GPIOB, 18};
const dsy_gpio_pin LEFT_WHITE_PIN = {DSY_GPIOB, 19};
const dsy_gpio_pin RIGHT_WHITE_PIN = {DSY_GPIOB, 21};
// Audio processing parameters
const int SAMPLE_RATE = 44100;
const int WINDOW_SIZE = 128; // Window size for FFT must be a power of 2
// Frequency thresholds for LED control
const float RED_THRESHOLD = 18700.0;
const float GREEN_THRESHOLD = 19200.0;
const float BLUE_THRESHOLD = 19700.0;
const float AUDIO_THRESHOLD = 19200.0;
const float REFERENCE_TONE_FREQUENCY = 18200.0; // Reference tone frequency
// Flag to indicate if reference tone is detected
bool referenceToneDetected = false;
// FFT arrays
double real[WINDOW_SIZE];
double imag[WINDOW_SIZE];
double dominantFrequency;
// Initialize GPIO pins for LED control
void InitLEDs()
{
dsy_gpio led_pins[] = {
{LEFT_RED_PIN}, {LEFT_GREEN_PIN}, {LEFT_BLUE_PIN},
{RIGHT_RED_PIN}, {RIGHT_GREEN_PIN}, {RIGHT_BLUE_PIN},
{LEFT_WHITE_PIN}, {RIGHT_WHITE_PIN},
};
for (int i = 0; i < 8; i++)
{
dsy_gpio_init(&led_pins[i]);
dsy_gpio_write(&led_pins[i], 0); // Turn off LEDs initially
}
}
// Set the state of a specific LED pin
void SetLEDState(dsy_gpio_pin pin, bool state)
{
dsy_gpio led_pin = {pin};
dsy_gpio_write(&led_pin, state);
}
// Initialize the Daisy Seed hardware
void InitDaisySeed()
{
hw.Configure();
hw.Init();
hw.SetAudioBlockSize(WINDOW_SIZE); // Set block size to match FFT window
hw.StartAudio(AudioCallback);
InitLEDs(); // Initialize LED pins
}
// Audio callback function for real-time processing
void AudioCallback(float **in, float **out, size_t size)
{
// Load input audio into FFT buffer
for (size_t i = 0; i < size; i++)
{
fft.Input(in[0][i]); // Mono input
}
// Perform FFT to analyze the audio frequencies
fft.Process();
dominantFrequency = fft.GetDominantFrequency();
// Check if the reference tone is detected
if (dominantFrequency == REFERENCE_TONE_FREQUENCY)
{
referenceToneDetected = true;
hw.PrintLine("Reference tone detected!"); // Debug message
}
// Control LEDs based on the detected frequency
if (referenceToneDetected)
{
if (dominantFrequency >= RED_THRESHOLD)
{
SetLEDState(LEFT_RED_PIN, true);
SetLEDState(RIGHT_RED_PIN, true);
hw.PrintLine("Red LEDs activated!"); // Debug message
}
else if (dominantFrequency >= GREEN_THRESHOLD)
{
SetLEDState(LEFT_GREEN_PIN, true);
SetLEDState(RIGHT_GREEN_PIN, true);
hw.PrintLine("Green LEDs activated!"); // Debug message
}
else if (dominantFrequency >= BLUE_THRESHOLD)
{
SetLEDState(LEFT_BLUE_PIN, true);
SetLEDState(RIGHT_BLUE_PIN, true);
hw.PrintLine("Blue LEDs activated!"); // Debug message
}
else if (dominantFrequency >= AUDIO_THRESHOLD)
{
SetLEDState(LEFT_WHITE_PIN, true);
SetLEDState(RIGHT_WHITE_PIN, true);
hw.PrintLine("White LEDs activated!"); // Debug message
}
else
{
// Turn off all LEDs if no valid frequency is detected
SetLEDState(LEFT_RED_PIN, false);
SetLEDState(LEFT_GREEN_PIN, false);
SetLEDState(LEFT_BLUE_PIN, false);
SetLEDState(RIGHT_RED_PIN, false);
SetLEDState(RIGHT_GREEN_PIN, false);
SetLEDState(RIGHT_BLUE_PIN, false);
SetLEDState(LEFT_WHITE_PIN, false);
SetLEDState(RIGHT_WHITE_PIN, false);
}
}
}
// Main setup function
void Setup()
{
InitDaisySeed(); // Initialize Daisy Seed and GPIO for LEDs
fft.Init(SAMPLE_RATE, WINDOW_SIZE); // Initialize FFT with sample rate and window size
}
// Main loop function (Daisy Seed generally runs on callbacks)
void loop()
{
// Audio processing happens in the callback; nothing to do here
}