Add files via upload

Add chord recognition by tonal.js
This commit is contained in:
Wiwi Kuan
2023-03-30 19:11:36 +08:00
committed by GitHub
parent 7d3cde5a53
commit 3a35d1cb56
3 changed files with 94 additions and 85 deletions

View File

@@ -5,6 +5,8 @@
<title>好和弦的鋼琴鍵盤顯示器</title>
<link rel="stylesheet" href="style.css">
<script src="p5.min.js"></script>
<script src="tonal.min.js"></script>
<script src="webmidi.js"></script>
<script src="globals.js"></script>
<script src="piano-visualizer.js"></script>
@@ -36,7 +38,7 @@
</span>
覺得好用嗎?到 <a href="https://nicechord.com">NiceChord.com</a> 逛逛支持我!
覺得好用嗎?到 <a href="https://nicechord.com">NiceChord.com</a> 逛逛支持我!原始碼在 <a href="https://github.com/wiwikuan/pianometer">GitHub</a>
</div>

View File

@@ -1,4 +1,3 @@
function setup() {
createCanvas(1098, 118).parent('piano-visualizer');
colorMode(HSB, 360, 100, 100, 100);
@@ -125,7 +124,9 @@ function drawTexts() {
// SHORT-TERM DENSITY
let shortTermDensity = shortTermTotal.reduce((accumulator, currentValue) => accumulator + currentValue, 0); // Sum the array.
if (shortTermDensity > notesSMax) { notesSMax = shortTermDensity };
if (shortTermDensity > notesSMax) {
notesSMax = shortTermDensity
};
let shortTermDensityText = "NPS(MAX)" + "\n" + shortTermDensity + " (" + notesSMax + ")";
text(shortTermDensityText, 190, 79);
@@ -136,7 +137,9 @@ function drawTexts() {
text(legatoText, 276, 79);
// NOW PLAYING
let nowPlayingText = "KEYS" + "\n" + truncateString(getPressedKeys(), 47);
let chordSymbol = Tonal.Chord.detect(getPressedKeys(false), { assumePerfectFifth: true })
let chordSymbolWithoutM = chordSymbol.map((str) => str.replace(/M($|(?=\/))/g, "")); // get rid of the M's
let nowPlayingText = truncateString(getPressedKeys(true), 47) + "\n" + truncateString(chordSymbolWithoutM.join(' '), 47);
text(nowPlayingText, 440, 79);
}
@@ -176,7 +179,7 @@ function convertNumberToBars(number) {
return combinedString;
}
function getPressedKeys() {
function getPressedKeys(returnString = true) {
let pressedOrPedaled = [];
for (let i = 0; i < isKeyOn.length; i++) {
@@ -184,12 +187,10 @@ function getPressedKeys() {
}
let noteNames = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']; // default if sharp
if ([0, 1, 3, 5, 8, 10].includes(pressedOrPedaled.indexOf(1) % 12)) {
// flat
noteNames = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
// noteNames = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
}
const pressedKeys = [];
@@ -201,8 +202,12 @@ function getPressedKeys() {
pressedKeys.push(`${noteName}${octave}`);
}
}
if (returnString == true){
return pressedKeys.join(' ');
} else {
return pressedKeys;
}
}
function truncateString(str, maxLength = 40) {

2
tonal.min.js vendored Normal file

File diff suppressed because one or more lines are too long