@pompeii-labs/audio 0.1.2 → 0.1.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/dist/index.js +35 -3
- package/dist/index.mjs +35 -3
- package/dist/voice.js +35 -3
- package/dist/voice.mjs +35 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -213,20 +213,52 @@ function generateFadeOutSamples(lastSampleValue, fadeDurationMs, sampleRate) {
|
|
|
213
213
|
|
|
214
214
|
// src/helpers/resamplePcm.ts
|
|
215
215
|
function resamplePcm(pcm, originalSampleRate, targetSampleRate) {
|
|
216
|
+
if (originalSampleRate === targetSampleRate) {
|
|
217
|
+
return pcm;
|
|
218
|
+
}
|
|
216
219
|
const ratio = originalSampleRate / targetSampleRate;
|
|
217
220
|
const newLength = Math.floor(pcm.length / ratio);
|
|
218
221
|
const newSamples = new Int16Array(newLength);
|
|
222
|
+
if (ratio < 1) {
|
|
223
|
+
for (let i = 0; i < newSamples.length; i++) {
|
|
224
|
+
const exactPos = i * ratio;
|
|
225
|
+
const lowerIndex = Math.floor(exactPos);
|
|
226
|
+
const upperIndex = Math.min(lowerIndex + 1, pcm.length - 1);
|
|
227
|
+
const fraction = exactPos - lowerIndex;
|
|
228
|
+
const lowerSample = pcm[lowerIndex];
|
|
229
|
+
const upperSample = pcm[upperIndex];
|
|
230
|
+
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
231
|
+
}
|
|
232
|
+
return newSamples;
|
|
233
|
+
}
|
|
234
|
+
const nyquistFreq = targetSampleRate / 2;
|
|
235
|
+
const cutoffFreq = nyquistFreq * 0.9;
|
|
236
|
+
const filteredPcm = applyLowPassFilter(pcm, originalSampleRate, cutoffFreq);
|
|
219
237
|
for (let i = 0; i < newSamples.length; i++) {
|
|
220
238
|
const exactPos = i * ratio;
|
|
221
239
|
const lowerIndex = Math.floor(exactPos);
|
|
222
|
-
const upperIndex = Math.min(lowerIndex + 1,
|
|
240
|
+
const upperIndex = Math.min(lowerIndex + 1, filteredPcm.length - 1);
|
|
223
241
|
const fraction = exactPos - lowerIndex;
|
|
224
|
-
const lowerSample =
|
|
225
|
-
const upperSample =
|
|
242
|
+
const lowerSample = filteredPcm[lowerIndex];
|
|
243
|
+
const upperSample = filteredPcm[upperIndex];
|
|
226
244
|
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
227
245
|
}
|
|
228
246
|
return newSamples;
|
|
229
247
|
}
|
|
248
|
+
function applyLowPassFilter(pcm, sampleRate, cutoffFreq) {
|
|
249
|
+
const filterOrder = Math.max(3, Math.floor(sampleRate / (cutoffFreq * 4)));
|
|
250
|
+
const filtered = new Int16Array(pcm.length);
|
|
251
|
+
for (let i = 0; i < pcm.length; i++) {
|
|
252
|
+
let sum = 0;
|
|
253
|
+
let count = 0;
|
|
254
|
+
for (let j = Math.max(0, i - filterOrder); j <= Math.min(pcm.length - 1, i + filterOrder); j++) {
|
|
255
|
+
sum += pcm[j];
|
|
256
|
+
count++;
|
|
257
|
+
}
|
|
258
|
+
filtered[i] = Math.round(sum / count);
|
|
259
|
+
}
|
|
260
|
+
return filtered;
|
|
261
|
+
}
|
|
230
262
|
|
|
231
263
|
exports.bufferToInt16Array = bufferToInt16Array;
|
|
232
264
|
exports.decodeToPcm = decodeToPcm;
|
package/dist/index.mjs
CHANGED
|
@@ -211,19 +211,51 @@ function generateFadeOutSamples(lastSampleValue, fadeDurationMs, sampleRate) {
|
|
|
211
211
|
|
|
212
212
|
// src/helpers/resamplePcm.ts
|
|
213
213
|
function resamplePcm(pcm, originalSampleRate, targetSampleRate) {
|
|
214
|
+
if (originalSampleRate === targetSampleRate) {
|
|
215
|
+
return pcm;
|
|
216
|
+
}
|
|
214
217
|
const ratio = originalSampleRate / targetSampleRate;
|
|
215
218
|
const newLength = Math.floor(pcm.length / ratio);
|
|
216
219
|
const newSamples = new Int16Array(newLength);
|
|
220
|
+
if (ratio < 1) {
|
|
221
|
+
for (let i = 0; i < newSamples.length; i++) {
|
|
222
|
+
const exactPos = i * ratio;
|
|
223
|
+
const lowerIndex = Math.floor(exactPos);
|
|
224
|
+
const upperIndex = Math.min(lowerIndex + 1, pcm.length - 1);
|
|
225
|
+
const fraction = exactPos - lowerIndex;
|
|
226
|
+
const lowerSample = pcm[lowerIndex];
|
|
227
|
+
const upperSample = pcm[upperIndex];
|
|
228
|
+
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
229
|
+
}
|
|
230
|
+
return newSamples;
|
|
231
|
+
}
|
|
232
|
+
const nyquistFreq = targetSampleRate / 2;
|
|
233
|
+
const cutoffFreq = nyquistFreq * 0.9;
|
|
234
|
+
const filteredPcm = applyLowPassFilter(pcm, originalSampleRate, cutoffFreq);
|
|
217
235
|
for (let i = 0; i < newSamples.length; i++) {
|
|
218
236
|
const exactPos = i * ratio;
|
|
219
237
|
const lowerIndex = Math.floor(exactPos);
|
|
220
|
-
const upperIndex = Math.min(lowerIndex + 1,
|
|
238
|
+
const upperIndex = Math.min(lowerIndex + 1, filteredPcm.length - 1);
|
|
221
239
|
const fraction = exactPos - lowerIndex;
|
|
222
|
-
const lowerSample =
|
|
223
|
-
const upperSample =
|
|
240
|
+
const lowerSample = filteredPcm[lowerIndex];
|
|
241
|
+
const upperSample = filteredPcm[upperIndex];
|
|
224
242
|
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
225
243
|
}
|
|
226
244
|
return newSamples;
|
|
227
245
|
}
|
|
246
|
+
function applyLowPassFilter(pcm, sampleRate, cutoffFreq) {
|
|
247
|
+
const filterOrder = Math.max(3, Math.floor(sampleRate / (cutoffFreq * 4)));
|
|
248
|
+
const filtered = new Int16Array(pcm.length);
|
|
249
|
+
for (let i = 0; i < pcm.length; i++) {
|
|
250
|
+
let sum = 0;
|
|
251
|
+
let count = 0;
|
|
252
|
+
for (let j = Math.max(0, i - filterOrder); j <= Math.min(pcm.length - 1, i + filterOrder); j++) {
|
|
253
|
+
sum += pcm[j];
|
|
254
|
+
count++;
|
|
255
|
+
}
|
|
256
|
+
filtered[i] = Math.round(sum / count);
|
|
257
|
+
}
|
|
258
|
+
return filtered;
|
|
259
|
+
}
|
|
228
260
|
|
|
229
261
|
export { bufferToInt16Array, decodeToPcm, decodeWAV, encodePcm, generateFadeOutSamples, identifyAudioFormat, int16ArrayToBuffer, mulawToPcm16, pcm16ToMulaw, resamplePcm };
|
package/dist/voice.js
CHANGED
|
@@ -61,20 +61,52 @@ function generateFadeOutSamples(lastSampleValue, fadeDurationMs, sampleRate) {
|
|
|
61
61
|
|
|
62
62
|
// src/helpers/resamplePcm.ts
|
|
63
63
|
function resamplePcm(pcm, originalSampleRate, targetSampleRate) {
|
|
64
|
+
if (originalSampleRate === targetSampleRate) {
|
|
65
|
+
return pcm;
|
|
66
|
+
}
|
|
64
67
|
const ratio = originalSampleRate / targetSampleRate;
|
|
65
68
|
const newLength = Math.floor(pcm.length / ratio);
|
|
66
69
|
const newSamples = new Int16Array(newLength);
|
|
70
|
+
if (ratio < 1) {
|
|
71
|
+
for (let i = 0; i < newSamples.length; i++) {
|
|
72
|
+
const exactPos = i * ratio;
|
|
73
|
+
const lowerIndex = Math.floor(exactPos);
|
|
74
|
+
const upperIndex = Math.min(lowerIndex + 1, pcm.length - 1);
|
|
75
|
+
const fraction = exactPos - lowerIndex;
|
|
76
|
+
const lowerSample = pcm[lowerIndex];
|
|
77
|
+
const upperSample = pcm[upperIndex];
|
|
78
|
+
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
79
|
+
}
|
|
80
|
+
return newSamples;
|
|
81
|
+
}
|
|
82
|
+
const nyquistFreq = targetSampleRate / 2;
|
|
83
|
+
const cutoffFreq = nyquistFreq * 0.9;
|
|
84
|
+
const filteredPcm = applyLowPassFilter(pcm, originalSampleRate, cutoffFreq);
|
|
67
85
|
for (let i = 0; i < newSamples.length; i++) {
|
|
68
86
|
const exactPos = i * ratio;
|
|
69
87
|
const lowerIndex = Math.floor(exactPos);
|
|
70
|
-
const upperIndex = Math.min(lowerIndex + 1,
|
|
88
|
+
const upperIndex = Math.min(lowerIndex + 1, filteredPcm.length - 1);
|
|
71
89
|
const fraction = exactPos - lowerIndex;
|
|
72
|
-
const lowerSample =
|
|
73
|
-
const upperSample =
|
|
90
|
+
const lowerSample = filteredPcm[lowerIndex];
|
|
91
|
+
const upperSample = filteredPcm[upperIndex];
|
|
74
92
|
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
75
93
|
}
|
|
76
94
|
return newSamples;
|
|
77
95
|
}
|
|
96
|
+
function applyLowPassFilter(pcm, sampleRate, cutoffFreq) {
|
|
97
|
+
const filterOrder = Math.max(3, Math.floor(sampleRate / (cutoffFreq * 4)));
|
|
98
|
+
const filtered = new Int16Array(pcm.length);
|
|
99
|
+
for (let i = 0; i < pcm.length; i++) {
|
|
100
|
+
let sum = 0;
|
|
101
|
+
let count = 0;
|
|
102
|
+
for (let j = Math.max(0, i - filterOrder); j <= Math.min(pcm.length - 1, i + filterOrder); j++) {
|
|
103
|
+
sum += pcm[j];
|
|
104
|
+
count++;
|
|
105
|
+
}
|
|
106
|
+
filtered[i] = Math.round(sum / count);
|
|
107
|
+
}
|
|
108
|
+
return filtered;
|
|
109
|
+
}
|
|
78
110
|
|
|
79
111
|
// src/voice/helpers.ts
|
|
80
112
|
function splitTextIntoChunks(text, targetLength = 100) {
|
package/dist/voice.mjs
CHANGED
|
@@ -55,20 +55,52 @@ function generateFadeOutSamples(lastSampleValue, fadeDurationMs, sampleRate) {
|
|
|
55
55
|
|
|
56
56
|
// src/helpers/resamplePcm.ts
|
|
57
57
|
function resamplePcm(pcm, originalSampleRate, targetSampleRate) {
|
|
58
|
+
if (originalSampleRate === targetSampleRate) {
|
|
59
|
+
return pcm;
|
|
60
|
+
}
|
|
58
61
|
const ratio = originalSampleRate / targetSampleRate;
|
|
59
62
|
const newLength = Math.floor(pcm.length / ratio);
|
|
60
63
|
const newSamples = new Int16Array(newLength);
|
|
64
|
+
if (ratio < 1) {
|
|
65
|
+
for (let i = 0; i < newSamples.length; i++) {
|
|
66
|
+
const exactPos = i * ratio;
|
|
67
|
+
const lowerIndex = Math.floor(exactPos);
|
|
68
|
+
const upperIndex = Math.min(lowerIndex + 1, pcm.length - 1);
|
|
69
|
+
const fraction = exactPos - lowerIndex;
|
|
70
|
+
const lowerSample = pcm[lowerIndex];
|
|
71
|
+
const upperSample = pcm[upperIndex];
|
|
72
|
+
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
73
|
+
}
|
|
74
|
+
return newSamples;
|
|
75
|
+
}
|
|
76
|
+
const nyquistFreq = targetSampleRate / 2;
|
|
77
|
+
const cutoffFreq = nyquistFreq * 0.9;
|
|
78
|
+
const filteredPcm = applyLowPassFilter(pcm, originalSampleRate, cutoffFreq);
|
|
61
79
|
for (let i = 0; i < newSamples.length; i++) {
|
|
62
80
|
const exactPos = i * ratio;
|
|
63
81
|
const lowerIndex = Math.floor(exactPos);
|
|
64
|
-
const upperIndex = Math.min(lowerIndex + 1,
|
|
82
|
+
const upperIndex = Math.min(lowerIndex + 1, filteredPcm.length - 1);
|
|
65
83
|
const fraction = exactPos - lowerIndex;
|
|
66
|
-
const lowerSample =
|
|
67
|
-
const upperSample =
|
|
84
|
+
const lowerSample = filteredPcm[lowerIndex];
|
|
85
|
+
const upperSample = filteredPcm[upperIndex];
|
|
68
86
|
newSamples[i] = Math.round(lowerSample + (upperSample - lowerSample) * fraction);
|
|
69
87
|
}
|
|
70
88
|
return newSamples;
|
|
71
89
|
}
|
|
90
|
+
function applyLowPassFilter(pcm, sampleRate, cutoffFreq) {
|
|
91
|
+
const filterOrder = Math.max(3, Math.floor(sampleRate / (cutoffFreq * 4)));
|
|
92
|
+
const filtered = new Int16Array(pcm.length);
|
|
93
|
+
for (let i = 0; i < pcm.length; i++) {
|
|
94
|
+
let sum = 0;
|
|
95
|
+
let count = 0;
|
|
96
|
+
for (let j = Math.max(0, i - filterOrder); j <= Math.min(pcm.length - 1, i + filterOrder); j++) {
|
|
97
|
+
sum += pcm[j];
|
|
98
|
+
count++;
|
|
99
|
+
}
|
|
100
|
+
filtered[i] = Math.round(sum / count);
|
|
101
|
+
}
|
|
102
|
+
return filtered;
|
|
103
|
+
}
|
|
72
104
|
|
|
73
105
|
// src/voice/helpers.ts
|
|
74
106
|
function splitTextIntoChunks(text, targetLength = 100) {
|