Hey all,
I am working on a little effect board with two potis and three buttons, Line In and Out. I have a Teensy 4.0 and an Audio Shield Rev D and i realized it only works when i plug the usb cable into my mac mini. It doesnt work when being plugged via USB to the power socket.
I think it has something to do with my code, because this doesnt happen with the Part 1 Hardware Test with the beep.
I post my code at the bottom.
Any idea?
Thanks in advance!
All the best,
Norbert
I am working on a little effect board with two potis and three buttons, Line In and Out. I have a Teensy 4.0 and an Audio Shield Rev D and i realized it only works when i plug the usb cable into my mac mini. It doesnt work when being plugged via USB to the power socket.
I think it has something to do with my code, because this doesnt happen with the Part 1 Hardware Test with the beep.
I post my code at the bottom.
Any idea?
Thanks in advance!
All the best,
Norbert
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
const int myInput = AUDIO_INPUT_LINEIN;
// const int myInput = AUDIO_INPUT_MIC;
// GUItool: begin automatically generated code
AudioInputI2S audioInput; //xy=1084.3333206176758,1320.3333282470703
AudioFilterStateVariable filter3; //xy=1427.3333206176758,1094.3333282470703
AudioFilterStateVariable filter2; //xy=1490.3333206176758,1454.3333282470703
AudioFilterLadder ladder1; //xy=1494.3333206176758,1513.3333282470703
AudioFilterStateVariable filter1; //xy=1495.6666107177734,1230.6666069030762
AudioFilterLadder ladder2; //xy=1505.3333168029785,1290.6666584014893
AudioMixer4 mixer1; //xy=1661.3333587646484,1175.0000915527344
AudioMixer4 mixer3; //xy=1668.0000667572021,1422.0000972747803
AudioAnalyzePeak peak1; //xy=1694.3333206176758,1039.3333282470703
AudioEffectFreeverb freeverb1; //xy=1787.3333206176758,1342.3333282470703
AudioEffectFlange flange1; //xy=1789.3333206176758,1304.3333282470703
AudioEffectFreeverb freeverb2; //xy=1839.3333206176758,1493.3333282470703
AudioEffectFlange flange2; //xy=1840.3333206176758,1450.3333282470703
AudioMixer4 mixer2; //xy=1940.3333206176758,1209.3333282470703
AudioMixer4 mixer4; //xy=2010.3333206176758,1468.3333282470703
AudioMixer4 mixer5; //xy=2229.333320617676,1157.3333282470703
AudioEffectDelay delay1; //xy=2243.333320617676,1297.3333282470703
AudioMixer4 mixer6; //xy=2246.333320617676,1420.3333282470703
AudioEffectDelay delay2; //xy=2255.333320617676,1522.3333282470703
AudioOutputI2S i2s1; //xy=2573.333320617676,1062.3333282470703
AudioConnection patchCord1(audioInput, 0, filter1, 0);
AudioConnection patchCord2(audioInput, 0, ladder2, 0);
AudioConnection patchCord3(audioInput, 0, filter3, 0);
AudioConnection patchCord4(audioInput, 0, mixer1, 0);
AudioConnection patchCord5(audioInput, 1, filter2, 0);
AudioConnection patchCord6(audioInput, 1, ladder1, 0);
AudioConnection patchCord7(audioInput, 1, mixer3, 0);
AudioConnection patchCord8(filter3, 0, peak1, 0);
AudioConnection patchCord9(filter2, 2, mixer3, 1);
AudioConnection patchCord10(ladder1, 0, mixer3, 2);
AudioConnection patchCord11(filter1, 2, mixer1, 1);
AudioConnection patchCord12(ladder2, 0, mixer1, 2);
AudioConnection patchCord13(mixer1, flange1);
AudioConnection patchCord14(mixer1, 0, mixer2, 0);
AudioConnection patchCord15(mixer1, freeverb1);
AudioConnection patchCord16(mixer3, flange2);
AudioConnection patchCord17(mixer3, freeverb2);
AudioConnection patchCord18(mixer3, 0, mixer4, 0);
AudioConnection patchCord19(freeverb1, 0, mixer2, 2);
AudioConnection patchCord20(flange1, 0, mixer2, 1);
AudioConnection patchCord21(freeverb2, 0, mixer4, 2);
AudioConnection patchCord22(flange2, 0, mixer4, 1);
AudioConnection patchCord23(mixer2, 0, mixer5, 0);
AudioConnection patchCord24(mixer4, 0, mixer6, 0);
AudioConnection patchCord25(mixer5, delay1);
AudioConnection patchCord26(mixer5, 0, i2s1, 0);
AudioConnection patchCord27(delay1, 0, mixer5, 1);
AudioConnection patchCord28(mixer6, 0, i2s1, 1);
AudioConnection patchCord29(mixer6, delay2);
AudioConnection patchCord30(delay2, 0, mixer6, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=1102.666675567627,1398.0000495910645
// GUItool: end automatically generated code
// Define analog pins for Potentiometers A2 and A3
const int potiA2Pin = A2;
const int potiA3Pin = A3;
// Button pins and states
const int buttonPin1 = 0; // Pin for the button
const int buttonPin2 = 1; // Pin for the button
const int buttonPin3 = 2; // Pin for the button
bool button1Pressed = false; // Button 1 state
bool button2Pressed = false; // Button 2 state
bool button3Pressed = false; // Button 3 state
bool delayTimeSet = false; // Flag to track if the delay time has been set
unsigned long lastBeatTime = 0; // Time of the last detected beat
unsigned long interval = 0; // Measured time interval between beats
// Parameters for the low-pass filter
const float cutoffFrequency = 250.0; // Cutoff frequency in Hz
// Number of samples in each delay line
#define FLANGE_DELAY_LENGTH (AUDIO_BLOCK_SAMPLES)
// Allocate the delay lines for left and right channels
short l_delayline[FLANGE_DELAY_LENGTH];
short r_delayline[FLANGE_DELAY_LENGTH];
int s_idx = FLANGE_DELAY_LENGTH / 17;
int s_depth = FLANGE_DELAY_LENGTH /17;
float s_freq = 0.5;
double mod_freq = 0.5;
//#define NUM_SAMPLES 10 // Number of samples to average
//// SETUP //////////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial) ;
// Audio initialization
AudioMemory(1000); // Allocate memory for the audio library
sgtl5000_1.enable(); // Enable audio shield
sgtl5000_1.volume(0.5); // Set initial output volume
// Configure the low-pass filter
filter1.frequency(cutoffFrequency); // Corrected parenthesis
// Set up button pins
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
// Set up flanger
flange1.begin(l_delayline, FLANGE_DELAY_LENGTH, s_idx, s_depth, s_freq);
flange2.begin(r_delayline, FLANGE_DELAY_LENGTH, s_idx, s_depth, s_freq);
// Set initial mixer gain levels to 0 for Mixer 2 and Mixer 4
mixer2.gain(0, 0); // Port 0
mixer2.gain(1, 0); // Port 1
mixer2.gain(2, 0); // Port 2
mixer4.gain(0, 0); // Port 0
mixer4.gain(1, 0); // Port 1
mixer4.gain(2, 0); // Port 2
// Set initial mixer gain levels to 0 for Mixer 1 and Mixer 3
mixer1.gain(0, 0); // Port 0
mixer1.gain(1, 0); // Port 1
mixer1.gain(2, 0); // Port 2
mixer3.gain(0, 0); // Port 0
mixer3.gain(1, 0); // Port 1
mixer3.gain(2, 0); // Port 2
mixer5.gain(0, 1); // Port 0
mixer5.gain(1, 0); // Port 1
mixer6.gain(0, 1); // Port 0
mixer6.gain(1, 0); // Port 1
}
///////////////////////////////L O O P /////////////////////////////////
void loop() {
// Read potentiometer values
int potiA2Value = analogRead(potiA2Pin); // Read Poti A2 value
int potiA3Value = analogRead(potiA3Pin); // Read Poti A3 value
// Calculate the levels for each mixer input based on the Poti A3 value
float gainPort0A3 = calcGainPort0(potiA3Value);
float gainPort1A3 = calcGainPort1(potiA3Value);
float gainPort2A3 = calcGainPort2(potiA3Value);
// Set the mixer gains for Poti A3
mixer2.gain(0, gainPort0A3); // Set gain for Port 0
mixer2.gain(1, gainPort1A3); // Set gain for Port 1
mixer2.gain(2, gainPort2A3); // Set gain for Port 2
mixer4.gain(0, gainPort0A3); // Set gain for Port 0
mixer4.gain(1, gainPort1A3); // Set gain for Port 1
mixer4.gain(2, gainPort2A3); // Set gain for Port 2
// Calculate the levels for each mixer input based on the Poti A2 value
float gainPort0A2 = calcGainPort0(potiA2Value);
float gainPort1A2 = calcGainPort1(potiA2Value);
float gainPort2A2 = calcGainPort2(potiA2Value);
// Set the mixer gains for Poti A2
mixer1.gain(0, gainPort0A2); // Set gain for Port 0
mixer1.gain(1, gainPort1A2); // Set gain for Port 1
mixer1.gain(2, gainPort2A2); // Set gain for Port 2
mixer3.gain(0, gainPort0A2); // Set gain for Port 0
mixer3.gain(1, gainPort1A2); // Set gain for Port 1
mixer3.gain(2, gainPort2A2); // Set gain for Port 2
// Set the frequency and resonance of the ladder effect based on gainPort2A2
setLadderParameters(gainPort2A2);
/// Set the frequency of gain Filter
setFilterParameters(gainPort1A2);
///Set the Room Size
setRoomSize(gainPort2A3);
// Read the smoothed potentiometer value
//int smoothedPotiA3Value = readPotentiometerSmooth(gainPort1A3);
// Set the flanger frequency based on the smoothed value
setFlangerFrequency(gainPort1A3);
// Print potentiometer values to Serial Monitor for debugging
Serial.print("Poti A3 Value: ");
Serial.println(potiA3Value); // Print value of Poti A2
Serial.print("gainPort1A3 :");
Serial.println(gainPort1A3);
Serial.print("gainPort2A3 :");
Serial.println(gainPort2A3);
// Continuously measure beat interval
measureBeatInterval();
// Read button states
bool currentButton1State = digitalRead(buttonPin1) == LOW;
bool currentButton2State = digitalRead(buttonPin2) == LOW;
bool currentButton3State = digitalRead(buttonPin3) == LOW;
//////// BUTTON 1 LOGIC
if (currentButton1State && !button1Pressed) {
button1Pressed = true;
// Calculate adjusted interval
unsigned long adjustedInterval = (interval == 110 || interval == 0) ? 0 : interval;
// If the adjusted interval is valid (not 0), set the delay
if (adjustedInterval != 0 && !delayTimeSet) {
delay1.delay(0, adjustedInterval); // Set the delay time
delay2.delay(0, adjustedInterval); // Set the delay time
delayTimeSet = true; // Mark that the delay time has been set
mixer5.gain(0, 0.4); // Set mixer volume for input 0
mixer5.gain(1, 0.75); // Set mixer volume for input 1
mixer6.gain(0, 0.4); // Set mixer volume for input 0
mixer6.gain(1, 0.75); // Set mixer volume for input 1
Serial.print("Delay time set to: ");
Serial.print(adjustedInterval);
Serial.println(" ms");
}
} else if (!currentButton1State && button1Pressed) {
button1Pressed = false;
delayTimeSet = false; // Allow delay time to be set again
mixer5.gain(0, 0.95); // Reset mixer volume for input 0
mixer5.gain(1, 0.0); // Reset mixer volume for input 1
mixer6.gain(0, 0.95); // Reset mixer volume for input 0
mixer6.gain(1, 0.0); // Reset mixer volume for input 1
Serial.println("Button 1 released.");
}
//////// BUTTON 2 LOGIC
if (currentButton2State && !button2Pressed) {
button2Pressed = true;
Serial.println("Button 2 pressed");
delay1.delay(0, potiA3Value / 2.8); // Set the delay time
delay2.delay(0, potiA3Value / 2.8); // Set the delay time
mixer5.gain(0, 0.45); // Set mixer volume for input 0
mixer5.gain(1, 0.6); // Set mixer volume for input 1
mixer6.gain(0, 0.45); // Set mixer volume for input 0
mixer6.gain(1, 0.6); // Set mixer volume for input 1
} else if(!currentButton2State && button2Pressed) {
button2Pressed = false;
Serial.println("Button 2 released.");
mixer5.gain(0, 0.95); // Reset mixer volume for input 0
mixer5.gain(1, 0.0); // Reset mixer volume for input 1
mixer6.gain(0, 0.95); // Reset mixer volume for input 0
mixer6.gain(1, 0.0); // Reset mixer volume for input 1
}
//////// BUTTON 3 LOGIC
if (currentButton3State && !button3Pressed) {
button3Pressed = true;
Serial.println("Button 3 pressed");
delay1.delay(0, potiA2Value / 31); // Set the delay time
delay2.delay(0, potiA2Value / 31); // Set the delay time
Serial.println("POTIA2 Value ->>>>>>>.");
Serial.println(potiA2Value);
mixer5.gain(0, 0.5); // Set mixer volume for input 0
mixer5.gain(1, 0.7); // Set mixer volume for input 1
mixer6.gain(0, 0.5); // Set mixer volume for input 0
mixer6.gain(1, 0.7); // Set mixer volume for input 1
} else if (!currentButton3State && button3Pressed) {
button3Pressed = false;
//Serial.println("Button 3 released.");
mixer5.gain(0, 0.95); // Reset mixer volume for input 0
mixer5.gain(1, 0.0); // Reset mixer volume for input 1
mixer6.gain(0, 0.95); // Reset mixer volume for input 0
mixer6.gain(1, 0.0); // Reset mixer volume for input 1
}
// Add a small delay to avoid reading the potentiometer too frequently
delay(30);
}
///////////////////////// HIER IST DER NORMALE LOOP ZUENDE UND ES KOMMEN VOIDS /////////////////////////////
// CLEAN LEVEL Function to calculate gain for Port 0
float calcGainPort0(int potiValue) {
if (potiValue >= 509 && potiValue <= 514) {
return 1.0; // 100% gain when within range 500 to 523
} else if (potiValue < 514) {
return (float)potiValue / 514.0; // Gradually decrease from 100%
} else {
return 1.0 - (float)(potiValue - 514) / 514.0; // Gradual decrease from 100%
}
}
// Function to calculate gain for Port 1
float calcGainPort1(int potiValue) {
if (potiValue >= 514 && potiValue <= 1023) {
return (float)(potiValue - 514) / 514.0; // Gradually increase to 100%
} else {
return 0; // Default 0% gain at lower ranges
}
}
// Function to calculate gain for Port 2
float calcGainPort2(int potiValue) {
if (potiValue <= 509) {
return 1.0 - (float)potiValue / 509.0; // Gradually increase as potiValue approaches 0
} else {
return 0; // Default 0% gain if above 499
}
}
// Function to set the frequency and resonance of the ladder effect based on gain
void setLadderParameters(float gainPort1) {
// Map the gain value to an appropriate frequency range
float frequency = map(gainPort1 * 1000.0, 500, 1000, 5000.0, 150.0); // Map gain to frequency between 100 Hz and 2000 Hz
ladder1.frequency(frequency); // Set
ladder2.frequency(frequency); // Set
// Set the resonance (level) for both ladder filters to 1.8
ladder1.resonance(0.8); // Set resonance for ladder1
ladder2.resonance(0.8); // Set resonance for ladder2
}
// Function to set the frequency of filter1 and filter2 based on gain
void setFilterParameters(float gainPort2) {
// Map the gain value to an appropriate frequency range
float frequency = map(gainPort2 * 1000.0, 500, 1000, 100.0, 4000.0); // Map gain to frequency between 100 Hz and 5000 Hz
filter1.frequency(frequency); // Set frequency for filter1
filter2.frequency(frequency); // Set frequency for filter2
// Set the resonance (Q) for both filters to 5
filter1.resonance(4.0); // Set resonance for filter1
filter2.resonance(4.0); // Set resonance for filter2
}
// Function to set room size for the freeverb effects
void setRoomSize(float gainPort1) {
// Map the gainPortA31 to a suitable room size range
// Assuming room size range is between 0 (no room) and 1 (max room size)
float roomSizeValue = map(gainPort1 * 1023, 0, 1000, 0.0, 0.9);
freeverb1.roomsize(roomSizeValue); // Set room size for freeverb 1
freeverb2.roomsize(roomSizeValue); // Set room size for freeverb 2
}
// Function to set the frequency of the flanger effect based on gainPort1A3
void setFlangerFrequency(float gainPort2) {
// Map gainPort1A3 to a frequency range suitable for the flanger effect
float flangerFreq = map(gainPort2 * 1023, 390, 1023, 0.3, 5000.0); // Map to 0.1 Hz to 1 Hz
// Update the global variable s_freq for the flanger effect
s_freq = flangerFreq;
// Reinitialize the flanger with the new frequency by adjusting the delay index and depth
flange1.voices(s_idx,s_depth,s_freq);
flange2.voices(s_idx,s_depth,s_freq);
// Optionally print the frequency for debugging
//Serial.print("Flanger Frequency (s_freq): ");
//Serial.println(s_freq);
}
///// MEASURE BEAT INTERVAL
void measureBeatInterval() {
// Read the peak level
float peakLevel = peak1.read();
// Simple beat detection
const float threshold = 0.14; // Adjust this value based on observed levels
if (peakLevel > threshold) {
unsigned long currentTime = millis();
// If this is the first beat detected
if (lastBeatTime == 0) {
lastBeatTime = currentTime;
Serial.println("First beat detected.");
} else {
// Calculate time interval between beats
interval = currentTime - lastBeatTime;
lastBeatTime = currentTime;
Serial.print("Interval: ");
Serial.print(interval);
Serial.println(" ms");
}
delay(100); // Debounce
}
}