-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
156 lines (133 loc) · 5.22 KB
/
cli.js
File metadata and controls
156 lines (133 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
import readline from 'readline';
import {
identifyChord,
notesToChordSymbol,
detectKey,
getExtensions,
ChordSymbols,
PitchNames
} from './src/index.js';
const { listPitchNameToNum, midiToPitchName } = PitchNames;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Store chord progression for key detection
let progression = [];
function printHelp() {
console.log(`
╔══════════════════════════════════════════════════════════════╗
║ JAZZIFY CLI ║
╠══════════════════════════════════════════════════════════════╣
║ Commands: ║
║ <notes> Enter notes separated by spaces ║
║ e.g., "C4 E4 G4" or "C E G" ║
║ ║
║ key Analyze key of stored progression ║
║ clear Clear stored progression ║
║ show Show stored progression ║
║ help Show this help message ║
║ exit Exit the CLI ║
║ ║
║ Note format: ║
║ - Pitch name: C, D, E, F, G, A, B ║
║ - Accidentals: # (sharp) or b (flat) ║
║ - Octave: 0-8 (optional, default octave 4) ║
║ Examples: C4, Eb4, F#3, Bb, G ║
╚══════════════════════════════════════════════════════════════╝
`);
}
function parseNotes(input) {
const noteStrings = input.trim().split(/\s+/).filter(s => s.length > 0);
try {
return listPitchNameToNum(noteStrings, 60, true);
} catch (e) {
return null;
}
}
function formatChordResult(notes) {
const symbols = notesToChordSymbol(notes, true, true);
const names = identifyChord(notes);
const noteNames = notes.map(n => midiToPitchName(n)).join(' ');
console.log(`\n Notes: ${noteNames} (MIDI: ${notes.join(', ')})`);
if (names.length === 0) {
console.log(' Chord: Unknown');
} else if (names.length === 1) {
console.log(` Chord: ${names[0]}`);
} else {
console.log(` Chord: ${names[0]}`);
console.log(` Other possibilities: ${names.slice(1).join(', ')}`);
}
if (symbols.length > 0 && symbols[0].bass !== null) {
const bassName = midiToPitchName(symbols[0].bass % 12 + 60);
console.log(` Bass: ${bassName}`);
}
console.log();
}
function analyzeKey() {
if (progression.length < 2) {
console.log('\n Need at least 2 chords for key analysis. Add more chords!\n');
return;
}
const analysis = detectKey(progression);
console.log('\n ═══ Key Analysis ═══');
for (let i = 0; i < progression.length; i++) {
const noteNames = progression[i].map(n => midiToPitchName(n)).join(' ');
const chordName = identifyChord(progression[i])[0] || 'Unknown';
console.log(` ${i + 1}. ${chordName.padEnd(20)} │ ${analysis.functions[i].padEnd(12)} │ ${analysis.keys[i]}`);
}
console.log();
}
function showProgression() {
if (progression.length === 0) {
console.log('\n No chords stored yet.\n');
return;
}
console.log('\n ═══ Stored Progression ═══');
progression.forEach((chord, i) => {
const noteNames = chord.map(n => midiToPitchName(n)).join(' ');
const chordName = identifyChord(chord)[0] || 'Unknown';
console.log(` ${i + 1}. ${chordName} (${noteNames})`);
});
console.log();
}
function prompt() {
const chordCount = progression.length;
const promptText = chordCount > 0
? `jazzify [${chordCount} chords] > `
: 'jazzify > ';
rl.question(promptText, (input) => {
const trimmed = input.trim().toLowerCase();
if (trimmed === 'exit' || trimmed === 'quit' || trimmed === 'q') {
console.log('Goodbye! 🎵');
rl.close();
return;
}
if (trimmed === 'help' || trimmed === '?') {
printHelp();
} else if (trimmed === 'key') {
analyzeKey();
} else if (trimmed === 'clear') {
progression = [];
console.log('\n Progression cleared.\n');
} else if (trimmed === 'show') {
showProgression();
} else if (trimmed === '') {
// Do nothing for empty input
} else {
const notes = parseNotes(input);
if (notes && notes.length > 0) {
formatChordResult(notes);
progression.push(notes);
} else {
console.log('\n Invalid input. Type "help" for usage.\n');
}
}
prompt();
});
}
// Main
console.log('\n🎹 Welcome to Jazzify CLI - Jazz Theory Analysis Tool');
console.log(' Type "help" for commands, or enter notes like "C4 E4 G4"\n');
prompt();