base for a cached sequence
This commit is contained in:
43
src/API.ts
43
src/API.ts
@ -3,7 +3,7 @@ import { scale } from "./Scales";
|
|||||||
import { tryEvaluate } from "./Evaluator";
|
import { tryEvaluate } from "./Evaluator";
|
||||||
import { MidiConnection } from "./IO/MidiConnection";
|
import { MidiConnection } from "./IO/MidiConnection";
|
||||||
import { DrunkWalk } from "./Utils/Drunk";
|
import { DrunkWalk } from "./Utils/Drunk";
|
||||||
import { Pitch, Chord, Rest, Event, Start, cachedStart } from "zifferjs";
|
import { Pitch, Chord, Rest, Event, Start, cachedStart, pattern } from "zifferjs";
|
||||||
import {
|
import {
|
||||||
superdough,
|
superdough,
|
||||||
samples,
|
samples,
|
||||||
@ -13,6 +13,13 @@ import {
|
|||||||
} from "superdough";
|
} from "superdough";
|
||||||
import { LRUCache } from 'lru-cache';
|
import { LRUCache } from 'lru-cache';
|
||||||
|
|
||||||
|
interface Pattern<T> {
|
||||||
|
pattern: any[];
|
||||||
|
options: {
|
||||||
|
[key: string]: T;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
const cache = new LRUCache({max: 1000, ttl: 1000 * 60 * 5});
|
const cache = new LRUCache({max: 1000, ttl: 1000 * 60 * 5});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -426,7 +433,7 @@ export class UserAPI {
|
|||||||
return btoa(JSON.stringify(pattern));
|
return btoa(JSON.stringify(pattern));
|
||||||
}
|
}
|
||||||
|
|
||||||
sequence(input: any[]) {
|
seq(...input: any[]) {
|
||||||
/**
|
/**
|
||||||
* Returns a value in a sequence stored using an LRU Cache.
|
* Returns a value in a sequence stored using an LRU Cache.
|
||||||
* The sequence is stored in the cache with an hash identifier
|
* The sequence is stored in the cache with an hash identifier
|
||||||
@ -440,25 +447,27 @@ export class UserAPI {
|
|||||||
* containing options for the sequence function.
|
* containing options for the sequence function.
|
||||||
* @returns A value in a sequence stored using an LRU Cache
|
* @returns A value in a sequence stored using an LRU Cache
|
||||||
*/
|
*/
|
||||||
const default_options: object = { index: 0 }
|
|
||||||
if (typeof input[input.length - 1] === "object") {
|
if (cache.has(this._sequence_key_generator(input))) {
|
||||||
const pattern_options: object = {
|
|
||||||
...input.pop(),
|
let sequence = cache.get(this._sequence_key_generator(input)) as Pattern<any>;
|
||||||
...default_options as object
|
sequence.options.index += 1;
|
||||||
};
|
cache.set(this._sequence_key_generator(input), sequence);
|
||||||
} else {
|
return sequence.pattern[
|
||||||
const pattern_options = default_options;
|
sequence.options.index % sequence.pattern.length
|
||||||
}
|
];
|
||||||
const sequence_key = this._sequence_key_generator(input);
|
|
||||||
if (cache.has(sequence_key)) {
|
|
||||||
return cache.get(sequence_key);
|
|
||||||
} else {
|
} else {
|
||||||
cache.set(sequence_key, {
|
let pattern_options = { index: 0 };
|
||||||
pattern: input,
|
if (typeof input[input.length - 1] === "object") {
|
||||||
|
pattern_options = { ...input.pop(), ...pattern_options as object };
|
||||||
|
}
|
||||||
|
cache.set(this._sequence_key_generator(input), {
|
||||||
|
pattern: input as any[],
|
||||||
options: pattern_options
|
options: pattern_options
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return cache.get(this._sequence_key_generator(input));
|
||||||
}
|
}
|
||||||
return cache.get(sequence_key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pick<T>(...array: T[]): T {
|
pick<T>(...array: T[]): T {
|
||||||
|
|||||||
Reference in New Issue
Block a user