MIDI over UDP (Teensy 4.1 + Ethernet Kit)

Lestra

Member
Hello! I want to send midi messages via ethernet to be received by ipMidi on pc side. Going thru Teensy Examples but I can't find exact code for this. What library to include and how to alter this code to get this work?

Code:
const int channel = 10;  // General MIDI: channel 10 = percussion sounds
const int note = 38;     // General MIDI: note 38 = acoustic snare

const int analogPin = A0;
const int thresholdMin = 60;  // minimum reading, avoid noise and false starts
const int peakTrackMillis = 12;
const int aftershockMillis = 25; // aftershocks & vibration reject


void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 2500) /* wait for serial monitor */ ;
  Serial.println("Piezo Peak Capture");
}


void loop() {
  int piezo = analogRead(analogPin);
  peakDetect(piezo);
  // Add other tasks to loop, but avoid using delay() or waiting.
  // You need loop() to keep running rapidly to detect Piezo peaks!

  // MIDI Controllers should discard incoming MIDI messages.
  // [url]http://forum.pjrc.com/threads/24179-Teensy-3-Ableton-Analog-CC-causes-midi-crash[/url]
  while (usbMIDI.read()) {
    // ignore incoming messages
  }
}


void peakDetect(int voltage) {
  // "static" variables keep their numbers between each run of this function
  static int state;  // 0=idle, 1=looking for peak, 2=ignore aftershocks
  static int peak;   // remember the highest reading
  static elapsedMillis msec; // timer to end states 1 and 2

  switch (state) {
    // IDLE state: wait for any reading is above threshold.  Do not set
    // the threshold too low.  You don't want to be too sensitive to slight
    // vibration.
    case 0:
      if (voltage > thresholdMin) {
        //Serial.print("begin peak track ");
        //Serial.println(voltage);
        peak = voltage;
        msec = 0;
        state = 1;
      }
      return;

    // Peak Tracking state: capture largest reading
    case 1:
      if (voltage > peak) {
        peak = voltage;     
      }
      if (msec >= peakTrackMillis) {
        //Serial.print("peak = ");
        //Serial.println(peak);
        int velocity = map(peak, thresholdMin, 1023, 1, 127);
        usbMIDI.sendNoteOn(note, velocity, channel);
        msec = 0;
        state = 2;
      }
      return;

    // Ignore Aftershock state: wait for things to be quiet again.
    default:
      if (voltage > thresholdMin) {
        msec = 0; // keep resetting timer if above threshold
      } else if (msec > aftershockMillis) {
        usbMIDI.sendNoteOff(note, 0, channel);
        state = 0; // go back to idle when
      }
  }
}

THANKS!
 
Last edited:
Code:
const int channel = 10; // General MIDI: channel 10 = percussion sounds
const int note = 38; // General MIDI: note 38 = acoustic snare

const int analogPin = A0;
const int thresholdMin = 60; // minimum reading, avoid noise and false starts
const int peakTrackMillis = 12;
const int aftershockMillis = 25; // aftershocks & vibration reject


void setup() {
	Serial.begin(115200);
	while (!Serial && millis() < 2500) /* wait for serial monitor */;
	Serial.println("Piezo Peak Capture");
}


void loop() {
	int piezo = analogRead(analogPin);
	peakDetect(piezo);
	// Add other tasks to loop, but avoid using delay() or waiting.
	// You need loop() to keep running rapidly to detect Piezo peaks!

	// MIDI Controllers should discard incoming MIDI messages.
	// http://forum.pjrc.com/threads/24179-...ses-midi-crash
	while (usbMIDI.read()) {
		// ignore incoming messages
	}
}


void peakDetect(int voltage) {
	// "static" variables keep their numbers between each run of this function
	static int state; // 0=idle, 1=looking for peak, 2=ignore aftershocks
	static int peak; // remember the highest reading
	static elapsedMillis msec; // timer to end states 1 and 2

	switch (state) {
		// IDLE state: wait for any reading is above threshold. Do not set
		// the threshold too low. You don't want to be too sensitive to slight
		// vibration.
	case 0:
		if (voltage > thresholdMin) {
			//Serial.print("begin peak track ");
			//Serial.println(voltage);
			peak = voltage;
			msec = 0;
			state = 1;
		}
		return;

		// Peak Tracking state: capture largest reading
	case 1:
		if (voltage > peak) {
			peak = voltage;
		}
		if (msec >= peakTrackMillis) {
			//Serial.print("peak = ");
			//Serial.println(peak);
			int velocity = map(peak, thresholdMin, 1023, 1, 127);
			usbMIDI.sendNoteOn(note, velocity, channel);
			msec = 0;
			state = 2;
		}
		return;

		// Ignore Aftershock state: wait for things to be quiet again.
	default:
		if (voltage > thresholdMin) {
			msec = 0; // keep resetting timer if above threshold
		}
		else if (msec > aftershockMillis) {
			usbMIDI.sendNoteOff(note, 0, channel);
			state = 0; // go back to idle when
		}
	}
}
In future could you enclose your posted code between code tags using the # button, I think you will agree that it makes your code much easier to understand. This will make it more likely that someone will be able to help you.
Unfortunately I have no experience using Midi so I do not fall into that group.
 
Played with that lib briefly with Serial and USB Midi on Mega and Leonardo and noted IpMidi support. No use case for IpMidi or Ethernet here so both are unexplored territory.

The lib uses the same underlying FortySevenEffects library as Teensy does and looks like it has some support for Teensy.

The example, BasicIO.ino is sending note messages which I'd assume would work on say a Leonardo or Mega with an Ethernet shield so that's what I'd start with, get one working exactly like the lib shows then start looking at Teensy.

The unknown quantity is whether or not it will work with Teensy 4.1's Ethernet hardware so here's where you dive into the rabbithole.
 
Thank you MatrixRat. I will try my best and will come back with results but it will be better if someone more experienced than me come with answers...
 
Back
Top