| 44 | * the length of the inner array and then call the recursive function on that inner array |
| 45 | */ |
| 46 | const recursivelyApplyPatternToNotes = ( |
| 47 | patternArr: PatternElement[], |
| 48 | length: number, |
| 49 | parentNoteLength: number | boolean |
| 50 | ) => { |
| 51 | let totalLength = 0; |
| 52 | patternArr.forEach((char, idx) => { |
| 53 | if (typeof char === 'string') { |
| 54 | let note: string[] | null = null; |
| 55 | |
| 56 | if (char === '-') { |
| 57 | // note = null; |
| 58 | } else if ( |
| 59 | char === 'R' && |
| 60 | randomInt() && // Use 1/2 probability for R to pick from param.notes |
| 61 | params.randomNotes && |
| 62 | params.randomNotes.length > 0 |
| 63 | ) { |
| 64 | note = params.randomNotes[ |
| 65 | randomInt(params.randomNotes.length - 1) |
| 66 | ] as string[]; |
| 67 | } else if (params.notes) { |
| 68 | note = params.notes[step] as string[]; |
| 69 | } |
| 70 | |
| 71 | if (char === 'x' || char === 'R') { |
| 72 | step++; |
| 73 | } |
| 74 | |
| 75 | // Push only note on OR off messages to the clip notes array |
| 76 | if (char === 'x' || char === '-' || char === 'R') { |
| 77 | clipNotes.push({ |
| 78 | note, |
| 79 | length, |
| 80 | level: |
| 81 | char === 'R' && !params.randomNotes |
| 82 | ? (params.accentLow as number) |
| 83 | : (params.amp as number), |
| 84 | }); |
| 85 | totalLength += length; |
| 86 | } |
| 87 | |
| 88 | // In case of an underscore, simply extend the previous note's length |
| 89 | if (char === '_' && clipNotes.length) { |
| 90 | clipNotes[clipNotes.length - 1].length += length; |
| 91 | totalLength += length; |
| 92 | } |
| 93 | |
| 94 | // if there were triplets in this iteration then ajust length of the last note |
| 95 | if ( |
| 96 | parentNoteLength && |
| 97 | totalLength !== parentNoteLength && |
| 98 | idx === patternArr.length - 1 |
| 99 | ) { |
| 100 | const diff: number = Math.abs( |
| 101 | (parentNoteLength as number) - totalLength |
| 102 | ); |
| 103 | const lastClipNote = clipNotes[clipNotes.length - 1]; |