How to translate Mouse to MIDI?

Hey guys,

I am a beginner and try to read out a mouse to translate it into MIDI signals, but I have a few problems. First of all I dont know why the mouse output to the left and right is not the same. It should be equal and I tried to fix it, but it only gets better when i write it in the draw(). But thats not my main issue I need to translate the mouse signal into a MIDI signal and I have no glue how to manage this. I guess I have to use the MIDIbus library, but HOW??? :smiley:

Would be great if anyone could help me out.

Thx in advance
tootha

This is the MouseRead:


import themidibus.*;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;



Robot robot;
int mouseSumX = 0;
int mouseSumY = 0;

final int mouseXMax = 2000;
final int mouseYMax = 400;

void setup() {                
  size(900, 400);
  try {
    robot = new Robot();
  } 
  catch (Exception e) {
    println("Can't Initialize the Robot");
  }
  resetMouse();
  println(mouseX + " " + mouseY);
  //noCursor();
}


void draw() {


    checkMouse();             
    
}

void mousePressed() {            
  if (mouseButton == LEFT) {  
    mouseSumX = 0;
    mouseSumY = 0;
  } else {
    mouseSumX = mouseXMax;
    mouseSumY = mouseYMax;
  }
}

void checkMouse() {            
  int mouseMoveX = mouseX - width/2;
  int mouseMoveY = mouseY - (height/2 - 22);  // -22 to set mouse in middle
  resetMouse();
  //println(mouseMoveX);

  if (frameCount > 1 && (abs(mouseMoveX) > 0 || abs(mouseMoveY) > 0)) {

    mouseSumX += mouseMoveX;
    mouseSumY += mouseMoveY;
    if (abs(mouseMoveX) > 0) {
      println("Event: x: " + mouseSumX + " " + mouseSumY);
    }
    if (abs(mouseMoveY) > 0) {
      println("Event: y: " + mouseSumX + " " + mouseSumY);
    }
  }
}

void resetMouse() {
  javax.swing.JFrame f = getJFrame(getSurface());
  //println(f.getLocationOnScreen().x + " " + f.getLocationOnScreen().y);
  robot.mouseMove(f.getLocationOnScreen().x + width/2, f.getLocationOnScreen().y + height/2);
}
static final javax.swing.JFrame getJFrame(final PSurface surf) {
  return
    (javax.swing.JFrame)
    ((processing.awt.PSurfaceAWT.SmoothCanvas)
    surf.getNative()).getFrame();
}

besides the import i not see much of a MIDI code
and i not want to follow that mouse code you have,

but to give a short idea how to start with a mouse piano
i just tested this: ( starting from the BASIC example and cut it down )

import themidibus.*; //Import the library

MidiBus myBus; // The MidiBus
ControlChange change;
Note note;
int channel = 0;
int pitch = 64;
int velocity = 127;

void setup() {
  size(400, 100);
  background(0);
  MidiBus.list(); // List all available Midi devices on STDOUT. This will show each device's index and name.
  myBus = new MidiBus(this, 0, 1);
  note = new Note(channel, pitch, velocity);
}

void draw() {
  myBus.sendNoteOn(note); // Send a Midi noteOn
  delay(500);
  myBus.sendNoteOff(note); // Send a Midi nodeOff
  delay(500);
}

void delay(int time) {
  int current = millis();
  while (millis () < current+time) Thread.yield();
}

void mousePressed() {
  if ( mouseButton == LEFT ) {                     // change note and loud by click
    pitch = int(map(mouseX, 0, width, 0, 127));
    print("mousePressed_LEFT_pitch X: "+pitch);    // Note
    velocity = int(map(mouseY, 0, height, 0, 127));
    println(" _velocity Y: "+velocity);            // loud
    note = new Note(channel, pitch, velocity);
  }
}