experimental stuff

This commit is contained in:
2023-08-05 23:02:12 +02:00
parent 9dc6e8750d
commit 095ba9dd4e
4 changed files with 98 additions and 15 deletions

View File

@ -4,6 +4,9 @@ import { tryEvaluate } from "./Evaluator";
import { MidiConnection } from "./IO/MidiConnection";
// @ts-ignore
import { webaudioOutput, samples } from '@strudel.cycles/webaudio';
import { MiniLanguage } from "./Walker";
import * as astring from 'astring';
const sound = (value: any) => ({
value, context: {},
@ -139,7 +142,6 @@ export class UserAPI {
// would be 1.0, which is the current rate (very speedy).
}
script(...args: number[]): void {
/**
* Evaluates 1-n local script(s)
@ -180,7 +182,6 @@ export class UserAPI {
}
cps = this.copyscript
// =============================================================
// MIDI related functions
// =============================================================
@ -208,7 +209,7 @@ export class UserAPI {
}
}
public note(note: number, channel: number, velocity: number, duration: number): void {
public note(note: number, channel: number = 0, velocity: number = 100, duration: number = 0.5): void {
/**
* Sends a MIDI note to the current MIDI output.
* TODO: Fix note duration
@ -313,15 +314,7 @@ export class UserAPI {
// Return current iterator value
return this.iterators[name].value;
}
it = this.iterator
get _() {
return this.iterator('_');
}
get A() {
return this.iterator('A');
}
$ = this.iterator
// =============================================================
// Drunk mechanism
@ -466,7 +459,7 @@ export class UserAPI {
* @param array - The array of values to pick from
*/
return array[this.app.clock.time_position.pulse % array.length]
}
}
// =============================================================
// Randomness functions
@ -953,4 +946,10 @@ export class UserAPI {
sound = async (values: object) => {
webaudioOutput(sound(values), 0.00)
}
ast(code: string) {
const ast = MiniLanguage.parse(code, { ecmaVersion: 2020 });
console.log(astring.generate(ast))
return
}
}

66
src/Walker.ts Normal file
View File

@ -0,0 +1,66 @@
import * as acorn from 'acorn';
import * as walk from 'acorn-walk';
import * as astring from 'astring';
// Create a custom Acorn plugin
function myPlugin(Parser: typeof acorn.Parser): any {
return class extends Parser {
parseLiteral(...args: Parameters<acorn.Parser['parseLiteral']>) {
const node = super.parseLiteral(...args);
// Check if the literal is a string and if it's wrapped in single quotes
if (typeof node.value === 'string' && this.input.slice(node.start, node.end).startsWith("'")) {
const transformed = this.transformMyString(node.value);
// Replace the Literal node with an ArrayExpression node
return {
type: 'SpreadElement',
argument: {
type: 'ArrayExpression',
elements: transformed.map(value => ({
type: 'Literal',
value,
raw: value.toString()
}))
},
start: node.start,
end: node.end
};
}
return node;
}
transformMyString(string: string): number[] {
const matches = string.match(/\d+!*\d*/g);
const values: number[] = [];
matches?.forEach(match => {
const parts = match.split('!');
const number = parseInt(parts[0]);
const times = parts[1] ? parseInt(parts[1]) : 1;
for (let i = 0; i < times; i++) {
values.push(number);
}
});
return values;
}
};
}
export const MiniLanguage = acorn.Parser.extend(myPlugin);
// Sample code
// const code = `
// const a = '3!4 5'; // This should become const a = [...[3,3,3,3,5]];
// `;
// Parse the code
// const ast = MiniLanguage.parse(code, { ecmaVersion: 2020 });
// Convert the transformed AST back into source code
// const newCode = astring.generate(ast);
// console.log(newCode);