Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ let piano = midi.channel(1);
piano.pitchBend(8192 + input.acceleration(Dimension.X) * 8)
```

## Logging packets

Add the **midi use console** block will print all MIDI packets to the console.

```block
midi.useConsole()
```

## Radio serial

You can use radio to send MIDI messages from various @boardname@ and play them via [Hairless MIDI](http://projectgus.github.io/hairless-midiserial/).
Expand Down
1 change: 0 additions & 1 deletion enums.d.ts

This file was deleted.

8 changes: 0 additions & 8 deletions extension.cpp

This file was deleted.

24 changes: 7 additions & 17 deletions midi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,14 @@ namespace midi {
}

/**
* Send human readable MIDI messages via serial
* Send human readable MIDI messages via console
*/
//% blockId=midi_serial_transport block="midi use serial"
//% blockId=midi_serial_transport block="midi use console"
//% weight=1 blockGap=8
//% group=Transports
export function useSerial() {
export function useConsole() {
function send(data: Buffer): void {
// waiting for beta
//const buf = pins.createBuffer(data.length);
//for (let i = 0; i < data.length; ++i)
// buf[i] = data[i];
// serial.writeBuffer(buf);
serial.writeString("midi:")
for (let i = 0; i < data.length; ++i) {
if (i > 0) serial.writeString(",");
serial.writeNumber(data[i]);
}
serial.writeLine("");
console.log(`midi: ${data.toHex()}`)
}
setTransport(send)
}
Expand Down Expand Up @@ -359,17 +349,17 @@ namespace midi {
channel(1).pitchBend(bend);
}

const notes = [8, 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 18, 19, 21, 22, 23, 24, 26, 28, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544];
/**
* Maps a frequency to a note key
* @param frequency
*/
//% blockId=midi_frequency_to_key block="key at %frequency=device_note" useEnumVal=1
//% shim=midi::frequencyToKey weight=4 blockGap=8
//% weight=4 blockGap=8
//% group="Constants"
export function frequencyToKey(frequency: number): number {
const notes = [8, 9, 9, 10, 10, 11, 12, 12, 13, 14, 15, 15, 16, 17, 18, 19, 21, 22, 23, 24, 26, 28, 29, 31, 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62, 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123, 131, 139, 147, 156, 165, 175, 185, 196, 208, 220, 233, 247, 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951, 4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645, 7040, 7459, 7902, 8372, 8870, 9397, 9956, 10548, 11175, 11840, 12544];
// return Math.max(0, Math.min(127, Math.round(Math.log(frequency/440.0)/Math.log(2) * 12 + 69)));
let left = 0, right = 0x7f, mid = 69;

while (right - left > 1) {
const midf = notes[mid];
if (frequency == midf)
Expand Down
7 changes: 2 additions & 5 deletions pxt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "midi",
"version": "2.1.11",
"version": "3.0.0",
"description": "A MIDI output controller package",
"license": "MIT",
"dependencies": {
Expand All @@ -10,10 +10,7 @@
"README.md",
"midi.ts",
"instruments.ts",
"drums.ts",
"extension.cpp",
"shims.d.ts",
"enums.d.ts"
"drums.ts"
],
"testFiles": [
"tests.ts"
Expand Down
1 change: 0 additions & 1 deletion shims.d.ts

This file was deleted.

5 changes: 2 additions & 3 deletions tests.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// tests go here; this will not be compiled when this package is used as a library
midi.useConsole();
midi.playTone(400, 500);
for (let i = 0; i < 127; ++i)
midi.playDrum(i)
let i = midi.channel(1);
i.setInstrument(MidiInstrument.Banjo);

let piano = midi.channel(0);
let trumpet = midi.channel(1);
piano.setInstrument(MidiInstrument.Trumpet);

serial.writeLine('B ' + Note.B + ' -> ' + midi.frequencyToKey(Note.B))
console.log('B ' + Note.B + ' -> ' + midi.frequencyToKey(Note.B))