Go Down

Topic: MIDI Note gets triggered twice and chops note (Read 761 times) previous topic - next topic

prismflux

Hello fellow tinkerers,

I'm trying to build a 808 Generator and I implemented midi today, but something doesn't work as expected. Whenever the Arduino receives a NoteOn Command that is longer than a simple click I get two midi NoteOns, one with the right velocity and the second with full velocity. However, when I'm sending only one small click it works as intended. Tested with both FL Studio and an Arturia USB Keyboard routed through Hairless MIDI <-> Serial Bridge. Code is attached below

Code: [Select]
/**
   808 Bassbox by Thallosaurus

   Usage: Connect a Speaker to Pin 9 and a Button to Pin 2

*/

#include <MozziGuts.h>
#include <Oscil.h>
#include <ADSR.h>
#include <tables/sin8192_int8.h>
#include <EventDelay.h>
#include <MIDI.h>           //MIDI
#include <mozzi_midi.h>     //MIDI
#include <Portamento.h>

#define CONTROL_RATE 64

#define BTN_LOW 2
#define BTN_HIGH 4
#define LED 11

int low_freq = 50;
int high_freq = 110;

Oscil <8192, AUDIO_RATE> aSin(SIN8192_DATA);
ADSR <AUDIO_RATE, AUDIO_RATE> adsr;
Portamento <AUDIO_RATE> glide;
//EventDelay noteDelay;

MIDI_CREATE_DEFAULT_INSTANCE();     //MIDI

unsigned int a, d, s, r; //attack, decay, sustain, release
byte volume;
unsigned int glideMS;

int play = 1;

void HandleNoteOn(byte channel, byte note, byte velocity) {   //MIDI
  aSin.setFreq(mtof((int) note));
  //adsr.setAttackLevel(velocity * 2);
  adsr.setADLevels(velocity * 2, 0);  //replace Decay Level with Potentiometer
  //glide.start((uint8_t) note);
  adsr.noteOn();
}

void HandleNoteOff(byte channel, byte note, byte velocity) {  //MIDI
  adsr.noteOff();
}

void readFromPotentiometer() {
  a = 1;
  d = 100;
  s = 0;
  r = 1000;
  adsr.setTimes(a, d, s, r);

  //glide.setTime(100);
}

void setup() {
  // put your setup code here, to run once:
  //Serial.begin(9600);

  MIDI.setHandleNoteOn(HandleNoteOn);
  MIDI.setHandleNoteOff(HandleNoteOff);

  MIDI.begin(MIDI_CHANNEL_OMNI);
 
  Serial.begin(115200);
  //pinMode(BTN_LOW, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  startMozzi(CONTROL_RATE);
}

void updateLED(unsigned char value) {
  if (value != 1) {
    play = 1;
    analogWrite(LED, value);
  } else {
    analogWrite(LED, LOW);
  }
}

void updateControl() {
  //aSin.setFreq_Q16n16(glide.next());
  MIDI.read();
  //int btnPressLow = digitalRead(BTN_LOW);

  //if (btnPressLow == HIGH) {
  //  aSin.setFreq(low_freq);
  //  adsr.noteOn();
  //} else {
  //  adsr.noteOff();
  //}
  readFromPotentiometer();
  updateLED(adsr.next());
}

int updateAudio() {
  adsr.update();
  return (int) (play * (adsr.next() * aSin.next())) >> 8;
}

void loop() {
  audioHook();
}


Why does this happen and how do I fix it? Every example I've read (both Arduino and Mozzi Library Examples do it this way) does work...

Go Up