diff --git a/src/lib/exporters/ableton/builders.ts b/src/lib/exporters/ableton/builders.ts index f85715c..ea96969 100644 --- a/src/lib/exporters/ableton/builders.ts +++ b/src/lib/exporters/ableton/builders.ts @@ -32,6 +32,7 @@ import { gzipString, koEnvRangeToSeconds, loadTemplate, + resetIds, TIME_SIGNATURES, toNativePath, } from './utils'; @@ -611,6 +612,7 @@ async function buildReturnTrack(projectData: ProjectRawData, trackId: number = 0 } export async function buildProject(projectData: ProjectRawData, exporterParams: ExporterParams) { + resetIds(); const transformedData = abletonTransformer(projectData, exporterParams); if (import.meta.env.DEV) { diff --git a/src/lib/exporters/ableton/utils.ts b/src/lib/exporters/ableton/utils.ts index c791129..f0b69f1 100644 --- a/src/lib/exporters/ableton/utils.ts +++ b/src/lib/exporters/ableton/utils.ts @@ -31,6 +31,7 @@ const MIN_ID = 22000; const START_ID = 22000; let _id = START_ID; +let assignedNodes = new WeakSet(); const templateCache: Record = {}; const templateMap: Record Promise> = { @@ -80,27 +81,34 @@ export function fixIds(node: any): any { return node.map((n) => fixIds(n)); } + if (!node || typeof node !== 'object') { + return node; + } + if (node?.['@Id']) { const idNum = parseInt(String(node['@Id']), 10); - if (idNum > MIN_ID) { + if (idNum > MIN_ID && !assignedNodes.has(node)) { node['@Id'] = _id; - + assignedNodes.add(node); _id++; } } - if (typeof node === 'object') { - Object.keys(node).forEach((key) => { - if (!key.startsWith('@')) { - node[key] = fixIds(node[key]); - } - }); - } + Object.keys(node).forEach((key) => { + if (!key.startsWith('@')) { + node[key] = fixIds(node[key]); + } + }); return node; } +export function resetIds() { + _id = START_ID; + assignedNodes = new WeakSet(); +} + export function getId() { return _id; }