Waveform Object Problem with Midi Input

Status
Not open for further replies.

MidiLifeCrisis

New member
Hey Everyone, I'm designing a wavetable synth with the Teensy 3.2 (using the Teensy Audio Library), with midi input and audio out for a class I'm taking at Portland Community College. I've noticed when I use the "waveform" object in my code, when I press a key on the midi keyboard it plays the correct tone but also plays another tone also, which muddies the pitch and you can't hear the difference between notes (this is the best way I can describe it). To troubleshoot, I tried my same code using the "sine" object and it works just as it's supposed to (same with "drum" and "string"), so I'm wondering what I could be doing wrong that would allow me to have this problem.

I have an array in my code with 128 floats that contains all the frequencies related to their corresponding midi notes that get's called to determine the frequency of the oscillator when a note on message is received. I'm also using a 6N138 optocoupler for my midi input.

Here's my code with the "waveform" object:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveform waveform1; //xy=236,205
AudioMixer4 mixer1; //xy=399,211
AudioOutputAnalog dac1; //xy=556,211
AudioConnection patchCord1(waveform1, 0, mixer1, 0);
AudioConnection patchCord2(mixer1, dac1);
// GUItool: end automatically generated code



#include <MIDI.h>

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

const float noteFreqs[128] = {8.176, 8.662, 9.177, 9.723, 10.301, 10.913, 11.562, 12.25, 12.978, 13.75, 14.568, 15.434, 16.352, 17.324, 18.354, 19.445, 20.602, 21.827, 23.125, 24.5, 25.957, 27.5, 29.135, 30.868, 32.703, 34.648, 36.708, 38.891, 41.203, 43.654, 46.249, 48.999, 51.913, 55, 58.27, 61.735, 65.406, 69.296, 73.416, 77.782, 82.407, 87.307, 92.499, 97.999, 103.826, 110, 116.541, 123.471, 130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942, 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883, 523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767, 1046.502, 1108.731, 1174.659, 1244.508, 1318.51, 1396.913, 1479.978, 1567.982, 1661.219, 1760, 1864.655, 1975.533, 2093.005, 2217.461, 2349.318, 2489.016, 2637.02, 2793.826, 2959.955, 3135.963, 3322.438, 3520, 3729.31, 3951.066, 4186.009, 4434.922, 4698.636, 4978.032, 5274.041, 5587.652, 5919.911, 6271.927, 6644.875, 7040, 7458.62, 7902.133, 8372.018, 8869.844, 9397.273, 9956.063, 10548.08, 11175.3, 11839.82, 12543.85};

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(57600);
Serial.println("MIDI Input Test");
AudioMemory(10);
analogWrite(A14, 1);
delay(10);

}

unsigned long t=0;

void loop() {

waveform1.begin(1.0, noteFreqs[MIDI.getData1()], WAVEFORM_SAWTOOTH);
waveform1.amplitude(1.0);

int type, note, velocity, channel, d1, d2;
if (MIDI.read()) { // Is there a MIDI message incoming ?
byte type = MIDI.getType();
switch (type) {
case midi::NoteOn:

note = MIDI.getData1();
velocity = MIDI.getData2();
channel = MIDI.getChannel();
if (velocity > 0) {
Serial.println(String("Note On: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
} else {
Serial.println(String("Note Off: ch=") + channel + ", note=" + note);
}
Serial.println(noteFreqs[MIDI.getData1()]);

break;
case midi::NoteOff:
note = MIDI.getData1();
velocity = MIDI.getData2();
channel = MIDI.getChannel();
Serial.println(String("Note Off: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
break;
default:
d1 = MIDI.getData1();
d2 = MIDI.getData2();
Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
}

}
}

--------------------------------------------------------------------------------------------------

And here's my code with the "sine" object:

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioSynthWaveformSine sine1; //xy=189,195
AudioMixer4 mixer1; //xy=399,211
AudioOutputAnalog dac1; //xy=556,211
AudioConnection patchCord1(sine1, 0, mixer1, 0);
AudioConnection patchCord2(mixer1, dac1);
// GUItool: end automatically generated code

#include <MIDI.h>

MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, MIDI);

const float noteFreqs[128] = {8.176, 8.662, 9.177, 9.723, 10.301, 10.913, 11.562, 12.25, 12.978, 13.75, 14.568, 15.434, 16.352, 17.324, 18.354, 19.445, 20.602, 21.827, 23.125, 24.5, 25.957, 27.5, 29.135, 30.868, 32.703, 34.648, 36.708, 38.891, 41.203, 43.654, 46.249, 48.999, 51.913, 55, 58.27, 61.735, 65.406, 69.296, 73.416, 77.782, 82.407, 87.307, 92.499, 97.999, 103.826, 110, 116.541, 123.471, 130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220, 233.082, 246.942, 261.626, 277.183, 293.665, 311.127, 329.628, 349.228, 369.994, 391.995, 415.305, 440, 466.164, 493.883, 523.251, 554.365, 587.33, 622.254, 659.255, 698.456, 739.989, 783.991, 830.609, 880, 932.328, 987.767, 1046.502, 1108.731, 1174.659, 1244.508, 1318.51, 1396.913, 1479.978, 1567.982, 1661.219, 1760, 1864.655, 1975.533, 2093.005, 2217.461, 2349.318, 2489.016, 2637.02, 2793.826, 2959.955, 3135.963, 3322.438, 3520, 3729.31, 3951.066, 4186.009, 4434.922, 4698.636, 4978.032, 5274.041, 5587.652, 5919.911, 6271.927, 6644.875, 7040, 7458.62, 7902.133, 8372.018, 8869.844, 9397.273, 9956.063, 10548.08, 11175.3, 11839.82, 12543.85};

void setup() {
MIDI.begin(MIDI_CHANNEL_OMNI);
Serial.begin(57600);
Serial.println("MIDI Input Test");
AudioMemory(10);
analogWrite(A14, 1);
delay(10);

}

unsigned long t=0;

void loop() {

sine1.amplitude(1.0);

int type, note, velocity, channel, d1, d2;
if (MIDI.read()) { // Is there a MIDI message incoming ?
byte type = MIDI.getType();
switch (type) {
case midi::NoteOn:

note = MIDI.getData1();
velocity = MIDI.getData2();
channel = MIDI.getChannel();
if (velocity > 0) {
Serial.println(String("Note On: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
} else {
Serial.println(String("Note Off: ch=") + channel + ", note=" + note);
}
sine1.frequency(noteFreqs[MIDI.getData1()]);

Serial.println(noteFreqs[MIDI.getData1()]);

break;
case midi::NoteOff:
note = MIDI.getData1();
velocity = MIDI.getData2();
channel = MIDI.getChannel();
Serial.println(String("Note Off: ch=") + channel + ", note=" + note + ", velocity=" + velocity);
break;
default:
d1 = MIDI.getData1();
d2 = MIDI.getData2();
Serial.println(String("Message, type=") + type + ", data = " + d1 + " " + d2);
}

}
}

----------------------------------------------------------------------------------------------------

Thank you SO much for taking the time to read this!
 
waveform1.begin(1.0, noteFreqs[MIDI.getData1()], WAVEFORM_SAWTOOTH);
waveform1.amplitude(1.0);.............if amplitude is constant only needs called once....but can be in loop if need it changed
Waveform1 only needs to "begin once" so put outside loop in setup or some other way if you want to change it while prog runs
This starts the sawtooth..... I don't see where you start the sine1 waveform....it will need its own begin

This is the DAC pin output why have you this line........."analogWrite(A14, 1);" ......I don't think its needed

Are you using velocity to control amplitude........then control it by velocity midi message

I have only glanced through this briefly....so these are some thing to sort and think about.......there is an example in the teensy audio library



This line ......sine1.frequency(noteFreqs[MIDI.getData1()]); set sine1 at a frequency ..........you need similar line for the sawtooth part
 
Hey thanks for the reply! I moved
waveform1.begin(WAVEFORM_SAWTOOTH);
waveform1.amplitude(1.0);

to the setup loop and it worked! I could've sworn my teacher and I tried that but I guess we in fact didn't.

I thought that the "analogWrite(A14, 1);" was needed to turn on the DAC - and when I omit it I do not get any sound. I was told that both analogWrite or digitalWrite work in this instance.

And also I haven't begun to think about velocity just yet, was more concerned with troubleshooting this pitch problem I was having.

Thanks for your reply!
 
Status
Not open for further replies.
Back
Top