Write a chord interpreter to read a string of complex chord progressions like this one: "Amaj7sus2/Ab Gmaj7sus2 C69#11 Bm11 Ab7#5b9 Dbm11 Cmaj7b5/6 Abm11 F#m11". Then play the sequence of chords in an audio signaling processor:
//implement chord functions here
function note(n,base=440,keys=12){return baseMath.pow(2,n/keys);}
function sine(x){return Math.sin(TAUx);}
function pulse(x){return x%1<.5?-1:1;};
function dsp(t){
var out=0;
/*
do stuff here...
*/
return out;
}
return dsp;
Assistant
Certainly! Below is a JavaScript implementation that interprets the complex chord progression string, parses each chord, determines the intervals, calculates the frequencies, and plays the sequence of chords using the provided audio signal processing function dsp(t):
javascript
// Implement chord functions here
var TAU = Math.PI * 2;
// Mapping of note names to semitone numbers
var noteNumbers = {
'C': 0,
'C#': 1, 'Db':1,
'D': 2,
'D#':3, 'Eb':3,
'E':4,
'F':5,
'F#':6, 'Gb':6,
'G':7,
'G#':8, 'Ab':8,
'A':9,
'A#':10, 'Bb':10,
'B':11
};
// Function to parse a chord string into root note, modifiers, and bass note
function parseChordString(chordStr) {
// Split the chord by slash to separate chord and bass note
var parts = chordStr.split('/');
var chordPart = parts[0];
var bassNote = parts[1] || null;
// Extract the root note and modifiers from the chordPart
var match = chordPart.match(/^([A-G][#b]?)(.*)$/);
if (!match) {
throw new Error("Invalid chord: " + chordStr);
}
var rootNote = match[1];
var modifiers = match[2];
return {
rootNote: rootNote,
modifiers: modifiers,
bassNote: bassNote
};
}
// Function to parse chord modifiers into tokens
function parseModifiers(modifiers) {
// Patterns for different tokens
var patterns = [
{pattern: /^maj7/, token: 'maj7'},
{pattern: /^maj9/, token: 'maj9'},
{pattern: /^maj11/, token: 'maj11'},
{pattern: /^maj13/, token: 'maj13'},
{pattern: /^maj/, token: 'maj'},
{pattern: /^min13/, token: 'min13'},
{pattern: /^min11/, token: 'min11'},
{pattern: /^min9/, token: 'min9'},
{pattern: /^min7/, token: 'min7'},
{pattern: /^min/, token: 'min'},
{pattern: /^m/, token: 'min'},
{pattern: /^dim7/, token: 'dim7'},
{pattern: /^dim/, token: 'dim'},
{pattern: /^aug/, token: 'aug'},
{pattern: /^sus2/, token: 'sus2'},
{pattern: /^sus4/, token: 'sus4'},
{pattern: /^sus/, token: 'sus'},
{pattern: /^add\d+/, token: 'add'},
{pattern: /^6\/9/, token: '69'},
{pattern: /^7#9/, token: '7#9'},
{pattern: /^7b9/, token: '7b9'},
{pattern: /^7#5b9/, token: '7#5b9'},
{pattern: /^7#5/, token: '7#5'},
{pattern: /^7b5/, token: '7b5'},
{pattern: /^7/, token: '7'},
{pattern: /^5/, token: '5'},
{pattern: /^6/, token: '6'},
{pattern: /^9/, token: '9'},
{pattern: /^11/, token: '11'},
{pattern: /^13/, token: '13'},
{pattern: /^#\d+/, token: 'sharp'},
{pattern: /^b\d+/, token: 'flat'},
{pattern: /^#/, token: '#'},
{pattern: /^b/, token: 'b'},
{pattern: /^\//, token: '/'},
];
var tokens = [];
var str = modifiers;
while (str.length > 0) {
var matched = false;
for (var i = 0; i < patterns.length; i++) {
var res = patterns[i].pattern.exec(str);
if (res) {
if (patterns[i].token == 'number' || patterns[i].token == 'add' ||
patterns[i].token == 'sharp' || patterns[i].token == 'flat') {
tokens.push(res[0]);
} else {
tokens.push(patterns[i].token);
}
str = str.substr(res[0].length);
matched = true;
break;
}
}
if (!matched) {
// Unrecognized token, skip one character
str = str.substr(1);
}
}
return tokens;
}
// Function to get chord intervals based on modifiers
function getChordIntervals(modifiers) {
var tokens = parseModifiers(modifiers);
var intervals = [];
// Start with basic triad
var chordQuality = 'major'; // default
if (tokens.includes('maj')) {
chordQuality = 'major';
} else if (tokens.includes('min') || tokens.includes('m')) {
chordQuality = 'minor';
} else if (tokens.includes('dim')) {
chordQuality = 'diminished';
} else if (tokens.includes('aug')) {
chordQuality = 'augmented';
} else {
chordQuality = 'major'; // default
}
// Build the basic triad intervals
if (chordQuality == 'major') {
intervals.push(0, 4, 7);
} else if (chordQuality == 'minor') {
intervals.push(0, 3, 7);
} else if (chordQuality == 'diminished') {
intervals.push(0, 3, 6);
} else if (chordQuality == 'augmented') {
intervals.push(0, 4, 8);
}
// Handle extensions and added tones
if (tokens.includes('maj7')) {
intervals.push(11);
} else if (tokens.includes('7')) {
intervals.push(10);
}
if (tokens.includes('9')) {
if (!intervals.includes(10) && !intervals.includes(11)) {
intervals.push(10); // add dominant 7th
}
intervals.push(14);
}
if (tokens.includes('11')) {
if (!intervals.includes(10) && !intervals.includes(11)) {
intervals.push(10);
}
if (!intervals.includes(14)) {
intervals.push(14);
}
intervals.push(17);
}
if (tokens.includes('13')) {
if (!intervals.includes(10) && !intervals.includes(11)) {
intervals.push(10);
}
if (!intervals.includes(14)) {
intervals.push(14);
}
if (!intervals.includes(17)) {
intervals.push(17);
}
intervals.push(21);
}
if (tokens.includes('6')) {
intervals.push(9);
}
if (tokens.includes('sus2')) {
// Replace the third with major 2nd
var index = intervals.indexOf(chordQuality == 'major' ? 4 : 3);
if (index >= 0) intervals[index] = 2;
} else if (tokens.includes('sus4')) {
// Replace the third with perfect 4th
var index = intervals.indexOf(chordQuality == 'major' ? 4 : 3);
if (index >= 0) intervals[index] = 5;
}
// Handle alterations
for (var i = 0; i < tokens.length; i++) {
if (tokens[i] == '#5') {
// Replace the fifth with augmented fifth
var index = intervals.indexOf(7);
if (index >= 0) intervals[index] = 8;
} else if (tokens[i] == 'b5') {
// Replace the fifth with diminished fifth
var index = intervals.indexOf(7);
if (index >= 0) intervals[index] = 6;
} else if (tokens[i] == '#9') {
intervals.push(15); // sharp ninth
} else if (tokens[i] == 'b9') {
intervals.push(13); // flat ninth
} else if (tokens[i] == '#11') {
intervals.push(18); // sharp eleventh
} else if (tokens[i] == 'b13') {
intervals.push(20); // flat thirteenth
}
}
// Remove duplicates and sort intervals
intervals = intervals.filter((v, i, a) => a.indexOf(v) === i);
intervals.sort(function(a, b){return a - b});
return intervals;
}
// Note frequency calculation
function note(n, base=440, keys=12){return base * Math.pow(2, n / keys);}
// Sine wave function
function sine(x){return Math.sin(TAU * x);}
// Main DSP function
function dsp(t) {
var out = 0;
var chordDuration = 2; // seconds per chord
var chords = "Amaj7sus2/Ab Gmaj7sus2 C69#11 Bm11 Ab7#5b9 Dbm11 Cmaj7b5/6 Abm11 F#m11".split(" ");
var totalChords = chords.length;
var chordIndex = Math.floor(t / chordDuration) % totalChords;
var chordTime = t % chordDuration;
var chordStr = chords[chordIndex];
// Parse the chord
var chord = parseChordString(chordStr);
var rootNote = chord.rootNote;
var modifiers = chord.modifiers;
var bassNote = chord.bassNote || chord.rootNote;
var intervals = getChordIntervals(modifiers);
// Get the root note number
var rootNoteNumber = noteNumbers[rootNote];
// Get the semitone offset from A4 (note number 9)
var semitoneOffset = rootNoteNumber - 9;
// Calculate frequencies and sum the sine waves
var numNotes = intervals.length + 1; // number of notes in the chord including bass
for (var i = 0; i < intervals.length; i++) {
var n = semitoneOffset + intervals[i]; // total semitone offset from A4
var freq = note(n);
out += sine(freq * t);
}
// Add the bass note an octave lower
if (bassNote) {
var bassNoteNumber = noteNumbers[bassNote];
var bassSemitoneOffset = bassNoteNumber - 9 - 12; // bass an octave lower
var bassFreq = note(bassSemitoneOffset);
out += sine(bassFreq * t);
}
// Normalize output
out = out / numNotes;
return out;
}
return dsp;
Explanation:
Chord Parsing: Functions parseChordString, parseModifiers, and getChordIntervals work together to parse each chord string, extract the root note, modifiers, and intervals, and compute the semitone intervals for the chord.
Note Frequencies: The note function calculates the frequency of a note based on its semitone distance from the base frequency (A4 = 440 Hz).
Signal Generation: The dsp function calculates which chord should be played at time t, sums the sine waves of all the notes in the chord (including the bass note, which is played an octave lower), and normalizes the output to prevent clipping.
This code will generate an audio signal that plays the sequence of chords specified in the progression, interpreting complex chord names and correctly calculating the frequencies for each note in each chord.