Hi,
I am trying to create a midi controller for Logic or Mainstage. In order to start simple, I have created a project with one encoder that controls one parameter (for instance the volume). This is working fine.
But now I want to receive the current setting from Logic or Mainstage. For instance, when the channel volume is changed by another way (for instance by using the mouse), I want to receive the new setting with teensy. This way I can update the setting for the encoder, and prevent any jumping.
However, I seem not the receive any messages from Logic or Mainstage at all.
Below the code that I am currently using to check if I am receiving any midi information at all. But none so far. Still, this should be possible. I saw examples of motorized faders responding to changes in the DAW.
Any help is very much apreciated!
It should print the message type if any message is received. But I am receiving none at all.
To be complete, below the whole code:
I am trying to create a midi controller for Logic or Mainstage. In order to start simple, I have created a project with one encoder that controls one parameter (for instance the volume). This is working fine.
But now I want to receive the current setting from Logic or Mainstage. For instance, when the channel volume is changed by another way (for instance by using the mouse), I want to receive the new setting with teensy. This way I can update the setting for the encoder, and prevent any jumping.
However, I seem not the receive any messages from Logic or Mainstage at all.
Below the code that I am currently using to check if I am receiving any midi information at all. But none so far. Still, this should be possible. I saw examples of motorized faders responding to changes in the DAW.
Any help is very much apreciated!
Code:
void MidiMessages(){
if(usbMIDI.read()){ //true if new midi message
Serial.println(usbMIDI.getType());
}
}
It should print the message type if any message is received. But I am receiving none at all.
To be complete, below the whole code:
Code:
#include <Encoder.h>
void setMidiPos(int valChange,int scale);//calculate valid mide value for the encoder
int setencSpeedFct(int interv);//calculate speed factor based on encoder rotation speed
void myControlChange(byte channel, byte control, byte value);//event handler midi control change
Encoder myEnc(6, 5);
unsigned long previousMillis = 0;//needed to calculate speed of turning the encoder
unsigned long currentMillis = 0;//needed to calculate speed of turning the encoder
long oldPosition = 0;//needed to calculate the position change of the encoder
long newPosition = 0;//needed to calculate the position change of the encoder
int encPosChange=0;//needed to keep track of the position change
bool encMoving=false;//flag indicates that the encoder is still being turned
int midiPos=0;//current midi value for the encoder
int encSpeedFct=1;//speed factor for multiplying the value change
const long interval = 200; // interval after which encodermovement is assumed to have stopped
const int midiCC=7; //7 is common for volume
void setup() {
// put your setup code here, to run once:
usbMIDI.setHandleControlChange(myControlChange);
}
void loop() {
// put your main code here, to run repeatedly:
MidiMessages();//read any incoming midimessages
EncoderMovement();//respond to encoder
}
//check for incoming midi messages
void MidiMessages(){
if(usbMIDI.read()){ //true if new midi message
Serial.println(usbMIDI.getType());//check if anything is received
}
}
void EncoderMovement(){
currentMillis = millis();//keep track of time
newPosition = myEnc.read();//check encoder movement
if (newPosition != oldPosition) {//encoder moved
encMoving=true;
encPosChange=encPosChange+newPosition-oldPosition;//keep track of posisiton changes
//filter out multiple pos changes that happen with each click
//otherwise you do not get a proper speed calculation
if(abs(encPosChange)>2)
{
setencSpeedFct(currentMillis-previousMillis);//calculate speed of turning rotary
setMidiPos(encSpeedFct*encPosChange/3,128);//calculate new midi value
usbMIDI.sendControlChange(midiCC, midiPos, 1);//sent value
previousMillis = currentMillis;//reset timer
encPosChange=0;//reset position changes
}
oldPosition= newPosition;
}
else{
if (currentMillis - previousMillis >= interval) {
//to much time without movement: stopped moving
if(encMoving) { //reset if it was moving
encMoving=false;
previousMillis = currentMillis;
encPosChange=0;
oldPosition= newPosition;
}
}
}
}
//calculate valid mide value
void setMidiPos(int valChange,int scale){
midiPos=midiPos+valChange;
if(midiPos>scale)
midiPos=scale-1;
if(midiPos<0)
midiPos=0;
}
//calculate multiply factor based on speed of encoder movement
int setencSpeedFct(int interv)
{
if(interv<100)
encSpeedFct=4;
else if(interv<200)
encSpeedFct=3;
else if(interv<400)
encSpeedFct=2;
else
encSpeedFct=1;
}
//respond to incoming control changes
void myControlChange(byte channel, byte control, byte value){
switch(control){
case midiCC:
setMidiPos(value,128);
return;
default:
Serial.println("Control:" + control);
return;
}
}