Simple MIDI library

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello. I'm quite new to those things, but I know the basics of programming AVR chips. I am looking for a very simple library, that would just send MIDI data over a specified pin as fast as possible. I am planning to make a MIDI sequencer, but i would like to avoid arduinos and such, because of the number of instruction it takes to make the same thing as port manipulation can do in one. I would also like to create my own custom PCB, so that would push me away from arduino boards even more. Also I want to use the standard DIN connector instead of sending data over USB. 

 

Thanks for your replies and I apologize, if my question seems to be too much dumb. 

Last Edited: Fri. Nov 22, 2019 - 07:56 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, MIDI is just serial data - so the "simplest" MIDI library is just sending & receiving serial data over a UART.

 

As for speed, the baud rate is fixed by the MIDI specification.

 

MIDI has been around for a long time - long before Arduino - so there are plenty of resources out there.

 

You can download the specifications here:  https://www.midi.org/specifications

 

Apart from a general google search, a search on Github is always good when looking specifically for source code.

 

And, of course, search this forum!

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

KodzghlyCZ wrote:
I am planning to make a MIDI sequencer, but i would like to avoid arduinos and such, because of the number of instruction it takes to make the same thing as port manipulation can do in one. I would also like to create my own custom PCB, so that would push me away from arduino boards even more.

 

What difference would port manipulation make to MIDI? Besides you can easily do direct port manipulation with the Arduino tools exactly the same as you would with non-Arduino tools. The port manipulation stuff is relatively slow using the Arduino digitalRead/Write functions as they decided to sacrifice speed with ease of use. The other functions don't need to make such sacrifices. You'd have to work hard to write adc or i2c code that works faster than the standard Arduino functions. Again, you're not forced to use them.

 

Arduino is open source - there is nothing to stop you from making your own boards. The 'only' thing specific to Arduino in the hardware is the bootloader - this is to make loading the code easy. The actual code does not care if you use a bootloader or not. As well, the code does not have to be generated by Arduino tools - the board is just a plain mega328 with the minimum required hardware to get it to work and load code.

 

Apart from the above, there's no reason why you can't use an Arduino library for MIDI - at least you know it has a fair chance of being tested by thousands of people. Code wise, it would be much the same if it wasn't an Arduino library. Arduino doesn't infer any sort of 'magic' that would make the code structure any different.

 

your choice - use something that is well tested or go the less travelled road.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KodzghlyCZ wrote:
, because of the number of instruction it takes to make the same thing as port manipulation can do in one.
Then you do not understand MIDI. It is UART at 31250 baud. To send a byte over MIDI the simplest form is:

UDR = byte;

Of course you probably want some flow control in that so maybe more like:

while (!(UCSR0A & (1 <<UDRE)));
UDR = byte;

now, if you were to try and "bit-bang" that over a port pin it would be something like:

void send_MIDI(uint8_t byte) {
    uint8_t i;
    TX_PIN_LOW; // send start bit
    _delay_us(32); // 32us pulses for 31250 baud
    for (i = 0; i < 8; i++) {
        if (byte & 1) {
            TX_PIN_HIGH;
        }
        else {
            TX_PIN_LOW;
        }
        _delay_us(32);
        byte >>= 1;
    }
    TX_PIN_HIGH; // stop bit
    _delay_us(32);
}

I just made that up though I *think* it probably does the right thing. But it gives an idea of how much more complex it is to bit-bang MIDI on a pin. Of course the ultimate would be if you could just write the entire program as:

void setup() {
    MIDI.begin();
}

void loop() {
    MIDI.send(byte);
}

Oh, wait a minute, I think I just invented Arduino  !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
there's no reason why you can't use an Arduino library for MIDI ... Arduino doesn't infer any sort of 'magic'

Indeed.

 

the board is just a plain mega328

Other cores are available

 

 

awneil wrote:
 MIDI is just serial data

So the only bit that would be specific to AVR is the lowest-level detail of the UART driver.

 

Above that, it should all be entirely platform-independent.

 

So you can take your example code from any platform ...

 

laugh

 

EDIT

 

I see clawson has also made the "it's just a UART" point while I was typing ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Nov 22, 2019 - 09:29 AM