UNPKG

u-he-preset-randomizer

Version:

Create random u-he synth presets through randomization and merging of your existing presets.

82 lines 3.57 kB
/** * @file Main preset generation orchestration module. * Coordinates loading, filtering, analysis, and generation of presets. */ import chalk from 'chalk'; import fs from 'fs-extra'; import { analyzeParamsTypeAndRange, convertParamsModelBySection, } from './analyzer.js'; import { narrowDownByAuthor, narrowDownByCategory, narrowDownByFavoritesFile, } from './libraryFilters.js'; import { loadPresetLibrary, writePresetLibrary, } from './presetLibrary.js'; import { generateFullyRandomPresets, generateMergedPresets, generateRandomizedPresets, } from './randomizer.js'; /** * Main function to generate presets based on the provided configuration. * @param inputConfig Configuration options for preset generation. * @param inputPresetLibrary Optional preset library to reuse if already loaded (avoids slow filesystem reload). */ export function generatePresets(inputConfig, inputPresetLibrary) { const config = { ...inputConfig, }; if (!config.synth) { throw new Error('Synth not specified in config'); } if (config.folder && config.folder !== true) { config.pattern = `${config.folder}${config.pattern ?? '**/*'}`; } const presetLibrary = inputPresetLibrary ?? loadPresetLibrary(config.synth, config); const filteredLibrary = applyPresetFilters(presetLibrary, config); const paramsModel = analyzeParamsTypeAndRange(filteredLibrary, config); if (config.debug) { dumpParamsModel(paramsModel); console.debug(chalk.gray(JSON.stringify(config, null, 2))); } let generatedPresets; let writtenFiles; // Ensure the RANDOM output folder exists upfront const randomOutputFolder = `${presetLibrary.userPresetsFolder}/RANDOM`; fs.ensureDirSync(randomOutputFolder); if (config.merge) { generatedPresets = generateMergedPresets(filteredLibrary, paramsModel, config); writtenFiles = writePresetLibrary(generatedPresets); } else if (config.preset) { generatedPresets = generateRandomizedPresets(filteredLibrary, paramsModel, config); writtenFiles = writePresetLibrary(generatedPresets); } else { generatedPresets = generateFullyRandomPresets(filteredLibrary, paramsModel, config); writtenFiles = writePresetLibrary(generatedPresets); } return { writtenFiles, outputFolder: generatedPresets.userPresetsFolder, presetCount: generatedPresets.presets.length, }; } function applyPresetFilters(presetLibrary, config) { const filteredLibrary = { ...presetLibrary, presets: [...presetLibrary.presets], }; if (config.favorites && config.favorites !== true) { filteredLibrary.presets = narrowDownByFavoritesFile(filteredLibrary, config.favorites); } if (config.author && config.author !== true) { filteredLibrary.presets = narrowDownByAuthor(filteredLibrary, config.author); } if (config.category && config.category !== true) { filteredLibrary.presets = narrowDownByCategory(filteredLibrary, config.category); } return filteredLibrary; } function dumpParamsModel(paramsModel) { const outputParamsModel = JSON.parse(JSON.stringify(paramsModel)); for (const paramKey in outputParamsModel) { const param = outputParamsModel[paramKey]; if (param && 'values' in param) { param.values = []; } } fs.outputFileSync('./tmp/paramsModel.json', JSON.stringify(convertParamsModelBySection(outputParamsModel), null, 2)); } //# sourceMappingURL=generatePresets.js.map