Polyphonic Guitar Chord Generator (Portable)

122 views
Skip to first unread message

Stephen Adams

unread,
Jul 21, 2023, 9:14:50 AM7/21/23
to Mozzi-users
I found the Mozzi examples and explanation confusing, but after some experimentation I produced this Arduino sketch which works for me.
The hardware is an Arduino, attached to a 8403 postage stamp amplifier through the two resistors attached to D9 and D10.
The amplifier is fed from the Arduino +5v and GND pins.
The power supply is a powerbank applied to Vin and Ground via a switch.
Pressing one of the three input switches puts out a chord of C, G or F.
Just the thing to start off a mixed choral and musicians group in sync.
Here is the code
/*  Guitar Polyphonic Chord Player
 *  


    Demonstrates the use of Oscil to play a wavetable.

    Circuit: Audio output on digital pin 9 on a Uno or similar, or
    DAC/A14 on Teensy 3.1, or
    check the README or http://sensorium.github.io/Mozzi/

    Mozzi documentation/API
    https://sensorium.github.io/Mozzi/doc/html/index.html

    Mozzi help/discussion/announcements:
    https://groups.google.com/forum/#!forum/mozzi-users

    Tim Barrass 2012, CC by-nc-sa.
    Stephen Adams 16/07/2023 */

#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#include <RollingAverage.h>
#include <ControlDelay.h>
#define INPUT_PIN 0 // analog control input
#define CONTROL_RATE 64
int OFF = 0;
int STOP_PIN = 2;
int STOP_PIN3 = 3;
int STOP_PIN4 = 4 ;  
boolean pause_triggered, paused = false;;
unsigned int count = 0;
unsigned int echo_cells_1 = 32;
unsigned int echo_cells_2 = 60;
unsigned int echo_cells_3 = 127;

// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
// Default output is Asin, aSin1,ASin2
// the rest is copied in to the default to play new notes
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin0(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin1(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin2(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin3(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin4(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin5(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin6(SIN2048_DATA);
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin7(SIN2048_DATA);
ControlDelay <128, int> kDelay; // 2seconds
byte gain = 255;
float Chord_C = 261.63;
float Chord_E = 329.63;
float Chord_G = 392;


float Chord_B = 293.66;
float Chord_D = 311.13;

float Chord_F = 349.23;
float Chord_A = 4400;
float Chord_C2 = 523.25;

void setup(){
    pinMode(STOP_PIN, INPUT_PULLUP);
    pinMode(STOP_PIN3, INPUT_PULLUP);
    pinMode(STOP_PIN4, INPUT_PULLUP);
 //  kDelay.set(echo_cells_1);
  startMozzi(CONTROL_RATE); // :)
  aSin.setFreq(Chord_C); // set the frequency
  aSin0.setFreq(Chord_E);
  aSin1.setFreq(Chord_G);
  // Second Chord
  aSin2.setFreq(Chord_G);
  aSin3.setFreq(Chord_B);
  aSin4.setFreq(Chord_D);  
    // Third Chord
  aSin5.setFreq(Chord_F);
  aSin6.setFreq(Chord_A);
  aSin7.setFreq(Chord_C2);
}


void updateControl(){
  // put changing controls in here
// copy new phrase into default notes a0 - a2!!
//  Gain controls ON/OFF of Tone
OFF = 0;
  if (digitalRead(STOP_PIN) == LOW) {  
  aSin.setFreq(Chord_C); // set the default frequency
  aSin0.setFreq(Chord_E);
  aSin1.setFreq(Chord_G);
  gain = 1;
  OFF = 1;
  }
    if (digitalRead(STOP_PIN3) == LOW) {
  aSin0 =aSin2;
  aSin1 =aSin3;
  aSin0 =aSin4;
   gain = 1;
    OFF = 1;
  }

    if (digitalRead(STOP_PIN4) == LOW)
    {
  aSin0 =aSin5;
  aSin1 =aSin6;
  aSin0 =aSin7;
   gain = 1;
    OFF = 1;
  }
}
int updateAudio()
{
  if (OFF == 0)
  {
    gain = 0;
  }
  // gain = 1 turns on tone sequence
  return 3*((int)(aSin0.next()* gain) +(aSin1.next()* gain) +(aSin2.next()* gain) >>1)>>3;
}

void loop(){
//  if (OFF == 0)
//  {
//    stopMozzi();
//  }
//   if (OFF == 1)
//   {
//    startMozzi();
//   }
//     if(OFF == 0) audioHook();
 audioHook(); // required here
}


Reply all
Reply to author
Forward
0 new messages