(params: ClipParams)
| 31 | * Applies the pattern to notes, then optionally adds sizzle and accent dynamics. |
| 32 | */ |
| 33 | export const clip = (params: ClipParams): NoteObject[] => { |
| 34 | params = preprocessClipParams(params); |
| 35 | |
| 36 | const clipNotes: NoteObject[] = []; |
| 37 | let step = 0; |
| 38 | /** |
| 39 | * Recursively apply pattern to notes |
| 40 | * |
| 41 | * Pass in a pattern array such as ['x', '-', 'x', 'x'] with a length for each element |
| 42 | * The length is the HDR speed or tick length (obtained from the hdr object in this script) |
| 43 | * If the element of this array is also a (pattern) array, then divide the length by |
| 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; |
no test coverage detected