-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlsystem.js
173 lines (162 loc) · 7.24 KB
/
lsystem.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// --------------------------------------------------------------------------
// -- lsystem.js
// -- initial author: Renick Bell ([email protected])
// -- initial creation date: Wed Jun 28 10:08:48 AM CST 2023
// -- contributors: Yiler Huang ([email protected]); Steve Wang ([email protected])
// -- license: GPL 3.0
// --------------------------------------------------------------------------
/**
* Generates an L-system for a number of generations based on a starting inputString and a set of rules.
* @param {string} inputString - The axiom of the L-system.
* @param {object} rules - The rules of the L-system.
* @param {number} generations - The number of generations the L-system has.
* @example
* console.log(generativeParseString('a', {'a': 'ba', 'b': 'aaa'}, 5))
*/
function generativeParseString (inputString,rules,generations) {
return Array.from({length: generations}, () => {
inputString=parseString(inputString,rules)
return inputString
})[generations - 1]
}
//Chatgpt helped with removing for loop
//Gnerated the QuantizedMap for the lsystem chord progression:
/**
* Generates a random L-system in form of a QuantizedMap.
* @example
* console.log(generateRandomLsystemChordProgression ()) //QuantizedMap {
keyspan: 6,
keys: [ 17, 22, 16, 17, 15, 20 ],
values: [
{ data: [Array], bool: true },
{ data: [Array], bool: false },
{ data: [Array], bool: true },
{ data: [Array], bool: true },
{ data: [Array], bool: false },
{ data: [Array], bool: false }
]
}
*/
function generateRandomLsystemChordProgression (){
let velocityData = countLetterChanges(generateRandomLsystemString(20))
let boolsData = generateLsystemBoolsData()
let octaveData = countLetterChanges(generateRandomLsystemString(20))
let notesData = generateLsystemNoteData()
let noteDurationData = generateLsystemNoteData()
boolsData = A.resizeArray(notesData.length, boolsData)
noteDurationData = A.resizeArray(notesData.length, noteDurationData)
octaveData = A.resizeArray(notesData.length, octaveData).map(x => {return x * 2})
velocityData = A.resizeArray(notesData.length, velocityData).map(x => {return x * (x * 10)})
return new QuantizedMap(notesData.length, noteDurationData, notesData.map((x, i) => {return {data: [{note: x, octave: octaveData[i], velocity: velocityData[i]}], bool: boolsData[i]}}))
}
//Generates lsystem in string for for for the lsystem chord progression:
/**
* Generates an L-system of a specific length based on the pickedAlphabets.
* @param {number} [length=30] - The minimum length of the L-system.
* @param {array} pickedAlphabets - An array filled with letters.
* @example
* console.log(generateRandomLsystemString()) //'qyppppyqyyqiqyppppyqyyqiqyppppyqyyqijqpiyqyppppyqyyqiqyppppyqyyqiqyppppyqyyqijyqqyppppyqyyqiqyppppyqyyqiqyppppyqyyqijypiip'
* console.log(generateRandomLsystemString(2)) //'jjuujuujuugjjjgjjjjuugjjjgjjjjuujuugjjjgjjjjuugjjjgujuujuujuugjjjgjjjjuugjjjgjjjjuujuugjjjgjjjjuugjjjg'
* console.log(generateRandomLsystemString(2, ['a'])) //'aaaaaaaaaaaaaaaa'
* console.log(generateRandomLsystemString(2, ['a', 'b'])) //'bbbbbbaababbaababbbaababbbbaababbaababbbaababbbbbaababbaababbbaababbbbbbbaababbaababbbaababbbbaababbaababbbaababbbbbaababbaababbbaababb'
*/
function generateRandomLsystemString (length = 30, pickedAlphabets){
let configuration = generateRandomLSystemConfiguration(pickedAlphabets)
console.log('configuration', configuration)
let finalLsystem = ''
let failedAttemps = 0
while (finalLsystem.length < length){
failedAttemps += 1
finalLsystem = generativeParseString(configuration.startingLetters, configuration.conditions, 3 ** failedAttemps)
}
return finalLsystem
}
//Generates bools data for the lsystem chord progression:
/**
* Generatest L-system bools data. Returns an array filled with true or false booleans.
* @example console.log(generateLsystemBoolsData()) //[
false, true, false, true, false,
true, false, true, false, true,
false, true, false, true, false,
true, true, false, true, false,
true, false, true, false, true,
false, true, false, true, false,
true
]
*/
function generateLsystemBoolsData (){
return generateRandomLsystemString(30, ['a', 'b']).split('').map(x => {
if (x === 'a'){
return true
}
else {
return false
}
})
}
//generates note data for the lsystem chord progression:
function generateLsystemNoteData (){
let noteData = countLetterChanges(generateRandomLsystemString(20))
let addedNotesData = []
let tenLengthArray = Array.from({length: 10})
Array.from({length: Math.floor(noteData.length / 10)}).forEach((x, i) =>{
let total = 0
tenLengthArray.forEach((x, n) =>{
total += noteData[(i * 10) + n]
})
addedNotesData.push(total)
})
return addedNotesData
}
//generates alphabets for the lsystem chord progression:
function generateLsystemAlphabets (){
let alphabets = Array.from({ length: 26 }, (_, i) => String.fromCharCode(97 + i));
return Array.from({length: randomRange(2, 10)}, () => {
return A.pick(alphabets)
})
}
//generates conditions for the lsystem chord progression:
function generateCondition (chosenAlphabets, min, max){
let condition = ''
chosenAlphabets.forEach((x, i) =>{
condition += repeatString(randomRange(min, max), x)
})
return shuffleString(condition)
}
//Generate random configurations for the lsystem chord progression:
function generateRandomLSystemConfiguration (pickedAlphabets){
let chosenAlphabets;
if (pickedAlphabets === undefined){
chosenAlphabets = generateLsystemAlphabets()
}
else{
chosenAlphabets = pickedAlphabets
}
let startingLetters = chosenAlphabets.map(x => {
return repeatString(randomRange(1, 3), x)
}).join('')
let conditions = {}
conditions[chosenAlphabets[0]] = generateCondition(chosenAlphabets, 0, 5) + chosenAlphabets[0]
Array.from({length: randomRange(1, 5)}, () => {
let rule = generateCondition(chosenAlphabets, 1, 3)
conditions[rule] = generateCondition(chosenAlphabets, 0, 5)
})
startingLetters = shuffleString(startingLetters)
return {conditions, startingLetters}
}
function convertLsystemStringToNumbersViaAssignedLetters (chosenAlphabets, lsystem, availableNumbers){
return lsystem.split('').map((x, i) =>{
return availableNumbers[chosenAlphabets.indexOf(x)]
})
}
function generateLsystemByAssigningNumberToLetter (mode, octaves,length) {
let alphabets = Array.from({ length: 26 }, (_, i) => String.fromCharCode(97 + i));
let modeAlphabets = A.safeSplice(alphabets, alphabets.length, mode.length)
let modeData = convertLsystemStringToNumbersViaAssignedLetters(modeAlphabets, generateRandomLsystemString(length, modeAlphabets), mode)
let octavesAlphabets = A.safeSplice(alphabets, alphabets.length, octaves.length)
let octavesData = convertLsystemStringToNumbersViaAssignedLetters(octavesAlphabets, generateRandomLsystemString(length, octavesAlphabets), octaves)
return modeData.map((x, i) =>{
return {note: x, octave: octavesData[i]}
})
}
// hi = generateLsystemByAssigningNumberToLetter([0,2,4,7,9,11], [4 ,5, 6], 10)