Arduino Forum

Using Arduino => Audio => Topic started by: OzGrant on Feb 10, 2011, 09:10 am

Title: MIDI Serial Port
Post by: OzGrant on Feb 10, 2011, 09:10 am
G'day,
Is it possible to use NewSoftSerial so that another pin(port) for the MIDI signal in the MIDI library is used.
Tks
Title: Re: MIDI Serial Port
Post by: AndrewGarza on Feb 10, 2011, 12:23 pm

Is it possible to use NewSoftSerial so that another pin(port) for the MIDI signal in the MIDI library is used.


Is there a MIDI library? And don't you only use port 1 (TX) port for outputting MIDI? What port would you rather use?
Title: Re: MIDI Serial Port
Post by: robtillaart on Feb 10, 2011, 12:46 pm

Looking at the NewSoftSerial.cpp file it has an entry for 31250 baud in its tables which is IIRC the MIDI speed. Start/stop/parity bits unkown.


Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 10, 2011, 05:55 pm
Not sure if I explained my problem correctly.
The MIDI library uses digital pins 0 & 1 and I want to use pins 8 & 9.
The command MIDI.begin() does not give you the option of altering the serial port, and am hoping that NewSoftSerial library can be used to achieve this.
Can this be done.
Title: Re: MIDI Serial Port
Post by: robtillaart on Feb 10, 2011, 06:03 pm
Do you have an URL to the MIDI library to peek into the code?
Title: Re: MIDI Serial Port
Post by: Grumpy_Mike on Feb 10, 2011, 06:50 pm
Quote
The MIDI library uses digital pins 0 & 1

Yes that is because it uses the hardware serial connections. To make the library use the softwa  serial routines you will have to hack it so that all the references to the serial port now refer to the software serial commands.
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 10, 2011, 10:21 pm
Rob,
Am using theV2.5  MIDI library from the playground http://www.arduino.cc/playground/Main/MIDILibrary.
Will now start to look at how to hack it.
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 11, 2011, 03:18 am
Is it possible to use pin 1 (TX) to communicate with the USB and Pin 0 (Rx) to accept MIDI inputs.
That way I can use to use the existing MIDI library.
I need to use the USB port to send the MIDI date so that I can read it on the PC.
Title: Re: MIDI Serial Port
Post by: Grumpy_Mike on Feb 11, 2011, 11:03 am
Quote
Is it possible to use pin 1 (TX) to communicate with the USB and Pin 0 (Rx) to accept MIDI inputs.
That way I can use to use the existing MIDI library.

No you can't run the hardware at different baud rates.
Title: Re: MIDI Serial Port
Post by: robtillaart on Feb 12, 2011, 12:38 pm
@OzGrant
Had a quick view in the midilib and somewhere in the top of the CPP file there is    #define UART Serial    which is used through the code as Serial port.

Quick hack:
- include the NSS library,
- declare an instance of NSS e.g. MidiSerial
- do a global search/replace of UART with MidiSerial

Better:
- accept param TX and RX in the constructor (default 0 & 1 to be BW compatible)
- if 0 and 1 => use hardware serial
- if other pinnr's => use NSS


Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 12, 2011, 08:51 pm
Rob,
Yup found that declare, so will give it a shot. Haven't done this level of program altering in a library, so it should be interesting.
Thanks for the pointer.
Title: Re: MIDI Serial Port
Post by: nickvd on Feb 12, 2011, 09:36 pm
I haven't really dug into the code, but I am using the Midi library (notice that it is not all-caps) from here: http://timothytwillman.com/itp_blog/?page_id=240

You initialize the class using:
Code: [Select]
Midi midi(Serial);

So it should be as simple as passing in your nss object to the midi constructor...

(i also find this midi library much more versatile for midi-input)..
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 12, 2011, 11:56 pm
Timothy,
Looks like your MIDI Lib is what I'm after.
Down loaded it (the tgz extension ended up as a .tar) but my winzip ver 14.5 gave me the msg
"Error reading header after processing 0 entries"
Does this mean WinZip does not do tar files or is something else required.
Grant
Title: Re: MIDI Serial Port
Post by: nickvd on Feb 13, 2011, 09:28 am
I used izarc without issues... it's free and imo better than winzip
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 13, 2011, 12:08 pm
Tks Nick (got the name correct this time)
Downloaded the Lib Ok and unzipped with IzArc.
So will now see if I can use the other port.
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 15, 2011, 08:47 am
Code: [Select]
Well after many hrs have return to the forum so some clever person can tell me what I've done wrong.
The Led does not flash whne a midi signal is rx'ed.
The sketch is[code#include "Midi.h"
#include "NewSoftSerial.h"

NewSoftSerial mySerial(10, 11);
class MyMidi : public Midi {
  public:
    MyMidi(HardwareSerial &mySerial) : Midi(mySerial) {}
 
  void handleNoteOn(unsigned int channel, unsigned int note, unsigned int velocity)
  {
    digitalWrite(13, HIGH);
  }

  void handleNoteOff(unsigned int channel, unsigned int note, unsigned int velocity)
  {
    digitalWrite(13, LOW);
  }
};

// Create an instance of the MyMidi class.
MyMidi midi(Serial);

void setup()
{
  pinMode(13, OUTPUT);
  mySerial.begin(31250);
  midi.begin(0);
}

void loop()

   midi.poll();
}

]
Title: Re: MIDI Serial Port
Post by: nickvd on Feb 16, 2011, 01:23 am
Code: [Select]
MyMidi midi(Serial);

You're still passing the hardware serial to the midi lib... you need to pass the nss object that you created earlier..

Code: [Select]
NewSoftSerial mySerial(10, 11);
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 16, 2011, 07:06 pm
Had declared NewSoftSerial mySerial(10, 11); after the includes.
But as you pointed out was still using MyMidi midi(Serial);

So changed that to MyMidi midi(mySerial);  then got a "no matching function error"
Tried MyMidi midi(HardwareSerial); then got non class type when midi.begin(0) compiled.

I just do not know enough about what I am doing.
The example sketch had MyMidi(HardwareSerial &s) : Midi(s) {}
Not even  sure if my change to  MyMidi(HardwareSerial &mySerial) : Midi(mySerial) {} is OK
So am lost in code.
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 18, 2011, 02:06 am
G'day,
It seems my problem is not knowing how to use the Class statement.
Have started a thread in "Programmimg questions" as that is the root of my problem with the MIDI.
Tks for your help up to this stage.
Title: Re: MIDI Serial Port
Post by: OzGrant on Feb 18, 2011, 07:48 pm
It seems the MIDI 2.5 library does allow a different  tx/rx port to be used see http://blog.makezine.com/archive/2009/01/midi-programming-library-for-arduin.html
Has anyone done it.