#include <MozziGuts.h>
#include <Oscil.h> // oscillator template
#include <tables/sin2048_int8.h> // sine table for oscillator
#include <mozzi_midi.h>
#include <tables/sin2048_int8.h>
#include <tables/saw2048_int8.h>
#include <tables/triangle2048_int8.h>
#include <tables/cos2048_int8.h>
#include <AutoMap.h>
#include <Metronome.h>
#include <IntMap.h>
// use: Oscil <table_size, update_rate> oscilName (wavetable), look in .h file of table #included above
Oscil <2048, AUDIO_RATE> inBass;
const char INPUT_PIN = A0; // set the input for the knob to analog pin 0
// to convey the volume level from updateControl() to updateAudio()
byte pitch;
long out;
long finalOut;
// Basic pitch of the instrument (MIDI 45 = A2)
#define MIDI_NOTE_MIN (Q16n16)(33L << 16L)
uint16_t antilogTable[] = {
64830,64132,63441,62757,62081,61413,60751,60097,59449,58809,58176,57549,56929,56316,55709,55109,
54515,53928,53347,52773,52204,51642,51085,50535,49991,49452,48920,48393,47871,47356,46846,46341,
45842,45348,44859,44376,43898,43425,42958,42495,42037,41584,41136,40693,40255,39821,39392,38968,
38548,38133,37722,37316,36914,36516,36123,35734,35349,34968,34591,34219,33850,33486,33125,32768
};
uint16_t mapPhaseInc(uint16_t input) {
return (antilogTable[input & 0x3f]) >> (input >> 6);
}
void setup(){
//Serial.begin(9600); // for Teensy 3.1, beware printout can cause glitches
Serial.begin(115200); // set up the Serial output so we can look at the piezo values // set up the Serial output so we can look at the input values
pinMode(INPUT_PIN, INPUT_PULLUP);
Serial.println(finalOut, BIN);
startMozzi(); // :))
}
void updateControl(){
static Q16n16 midiNote, midiNoteInt, baseFrequency;
static long midiNoteFrac;
// map it to an 8 bit range for efficient calculations in updateAudio
pitch = mozziAnalogRead(INPUT_PIN);
midiNote = MIDI_NOTE_MIN + (Q16n16)(pitch << 11);
midiNoteInt = (midiNote + 0x00008000) & 0xFFFF0000; // Nearest MIDI note (Q16n16 rounding)
midiNoteFrac = (long)midiNote - (long)midiNoteInt; /* Fractional part only */ /* Scale the fractional part */
midiNote = midiNoteInt + midiNoteFrac;
baseFrequency = Q16n16_mtof(midiNote);
inBass.setFreq_Q16n16(baseFrequency);
// print the value to the Serial monitor for debugging
Serial.print("pitch = ");
Serial.println((int)pitch);
Serial.println(finalOut, BIN);
}
int updateAudio(){
out = (inBass.next());
finalOut = (out);
return finalOut;
}
void loop(){
audioHook(); // required here
}