Page 1 of 1

karplus strong?

PostPosted: Tue Apr 03, 2012 8:01 pm
by loss1234
does anyone know if this might be possible with the audio codec shield on arduino?

i played around with a really dirty KS sketch with an 8 bit DAC and while it did sound metallic, it didnt sound very HI-FI...the shield could take care of the fidelity but I am not sure if there is enough buffer to do it or not


but it might be possible!

Re: karplus strong?

PostPosted: Tue Apr 03, 2012 8:41 pm
by guest
a friend of mine introduced me to karplus-strong on sunday
they look really cool
and are not computationally expensive
although i will have to see how much ram they require

Re: karplus strong?

PostPosted: Tue Apr 03, 2012 9:32 pm
by guest
this stuff is pretty amazing
it basically takes no time to process

Code: Select all
/*
karplus.pde
guest openmusiclabs 4.3.12
*/


// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1Khz
#define ADCS 1 // no ADCs are being used

// include necessary libraries
#include <Wire.h>
#include <SPI.h>
#include <AudioCodec.h>

// create data variables for audio transfer
int left_in = 0x0000;
int left_out = 0x0000;
int right_in = 0x0000;
int right_out = 0x0000;
int last_value = 0x0000;
int curr_value = 0x0000;
unsigned int decay = 0x0000;
#define DECAY 65000
unsigned int mod0_value =0;

// create a delay buffer in memory
#define SIZE 800 // buffer size is limited by microcontroller SRAM size
int delaymem[SIZE]; // 800 positions x 2 bytes = 1600 bytes of SRAM
unsigned int location = 0; // buffer location to read/write from
unsigned int boundary = SIZE;

void setup() {
 
  for (int i = 0; i < SIZE; i++) {
    delaymem[i] = random(65535);
  }
 
  AudioCodec_init(); // setup codec registers
  // call this last if setting up other parts
}

void loop() {
  while (1); // reduces clock jitter
}

// timer1 interrupt routine - all data processed here
ISR(TIMER1_COMPA_vect, ISR_NAKED) { // dont store any registers

  // &'s are necessary on data_in variables
  AudioCodec_data(&left_in, &right_in, left_out, right_out);
 
   // & is required before adc variable
  AudioCodec_ADC(&mod0_value);
 
  if (decay++ < DECAY) {
 
    delaymem[location++] = left_out;
    // check if location has gotten bigger than buffer size
    if (location >= boundary) {
      location = 0; // reset location
    }
    curr_value = delaymem[location];
    left_out = (last_value>>1) + (curr_value>>1);
    last_value = curr_value;
    }
  else {
    for (int i = 0; i < SIZE; i++) {
      delaymem[i] = random(65535);
    }
    decay = 0;
   
    unsigned int scale = SIZE;
    MultiU16X16toH16(boundary, mod0_value, scale);
  }
  // dont forget to return from interrupt
  reti();
}

Re: karplus strong?

PostPosted: Tue Apr 03, 2012 9:37 pm
by guest
oh yeah
DECAY sets the note repeat time
and MOD0 sets the note frequency

it is a tad metallic
but i think starting with a different lookup table
would cure that problem
i have a lookup table generator for Pure Data
thats linked off the mixtape_alpha wiki page
a bandpass filtered noise source would be good
this could be stored in flash
and then reloaded to create each note

but it is still monophonic
and the ram limits this
going down to 22k
and lmiting the lowest note to 100hz
could give 4 note polyphony

Re: karplus strong?

PostPosted: Thu Apr 05, 2012 6:39 am
by loss1234
thanks for this!!!
trying it out right now

Re: karplus strong?

PostPosted: Thu Apr 05, 2012 6:46 am
by loss1234
it sounds great!

Re: karplus strong?

PostPosted: Mon Aug 27, 2012 5:09 am
by JLS
Very interesting for me.

Many thanks

Re: karplus strong?

PostPosted: Fri Aug 31, 2012 7:40 am
by donal
I changed :
#define DECAY 10000
unsigned int mod0_value =0;

// create a delay buffer in memory
#define SIZE 10 /

Sounds lovely! Nice percussive stuff.

Re: karplus strong?

PostPosted: Fri Aug 31, 2012 12:27 pm
by guest
its good to hear it works well with small SIZE
that means you can setup a bunch of buffers
and have some polyphony going