-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc82870
commit 7d463ca
Showing
8 changed files
with
117 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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 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,79 @@ | ||
import { sampleNameToUrl } from "./sample-mapping.mjs"; | ||
|
||
export function parseAsciiPattern(patternString) { | ||
const lines = patternString.split('\n'); | ||
if (lines[lines.length-1].trim() === '') lines.pop(); | ||
const [a, b] = lines.shift().split(' '); | ||
return { | ||
header: { | ||
a, b | ||
}, | ||
tracks: lines.map((line) => { | ||
let pipeIdx = line.indexOf('|'); | ||
let pipeIdx2 = line.indexOf('|', pipeIdx + 1); | ||
const name = line.substring(0, pipeIdx).trim(); | ||
const url = sampleNameToUrl(name); | ||
const times = line.substring(pipeIdx + 1, pipeIdx2).split('').map((ch) => ch !== ' '); | ||
return { name, url, times }; | ||
}) | ||
}; | ||
} | ||
|
||
const adaptDur = (dur) => { | ||
switch (dur) { | ||
case 2: return 16; | ||
case 4: return 8; | ||
case 8: return 4; | ||
case 16: return 2; | ||
case 32: return 1; | ||
//default: throw dur; | ||
default: return 0; | ||
} | ||
}; | ||
|
||
export function asciiPatternToLy(pattern) { | ||
const pitchNames = pattern.tracks.map((track) => track.name); | ||
const times = pattern.tracks.map((track) => track.times); | ||
const lenT = times[0].length; | ||
const lenP = pitchNames.length; | ||
|
||
let lastIT = 0; | ||
const sequenceOfPairs = []; | ||
for (let iT = 0; iT < lenT; ++iT) { | ||
const dIT = iT - lastIT; | ||
const tickPitches = []; | ||
for (let iP = 0; iP < lenP; ++iP) { | ||
const pn = pitchNames[iP]; | ||
const isOn = times[iP][iT]; | ||
if (isOn) { | ||
tickPitches.push(pn); | ||
lastIT = iT; | ||
} | ||
} | ||
if (tickPitches.length > 0) { | ||
sequenceOfPairs.push([tickPitches, dIT, iT]); | ||
} | ||
} | ||
|
||
const sequenceOfPairs2 = sequenceOfPairs.reduceRight( | ||
([sop, dIT2], [pitches, dIT, iT], i) => { | ||
sop[i] = [pitches, adaptDur(dIT2 === undefined ? lenT - iT : dIT2)]; | ||
return [sop, dIT]; | ||
}, | ||
[structuredClone(sequenceOfPairs), undefined] | ||
)[0]; | ||
|
||
//console.log(sequenceOfPairs); | ||
//console.log(sequenceOfPairs2); | ||
|
||
let lastD = undefined; | ||
const ly = sequenceOfPairs2 | ||
.map(([pitches, dur]) => { | ||
const p = pitches.length > 1 ? `<${pitches.join(' ')}>` : pitches[0]; | ||
const d = lastD === dur ? '' : dur; | ||
lastD = dur; | ||
return `${p}${d}`; | ||
}).join(' '); | ||
|
||
return ly; | ||
} |
This file contains 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 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 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
Binary file not shown.
This file contains 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 |
---|---|---|
@@ -1,20 +1,29 @@ | ||
import { getPatternsFromLy, parseLyPattern, lyPatternToAscii } from './ly-pattern-parser.mjs'; | ||
import { parseAsciiPattern, asciiPatternToLy } from './ascii-pattern-parser.mjs'; | ||
import { fetchText, printAscii } from './utils.mjs'; | ||
|
||
const lyFileContents = await fetchText('./ly/p04.ly'); | ||
|
||
const patterns = getPatternsFromLy(lyFileContents); | ||
console.log('patterns', patterns); | ||
const lyPatterns = getPatternsFromLy(lyFileContents); | ||
console.log('lyPatterns', lyPatterns); | ||
|
||
const pattern = patterns[8].contents; | ||
console.log('pattern', pattern); | ||
printAscii('PATTERN:', ['bold']); | ||
printAscii(pattern); | ||
const lyPattern = lyPatterns[8].contents; | ||
console.log('lyPattern', lyPattern); | ||
printAscii('LY PATTERN:', ['bold']); | ||
printAscii(lyPattern); | ||
|
||
const parsedPattern = await parseLyPattern(pattern); | ||
console.log('parsedPattern', parsedPattern); | ||
const parsedLyPattern = await parseLyPattern(lyPattern); | ||
console.log('parsedLyPattern', parsedLyPattern); | ||
|
||
printAscii('ASCII:', ['bold']); | ||
const ascii = lyPatternToAscii(parsedPattern); | ||
printAscii('ASCII PATTERN:', ['bold']); | ||
const ascii = lyPatternToAscii(parsedLyPattern); | ||
console.log(ascii); | ||
printAscii(ascii); | ||
|
||
/// | ||
|
||
printAscii('LY PATTERN:', ['bold']); | ||
const parsedAsciiPattern = parseAsciiPattern(ascii); | ||
const ly = asciiPatternToLy(parsedAsciiPattern); | ||
console.log(ly); | ||
printAscii(ly); |
This file contains 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