Skip to content

Commit

Permalink
add midi chord detection
Browse files Browse the repository at this point in the history
  • Loading branch information
deleolajide committed Dec 9, 2024
1 parent 3dd041c commit bf54a06
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
<script src='./js/0260_JCLive_sf2_file.js'></script>
<script src="./assets/pedalboard.js" type="module"></script>
<script src="./js/basic-pitch.js"></script>
<script src="./js/tonal.min.js"></script>
<script src="./js/stream-deck.js"></script>
<script src="./js/chordsheet.js"></script>
<script src="./js/stophe.min.js"></script>
Expand Down
33 changes: 33 additions & 0 deletions docs/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const WHAMMY = 2;
const LOGO = 12;
const CONTROL = 100;

var midiNotes = new Map();
var streamDeckPointer = 0;
var streamDeck = null;
var bassVol = 95;
Expand Down Expand Up @@ -1908,6 +1909,7 @@ function toggleStrumUpDown() {

function handleNoteOff(note, device, velocity, channel) {
console.debug("handleNoteOff", inputDeviceType, note);
midiNotes.delete(note.number);

if (inputDeviceType == "chorda" && device != "INSTRUMENT1") {
//console.debug("chorda handleNoteOff", note.number, device, velocity, channel);
Expand Down Expand Up @@ -2024,6 +2026,37 @@ function handleNoteOff(note, device, velocity, channel) {

function handleNoteOn(note, device, velocity, channel) {
console.debug("handleNoteOn", inputDeviceType, note, device, velocity, channel);
midiNotes.set(note.number, {inputDeviceType, note, device, velocity, channel});

if (midiNotes.size == 4 || midiNotes.size == 3) {
const chord = [];
for (let [keyNote, value] of midiNotes) chord.push(value.note.number);
chord.sort();

const sorted = [];
for (no of chord) sorted.push(Tonal.Midi.midiToNoteName(no));
const chords = Tonal.Chord.detect(sorted);

if (chords.length > 0) {
const chordName = (midiNotes.size == 4 && chords.length > 1) ? chords[1] : chords[0];
const detectedChord = Tonal.Chord.get(chordName);
console.debug("detected chord", detectedChord, midiNotes.size, chord);

const arrChordType = (detectedChord.type == "suspended fourth" ? "sus" : (detectedChord.type == "minor" ? "min" : (detectedChord.type == "major" ? "maj" : "maj7")));
const arrChord = (midiNotes.size == 4 ? chord[1] : chord[0]) % 12;
const key = "key" + arrChord + "_" + arrChordType + "_" + SECTION_IDS[sectionChange];
const bassKey = "key" + (chord[0] % 12) + "_" + arrChordType + "_" + SECTION_IDS[sectionChange];

if (arranger == "webaudio" && realInstrument && styleStarted) {
const bassChecked = document.getElementById("arr-instrument-17")?.checked;
const chordChecked = document.getElementById("arr-instrument-18")?.checked;
console.debug("playing chord", bassChecked, bassKey, chordChecked, key);

if (bassLoop && bassChecked) bassLoop.update(bassKey, false);
if (chordLoop && chordChecked) chordLoop.update(key, false);
}
}
}

if (inputDeviceType == "chorda" && device != "INSTRUMENT1") {
//console.debug("chorda handleNoteOn", note.number, device, velocity);
Expand Down
2 changes: 2 additions & 0 deletions docs/js/tonal.min.js

Large diffs are not rendered by default.

0 comments on commit bf54a06

Please sign in to comment.