Skip to content

Commit

Permalink
ascii to ly
Browse files Browse the repository at this point in the history
  • Loading branch information
JosePedroDias committed Nov 4, 2023
1 parent dc82870 commit 7d463ca
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 36 deletions.
11 changes: 11 additions & 0 deletions REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ pip install python-ly
ly musicxml p04.ly > p04.musicxml
https://python-ly.readthedocs.io/en/latest/command.html#commands
fails. requires python 2?



# lilypond from source

https://lilypond.org/doc/v2.25/Documentation/contributor/compiling
tar -xzf lilypond-2.25.9.tar.gz
cd lilypond-2.25.9
mkdir build/
cd build/
../autogen.sh --noconfigure
79 changes: 79 additions & 0 deletions ascii-pattern-parser.mjs
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;
}
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { waitForClick, fetchText, printAscii } from './utils.mjs';
import { parseAsciiPattern } from './ascii-pattern-parser.mjs';
import {
getAudioContext,
bpmToSecs,
parseAsciiPattern,
loadPatternSamples,
schedulePatternPlayback,
//playNoise,
Expand Down
3 changes: 2 additions & 1 deletion ly-pattern-parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,6 @@ export function lyPatternToAscii(parsedLyPattern) {
return `${pn2}|${arr2}|`;
}).join('\n');

return ascii;
return `TODO
${ascii}`;
}
10 changes: 5 additions & 5 deletions ly/p04.ly
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ pat_iii = \drummode {
}

pat_iv = \drummode {
<hh bd>16 bd16 hh8 <hh sn> hh16 bd <hh bd>8 <hh bd> <hh sn> hh16 bd
<hh bd>16 bd hh8 <hh sn> hh16 bd <hh bd>8 <hh bd> <hh sn> hh16 bd
}

pat_v = \drummode {
<hh bd>16 bd16 hh8 <hh sn> hh16 bd <hh bd>8 <hh bd> <hh sn>16 bd hh8
<hh bd>16 bd hh8 <hh sn> hh16 bd <hh bd>8 <hh bd> <hh sn>16 bd hh8
}

pat_vi = \drummode {
<hh bd>16 bd16 hh8 <hh sn> hh16 bd <hh bd>8 <hh bd> <hh sn>16 bd hh bd
<hh bd>16 bd hh8 <hh sn> hh16 bd <hh bd>8 <hh bd> <hh sn>16 bd hh bd
}

pat_vii = \drummode {
%1 %2 %3 %4
<hh bd>16 bd16 hh bd <hh sn>8 hh16 bd <hh bd>8 <hh bd> <hh sn>16 bd hh bd
<hh bd>16 bd hh bd <hh sn>8 hh16 bd <hh bd>8 <hh bd> <hh sn>16 bd hh bd
}

pat_viii = \drummode {
%1 %2 %3 %4
<hh bd>16 bd16 hh bd <hh sn>8 hh16 bd <hh bd> sn <hh bd>8 <hh sn>16 bd hh bd
<hh bd>16 bd hh bd <hh sn>8 hh16 bd <hh bd> sn <hh bd>8 <hh sn>16 bd hh bd
}

dr = {
Expand Down
Binary file modified ly/p04.pdf
Binary file not shown.
29 changes: 19 additions & 10 deletions parse.mjs
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);
19 changes: 0 additions & 19 deletions sequencer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,6 @@ export async function fetchSample(audioContext, filepath) {
return audioBuffer;
}

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 };
})
};
}

export async function loadPatternSamples(pattern, audioCtx, samples = {}) {
for (const { url, name } of pattern.tracks) {
if (samples[name]) continue;
Expand Down

0 comments on commit 7d463ca

Please sign in to comment.