Here is the code:
Wednesday, January 28, 2015
BitCrushing with the Arduino
Now that I have the previous examples working fairly easily its time to do some more interesting effects with the Arduino due. Time to make a bit crusher! This blog will be all about downsampling the input signal in real time. This creates an effect called aliasing. Aliasing happens when then audio frequency being processed is below the Nyquist rate. It creates really grainy distorted sounds. I personally love this effect. I like digital effects that try to sound digital but hate digital effects that try to sound analog.
Here is the code:
#define MAX_ADC_RESOLUTION 12
void setup() {
// Serial.begin(9600);
REG_ADC_MR = (REG_ADC_MR & 0xFFF0FFFF) | 0x00020000;
analogReadResolution(MAX_ADC_RESOLUTION); //
analogWriteResolution(MAX_ADC_RESOLUTION);
//adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX*2, 3);
pinMode(ADC0,INPUT);
pinMode(DAC0,OUTPUT);
}
void loop() {
int value = sampleAndHold(analogRead(ADC0), lfo());
analogWrite(DAC0, value);
}
int i=0;
int hold;
int sampleAndHold(int input, int holdTime){
if(i==0){
hold=input;
i++;
}else if(i < holdTime){
i++;
}else if(i >= holdTime){
i=0;
}
return hold;
}
int _lfo=0;
int wait=0;
int lfo(){
if(wait==0){
_lfo++;
wait++;
if(_lfo >= 500){
_lfo=0;
}
}else{
wait++;
if(wait >= 1000){
wait=0;
}
}
return _lfo;
}
Above a created a really simple sawtooth LFO to change the sample rate time. This controls the rate of the sampleAndHold function that basically stores and replays previous samples until the holdTime is reached. Once the holdTime is reached it samples again and will continue replaying the new sample.
Here is what it sounds like:
Here is the code:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment