Skip to content

Commit

Permalink
Improved/fixed loading, added TrackItemProgressNode
Browse files Browse the repository at this point in the history
  • Loading branch information
newcat committed Mar 14, 2024
1 parent c9ed56b commit 28bdbf8
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/audio/audio.libraryItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ export class AudioLibraryItem extends LibraryItem<AudioLibraryItemState> {

private async generateWaveform(): Promise<IWaveform> {
const samples = this.audioBuffer!.getChannelData(0);
// Important! Using transferable for the samples crashes Electron 8.4+
const rawWaveform = await WaveformWorker.generateWaveform(samples, AudioLibraryItem.sampleRate, 1024);

const parts: IWaveformPart[] = [];
Expand Down
3 changes: 3 additions & 0 deletions src/globalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const useGlobalState = defineStore("globalState", () => {
snapUnits.value = defaults.snapUnits;
bridgeUrl.value = "";

graphTemplates.reset();
await library.reset();
timeline.reset();
stage.reset();
Expand All @@ -89,7 +90,9 @@ export const useGlobalState = defineStore("globalState", () => {
const data: SavedState = JSON.parse(raw) as SavedState;
stage.load(data.stage);
graphTemplates.load(data.graphTemplates ?? []);
graphTemplates.skipSync.value = true;
await library.load(data.library);
graphTemplates.skipSync.value = false;
timeline.load(data.timeline);
bpm.value = data.bpm ?? defaults.bpm;
fps.value = data.fps ?? defaults.fps;
Expand Down
22 changes: 18 additions & 4 deletions src/graph/graphTemplateSync.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ref } from "vue";
import { Editor, GraphTemplate, IGraphTemplateState } from "baklavajs";

export function useGraphTemplateSync() {
const token = Symbol("useGraphTemplateSync");
const targets: Editor[] = [];
const templates = new Map<string, IGraphTemplateState>();
const skipSync = ref(false);
let updating = false;

function registerTarget(target: Editor) {
Expand All @@ -12,6 +14,9 @@ export function useGraphTemplateSync() {
updateTemplate(template.save());
});
target.events.removeGraphTemplate.subscribe(token, (template) => {
if (target.loading) {
return;
}
removeTemplate(template.id);
});
target.graphTemplateEvents.updated.subscribe(token, (_, template) => {
Expand All @@ -34,9 +39,9 @@ export function useGraphTemplateSync() {
function unregisterTarget(target: Editor) {
const index = targets.indexOf(target);
if (index !== -1) {
const target = targets[index];
targets.splice(index, 1);

const target = targets[index];
target.events.addGraphTemplate.unsubscribe(token);
target.events.removeGraphTemplate.unsubscribe(token);
target.graphTemplateEvents.updated.unsubscribe(token);
Expand All @@ -54,8 +59,17 @@ export function useGraphTemplateSync() {
}
}

function reset() {
templates.clear();
while (targets.length > 0) {
unregisterTarget(targets[0]);
}
updating = false;
skipSync.value = false;
}

function updateTemplate(template: IGraphTemplateState) {
if (updating) {
if (updating || skipSync.value) {
return;
}

Expand All @@ -76,7 +90,7 @@ export function useGraphTemplateSync() {
}

function removeTemplate(id: string) {
if (updating) {
if (updating || skipSync.value) {
return;
}

Expand All @@ -91,5 +105,5 @@ export function useGraphTemplateSync() {
updating = false;
}

return { registerTarget, unregisterTarget, save, load };
return { skipSync, registerTarget, unregisterTarget, save, load, reset };
}
19 changes: 19 additions & 0 deletions src/graph/nodes/input/TrackItemProgressNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { defineNode } from "baklavajs";
import { NumberInterface } from "../../interfaces";
import { LmsCalculationContext } from "../../types";

export const TrackItemProgressNode = defineNode({
type: "Track Item Progress",
inputs: {
min: () => new NumberInterface("Min", 0),
max: () => new NumberInterface("Max", 1),
},
outputs: {
value: () => new NumberInterface("Value", 0).setComponent(undefined),
},
calculate(inputs, context: LmsCalculationContext) {
const { min, max } = inputs;
const value = min + (max - min) * context.globalValues.relativeTrackItemProgress;
return { value };
},
});
1 change: 1 addition & 0 deletions src/graph/nodes/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./LfoNode";
export * from "./PatternNode";
export * from "./PeakNode";
export * from "./SpectrumNode";
export * from "./TrackItemProgressNode";
1 change: 1 addition & 0 deletions src/graph/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ICalculationData {
timeDomainData: Float32Array;
frequencyData: Float32Array;
trackValues: Map<string, number | INote[]>;
relativeTrackItemProgress: number;
}

export type LmsCalculationContext = CalculationContext<ICalculationData>;
5 changes: 3 additions & 2 deletions src/timeline/timelineProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class TimelineProcessor {
const audioData = this.audioProcessor!.getAudioData();

const uncontrolledFixtures = new Set(this.stage.fixtures.values()) as Set<BaseFixture>;
const calculationData: ICalculationData = {
const calculationData: Omit<ICalculationData, "relativeTrackItemProgress"> = {
resolution: this.globalState.resolution,
fps: this.globalState.fps,
position: unit,
Expand All @@ -105,7 +105,8 @@ export class TimelineProcessor {
const graphs = currentActiveItems.filter((i) => this.isType(i, LibraryItemType.GRAPH));
for (const g of graphs) {
try {
const results = await this.processGraph(g, unit, calculationData);
const relativeTrackItemProgress = (unit - g.start) / (g.end - g.start);
const results = await this.processGraph(g, unit, { ...calculationData, relativeTrackItemProgress });
if (g.libraryItem.error) {
g.libraryItem.error = "";
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/comlinkVueTransferHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import * as Comlink from "comlink";

const vueReactiveTransferHandler: Comlink.TransferHandler<any, any> = {
canHandle: ((x: any) => {
return isReactive(x) || (x && typeof x === "object" && Object.values(x).some((v) => isReactive(v)));
return (
!(x instanceof Float32Array) && (isReactive(x) || (x && typeof x === "object" && Object.values(x).some((v) => isReactive(v))))
);
}) as (x: any) => x is any,
serialize: (x) => [JSON.parse(JSON.stringify(x)), []],
deserialize: (x) => x,
Expand Down

0 comments on commit 28bdbf8

Please sign in to comment.