File size: 10,511 Bytes
b0bfea8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 | /**
* @typedef {{
* instrumentGenerators: Generator[],
* presetGenerators: Generator[],
* modulators: Modulator[],
* sample: BasicSample,
* sampleID: number,
* }} SampleAndGenerators
*/
import { generatorTypes } from "./generator.js";
import { Modulator } from "./modulator.js";
import { isXGDrums } from "../../utils/xg_hacks.js";
export class BasicPreset
{
/**
* The parent soundbank instance
* Currently used for determining default modulators and XG status
* @type {BasicSoundBank}
*/
parentSoundBank;
/**
* The preset's name
* @type {string}
*/
presetName = "";
/**
* The preset's MIDI program number
* @type {number}
*/
program = 0;
/**
* The preset's MIDI bank number
* @type {number}
*/
bank = 0;
/**
* The preset's zones
* @type {BasicPresetZone[]}
*/
presetZones = [];
/**
* Stores already found getSamplesAndGenerators for reuse
* @type {SampleAndGenerators[][][]}
*/
foundSamplesAndGenerators = [];
/**
* unused metadata
* @type {number}
*/
library = 0;
/**
* unused metadata
* @type {number}
*/
genre = 0;
/**
* unused metadata
* @type {number}
*/
morphology = 0;
/**
* Creates a new preset representation
* @param parentSoundBank {BasicSoundBank}
*/
constructor(parentSoundBank)
{
this.parentSoundBank = parentSoundBank;
for (let i = 0; i < 128; i++)
{
this.foundSamplesAndGenerators[i] = [];
}
}
/**
* @param allowXG {boolean}
* @param allowSFX {boolean}
* @returns {boolean}
*/
isDrumPreset(allowXG, allowSFX = false)
{
const xg = allowXG && this.parentSoundBank.isXGBank;
// sfx is not cool
return this.bank === 128 || (
xg &&
(isXGDrums(this.bank) && (this.bank !== 126 || allowSFX))
);
}
deletePreset()
{
this.presetZones.forEach(z => z.deleteZone());
this.presetZones.length = 0;
}
/**
* @param index {number}
*/
deleteZone(index)
{
this.presetZones[index].deleteZone();
this.presetZones.splice(index, 1);
}
// noinspection JSUnusedGlobalSymbols
/**
* Preloads all samples (async)
*/
preload(keyMin, keyMax)
{
for (let key = keyMin; key < keyMax + 1; key++)
{
for (let velocity = 0; velocity < 128; velocity++)
{
this.getSamplesAndGenerators(key, velocity).forEach(samandgen =>
{
if (!samandgen.sample.isSampleLoaded)
{
samandgen.sample.getAudioData();
}
});
}
}
}
/**
* Preloads a specific key/velocity combo
* @param key {number}
* @param velocity {number}
*/
preloadSpecific(key, velocity)
{
this.getSamplesAndGenerators(key, velocity).forEach(samandgen =>
{
if (!samandgen.sample.isSampleLoaded)
{
samandgen.sample.getAudioData();
}
});
}
/**
* Returns generatorTranslator and generators for given note
* @param midiNote {number}
* @param velocity {number}
* @returns {SampleAndGenerators[]}
*/
getSamplesAndGenerators(midiNote, velocity)
{
const memorized = this.foundSamplesAndGenerators[midiNote][velocity];
if (memorized)
{
return memorized;
}
if (this.presetZones.length < 1)
{
return [];
}
/**
* @param range {SoundFontRange}
* @param number {number}
* @returns {boolean}
*/
function isInRange(range, number)
{
return number >= range.min && number <= range.max;
}
/**
* @param main {Generator[]}
* @param adder {Generator[]}
*/
function addUnique(main, adder)
{
main.push(...adder.filter(g => !main.find(mg => mg.generatorType === g.generatorType)));
}
/**
* @param main {Modulator[]}
* @param adder {Modulator[]}
*/
function addUniqueMods(main, adder)
{
main.push(...adder.filter(m => !main.find(mm => Modulator.isIdentical(m, mm))));
}
/**
* @type {SampleAndGenerators[]}
*/
let parsedGeneratorsAndSamples = [];
/**
* global zone is always first, so it or nothing
* @type {Generator[]}
*/
let globalPresetGenerators = this.presetZones[0].isGlobal ? [...this.presetZones[0].generators] : [];
/**
* @type {Modulator[]}
*/
let globalPresetModulators = this.presetZones[0].isGlobal ? [...this.presetZones[0].modulators] : [];
const globalKeyRange = this.presetZones[0].isGlobal ? this.presetZones[0].keyRange : { min: 0, max: 127 };
const globalVelRange = this.presetZones[0].isGlobal ? this.presetZones[0].velRange : { min: 0, max: 127 };
// find the preset zones in range
let presetZonesInRange = this.presetZones.filter(currentZone =>
(
isInRange(
currentZone.hasKeyRange ? currentZone.keyRange : globalKeyRange,
midiNote
)
&&
isInRange(
currentZone.hasVelRange ? currentZone.velRange : globalVelRange,
velocity
)
) && !currentZone.isGlobal);
presetZonesInRange.forEach(zone =>
{
// the global zone is already taken into account earlier
if (zone.instrument.instrumentZones.length < 1)
{
return;
}
let presetGenerators = zone.generators;
let presetModulators = zone.modulators;
const firstZone = zone.instrument.instrumentZones[0];
/**
* global zone is always first, so it or nothing
* @type {Generator[]}
*/
let globalInstrumentGenerators = firstZone.isGlobal ? [...firstZone.generators] : [];
let globalInstrumentModulators = firstZone.isGlobal ? [...firstZone.modulators] : [];
const globalKeyRange = firstZone.isGlobal ? firstZone.keyRange : { min: 0, max: 127 };
const globalVelRange = firstZone.isGlobal ? firstZone.velRange : { min: 0, max: 127 };
let instrumentZonesInRange = zone.instrument.instrumentZones
.filter(currentZone =>
(
isInRange(
currentZone.hasKeyRange ? currentZone.keyRange : globalKeyRange,
midiNote
)
&&
isInRange(
currentZone.hasVelRange ? currentZone.velRange : globalVelRange,
velocity
)
) && !currentZone.isGlobal
);
instrumentZonesInRange.forEach(instrumentZone =>
{
let instrumentGenerators = [...instrumentZone.generators];
let instrumentModulators = [...instrumentZone.modulators];
addUnique(
presetGenerators,
globalPresetGenerators
);
// add the unique global preset generators (local replace global(
// add the unique global instrument generators (local replace global)
addUnique(
instrumentGenerators,
globalInstrumentGenerators
);
addUniqueMods(
presetModulators,
globalPresetModulators
);
addUniqueMods(
instrumentModulators,
globalInstrumentModulators
);
// default mods
addUniqueMods(
instrumentModulators,
this.parentSoundBank.defaultModulators
);
/**
* sum preset modulators to instruments (amount) sf spec page 54
* @type {Modulator[]}
*/
const finalModulatorList = [...instrumentModulators];
for (let i = 0; i < presetModulators.length; i++)
{
let mod = presetModulators[i];
const identicalInstrumentModulator = finalModulatorList.findIndex(
m => Modulator.isIdentical(mod, m));
if (identicalInstrumentModulator !== -1)
{
// sum the amounts
// (this makes a new modulator because otherwise it would overwrite the one in the soundfont!
finalModulatorList[identicalInstrumentModulator] = finalModulatorList[identicalInstrumentModulator].sumTransform(
mod);
}
else
{
finalModulatorList.push(mod);
}
}
// combine both generators and add to the final result
parsedGeneratorsAndSamples.push({
instrumentGenerators: instrumentGenerators,
presetGenerators: presetGenerators,
modulators: finalModulatorList,
sample: instrumentZone.sample,
sampleID: instrumentZone.generators.find(
g => g.generatorType === generatorTypes.sampleID).generatorValue
});
});
});
// save and return
this.foundSamplesAndGenerators[midiNote][velocity] = parsedGeneratorsAndSamples;
return parsedGeneratorsAndSamples;
}
} |