-
Notifications
You must be signed in to change notification settings - Fork 160
Extension Submission - "MIDI Devices" #342
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
Open
jexapatron
wants to merge
9
commits into
PenguinMod:main
Choose a base branch
from
jexapatron:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
041eec7
Create test
jexapatron f5397cf
Add files via upload
jexapatron 442751c
Delete static/images/jhub7682/test
jexapatron 2ae3dd8
Create MIDIdevices.js
jexapatron 84a6106
Update extensions.js
jexapatron f2843cc
Update extensions.js
jexapatron 07ec379
Update MIDIdevices.js
jexapatron 8e2c423
Holding multiple notes at once to an array
jexapatron 4041090
Tracking vel in an array too
jexapatron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,182 @@ | ||
| // Name: Midi Devices | ||
| // ID: mididevicesjhub7682 | ||
| // Description: An extention that exposes MIDI devices | ||
| // By: Jhub7682 | ||
| // License: MIT AND LGPL-3.0 | ||
|
|
||
| // Version V.1 | ||
|
|
||
| let lastNote = 0; | ||
| let lastVelocity = 0; | ||
| let pressedNotes = new Set(); | ||
| let pitchBendValue = 0; | ||
| let controlValues = new Map(); | ||
| let programNumber = 0; | ||
| let aftertouchValue = 0; | ||
| let polyAftertouchValues = new Map(); | ||
| let midiDeviceName = "None"; | ||
|
|
||
| navigator.requestMIDIAccess() | ||
| .then((midiAccess) => { | ||
| for (let input of midiAccess.inputs.values()) { | ||
| midiDeviceName = input.name; | ||
|
|
||
| input.onmidimessage = (msg) => { | ||
| const [status, data1, data2] = msg.data; | ||
| const command = status & 0xF0; | ||
|
|
||
| switch (command) { | ||
| case 0x90: // Note On | ||
| if (data2 > 0) { | ||
| lastNote = data1; | ||
| lastVelocity = data2; | ||
| pressedNotes.add(data1); | ||
| } else { | ||
| pressedNotes.delete(data1); // Note Off via velocity 0 | ||
| } | ||
| break; | ||
|
|
||
| case 0x80: // Note Off | ||
| pressedNotes.delete(data1); | ||
| break; | ||
|
|
||
| case 0xB0: // Control Change | ||
| controlValues.set(data1, data2); | ||
| break; | ||
|
|
||
| case 0xC0: // Program Change | ||
| programNumber = data1; | ||
| break; | ||
|
|
||
| case 0xD0: // Channel Pressure (Aftertouch) | ||
| aftertouchValue = data1; | ||
| break; | ||
|
|
||
| case 0xE0: // Pitch Bend | ||
| pitchBendValue = ((data2 << 7) | data1) - 8192; | ||
| break; | ||
|
|
||
| case 0xA0: // Polyphonic Aftertouch | ||
| polyAftertouchValues.set(data1, data2); | ||
| break; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| Scratch.extensions.register({ | ||
RedMan13 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| getInfo() { | ||
| return { | ||
| id: "MDjhub7682", | ||
jexapatron marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| name: "Midi Devices", | ||
| color1: "#ff6363ff", | ||
| blocks: [ | ||
| { | ||
| opcode: "midiNoteOn", | ||
| blockType: "reporter", | ||
| text: "last MIDI note" | ||
| }, | ||
| { | ||
| opcode: "midiVelocity", | ||
| blockType: "reporter", | ||
| text: "last velocity" | ||
| }, | ||
| { | ||
| opcode: "anyNotePressed", | ||
| blockType: "reporter", | ||
| text: "MIDI note pressed?" | ||
| }, | ||
| { | ||
| opcode: "getCCValue", | ||
| blockType: "reporter", | ||
| text: "CC [CCNUM] value", | ||
| arguments: { | ||
| CCNUM: { | ||
| type: "number", | ||
| defaultValue: 1 | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| opcode: "getPitchBend", | ||
| blockType: "reporter", | ||
| text: "pitch bend value" | ||
| }, | ||
| { | ||
| opcode: "getProgramNumber", | ||
| blockType: "reporter", | ||
| text: "program number" | ||
| }, | ||
| { | ||
| opcode: "getAftertouch", | ||
| blockType: "reporter", | ||
| text: "aftertouch value" | ||
| }, | ||
| { | ||
| opcode: "getPolyAftertouch", | ||
| blockType: "reporter", | ||
| text: "poly aftertouch for note [NOTE]", | ||
| arguments: { | ||
| NOTE: { | ||
| type: "number", | ||
| defaultValue: 60 | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| opcode: "getDeviceName", | ||
| blockType: "reporter", | ||
| text: "MIDI device name" | ||
| }, | ||
| { | ||
| opcode: "midiOctave", | ||
| blockType: "reporter", | ||
| text: "last MIDI octave" | ||
| } | ||
| ] | ||
| }; | ||
| }, | ||
|
|
||
| midiNoteOn() { | ||
| return lastNote; | ||
| }, | ||
|
|
||
| midiVelocity() { | ||
| return lastVelocity; | ||
| }, | ||
|
|
||
| anyNotePressed() { | ||
| return pressedNotes.size > 0; | ||
| }, | ||
|
|
||
| getCCValue(args) { | ||
| return controlValues.get(Number(args.CCNUM)) || 0; | ||
| }, | ||
|
|
||
| getPitchBend() { | ||
| return pitchBendValue; | ||
| }, | ||
|
|
||
| getProgramNumber() { | ||
| return programNumber; | ||
| }, | ||
|
|
||
| getAftertouch() { | ||
| return aftertouchValue; | ||
| }, | ||
|
|
||
| getPolyAftertouch(args) { | ||
| return polyAftertouchValues.get(Number(args.NOTE)) || 0; | ||
| }, | ||
|
|
||
| getDeviceName() { | ||
| return midiDeviceName; | ||
| }, | ||
|
|
||
| midiOctave() { | ||
| return Math.floor(lastNote / 12) - 1; | ||
| } | ||
| }); | ||
| }) | ||
| .catch((err) => { | ||
| console.error("MIDI access error:", err); | ||
| }); | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.