[PD] Arduino :: Translating [mtof]

Alexandre Quessy listes at sourcelibre.com
Sat Aug 26 00:39:41 CEST 2006


/**
 * Makes a piezo sing a minor harmonic scale, ascendant on 2 octavas.
 * Stand alone (no big computer)
 *
 * Port of [mtof] from Pure Data
 * @bug Uses too much memory
 * @author aalex http://alexandre.quessy.net
 */

int speakerOut = 9;
int ledPin = 13;
int statePin = LOW;
int i;
float midiNote;
int octava = 0;
int degree = 0;
// harmonic minor
int scale[7] = {0, 2, 3, 5, 7, 8, 11};
float freq;
float timeHigh;


/** setup */
void setup() {
 pinMode(ledPin, OUTPUT);
 pinMode(speakerOut, OUTPUT);
}


/** infinite loop */
void loop() {
  digitalWrite(speakerOut, LOW);

  // tempo
  statePin = !statePin;
  digitalWrite(ledPin, statePin);

  // choose a note
  degree++;
  if (octava == 1 && degree == 7) {
    degree = 0;
    octava = 0;
  } else if (degree == 7) {
    octava = 1;
    degree = 0;
  }
  midiNote = (octava * 12) + 48 + scale[degree];

  // find the pulse settings
  // MTOF FROM PD
  freq = (8.17579891564 * exp(.0577622650 * midiNote)); // mtof
  timeHigh = 1/(2 * freq);
  period = timeHigh * 2;

  // sings a note
  for (i = 0; i < freq; i++) {
    digitalWrite(speakerOut, HIGH);
    delayMicroseconds(timeHigh);
    digitalWrite(speakerOut, LOW);
    delayMicroseconds(timeHigh);
  }
  delay(1000);
}




More information about the Pd-list mailing list