Arduino-Pedal-Effects/c/variable_pitch_shift.ino

264 lines
5 KiB
Arduino
Raw Permalink Normal View History

2022-07-18 16:23:07 +00:00
// RING BUFFER VARIABLE PITCH SHIFT
// via www.Electrosmash.com
// via OpenMusicLabs
//defining hardware resources.
#define LED 13
#define FOOTSWITCH 12
#define TOGGLE 2
#define PUSHBUTTON_1 A5
#define PUSHBUTTON_2 A4
//defining the output PWM parameters
#define PWM_FREQ 0x00FF // pwm frequency - 31.3KHz
#define PWM_MODE 0 // Fast (1) or Phase Correct (0)
#define PWM_QTY 2 // 2 PWMs in parallel
#define BUFF_SIZE 500 // size of ring buffer, can theoretically be anything
// 500 should hold 15.9 ms of audio
//
// STRUCTURES
struct button_time_s
{
long footswitch;
long pb1;
long pb2;
};
struct count_s
{
int main;
int interrupt;
};
struct switch_flag_s
{
bool current;
bool old;
};
struct ring_buff_s
{
int buff[BUFF_SIZE];
int head;
int tail;
};
struct tempo_s
{
int interval;
long timeout;
};
struct divider_s
{
int input;
int output;
};
// VARIABLES
int input, vol_variable=512;
byte ADC_low, ADC_high;
bool led_flag;
int offset = 5;
long milli;
button_time_s button_time;
count_s count;
switch_flag_s switch_flag;
ring_buff_s buf;
divider_s divider;
tempo_s tempo;
// BUFFER FUNCTIONS
void push_to_buff(int in)
{
buf.buff[buf.head] = in;
buf.head++;
if (buf.head == BUFF_SIZE)
{
buf.head = 0;
}
}
int pop_from_buff (void)
{
int out = buf.buff[buf.tail];
if (buf.tail != buf.head)
{
buf.tail++;
}
else
{
buf.buff[buf.tail]--;
}
if (buf.tail == BUFF_SIZE)
{
buf.tail = 0;
}
return out;
}
void setup()
{
//setup IO
Serial.begin(9600);
pinMode(FOOTSWITCH, INPUT_PULLUP);
pinMode(PUSHBUTTON_1, INPUT_PULLUP);
pinMode(PUSHBUTTON_2, INPUT_PULLUP);
pinMode(LED, OUTPUT);
// setup ADC
ADMUX = 0x60; // left adjust, adc0, internal vcc
ADCSRA = 0xe5; // turn on adc, ck/32, auto trigger
ADCSRB = 0x07; // t1 capture for trigger
DIDR0 = 0x01; // turn off digital inputs for adc0
// setup PWM
TCCR1A = (((PWM_QTY - 1) << 5) | 0x80 | (PWM_MODE << 1)); //
TCCR1B = ((PWM_MODE << 3) | 0x11); // ck/1
TIMSK1 = 0x20; // interrupt on capture interrupt
ICR1H = (PWM_FREQ >> 8);
ICR1L = (PWM_FREQ & 0xff);
DDRB |= ((PWM_QTY << 1) | 0x02); // turn on outputs
sei(); // turn on interrupts - not really necessary with arduino
tempo.interval = 150;
divider.input = 0;
divider.output = 9;
}
void tempo_setter()
{
if (milli > tempo.timeout)
{
led_flag = !led_flag;
digitalWrite(LED, led_flag);
tempo.timeout = milli + tempo.interval;
}
}
void tempo_getter()
{
if (digitalRead(PUSHBUTTON_1) == false)
{
if (milli > button_time.pb1)
{
if (milli - button_time.pb1 > 150)
{
tempo.interval = milli - button_time.pb1;
tempo.timeout = milli + tempo.interval;
}
button_time.pb1 = milli;
}
}
}
void pitch_getter()
{
if (digitalRead(PUSHBUTTON_2) == false)
{
if (milli - button_time.pb2 > 150)
{
divider.output++;
if (divider.output > 15)
{
divider.output = 0;
}
if (divider.output == 1)
{
divider.output = 2;
}
button_time.pb2 = milli;
}
}
}
// MAIN LOOP
// checks the footswitch is pushed every 100 loops
// changes the switch_flag
// if switch flag is true call functions
// else just set LED here,
// doesn't need to optimised if no audio is being processed
// increment count
void loop()
{
if (count.main % 100 == 0)
{
switch_flag.current = digitalRead(FOOTSWITCH);
if (switch_flag.current != switch_flag.old)
{
button_time.footswitch = millis();
digitalWrite(LED, true);
Serial.println(button_time.footswitch);
}
switch_flag.old = switch_flag.current;
if (switch_flag.current == true)
{
milli = millis();
pitch_getter();
tempo_getter();
tempo_setter();
}
else
{
digitalWrite(LED, false);
}
}
count.main++;
}
// AUDIO INPUT
// pulls the audio from the ADC
// pushes it to the ring buffer
void audio_inputter(int divide)
{
ADC_low = ADCL; // you need to fetch the low byte first
ADC_high = ADCH;
input = ((ADC_high << 8) | ADC_low); // make a signed 16b value
if (input > 0x8000)
{
input = input + 0x8000;
}
push_to_buff(input);
}
// AUDIO OUTPUTTER
// if the eqn is != 0
// pop audio from ring buffer and output to PWM
void audio_outputter(int divide)
{
if (milli - button_time.footswitch > offset)
{
// Serial.println("YE");
if (count.interrupt % divide != 0)
{
int output = pop_from_buff();
OCR1AL = ((output + 0x8000) >> 8); // convert to unsigned, send out high byte
OCR1BL = output; // send out low byt
}
}
}
// TIMER1 INTERRUPT
// checks the switch flag is true
// if LED flag is true, apply effect
// else do not apply effect
ISR(TIMER1_CAPT_vect)
{
if (switch_flag.current == true)
{
if (led_flag == true)
{
audio_inputter(divider.input);
audio_outputter(divider.output);
}
else
{
audio_inputter(0);
audio_outputter(0);
}
count.interrupt++;
}
}