welcome: please sign in
location: Diff for "MiniArDSP"
Differences between revisions 8 and 9
Revision 8 as of 2013-01-18 06:48:19
Size: 1867
Editor: guest
Comment:
Revision 9 as of 2013-01-18 06:51:41
Size: 6073
Editor: guest
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
=== Minimalist Arduino DSP === === Minimalist Arduino DSP (MiniArDSP)===
Line 15: Line 15:
The following is an Arduino library (just a first rev, nothing fancy). Download the zipped file, and uncompress it. The entire folder needs to be placed in you ''libraries'' folder in your user directory. If you are not sure where this is, check out our description in the [[http://wiki.openmusiclabs.com/wiki/AudioCodecShield|CodecShield Wiki]].  Right now there is only one example in the library, and it is a tremolo effect. The following is an Arduino library (just a first rev, nothing fancy). Download the zipped file, and uncompress it. The entire folder needs to be placed in you ''libraries'' folder in your user directory. If you are not sure where this is, check out our description in the [[http://wiki.openmusiclabs.com/wiki/AudioCodecShield|CodecShield Wiki]].
Line 20: Line 20:

Right now there is only one example in the library, and it is a tremolo effect. It has two buttons, one to increase the tremolo rate, the other to decrease it. It is a full depth tremolo, so you will have to mix some of the original signal back in to get a lower depth. The buttons are very hacked at this point, they need to be tapped rather quickly to make small changes.

{{{
// mini_tremolo.pde
// minimalisitic tremelo program
// guest - openmusiclabs.com - 1.13.13
// takes in audio data from the ADC and plays it out on
// Timer1 PWM. 16b, Phase Correct, 31.25kHz. applies a variable
// frequency tremolo to the sound.

#include "MiniArDSP.h"

#define PWM_FREQ 0x00FF // pwm frequency - see table
#define PWM_MODE 0 // Fast (1) or Phase Correct (0)
#define PWM_QTY 2 // number of pwms, either 1 or 2

// create sinewave lookup table
// PROGMEM stores the values in the program memory
// it is automatically included with MiniArDSP.h
PROGMEM prog_int16_t sinewave[] = {
  // this file is stored in MiniArDSP and is a 1024 value
  // sinewave lookup table of signed 16bit integers
  // you can replace it with your own waveform if you like
  #include <sinetable.inc>
};
unsigned int location; // lookup table value location

byte rate = 5; // tremolo rate
byte rate_counter; // modifiable version of rate
unsigned int amplitude; // current tremolo amplitude
unsigned int button;

void setup() {
  // setup button pins
  PORTD |= 0x0c; // turn on pullups for pins 2 and 3
  
  // 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
  
  TIMSK0 = 0; // turn of t0 - no delay() or millis()
  sei(); // turn on interrupts - not really necessary with arduino
}

void loop() {
  while(1); // gets rid of jitter
  // nothing happens up here. if you want to put code up here
  // get rid of the ISR_NAKED and the reti(); below
}

ISR(TIMER1_CAPT_vect, ISR_NAKED) { // ISR_NAKED is used to save
// clock cycles, but prohibits code in the loop() section.
  
  // get ADC data
  byte temp1 = ADCL; // you need to fetch the low byte first
  byte temp2 = ADCH; // yes it needs to be done this way
  int input = (temp2 << 8) | temp1; // make a signed 16b value
  
  button--; // check buttons every so often
  if (button == 0) {
    byte temp3 = PIND & 0x0c; // get pin 2 and 3 values
    if (temp3 == 0) { // both buttons pressed
    } // do nothing
    else if (temp3 == 0x08) { // up button pressed
      if (rate == 1); // do nothing
      else rate--; // make tremolo faster
    }
    else if (temp3 == 0x04) { // down button pressed
      if (rate == 255); // do nothing
      else rate++; // make tremolo slower
    }
    button = 0x0400; // reset counter
  }

  rate_counter--; // decrement our counter
  if (rate_counter == 0) {
    // create a variable frequency and amplitude sinewave
    // fetch a sample from the lookup table
    amplitude = pgm_read_word_near(sinewave + location) + 0x8000;
    // the + 0x8000 turns the signed value to unsigned
    location++;
    // if weve gone over the table boundary -> loop back
    // around to the other side.
    location &= 0x03ff; // fast way of doing rollover for 2^n numbers
    rate_counter = rate; // reset rate counter
  }
  
 
  // multiply our input by the sinewave value
  // note the use of the special multiply macro
  // this is faster than the normal * method
  // this is a signed * unsigned, returning the high 16 bits
  int output; // define variable before using it
  MultiSU16X16toH16(output, input, amplitude);
  
  // output data
  OCR1AL = (output + 0x8000) >> 8; // convert to unsigned
  // and only output the top byte
  OCR1BL = output; // output the bottom byte
  reti(); // return from interrupt - required because of ISR_NAKED
}
}}}

=== Minimalist Arduino DSP (MiniArDSP)===

About Minimalist Arduino DSP

This is the the most basic setup for doing decent quality audio effects with an Arduino. The system is based upon both our PWM tutorial and our ATmega ADC tutorial. The basic setup is a 10 bit value read in from ADC0, and played out via a 16 bit, Dual PWM on pins 9 and 10. there are 2 buttons, on pins 2 and 3, which can be used to modify parameters. The reason for buttons and not knobs, is that the ADC is already completely consumed with the analog audio signal coming in. This is a more focused version of our basic Arduino PWM setup, targetted at effects pedals.

Hardware setup

The following schematic is the basic version to get started. There will eventually be a better version with a dual opamp, and gain, mix, feedback, and level knobs. But that might take a bit to finish. The 3.9k and 1M resistors should be 1% (or better) if possible. Also, the input signal needs to be from a mixer or other powered device with a low output impedance and strong signal. Plugging a guitar in won't work with this version.

pwm_basic_sm.jpg

Arduino library

The following is an Arduino library (just a first rev, nothing fancy). Download the zipped file, and uncompress it. The entire folder needs to be placed in you libraries folder in your user directory. If you are not sure where this is, check out our description in the CodecShield Wiki.

Example code

Right now there is only one example in the library, and it is a tremolo effect. It has two buttons, one to increase the tremolo rate, the other to decrease it. It is a full depth tremolo, so you will have to mix some of the original signal back in to get a lower depth. The buttons are very hacked at this point, they need to be tapped rather quickly to make small changes.

// mini_tremolo.pde
// minimalisitic tremelo program
// guest - openmusiclabs.com - 1.13.13
// takes in audio data from the ADC and plays it out on
// Timer1 PWM.  16b, Phase Correct, 31.25kHz.  applies a variable
// frequency tremolo to the sound.

#include "MiniArDSP.h"

#define PWM_FREQ 0x00FF // pwm frequency - see table
#define PWM_MODE 0 // Fast (1) or Phase Correct (0)
#define PWM_QTY 2 // number of pwms, either 1 or 2

// create sinewave lookup table
// PROGMEM stores the values in the program memory
// it is automatically included with MiniArDSP.h
PROGMEM  prog_int16_t sinewave[]  = {
  // this file is stored in MiniArDSP and is a 1024 value
  // sinewave lookup table of signed 16bit integers
  // you can replace it with your own waveform if you like
  #include <sinetable.inc>
};
unsigned int location; // lookup table value location

byte rate = 5; // tremolo rate
byte rate_counter; // modifiable version of rate
unsigned int amplitude; // current tremolo amplitude
unsigned int button;

void setup() {
  // setup button pins
  PORTD |= 0x0c; // turn on pullups for pins 2 and 3
  
  // 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
  
  TIMSK0 = 0; // turn of t0 - no delay() or millis()
  sei(); // turn on interrupts - not really necessary with arduino
}

void loop() {
  while(1); // gets rid of jitter
  // nothing happens up here.  if you want to put code up here
  // get rid of the ISR_NAKED and the reti(); below
}

ISR(TIMER1_CAPT_vect, ISR_NAKED) { // ISR_NAKED is used to save
// clock cycles, but prohibits code in the loop() section.
  
  // get ADC data
  byte temp1 = ADCL; // you need to fetch the low byte first
  byte temp2 = ADCH; // yes it needs to be done this way
  int input = (temp2 << 8) | temp1; // make a signed 16b value
  
  button--; // check buttons every so often
  if (button == 0) {
    byte temp3 = PIND & 0x0c; // get pin 2 and 3 values
    if (temp3 == 0) { // both buttons pressed
    } // do nothing
    else if (temp3 == 0x08) { // up button pressed
      if (rate == 1); // do nothing
      else rate--; // make tremolo faster
    }
    else if (temp3 == 0x04) { // down button pressed
      if (rate == 255); // do nothing
      else rate++; // make tremolo slower
    }
    button = 0x0400; // reset counter
  }

  rate_counter--; // decrement our counter
  if (rate_counter == 0) {
    // create a variable frequency and amplitude sinewave
    // fetch a sample from the lookup table
    amplitude = pgm_read_word_near(sinewave + location) + 0x8000;
    // the + 0x8000 turns the signed value to unsigned
    location++;
    // if weve gone over the table boundary -> loop back
    // around to the other side.
    location &= 0x03ff; // fast way of doing rollover for 2^n numbers
    rate_counter = rate; // reset rate counter
  }
  
 
  // multiply our input by the sinewave value
  // note the use of the special multiply macro
  // this is faster than the normal * method
  // this is a signed * unsigned, returning the high 16 bits
  int output; // define variable before using it
  MultiSU16X16toH16(output, input, amplitude);
  
  // output data
  OCR1AL = (output + 0x8000) >> 8; // convert to unsigned
  // and only output the top byte
  OCR1BL = output; // output the bottom byte
  reti(); // return from interrupt - required because of ISR_NAKED
}

MiniArDSP (last edited 2013-09-16 16:48:45 by guest)