Skip to content

Instantly share code, notes, and snippets.

@moonmilk
Created February 18, 2016 01:44
Show Gist options
  • Save moonmilk/640e6ba117be7adbcb31 to your computer and use it in GitHub Desktop.
Save moonmilk/640e6ba117be7adbcb31 to your computer and use it in GitHub Desktop.
instrument-a-day 17: teensy whisperer
// whisperer2 for teensy 3.1/3.2 with teensy audio board
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=253,396
AudioSynthNoisePink pink1; //xy=262,460
AudioMixer4 mixer1; //xy=423,426
AudioFilterBiquad filter1; //xy=576,368
AudioFilterBiquad filter3; //xy=576,497
AudioFilterBiquad filter2; //xy=577,431
AudioMixer4 mixer2; //xy=757,425
AudioOutputI2S i2s1; //xy=925,425
AudioConnection patchCord1(waveform1, 0, mixer1, 0);
AudioConnection patchCord2(pink1, 0, mixer1, 1);
AudioConnection patchCord3(mixer1, 0, filter1, 0);
AudioConnection patchCord4(mixer1, 0, filter2, 0);
AudioConnection patchCord5(mixer1, 0, filter3, 0);
AudioConnection patchCord6(filter1, 0, mixer2, 0);
AudioConnection patchCord7(filter3, 0, mixer2, 2);
AudioConnection patchCord8(filter2, 0, mixer2, 1);
AudioConnection patchCord9(mixer2, 0, i2s1, 0);
AudioConnection patchCord10(mixer2, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=253,200
// GUItool: end automatically generated code
AudioFilterBiquad *filters[] = {&filter1, &filter2, &filter3};
const int NUM_VOWELS = 10, NUM_FILTERS = 3;
const float vowels[NUM_VOWELS][NUM_FILTERS] = {
{570, 840, 2410}, // /ow/ bought
{300, 870, 2240}, // /oo/ boot
{440, 1020, 2240}, // /u/ foot
{730, 1090, 2440}, // /a/ hot
{520, 1190, 2390}, // /uh/ but
{490, 1350, 1690}, // /er/ bird
{660, 1720, 2410}, // /ae/ bat
{530, 1840, 2480}, // /e/ bet
{390, 1990, 2550}, // /i/ bit
{270, 2290, 3010} // /iy/ beet
};
void setup() {
AudioMemory(12);
sgtl5000_1.enable(); // Enable the audio shield
sgtl5000_1.volume(0.5);
waveform1.begin(0.0, 210, WAVEFORM_SAWTOOTH);
pink1.amplitude(0.0);
//setVowel(2);
usbMIDI.setHandleNoteOff(OnNoteOff);
usbMIDI.setHandleNoteOn(OnNoteOn);
usbMIDI.setHandleControlChange(OnControlChange);
pinMode(LED_BUILTIN, OUTPUT);
}
float noteFreq = 200.0;
float noteDir = 0.0;
int thisVowel = 0;
void loop() {
/*
setVowel(thisVowel);
delay(500);
thisVowel++;
if (thisVowel >= NUM_VOWELS) thisVowel=0;
*/
for (int i=0; i < 30; i++) {
usbMIDI.read(); // USB MIDI receive
delay(1);
}
noteFreq += noteDir;
waveform1.frequency(noteFreq);
}
void setVowel(int vowelIndex) {
for (int filterIndex=0; filterIndex<NUM_FILTERS; filterIndex++) {
//filters[filterIndex]->resonance(7.0);
//filters[filterIndex]->frequency(vowels[vowelIndex][filterIndex]);
filters[filterIndex]->setBandpass(0, vowels[vowelIndex][filterIndex], 7.0);
}
}
int currentNoteNum = 0;
float noiseAmount = 1.0;
float pitchedAmount = 0.0;
void OnNoteOn(byte channel, byte note, byte velocity) {
int v = note % 12;
v %= NUM_VOWELS;
setVowel(v);
currentNoteNum = note;
pink1.amplitude(noiseAmount * velocity / 127.0);
waveform1.amplitude(pitchedAmount * velocity / 127.0);
noteFreq = random(250,500);
noteDir = random(-100,100) / 50.0;
waveform1.frequency(noteFreq);
digitalWrite(LED_BUILTIN, 1);
}
void OnNoteOff(byte channel, byte note, byte velocity) {
if (note==currentNoteNum) {
pink1.amplitude(0);
waveform1.amplitude(0);
digitalWrite(LED_BUILTIN, 0);
}
}
void OnControlChange(byte channel, byte control, byte value) {
if (control==73) {
noiseAmount = value / 127.0;
}
else if (control==72) {
pitchedAmount = value / 127.0;
}
}
@moonmilk
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment