hi again
I am thoroughly enjoying this learning.
Dancing along to the beats from the teensy well it does get a little monotonous after a few bars.
i would like to make a Measure class that plays a array of patterns at the touch of the same buttons if the machine is put into that different state.
in the arduino ide i have created a new tab named Measure.h
in this class i will define all the variables and methods i need for the class. i know there is nothing in it for now. I shall probably just copy and paste much of the code from the main. such as voice parameters and boolean arrays, and the current samples so that each measure can have a different pattern, currentSample and drum voice parameters.
in the arduino ide i have created a new tab named Measure.cpp
i want to then create an array of 8 objects of this Measure class. Problem arise when i uncomment
//#include <Measure.h> line.
Measure
I am thoroughly enjoying this learning.
Dancing along to the beats from the teensy well it does get a little monotonous after a few bars.
i would like to make a Measure class that plays a array of patterns at the touch of the same buttons if the machine is put into that different state.
in the arduino ide i have created a new tab named Measure.h
Code:
#ifndef Measure_h
#define Measure_h
//#include <Arduino.h>
class Measure {
public:
Measure();
};
#endif
in this class i will define all the variables and methods i need for the class. i know there is nothing in it for now. I shall probably just copy and paste much of the code from the main. such as voice parameters and boolean arrays, and the current samples so that each measure can have a different pattern, currentSample and drum voice parameters.
in the arduino ide i have created a new tab named Measure.cpp
Code:
#include "Measure.h"
Measure::Measure()
{
//do stuff here like define initial pattern and starting drum parameters and starting sample to play
// copy paste relevant functions from main code.
}
i want to then create an array of 8 objects of this Measure class. Problem arise when i uncomment
//#include <Measure.h> line.
Measure
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <WS2812Serial.h>
//#include <Measure.h>
//WAV Files
#include "AudioSampleData.h"
// GUItool: begin automatically generated code
AudioPlayMemory sound1;//bd
AudioPlayMemory sound2;//sd
AudioPlayMemory sound3;//hh
AudioPlayMemory sound4;//tom
AudioSynthSimpleDrum drum4;
AudioSynthSimpleDrum drum2;
AudioSynthSimpleDrum drum3;
AudioSynthSimpleDrum drum1;
float freqSettings[24] = {65.4, 98.0, 130.8, 164.8, 196.0, 261.6, 329.6, 392.0,
523.5, 659.2, 784, 1046.5, 1568.0, 2093.0, 2637.0, 3136.0, 4186.0, 5274.0, 6272.0, 8500.0,
10000.0, 11000.0, 12000.0, 14000.0
};
#define NUMFREQS sizeof(freqSettings)
int drumFreqCount[4] = {3, 0, 8, 12};
int drumLengthCount[4] = {15, 3, 4, 2};
int drumSecondMixCount [4] = {0, 0, 10, 0};//increments of .1
int drumPitchModCount [4] = {6, 10, 0, 5};
AudioMixer4 mixer1;
AudioMixer4 mixer2;
AudioMixer4 mixer3;
AudioOutputAnalogStereo dacs1;
AudioConnection patchCord1(sound1, 0, mixer2, 0);//bd
AudioConnection patchCord2(sound2, 0, mixer2, 1);//hh
AudioConnection patchCord3(sound3, 0, mixer2, 2);//sd
AudioConnection patchCord6(sound4, 0, mixer2, 3);//tom
AudioConnection patchCord4(drum4, 0, mixer1, 3);
AudioConnection patchCord5(drum2, 0, mixer1, 1);
AudioConnection patchCord7(drum3, 0, mixer1, 2);
AudioConnection patchCord8(drum1, 0, mixer1, 0);
AudioConnection patchCord9(mixer2, 0, mixer3, 1);
AudioConnection patchCord10(mixer1, 0, mixer3, 0);
AudioConnection patchCord11(mixer3, 0, dacs1, 0);
AudioConnection patchCord12(mixer3, 0, dacs1, 1);
// GUItool: end automatically generated code
/*---------------------------------------------------------------------------------*/
//variables
static uint16_t bpm = 120;
static uint16_t rez = 4;// smallest musical time
static uint16_t tempo = (double)((60000) / (bpm * rez));
static uint32_t next;
static uint16_t beat = 0;
static uint16_t voiceSelect = 0;
static uint8_t current_kick_sample = 0;
static uint8_t current_snare_sample = 0;
int Val;
/*---------------------------------LEDs------------------------------------------*/
const int numled = 19;
const int pin = 10;
byte drawingMemory[numled * 3];
DMAMEM byte displayMemory[numled * 12];
WS2812Serial leds(numled, displayMemory, drawingMemory, pin, WS2812_GRB);
#define RED 0xFF0000
#define GREEN 0x00FF00
#define BLUE 0x0000FF
#define YELLOW 0xFFFF00
#define PINK 0xFF1088
#define ORANGE 0xE05800
#define WHITE 0xFFFFFF
#define BLACK 0X000000
/*--------------------------------BUTTONS------------------------------------------*/
//buttons
byte buttons [] = {12, 24, 25, 26, 27, 28, 29, 30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};// THE PIN THE BUTTONS ARE ON,
#define NUMBUTTONS sizeof(buttons)
byte pressed[NUMBUTTONS], justpressed[NUMBUTTONS], justreleased[NUMBUTTONS];
byte previous_keystate[NUMBUTTONS], current_keystate[NUMBUTTONS];
#define DEBOUNCE 15 // button debouncer, how many ms to debounce,
/*----------------------------------drum patterns----------------------------------*/
boolean pattern [8][16] = {{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 0, 0, 0}
};
/*---------------------------------------------------------------------------------*/
int machineState = 0;
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
void setup()
{
Serial.begin(115200);
leds.begin();
// audio library init
AudioMemory(20);
next = millis() + 1000;
AudioNoInterrupts();
//button setup
for (byte i = 0; i < NUMBUTTONS; i++)
{
pinMode(buttons[i], INPUT);
digitalWrite(buttons[i], HIGH);
}
/*-------------------------------set up pots---------------------------------------*/
Val = analogRead(A0);
/*---------------------------------Initialise drum voices--------------------------*/
//drum setup
changeDrumParameters();
mixer1.gain(0, 0.4);
mixer1.gain(1, 0.4);
mixer1.gain(2, 0.4);
mixer1.gain(3, 0.4);
/*---------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------*/
//sample setups
mixer2.gain(0, 0.4);
mixer2.gain(1, 0.7);
mixer2.gain(2, 0.1);
mixer2.gain(3, 0.4);
AudioInterrupts();
}
/*---------------------------------------------------------------------------------*/
/*--------------------------ok here we go------------------------------------------*/
/*---------------------------------------------------------------------------------*/
void loop()
{
//inputs
byte thisSwitch = thisSwitch_justPressed();
if (thisSwitch < 16 && machineState == 1)
{
pattern[voiceSelect][thisSwitch] = !pattern[voiceSelect][thisSwitch];
}
{
if (machineState == 0)
switch (thisSwitch)
{
if (voiceSelect >= 0 && voiceSelect <= 4)
{
case 0:
if (drumFreqCount[voiceSelect] > 0)
{
drumFreqCount[voiceSelect]--;
}
changeDrumParameters();
break;
case 1:
if (drumFreqCount[voiceSelect] < NUMFREQS - 1)
{
drumFreqCount[voiceSelect]++;
}
changeDrumParameters();
break;
case 2:
if (drumLengthCount[voiceSelect] > 1)
drumLengthCount[voiceSelect]--;
changeDrumParameters();
break;
case 3:
if (drumLengthCount[voiceSelect] < 20)
drumLengthCount[voiceSelect]++;
changeDrumParameters();
break;
case 4:
if (drumSecondMixCount [voiceSelect] > -20)
drumSecondMixCount [voiceSelect]--;
break;
case 5:
if (drumSecondMixCount [voiceSelect] < 20)
drumSecondMixCount [voiceSelect]++;
break;
case 6:
if (drumPitchModCount [voiceSelect] > -9)
drumPitchModCount [voiceSelect]--;
changeDrumParameters();
break;
case 7:
if (drumPitchModCount [voiceSelect] < 9)
drumPitchModCount [voiceSelect]++;
changeDrumParameters();
break;
}
case 8:
voiceSelect = 0;
drum1.noteOn();
break;
case 9:
voiceSelect = 1;
drum2.noteOn();
break;
case 10:
voiceSelect = 2;
drum3.noteOn();
break;
case 11:
voiceSelect = 3;
drum4.noteOn();
break;
case 12:
voiceSelect = 4;
current_kick_sample = (current_kick_sample + 1) % 16;
select_kick_sample();
break;
case 13:
voiceSelect = 5;
current_snare_sample = (current_snare_sample + 1) % 16;
select_snare_sample();
break;
case 14:
voiceSelect = 6;
sound3.play(AudioSampleHihat);
break;
case 15:
voiceSelect = 7;
sound4.play(AudioSampleTomtom);
break;
}
}
if (thisSwitch == 16)
{
leds.setPixel(16 + machineState, BLACK);
machineState = (machineState + 1) % 3;
leds.setPixel(16 + machineState, ORANGE);
leds.show();
}
/*----------------------------------drum patterns-clear----------------------------*/
if (thisSwitch == 17)//clear all
{
for (uint8_t i = 0; i < 8; i++)
{
for (uint8_t j = 0; j < 16; j++)
{
pattern[i][j] = 0;
}
}
}
/*---------------------------------------------------------------------------------*/
/*-----------------------------------sequencer-------------------------------------*/
/*---------------------------------------------------------------------------------*/
static uint32_t num = 0;
if (millis() >= next)
{
int Val = analogRead(A0);
bpm = map(Val, 0, 1023, 40, 250);
tempo = (double)((60000) / (bpm * rez));
Serial.print(bpm);
Serial.println(" ");
Serial.print("machineState is : ");
Serial.println(machineState);
/*---------------------------------------------------------------------------------*/
leds.setPixel(16 + machineState, ORANGE);
leds.setPixel(beat, BLACK);
leds.show();
beat = num % 16;
if (beat % 4 == 0)
leds.setPixel(beat, RED);
else
leds.setPixel(beat, BLUE);
for (uint8_t i = 0; i < 16; i++)
{
if (pattern[voiceSelect][i])
leds.setPixel(i, GREEN);
}
leds.show();
/*---------------------------------------------------------------------------------*/
if (pattern[0][beat])
drum1.noteOn();
if (pattern[1][beat])
drum2.noteOn();
if (pattern[2][beat])
drum3.noteOn();
if (pattern[3][beat])
drum4.noteOn();
if (pattern[4][beat])
select_kick_sample();
if (pattern[5][beat])
select_snare_sample();
if (pattern[6][beat])
sound3.play(AudioSampleHihat);
if (pattern[7][beat])
sound4.play(AudioSampleTomtom);
num++;
next = millis() + tempo;
}
}
/*---------------------------------------------------------------------------------*/
void select_kick_sample() {
switch (current_kick_sample) {
case 0:
sound1.play(AudioSampleKick);
break;
case 1:
sound1.play(AudioSampleKick_01);
break;
case 2:
sound1.play(AudioSampleKick_02);
break;
case 3:
sound1.play(AudioSampleKick_03);
break;
case 4:
sound1.play(AudioSampleKick_04);
break;
case 5:
sound1.play(AudioSampleKick_05);
break;
case 6:
sound1.play(AudioSampleKick_06);
break;
case 7:
sound1.play(AudioSampleKick_07);
break;
case 8:
sound1.play(AudioSampleKick_08);
break;
case 9:
sound1.play(AudioSampleKick_09);
break;
case 10:
sound1.play(AudioSampleKick_10);
break;
case 11:
sound1.play(AudioSampleKick_11);
break;
case 12:
sound1.play(AudioSampleKick_12);
break;
case 13:
sound1.play(AudioSampleKick_13);
break;
case 14:
sound1.play(AudioSampleKick_14);
break;
case 15:
sound1.play(AudioSampleKick_15);
break;
case 16:
sound1.play(AudioSampleKick_16);
break;
}
}
/*---------------------------------------------------------------------------------*/
void select_snare_sample() {
switch (current_snare_sample) {
case 0:
sound2.play(AudioSampleSnare);
break;
case 1:
sound2.play(AudioSampleSnare_01);
break;
case 2:
sound2.play(AudioSampleSnare_02);
break;
case 3:
sound2.play(AudioSampleSnare_03);
break;
case 4:
sound2.play(AudioSampleSnare_04);
break;
case 5:
sound2.play(AudioSampleSnare_05);
break;
case 6:
sound2.play(AudioSampleSnare_06);
break;
case 7:
sound2.play(AudioSampleSnare_07);
break;
case 8:
sound2.play(AudioSampleSnare_08);
break;
case 9:
sound2.play(AudioSampleSnare_09);
break;
case 10:
sound2.play(AudioSampleSnare_10);
break;
case 11:
sound2.play(AudioSampleSnare_11);
break;
case 12:
sound2.play(AudioSampleSnare_12);
break;
case 13:
sound2.play(AudioSampleSnare_13);
break;
case 14:
sound2.play(AudioSampleSnare_14);
break;
case 15:
sound2.play(AudioSampleSnare_15);
break;
case 16:
sound2.play(AudioSampleSnare_16);
break;
}
}
/*---------------------------------------------------------------------------------*/
byte thisSwitch_justPressed()
{
byte thisSwitch = 255;
check_buttons(); //check the switches & get the current state
for (byte i = 0; i < NUMBUTTONS; i++) {
current_keystate[i] = justpressed[i];
if (current_keystate[i] != previous_keystate[i]) {
if (current_keystate[i]) thisSwitch = i;
}
previous_keystate[i] = current_keystate[i];
}
return thisSwitch;
}
/*---------------------------------------------------------------------------------*/
/*-------------------------------PROGRAM THE DRUM SOUND PARAMETERS-----------------*/
void changeDrumParameters()
{
drum1.frequency(freqSettings[drumFreqCount[0]]);
drum1.length(drumLengthCount[0] * 100);
drum1.secondMix(drumSecondMixCount [0] * 0.1);
drum1.pitchMod(drumPitchModCount [0] * 0.1);
drum2.frequency(freqSettings[drumFreqCount[1]]);
drum2.length(drumLengthCount[1] * 100);
drum2.secondMix(drumSecondMixCount [1] * 0.1);
drum2.pitchMod(drumPitchModCount [1] * 0.1);
drum3.frequency(freqSettings[drumFreqCount[2]]);
drum3.length(drumLengthCount[2] * 100);
drum3.secondMix(drumSecondMixCount [2] * 0.1);
drum3.pitchMod(drumPitchModCount [2] * 0.1);
drum4.frequency(freqSettings[drumFreqCount[3]]);
drum4.length(drumLengthCount[3] * 100);
drum4.secondMix(drumSecondMixCount [3] * 0.1);
drum4.pitchMod(drumPitchModCount [3] * 0.1);
}
/*-----------------------------------CHECK BUTTONS---------------------------------*/
void check_buttons()
{
static byte previousstate[NUMBUTTONS];
static byte currentstate[NUMBUTTONS];
static long lasttime;
byte index;
if (millis() < lasttime) {
lasttime = millis(); // we wrapped around, lets just try again
}
if ((lasttime + DEBOUNCE) > millis()) {
return; // not enough time has passed to debounce
}
// ok we have waited DEBOUNCE milliseconds, lets reset the timer
lasttime = millis();
for (index = 0; index < NUMBUTTONS; index++)
{
justpressed[index] = 0; // when we start, we clear out the "just" indicators
justreleased[index] = 0;
currentstate[index] = digitalRead(buttons[index]); // read the button
if (currentstate[index] == previousstate[index]) {
if ((pressed[index] == LOW) && (currentstate[index] == LOW)) {
// just pressed
justpressed[index] = 1;
}
else if ((pressed[index] == HIGH) && (currentstate[index] == HIGH)) {
// just released
justreleased[index] = 1;
}
pressed[index] = !currentstate[index]; // remember, digital HIGH means NOT pressed
}
//Serial.println(pressed[index], DEC);
previousstate[index] = currentstate[index]; // keep a running tally of the buttons
}
}
/*---------------------------------------------------------------------------------*/