Simplest possible example

Simplest possible example

Postby rascalmicro » Mon Aug 15, 2011 6:47 am


Some code I wrote/modified-- feel free to use it for anything.

I think this is the simplest possible code that I can come up with based on Mark's Arduino code-- the two knobs are just volume controls for the left and right channels.

Code: Select all
// setup codec parameters
// must be done before #includes
// see readme file in libraries folder for explanations
#define SAMPLE_RATE 44 // 44.1kHz sample rate
#define ADCS 2 // use both ADCs

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

// create data variables for audio transfer
// even though the function is mono, the codec requires stereo data
int left_in = 0; // in from codec (LINE_IN)
int right_in = 0;
int left_out = 0; // out to codec (HP_OUT)
int right_out = 0;

// create variables for ADC results
// it only has positive values -> unsigned
unsigned int mod0_value = 0;
unsigned int mod1_value = 0;

void setup() {
  // call this last if you are setting up other things
  AudioCodec_init(); // setup codec and microcontroller registers

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);

  MultiSU16X16toH16(left_out, left_in, mod0_value);
  MultiSU16X16toH16(right_out, right_in, mod1_value);

  // & is required before adc variables
  AudioCodec_ADC(&mod0_value, &mod1_value);
  reti(); // dont forget to return from the interrupt

Also, here are a couple lines of Python that I wrote to generate 1024-entry lookup tables.

As it is, the first line just maps the values 0-1024 to the square of itself (x**2), but you could replace the x**2 with any other mathematical expression.

Code: Select all
lookup = [x**2 for x in range (0, 1024)]

f = open('', 'w')
for line in lookup:
    f.write(str(line) + ',\n')
Brandon Stafford
Rascal Micro: small computers for art and science
Posts: 8
Joined: Sat Aug 13, 2011 4:19 pm
Location: Somerville, MA, USA

Re: Simplest possible example

Postby guest » Mon Aug 15, 2011 7:09 pm

the script will be handy
ive been adding commas by hand
at the end of exel files i create
Site Admin
Posts: 449
Joined: Thu May 20, 2010 11:58 pm

Re: Simplest possible example

Postby mcanulty » Mon Aug 15, 2011 9:03 pm

the script will be handy
ive been adding commas by hand
at the end of exel files i create

A bit of sad programming poetry! I guess I've got to work on a video, but I'd like to play with some of the code later tonight or tomorrow. It's nice having new activity on the forum :)
Site Admin
Posts: 63
Joined: Thu May 20, 2010 10:46 pm

Return to Audio Codec Shield

Who is online

Users browsing this forum: No registered users and 1 guest