Fix nasty auto-save bug

This commit is contained in:
2023-11-14 12:51:26 +01:00
parent c46f553f21
commit 3b6f02b525
7 changed files with 130 additions and 165 deletions

View File

@ -1,6 +1,6 @@
import { type UserAPI } from "../API";
import { safeScale, stepsToScale } from "zifferjs";
export { };
export {};
declare global {
interface Array<T> {
@ -8,8 +8,8 @@ declare global {
sub(amount: number): number[];
mult(amount: number): number[];
div(amount: number): number[];
mouseX(): T[],
mouseY(): T[],
mouseX(): T[];
mouseY(): T[];
palindrome(): T[];
random(index: number): T;
rand(index: number): T;
@ -37,7 +37,6 @@ declare global {
}
export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.mouseX = function <T>(this: T[]): T {
/**
* @returns Value from list based on mouse X position
@ -59,14 +58,14 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[zoneIndex];
};
Array.prototype.square = function(): number[] {
Array.prototype.square = function (): number[] {
/**
* @returns New array with squared values.
*/
return this.map((x: number) => x * x);
};
Array.prototype.sometimes = function(func: Function): number[] {
Array.prototype.sometimes = function (func: Function): number[] {
if (api.randomGen() < 0.5) {
return func(this);
} else {
@ -74,11 +73,11 @@ export const makeArrayExtensions = (api: UserAPI) => {
}
};
Array.prototype.apply = function(func: Function): number[] {
Array.prototype.apply = function (func: Function): number[] {
return func(this);
};
Array.prototype.sqrt = function(): number[] {
Array.prototype.sqrt = function (): number[] {
/**
* @returns New array with square roots of values. Throws if any element is negative.
*/
@ -87,7 +86,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => Math.sqrt(x));
};
Array.prototype.add = function(amount: number): number[] {
Array.prototype.add = function (amount: number): number[] {
/**
* @param amount - The value to add to each element in the array.
* @returns New array with added values.
@ -95,7 +94,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x + amount);
};
Array.prototype.sub = function(amount: number): number[] {
Array.prototype.sub = function (amount: number): number[] {
/**
* @param amount - The value to subtract from each element in the array.
* @returns New array with subtracted values.
@ -103,7 +102,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x - amount);
};
Array.prototype.mult = function(amount: number): number[] {
Array.prototype.mult = function (amount: number): number[] {
/**
* @param amount - The value to multiply with each element in the array.
* @returns New array with multiplied values.
@ -111,7 +110,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x * amount);
};
Array.prototype.div = function(amount: number): number[] {
Array.prototype.div = function (amount: number): number[] {
/**
* @param amount - The value to divide each element in the array by.
* @returns New array with divided values. Throws if division by zero.
@ -120,7 +119,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this.map((x: number) => x / amount);
};
Array.prototype.pick = function() {
Array.prototype.pick = function () {
/**
* Returns a random element from an array.
*
@ -129,7 +128,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[Math.floor(api.randomGen() * this.length)];
};
Array.prototype.gen = function(min: number, max: number, times: number) {
Array.prototype.gen = function (min: number, max: number, times: number) {
/**
* Returns an array of random numbers.
* @param min - The minimum value of the random numbers
@ -146,7 +145,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
);
};
Array.prototype.bar = function(value: number = 1) {
Array.prototype.bar = function (value: number = 1) {
/**
* Returns an element from an array based on the current bar.
*
@ -161,7 +160,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
}
};
Array.prototype.beat = function(divisor: number = 1) {
Array.prototype.beat = function (divisor: number = 1) {
const chunk_size = divisor; // Get the first argument (chunk size)
const timepos = api.app.clock.pulses_since_origin;
const slice_count = Math.floor(
@ -171,47 +170,30 @@ export const makeArrayExtensions = (api: UserAPI) => {
};
Array.prototype.b = Array.prototype.beat;
// Array.prototype.dur = function(...durations) {
// const timepos = api.app.clock.pulses_since_origin;
// const ppqn = api.ppqn();
// let adjustedDurations = [];
// for (let i = 0; i < this.length; i++) {
// adjustedDurations.push(durations[i % durations.length]);
// }
// let cumulativeDuration = 0;
// let totalDuration = adjustedDurations.reduce((acc, duration) => acc + duration * ppqn, 0);
// let modulatedTimePos = timepos % totalDuration;
// for (let i = 0; i < this.length; i++) {
// const valueDuration = adjustedDurations[i] as any * ppqn;
// if (modulatedTimePos < cumulativeDuration + valueDuration) {
// return this[i];
// }
// cumulativeDuration += valueDuration;
// }
// // This point should not be reached if durations are correctly specified
// throw new Error('Durations array does not match the pattern length.');
// };
Array.prototype.dur = function(...durations) {
Array.prototype.dur = function (...durations) {
const timepos = api.app.clock.pulses_since_origin;
const ppqn = api.ppqn();
const adjustedDurations = this.map((_, index) => durations[index % durations.length]);
const adjustedDurations = this.map(
(_, index) => durations[index % durations.length]
);
// @ts-ignore
const totalDurationInPulses = adjustedDurations.reduce((acc, duration) => acc + duration * ppqn, 0);
const totalDurationInPulses = adjustedDurations.reduce(
(acc, duration) => acc + duration * ppqn,
0
);
const loopPosition = timepos % totalDurationInPulses;
let cumulativeDuration = 0;
for (let i = 0; i < this.length; i++) {
const valueDurationInPulses = adjustedDurations[i] as any * ppqn;
const valueDurationInPulses = (adjustedDurations[i] as any) * ppqn;
cumulativeDuration += valueDurationInPulses;
if (loopPosition < cumulativeDuration) {
return this[i];
}
}
throw new Error('Durations array does not match the pattern length.');
throw new Error("Durations array does not match the pattern length.");
};
Array.prototype.shuffle = function() {
Array.prototype.shuffle = function () {
/**
* Shuffles the array in place.
*
@ -230,7 +212,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.rotate = function(steps: number) {
Array.prototype.rotate = function (steps: number) {
/**
* Rotates the array in place.
*
@ -250,7 +232,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this;
};
Array.prototype.unique = function() {
Array.prototype.unique = function () {
/**
* Removes duplicate elements from the array in place.
*
@ -283,7 +265,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
if (this.length <= 1) {
return this;
}
for (let i = 0; i < this.length;) {
for (let i = 0; i < this.length; ) {
const rand = api.randomGen() * 100;
if (rand < amount) {
if (this.length > 1) {
@ -396,7 +378,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return left_to_right.concat(right_to_left);
};
Array.prototype.loop = function(index: number) {
Array.prototype.loop = function (index: number) {
/**
* Returns an element from the array based on the index.
* The index will wrap over the array.
@ -407,7 +389,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
return this[index % this.length];
};
Array.prototype.random = function() {
Array.prototype.random = function () {
/**
* Returns a random element from the array.
*
@ -418,7 +400,7 @@ export const makeArrayExtensions = (api: UserAPI) => {
Array.prototype.rand = Array.prototype.random;
};
Array.prototype.scale = function(
Array.prototype.scale = function (
scale: string = "major",
base_note: number = 0
) {
@ -442,7 +424,7 @@ Array.prototype.scale = function(
});
};
Array.prototype.scaleArp = function(
Array.prototype.scaleArp = function (
scaleName: string = "major",
boundary: number = 0
) {