Hey there Tim
, thanks for some advice. I have been working through this (haven't gotten to using pointers yet) and decided to try using one analog pin for all of the buttons. I'de like to start adding some cooler sounds than just the simple sin2048. Through staring at the examples and reading a bunch and watching vids, I've tried to put together my code. I've got the buttons working and 3 knobs as well. the audio out is a bit noisy, but it works. I've been trying to get my knobs to do different things and i usually end up with what sounds like just a pitch shift. I'd like to have some cutoff or filter knob, some other nice knobs to play with, a way to shift the whole scale up and down chromatically (too much to ask?) and again a way to switch to a different sound. Do you think working with the waveshaper or wavepackets might be a better idea for this? I'm still not exactly sure what they are except some massive table that eventually makes a sound. Anyways, here's my code, its quite messy because i've been trial and erring a lot.
#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
//#include <AudioDelay.h>
#include <tables/sin2048_int8.h> // sine table for oscillator
//#include <tables/cos2048_int8.h> // wavetable for mod
#define CONTROL_RATE 64 // powers of 2 please
//#include <mozzi_midi.h> // for mtof (midi to freq)
//#include <mozzi_fixmath.h> //needed for vibrato
#include <AutoMap.h> // maps unpredictable inputs to a range
#include <RollingAverage.h>
// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <SIN2048_NUM_CELLS, AUDIO_RATE> aSin(SIN2048_DATA);
//Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aCos(COS2048_DATA); //vibrato stuff
//Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aVibrato(COS2048_DATA); //vibrato stuff
//Oscil<COS2048_NUM_CELLS, CONTROL_RATE> kFreq(COS2048_DATA); //delay stuff
//const byte intensity = 255; //vibrato stuff
const int buttons = 0;
const int knob1 = 1;
const int knob2 = 2;
//byte volume; //vol knob to add later
int note[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440}; //indexed starting with 0
// mid C C# D D# E F F# G G# A
// min and max values of synth parameters to map AutoRanged analog inputs ALL FOR KNOBS
const int MIN_F = 5;
const int MAX_F = 1000;
const int MIN_BW = 1;
const int MAX_BW = 1000;
const int MIN_CF = 60;
const int MAX_CF = 2000;
// for smoothing the control signals
// use: RollingAverage <number_type, how_many_to_average> myThing ??
RollingAverage <int, 16> kAverageF;
RollingAverage <int, 16> kAverageBw;
RollingAverage <int, 16> kAverageCf;
AutoMap kMapF(0,1023,MIN_F,MAX_F);
AutoMap kMapBw(0,1023,MIN_BW,MAX_BW);
AutoMap kMapCf(0,1023,MIN_CF,MAX_CF);
//AudioDelay <256> aDel; //delay stuff
//int del_samps; //delay stuff
void setup(){
Serial.begin(115200);
delay(200);
startMozzi(CONTROL_RATE); // set a control rate of 64 (powers of 2 please)
}
void updateControl(){
int button_value = mozziAnalogRead(buttons);
//Serial.print("button value = ");
//Serial.print(button_value);
int fundamental = mozziAnalogRead(knob1); //fundamental freq
Serial.print(fundamental);
fundamental = kMapF(fundamental);
Serial.print(fundamental);
int bandwidth = mozziAnalogRead(knob2); // bandwidth
bandwidth = kMapBw(bandwidth);
int centre_freq = mozziAnalogRead(knob3); //centre freq
centre_freq = kMapCf(centre_freq);
//kFreq.setFreq(.63f);
//del_samps = 128+kFreq.next(); //delay stuff
//aVibrato.setFreq(8.f);
//button 1
if (button_value >=400 && button_value <=450){
//aSin.setFreq(mtof(60.f));
aSin.setFreq(note[0]); // + knob int's final output
}
//button 2
else if (button_value >=500 && button_value <=550){
aSin.setFreq(note[2]);
}
//button 3
else if (button_value >=650 && button_value <=710){
aSin.setFreq(note[4]);
}
//button 4
else if (button_value >=900 && button_value <=950){
aSin.setFreq(note[5]);
}
//button 5
else if (button_value >=980 && button_value <=1010){
aSin.setFreq(note[7]);
}
//button 6
else if (button_value >=1011 && button_value <=1040){
aSin.setFreq(note[9]);
}
//no buttons pressed
else{
aSin.setFreq(0);
//aVibrato.setFreq(0.0f); //vibrato stuff
}
Serial.println();
}
int updateAudio(){
//Q15n16 vibrato = (Q15n16) intensity * aVibrato.next(); //vibrato stuff
//return aSin.phMod(vibrato); // phase modulation to modulate frequency
//char asig = aDel.next(aSin.next(), del_samps); //delay stuff
//return (int) asig; //delay stuff
return aSin.next()>>8; // return an int signal centred around 0
}
void loop(){
audioHook(); // required here
}
Any help with a bit of direction would be awesome.