sf2-json 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/sf2-json.js +68 -101
package/package.json
CHANGED
package/src/sf2-json.js
CHANGED
|
@@ -147,8 +147,6 @@ function buildWavBuffer(audioData) {
|
|
|
147
147
|
else throw new Error(`buildWavBuffer: type '${type}' non supporté (SF3 compressé).`);
|
|
148
148
|
pcm16 = normalizeBuffer(pcm16);
|
|
149
149
|
|
|
150
|
-
|
|
151
|
-
|
|
152
150
|
const numChannels = 1;
|
|
153
151
|
const bitsPerSample = 16;
|
|
154
152
|
const byteRate = sampleRate * numChannels * (bitsPerSample / 8);
|
|
@@ -174,110 +172,79 @@ function buildWavBuffer(audioData) {
|
|
|
174
172
|
}
|
|
175
173
|
|
|
176
174
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
175
|
function extractZones(soundFont, parsed, presetHeaderIndex) {
|
|
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
|
-
zones.push({
|
|
232
|
-
generators: merged,
|
|
233
|
-
sampleHeader,
|
|
234
|
-
sample: parsed.samples[sampleId]
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
return zones;
|
|
176
|
+
const presetGeneratorsList = soundFont.getPresetGenerators(presetHeaderIndex);
|
|
177
|
+
const zonesMap = new Map();
|
|
178
|
+
let globalPresetGen = null;
|
|
179
|
+
|
|
180
|
+
for (const rawGenList of presetGeneratorsList) {
|
|
181
|
+
const presetGen = createPresetGeneratorObject(rawGenList);
|
|
182
|
+
if (presetGen.instrument === undefined) { globalPresetGen = presetGen; continue; }
|
|
183
|
+
|
|
184
|
+
const instrId = presetGen.instrument;
|
|
185
|
+
const instrGeneratorsList = soundFont.getInstrumentGenerators(instrId);
|
|
186
|
+
const defaults = convertToInstrumentGeneratorParams(DefaultInstrumentZone);
|
|
187
|
+
let globalInstrGen = null;
|
|
188
|
+
|
|
189
|
+
for (const rawInstrGenList of instrGeneratorsList) {
|
|
190
|
+
const instrGen = createInstrumentGeneratorObject(rawInstrGenList);
|
|
191
|
+
if (instrGen.sampleID === undefined) { globalInstrGen = instrGen; continue; }
|
|
192
|
+
|
|
193
|
+
const merged = { ...defaults };
|
|
194
|
+
if (globalInstrGen) Object.assign(merged, globalInstrGen);
|
|
195
|
+
Object.assign(merged, instrGen);
|
|
196
|
+
|
|
197
|
+
const applyPresetOffsets = (gen) => {
|
|
198
|
+
if (!gen) return;
|
|
199
|
+
for (const [key, val] of Object.entries(gen)) {
|
|
200
|
+
if (['keyRange', 'velRange', 'instrument', 'sampleID'].includes(key)) continue;
|
|
201
|
+
if (key in merged && typeof val === 'number') merged[key] += val;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
applyPresetOffsets(globalPresetGen);
|
|
205
|
+
applyPresetOffsets(presetGen);
|
|
206
|
+
|
|
207
|
+
const lo = merged.keyRange?.lo ?? 0;
|
|
208
|
+
const hi = merged.keyRange?.hi ?? 127;
|
|
209
|
+
const keyRangeStr = `${lo}-${hi}`;
|
|
210
|
+
|
|
211
|
+
const sampleHeader = parsed.sampleHeaders[merged.sampleID];
|
|
212
|
+
if (!sampleHeader || sampleHeader.isEnd) continue;
|
|
213
|
+
|
|
214
|
+
if (!zonesMap.has(keyRangeStr)) {
|
|
215
|
+
zonesMap.set(keyRangeStr, { generators: merged, sampleHeader, sample: parsed.samples[merged.sampleID] });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return Array.from(zonesMap.values());
|
|
239
220
|
}
|
|
240
221
|
|
|
241
222
|
|
|
242
223
|
async function buildZone(generators, sampleHeader, sample) {
|
|
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
|
-
const audioBase64 = mp3Buffer.toString('base64');
|
|
268
|
-
|
|
269
|
-
return {
|
|
270
|
-
originalPitch: midi * 100,
|
|
271
|
-
keyRangeLow: generators.keyRange?.lo ?? 0,
|
|
272
|
-
keyRangeHigh: generators.keyRange?.hi ?? 127,
|
|
273
|
-
loopStart,
|
|
274
|
-
loopEnd,
|
|
275
|
-
coarseTune: generators.coarseTune ?? 0,
|
|
276
|
-
fineTune,
|
|
277
|
-
sampleRate,
|
|
278
|
-
ahdsr: ahdsr,
|
|
279
|
-
file: audioBase64,
|
|
280
|
-
};
|
|
224
|
+
const { originalPitch, pitchCorrection, loopStart, loopEnd } = sampleHeader;
|
|
225
|
+
|
|
226
|
+
const rootKey = (generators.overridingRootKey !== undefined && generators.overridingRootKey !== 255)
|
|
227
|
+
? generators.overridingRootKey
|
|
228
|
+
: originalPitch;
|
|
229
|
+
|
|
230
|
+
const coarseTuneCents = (generators.coarseTune ?? 0) * 100;
|
|
231
|
+
const fineTuneCents = (generators.fineTune ?? 0) + (pitchCorrection ?? 0);
|
|
232
|
+
const totalPitchOffset = coarseTuneCents + fineTuneCents;
|
|
233
|
+
const calculatedPitch = (rootKey * 100) + totalPitchOffset;
|
|
234
|
+
|
|
235
|
+
const wavBuffer = buildWavBuffer(sample);
|
|
236
|
+
const mp3Buffer = encodeOpus(wavBuffer, 96, RESAMPLE_RATE);
|
|
237
|
+
|
|
238
|
+
return {
|
|
239
|
+
originalPitch: calculatedPitch,
|
|
240
|
+
keyRangeLow: generators.keyRange?.lo ?? 0,
|
|
241
|
+
keyRangeHigh: generators.keyRange?.hi ?? 127,
|
|
242
|
+
loopStart,
|
|
243
|
+
loopEnd,
|
|
244
|
+
sampleRate: sampleHeader.sampleRate,
|
|
245
|
+
ahdsr: true,
|
|
246
|
+
file: mp3Buffer.toString('base64'),
|
|
247
|
+
};
|
|
281
248
|
}
|
|
282
249
|
|
|
283
250
|
|