sf2-json 1.0.1 → 1.0.3
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 +14 -48
package/package.json
CHANGED
package/src/sf2-json.js
CHANGED
|
@@ -147,24 +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
|
-
// const loopLen = loopEnd - loopStart;
|
|
151
|
-
// const MIN_LOOP_SAMPLES = Math.ceil(2 * 1152 * sampleRate / RESAMPLE_RATE);
|
|
152
|
-
// if (loopLen > 0 && loopLen < MIN_LOOP_SAMPLES) {
|
|
153
|
-
// const preLoop = pcm16.slice(0, loopStart * 2);
|
|
154
|
-
// const loopData = pcm16.slice(loopStart * 2, loopEnd * 2);
|
|
155
|
-
// const postLoop = pcm16.slice(loopEnd * 2);
|
|
156
|
-
// const repeats = Math.ceil(MIN_LOOP_SAMPLES / loopLen);
|
|
157
|
-
// const loopRepeated = Buffer.concat(Array(repeats).fill(loopData));
|
|
158
|
-
// pcm16 = Buffer.concat([preLoop, loopRepeated, postLoop]);
|
|
159
|
-
// }
|
|
160
|
-
|
|
161
|
-
// const minSamples = Math.ceil(4 * 1152 * sampleRate / RESAMPLE_RATE);
|
|
162
|
-
// const minBytes = minSamples * 2;
|
|
163
|
-
// if (pcm16.byteLength < minBytes) {
|
|
164
|
-
// const pad = Buffer.alloc(minBytes - pcm16.byteLength);
|
|
165
|
-
// pcm16 = Buffer.concat([pcm16, pad]);
|
|
166
|
-
// }
|
|
167
|
-
|
|
168
150
|
const numChannels = 1;
|
|
169
151
|
const bitsPerSample = 16;
|
|
170
152
|
const byteRate = sampleRate * numChannels * (bitsPerSample / 8);
|
|
@@ -192,16 +174,12 @@ function buildWavBuffer(audioData) {
|
|
|
192
174
|
|
|
193
175
|
function extractZones(soundFont, parsed, presetHeaderIndex) {
|
|
194
176
|
const presetGeneratorsList = soundFont.getPresetGenerators(presetHeaderIndex);
|
|
195
|
-
const
|
|
196
|
-
const seenSampleIds = new Set();
|
|
177
|
+
const zonesMap = new Map();
|
|
197
178
|
let globalPresetGen = null;
|
|
198
179
|
|
|
199
180
|
for (const rawGenList of presetGeneratorsList) {
|
|
200
181
|
const presetGen = createPresetGeneratorObject(rawGenList);
|
|
201
|
-
if (presetGen.instrument === undefined) {
|
|
202
|
-
globalPresetGen = presetGen;
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
182
|
+
if (presetGen.instrument === undefined) { globalPresetGen = presetGen; continue; }
|
|
205
183
|
|
|
206
184
|
const instrId = presetGen.instrument;
|
|
207
185
|
const instrGeneratorsList = soundFont.getInstrumentGenerators(instrId);
|
|
@@ -215,42 +193,30 @@ function extractZones(soundFont, parsed, presetHeaderIndex) {
|
|
|
215
193
|
const merged = { ...defaults };
|
|
216
194
|
if (globalInstrGen) Object.assign(merged, globalInstrGen);
|
|
217
195
|
Object.assign(merged, instrGen);
|
|
196
|
+
|
|
218
197
|
const applyPresetOffsets = (gen) => {
|
|
219
198
|
if (!gen) return;
|
|
220
199
|
for (const [key, val] of Object.entries(gen)) {
|
|
221
|
-
if (
|
|
200
|
+
if (['keyRange', 'velRange', 'instrument', 'sampleID'].includes(key)) continue;
|
|
222
201
|
if (key in merged && typeof val === 'number') merged[key] += val;
|
|
223
202
|
}
|
|
224
203
|
};
|
|
225
204
|
applyPresetOffsets(globalPresetGen);
|
|
226
205
|
applyPresetOffsets(presetGen);
|
|
227
206
|
|
|
228
|
-
const
|
|
229
|
-
|
|
230
|
-
const
|
|
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];
|
|
231
212
|
if (!sampleHeader || sampleHeader.isEnd) continue;
|
|
232
|
-
|
|
233
|
-
|
|
213
|
+
|
|
214
|
+
if (!zonesMap.has(keyRangeStr)) {
|
|
215
|
+
zonesMap.set(keyRangeStr, { generators: merged, sampleHeader, sample: parsed.samples[merged.sampleID] });
|
|
216
|
+
}
|
|
234
217
|
}
|
|
235
218
|
}
|
|
236
|
-
return
|
|
237
|
-
|
|
238
|
-
// const byKeyRange = new Map();
|
|
239
|
-
// for (const zone of zones) {
|
|
240
|
-
// const lo = zone.generators.keyRange?.lo ?? 0;
|
|
241
|
-
// const hi = zone.generators.keyRange?.hi ?? 127;
|
|
242
|
-
// const key = `${lo}-${hi}`;
|
|
243
|
-
// if (!byKeyRange.has(key)) {
|
|
244
|
-
// byKeyRange.set(key, zone);
|
|
245
|
-
// } else {
|
|
246
|
-
// const center = (lo + hi) / 2;
|
|
247
|
-
// const existing = byKeyRange.get(key);
|
|
248
|
-
// const existingDist = Math.abs(existing.sampleHeader.originalPitch - center);
|
|
249
|
-
// const newDist = Math.abs(zone.sampleHeader.originalPitch - center);
|
|
250
|
-
// if (newDist < existingDist) byKeyRange.set(key, zone);
|
|
251
|
-
// }
|
|
252
|
-
// }
|
|
253
|
-
// return Array.from(byKeyRange.values());
|
|
219
|
+
return Array.from(zonesMap.values());
|
|
254
220
|
}
|
|
255
221
|
|
|
256
222
|
|