Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Midi signal via serial seems wrong #4

Closed
JonahPi opened this issue Mar 24, 2018 · 9 comments
Closed

Midi signal via serial seems wrong #4

JonahPi opened this issue Mar 24, 2018 · 9 comments

Comments

@JonahPi
Copy link

JonahPi commented Mar 24, 2018

I used the Midi package with a very simple program but was not able to get out any tone. Using a similar Python code worked perfectly fine. In both cases I transmit the Midi-code via Pin 0 and use the Midi via Serial command. When I compared the signals from the Phython code with the one from the PXT-code there was an obivious difference. (s.a. forum entry here: link )

@Gadgetoid
Copy link
Contributor

I've just realised this when testing today- it's not actually sending MIDI over Serial at all, but is instead sending a string representation of it.

See: https://github.com/Microsoft/pxt-midi/blob/fb9de98860c6c5cd0a60844673f450f1bc55abbf/midi.ts#L80-L95

The practical upshot of this is that the "MIDI" is very human-readable, but unfortunately isn't readable by any off-the-shell Serial->MIDI bridge software I've come across.

The format is:

midi:<command>,<arg>,<arg> \r\n

(Not I've taken liberty with the amount of whitespace, but that is part of the output)

You could write your own client in Python that handles MIDI messages in this format and injects them directly into the system. It'd be relatively easy using serial.readline.

However I think this library needs a new block: "useRawSerial()"

This block would run the following code:

function send(data: Buffer): void {
    serial.writeBuffer(data)
}
midi.setTransport(send);

This writes the raw - not human readable - MIDI data directly out to the serial port, and I can confirm this works with Hairless MIDI <-> Serial Bridge.

Gadgetoid added a commit to Gadgetoid/pxt-midi that referenced this issue May 2, 2018
pelikhan pushed a commit that referenced this issue May 2, 2018
@JonahPi
Copy link
Author

JonahPi commented May 20, 2018

Using the new block "midi use raw serial" it works like a charm. Thanks a lot for your help!

@JonahPi JonahPi closed this as completed May 20, 2018
@pelikhan
Copy link
Member

Anyone up to write a project on how to use this?

@JonahPi
Copy link
Author

JonahPi commented May 20, 2018

micromidiconnection
microbitsamplecode

This setup allows to connect any kind of instrument with a midi-input. I tested it with an e-piano and this little devices which was described in the German Make magazine.

@CaptainCredible
Copy link

CaptainCredible commented May 20, 2018 via email

@pelikhan
Copy link
Member

pelikhan commented May 21, 2018 via email

@JonahPi
Copy link
Author

JonahPi commented May 21, 2018

@pelikhan : Sure, 'would be cool to see more people using it.

@CaptainCredible : No long time tests run yet but I will work on a midi project soon, 'hope it works out.

@pelikhan
Copy link
Member

pelikhan commented May 21, 2018 via email

@JonahPi
Copy link
Author

JonahPi commented May 27, 2018

In this case I think it should be a little bit extended to see the differences between polymorphic and single tones:

let Guitar: midi.MidiController = null
input.onButtonPressed(Button.A, () => {
    basic.showIcon(IconNames.EigthNote)
    Guitar.note(midi.frequencyToKey(262), music.beat(BeatFraction.Whole))
    basic.pause(100)
    Guitar.note(midi.frequencyToKey(330), music.beat(BeatFraction.Whole))
    basic.pause(200)
    Guitar.note(midi.frequencyToKey(440), music.beat(BeatFraction.Whole))
    basic.pause(300)
    basic.showIcon(IconNames.Yes)
})
input.onButtonPressed(Button.B, () => {
    basic.showIcon(IconNames.Heart)
    Guitar.noteOn(midi.frequencyToKey(262))
    Guitar.noteOn(midi.frequencyToKey(330))
    Guitar.noteOn(midi.frequencyToKey(440))
    basic.pause(800)
    Guitar.noteOff(midi.frequencyToKey(262))
    basic.pause(500)
    Guitar.noteOff(midi.frequencyToKey(330))
    basic.pause(500)
    Guitar.noteOff(midi.frequencyToKey(440))
    basic.showIcon(IconNames.Yes)
})
serial.redirect(
SerialPin.P0,
SerialPin.P1,
BaudRate.BaudRate31250
)
midi.useRawSerial()
basic.showIcon(IconNames.Yes)
Guitar = midi.channel(1)
Guitar.setInstrument(MidiInstrument.AcousticGuitarNylon)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants