@waveform-playlist/webaudio-peaks 1.0.0

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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Naomi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Peaks type - represents a typed array of peak data
3
+ */
4
+ type Peaks = Int8Array | Int16Array;
5
+ /**
6
+ * Bits type - number of bits for peak data
7
+ */
8
+ type Bits = 8 | 16;
9
+ /**
10
+ * PeakData - result of peak extraction
11
+ */
12
+ interface PeakData {
13
+ /** Number of peaks extracted */
14
+ length: number;
15
+ /** Array of peak data for each channel */
16
+ data: Peaks[];
17
+ /** Bit depth of peak data */
18
+ bits: Bits;
19
+ }
20
+ /**
21
+ * Extract peaks from an AudioBuffer or Float32Array for waveform visualization
22
+ *
23
+ * @param source - AudioBuffer or Float32Array to extract peaks from
24
+ * @param samplesPerPixel - Number of audio samples per peak (default: 1000)
25
+ * @param isMono - Whether to merge channels to mono (default: true)
26
+ * @param cueIn - Start index for peak extraction (default: 0)
27
+ * @param cueOut - End index for peak extraction (default: source.length)
28
+ * @param bits - Bit depth for peak data: 8 or 16 (default: 16)
29
+ * @returns PeakData object containing peak arrays for each channel
30
+ */
31
+ declare function extractPeaksFromBuffer(source: AudioBuffer | Float32Array, samplesPerPixel?: number, isMono?: boolean, cueIn?: number, cueOut?: number, bits?: Bits): PeakData;
32
+
33
+ export { type Bits, type PeakData, type Peaks, extractPeaksFromBuffer as default };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Peaks type - represents a typed array of peak data
3
+ */
4
+ type Peaks = Int8Array | Int16Array;
5
+ /**
6
+ * Bits type - number of bits for peak data
7
+ */
8
+ type Bits = 8 | 16;
9
+ /**
10
+ * PeakData - result of peak extraction
11
+ */
12
+ interface PeakData {
13
+ /** Number of peaks extracted */
14
+ length: number;
15
+ /** Array of peak data for each channel */
16
+ data: Peaks[];
17
+ /** Bit depth of peak data */
18
+ bits: Bits;
19
+ }
20
+ /**
21
+ * Extract peaks from an AudioBuffer or Float32Array for waveform visualization
22
+ *
23
+ * @param source - AudioBuffer or Float32Array to extract peaks from
24
+ * @param samplesPerPixel - Number of audio samples per peak (default: 1000)
25
+ * @param isMono - Whether to merge channels to mono (default: true)
26
+ * @param cueIn - Start index for peak extraction (default: 0)
27
+ * @param cueOut - End index for peak extraction (default: source.length)
28
+ * @param bits - Bit depth for peak data: 8 or 16 (default: 16)
29
+ * @returns PeakData object containing peak arrays for each channel
30
+ */
31
+ declare function extractPeaksFromBuffer(source: AudioBuffer | Float32Array, samplesPerPixel?: number, isMono?: boolean, cueIn?: number, cueOut?: number, bits?: Bits): PeakData;
32
+
33
+ export { type Bits, type PeakData, type Peaks, extractPeaksFromBuffer as default };
package/dist/index.js ADDED
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ default: () => extractPeaksFromBuffer
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ function findMinMax(array) {
27
+ let min = Infinity;
28
+ let max = -Infinity;
29
+ for (let i = 0; i < array.length; i++) {
30
+ const curr = array[i];
31
+ if (min > curr) {
32
+ min = curr;
33
+ }
34
+ if (max < curr) {
35
+ max = curr;
36
+ }
37
+ }
38
+ return { min, max };
39
+ }
40
+ function convert(n, bits) {
41
+ const maxValue = Math.pow(2, bits - 1);
42
+ const v = n < 0 ? n * maxValue : n * (maxValue - 1);
43
+ return Math.max(-maxValue, Math.min(maxValue - 1, v));
44
+ }
45
+ function makeTypedArray(bits, length) {
46
+ switch (bits) {
47
+ case 8:
48
+ return new Int8Array(length);
49
+ case 16:
50
+ return new Int16Array(length);
51
+ }
52
+ }
53
+ function extractPeaks(channel, samplesPerPixel, bits) {
54
+ const chanLength = channel.length;
55
+ const numPeaks = Math.ceil(chanLength / samplesPerPixel);
56
+ const peaks = makeTypedArray(bits, numPeaks * 2);
57
+ for (let i = 0; i < numPeaks; i++) {
58
+ const start = i * samplesPerPixel;
59
+ const end = Math.min((i + 1) * samplesPerPixel, chanLength);
60
+ const segment = channel.subarray(start, end);
61
+ const extrema = findMinMax(segment);
62
+ const min = convert(extrema.min, bits);
63
+ const max = convert(extrema.max, bits);
64
+ peaks[i * 2] = min;
65
+ peaks[i * 2 + 1] = max;
66
+ }
67
+ return peaks;
68
+ }
69
+ function makeMono(channelPeaks, bits) {
70
+ const numChan = channelPeaks.length;
71
+ const weight = 1 / numChan;
72
+ const numPeaks = channelPeaks[0].length / 2;
73
+ const peaks = makeTypedArray(bits, numPeaks * 2);
74
+ for (let i = 0; i < numPeaks; i++) {
75
+ let min = 0;
76
+ let max = 0;
77
+ for (let c = 0; c < numChan; c++) {
78
+ min += weight * channelPeaks[c][i * 2];
79
+ max += weight * channelPeaks[c][i * 2 + 1];
80
+ }
81
+ peaks[i * 2] = min;
82
+ peaks[i * 2 + 1] = max;
83
+ }
84
+ return [peaks];
85
+ }
86
+ function extractPeaksFromBuffer(source, samplesPerPixel = 1e3, isMono = true, cueIn = 0, cueOut, bits = 16) {
87
+ if (bits !== 8 && bits !== 16) {
88
+ throw new Error("Invalid number of bits specified for peaks. Must be 8 or 16.");
89
+ }
90
+ let peaks = [];
91
+ if ("getChannelData" in source) {
92
+ const numChan = source.numberOfChannels;
93
+ const actualCueOut = cueOut ?? source.length;
94
+ for (let c = 0; c < numChan; c++) {
95
+ const channel = source.getChannelData(c);
96
+ const slice = channel.subarray(cueIn, actualCueOut);
97
+ peaks.push(extractPeaks(slice, samplesPerPixel, bits));
98
+ }
99
+ } else {
100
+ const actualCueOut = cueOut ?? source.length;
101
+ const slice = source.subarray(cueIn, actualCueOut);
102
+ peaks.push(extractPeaks(slice, samplesPerPixel, bits));
103
+ }
104
+ if (isMono && peaks.length > 1) {
105
+ peaks = makeMono(peaks, bits);
106
+ }
107
+ const numPeaks = peaks[0].length / 2;
108
+ return {
109
+ length: numPeaks,
110
+ data: peaks,
111
+ bits
112
+ };
113
+ }
114
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Peaks type - represents a typed array of peak data\n */\nexport type Peaks = Int8Array | Int16Array;\n\n/**\n * Bits type - number of bits for peak data\n */\nexport type Bits = 8 | 16;\n\n/**\n * PeakData - result of peak extraction\n */\nexport interface PeakData {\n /** Number of peaks extracted */\n length: number;\n /** Array of peak data for each channel */\n data: Peaks[];\n /** Bit depth of peak data */\n bits: Bits;\n}\n\n/**\n * Find minimum and maximum values in a typed array\n */\nfunction findMinMax(array: Float32Array): { min: number; max: number } {\n let min = Infinity;\n let max = -Infinity;\n\n for (let i = 0; i < array.length; i++) {\n const curr = array[i];\n if (min > curr) {\n min = curr;\n }\n if (max < curr) {\n max = curr;\n }\n }\n\n return { min, max };\n}\n\n/**\n * Convert a float peak to an integer based on bit depth\n */\nfunction convert(n: number, bits: Bits): number {\n const maxValue = Math.pow(2, bits - 1);\n const v = n < 0 ? n * maxValue : n * (maxValue - 1);\n return Math.max(-maxValue, Math.min(maxValue - 1, v));\n}\n\n/**\n * Create a typed array based on bit depth\n */\nfunction makeTypedArray(bits: Bits, length: number): Peaks {\n switch (bits) {\n case 8:\n return new Int8Array(length);\n case 16:\n return new Int16Array(length);\n }\n}\n\n/**\n * Extract peaks from a single audio channel\n */\nfunction extractPeaks(\n channel: Float32Array,\n samplesPerPixel: number,\n bits: Bits\n): Peaks {\n const chanLength = channel.length;\n const numPeaks = Math.ceil(chanLength / samplesPerPixel);\n\n // Create interleaved array of min,max\n const peaks = makeTypedArray(bits, numPeaks * 2);\n\n for (let i = 0; i < numPeaks; i++) {\n const start = i * samplesPerPixel;\n const end = Math.min((i + 1) * samplesPerPixel, chanLength);\n\n const segment = channel.subarray(start, end);\n const extrema = findMinMax(segment);\n const min = convert(extrema.min, bits);\n const max = convert(extrema.max, bits);\n\n peaks[i * 2] = min;\n peaks[i * 2 + 1] = max;\n }\n\n return peaks;\n}\n\n/**\n * Merge multiple channel peaks into a mono peak array\n */\nfunction makeMono(channelPeaks: Peaks[], bits: Bits): Peaks[] {\n const numChan = channelPeaks.length;\n const weight = 1 / numChan;\n const numPeaks = channelPeaks[0].length / 2;\n const peaks = makeTypedArray(bits, numPeaks * 2);\n\n for (let i = 0; i < numPeaks; i++) {\n let min = 0;\n let max = 0;\n\n for (let c = 0; c < numChan; c++) {\n min += weight * channelPeaks[c][i * 2];\n max += weight * channelPeaks[c][i * 2 + 1];\n }\n\n peaks[i * 2] = min;\n peaks[i * 2 + 1] = max;\n }\n\n // Return in array so channel number counts still work\n return [peaks];\n}\n\n/**\n * Extract peaks from an AudioBuffer or Float32Array for waveform visualization\n *\n * @param source - AudioBuffer or Float32Array to extract peaks from\n * @param samplesPerPixel - Number of audio samples per peak (default: 1000)\n * @param isMono - Whether to merge channels to mono (default: true)\n * @param cueIn - Start index for peak extraction (default: 0)\n * @param cueOut - End index for peak extraction (default: source.length)\n * @param bits - Bit depth for peak data: 8 or 16 (default: 16)\n * @returns PeakData object containing peak arrays for each channel\n */\nexport default function extractPeaksFromBuffer(\n source: AudioBuffer | Float32Array,\n samplesPerPixel: number = 1000,\n isMono: boolean = true,\n cueIn: number = 0,\n cueOut?: number,\n bits: Bits = 16\n): PeakData {\n if (bits !== 8 && bits !== 16) {\n throw new Error('Invalid number of bits specified for peaks. Must be 8 or 16.');\n }\n\n let peaks: Peaks[] = [];\n\n // Check if source is an AudioBuffer or Float32Array\n if ('getChannelData' in source) {\n // AudioBuffer\n const numChan = source.numberOfChannels;\n const actualCueOut = cueOut ?? source.length;\n\n for (let c = 0; c < numChan; c++) {\n const channel = source.getChannelData(c);\n const slice = channel.subarray(cueIn, actualCueOut);\n peaks.push(extractPeaks(slice, samplesPerPixel, bits));\n }\n } else {\n // Float32Array\n const actualCueOut = cueOut ?? source.length;\n const slice = source.subarray(cueIn, actualCueOut);\n peaks.push(extractPeaks(slice, samplesPerPixel, bits));\n }\n\n if (isMono && peaks.length > 1) {\n peaks = makeMono(peaks, bits);\n }\n\n const numPeaks = pe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C9B,YAAQ,SAAS,OAAO,IAAI;AAAA,EAC9B;AAEA,QAAM,WAAW,MAAM,CAAC,EAAE,SAAS;AAEnC,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,MAAM;AAAA,IACN;AAAA,EACF;AACF;","names":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,93 @@
1
+ // src/index.ts
2
+ function findMinMax(array) {
3
+ let min = Infinity;
4
+ let max = -Infinity;
5
+ for (let i = 0; i < array.length; i++) {
6
+ const curr = array[i];
7
+ if (min > curr) {
8
+ min = curr;
9
+ }
10
+ if (max < curr) {
11
+ max = curr;
12
+ }
13
+ }
14
+ return { min, max };
15
+ }
16
+ function convert(n, bits) {
17
+ const maxValue = Math.pow(2, bits - 1);
18
+ const v = n < 0 ? n * maxValue : n * (maxValue - 1);
19
+ return Math.max(-maxValue, Math.min(maxValue - 1, v));
20
+ }
21
+ function makeTypedArray(bits, length) {
22
+ switch (bits) {
23
+ case 8:
24
+ return new Int8Array(length);
25
+ case 16:
26
+ return new Int16Array(length);
27
+ }
28
+ }
29
+ function extractPeaks(channel, samplesPerPixel, bits) {
30
+ const chanLength = channel.length;
31
+ const numPeaks = Math.ceil(chanLength / samplesPerPixel);
32
+ const peaks = makeTypedArray(bits, numPeaks * 2);
33
+ for (let i = 0; i < numPeaks; i++) {
34
+ const start = i * samplesPerPixel;
35
+ const end = Math.min((i + 1) * samplesPerPixel, chanLength);
36
+ const segment = channel.subarray(start, end);
37
+ const extrema = findMinMax(segment);
38
+ const min = convert(extrema.min, bits);
39
+ const max = convert(extrema.max, bits);
40
+ peaks[i * 2] = min;
41
+ peaks[i * 2 + 1] = max;
42
+ }
43
+ return peaks;
44
+ }
45
+ function makeMono(channelPeaks, bits) {
46
+ const numChan = channelPeaks.length;
47
+ const weight = 1 / numChan;
48
+ const numPeaks = channelPeaks[0].length / 2;
49
+ const peaks = makeTypedArray(bits, numPeaks * 2);
50
+ for (let i = 0; i < numPeaks; i++) {
51
+ let min = 0;
52
+ let max = 0;
53
+ for (let c = 0; c < numChan; c++) {
54
+ min += weight * channelPeaks[c][i * 2];
55
+ max += weight * channelPeaks[c][i * 2 + 1];
56
+ }
57
+ peaks[i * 2] = min;
58
+ peaks[i * 2 + 1] = max;
59
+ }
60
+ return [peaks];
61
+ }
62
+ function extractPeaksFromBuffer(source, samplesPerPixel = 1e3, isMono = true, cueIn = 0, cueOut, bits = 16) {
63
+ if (bits !== 8 && bits !== 16) {
64
+ throw new Error("Invalid number of bits specified for peaks. Must be 8 or 16.");
65
+ }
66
+ let peaks = [];
67
+ if ("getChannelData" in source) {
68
+ const numChan = source.numberOfChannels;
69
+ const actualCueOut = cueOut ?? source.length;
70
+ for (let c = 0; c < numChan; c++) {
71
+ const channel = source.getChannelData(c);
72
+ const slice = channel.subarray(cueIn, actualCueOut);
73
+ peaks.push(extractPeaks(slice, samplesPerPixel, bits));
74
+ }
75
+ } else {
76
+ const actualCueOut = cueOut ?? source.length;
77
+ const slice = source.subarray(cueIn, actualCueOut);
78
+ peaks.push(extractPeaks(slice, samplesPerPixel, bits));
79
+ }
80
+ if (isMono && peaks.length > 1) {
81
+ peaks = makeMono(peaks, bits);
82
+ }
83
+ const numPeaks = peaks[0].length / 2;
84
+ return {
85
+ length: numPeaks,
86
+ data: peaks,
87
+ bits
88
+ };
89
+ }
90
+ export {
91
+ extractPeaksFromBuffer as default
92
+ };
93
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * Peaks type - represents a typed array of peak data\n */\nexport type Peaks = Int8Array | Int16Array;\n\n/**\n * Bits type - number of bits for peak data\n */\nexport type Bits = 8 | 16;\n\n/**\n * PeakData - result of peak extraction\n */\nexport interface PeakData {\n /** Number of peaks extracted */\n length: number;\n /** Array of peak data for each channel */\n data: Peaks[];\n /** Bit depth of peak data */\n bits: Bits;\n}\n\n/**\n * Find minimum and maximum values in a typed array\n */\nfunction findMinMax(array: Float32Array): { min: number; max: number } {\n let min = Infinity;\n let max = -Infinity;\n\n for (let i = 0; i < array.length; i++) {\n const curr = array[i];\n if (min > curr) {\n min = curr;\n }\n if (max < curr) {\n max = curr;\n }\n }\n\n return { min, max };\n}\n\n/**\n * Convert a float peak to an integer based on bit depth\n */\nfunction convert(n: number, bits: Bits): number {\n const maxValue = Math.pow(2, bits - 1);\n const v = n < 0 ? n * maxValue : n * (maxValue - 1);\n return Math.max(-maxValue, Math.min(maxValue - 1, v));\n}\n\n/**\n * Create a typed array based on bit depth\n */\nfunction makeTypedArray(bits: Bits, length: number): Peaks {\n switch (bits) {\n case 8:\n return new Int8Array(length);\n case 16:\n return new Int16Array(length);\n }\n}\n\n/**\n * Extract peaks from a single audio channel\n */\nfunction extractPeaks(\n channel: Float32Array,\n samplesPerPixel: number,\n bits: Bits\n): Peaks {\n const chanLength = channel.length;\n const numPeaks = Math.ceil(chanLength / samplesPerPixel);\n\n // Create interleaved array of min,max\n const peaks = makeTypedArray(bits, numPeaks * 2);\n\n for (let i = 0; i < numPeaks; i++) {\n const start = i * samplesPerPixel;\n const end = Math.min((i + 1) * samplesPerPixel, chanLength);\n\n const segment = channel.subarray(start, end);\n const extrema = findMinMax(segment);\n const min = convert(extrema.min, bits);\n const max = convert(extrema.max, bits);\n\n peaks[i * 2] = min;\n peaks[i * 2 + 1] = max;\n }\n\n return peaks;\n}\n\n/**\n * Merge multiple channel peaks into a mono peak array\n */\nfunction makeMono(channelPeaks: Peaks[], bits: Bits): Peaks[] {\n const numChan = channelPeaks.length;\n const weight = 1 / numChan;\n const numPeaks = channelPeaks[0].length / 2;\n const peaks = makeTypedArray(bits, numPeaks * 2);\n\n for (let i = 0; i < numPeaks; i++) {\n let min = 0;\n let max = 0;\n\n for (let c = 0; c < numChan; c++) {\n min += weight * channelPeaks[c][i * 2];\n max += weight * channelPeaks[c][i * 2 + 1];\n }\n\n peaks[i * 2] = min;\n peaks[i * 2 + 1] = max;\n }\n\n // Return in array so channel number counts still work\n return [peaks];\n}\n\n/**\n * Extract peaks from an AudioBuffer or Float32Array for waveform visualization\n *\n * @param source - AudioBuffer or Float32Array to extract peaks from\n * @param samplesPerPixel - Number of audio samples per peak (default: 1000)\n * @param isMono - Whether to merge channels to mono (default: true)\n * @param cueIn - Start index for peak extraction (default: 0)\n * @param cueOut - End index for peak extraction (default: source.length)\n * @param bits - Bit depth for peak data: 8 or 16 (default: 16)\n * @returns PeakData object containing peak arrays for each channel\n */\nexport default function extractPeaksFromBuffer(\n source: AudioBuffer | Float32Array,\n samplesPerPixel: number = 1000,\n isMono: boolean = true,\n cueIn: number = 0,\n cueOut?: number,\n bits: Bits = 16\n): PeakData {\n if (bits !== 8 && bits !== 16) {\n throw new Error('Invalid number of bits specified for peaks. Must be 8 or 16.');\n }\n\n let peaks: Peaks[] = [];\n\n // Check if source is an AudioBuffer or Float32Array\n if ('getChannelData' in source) {\n // AudioBuffer\n const numChan = source.numberOfChannels;\n const actualCueOut = cueOut ?? source.length;\n\n for (let c = 0; c < numChan; c++) {\n const channel = source.getChannelData(c);\n const slice = channel.subarray(cueIn, actualCueOut);\n peaks.push(extractPeaks(slice, samplesPerPixel, bits));\n }\n } else {\n // Float32Array\n const actualCueOut = cueOut ?? source.length;\n const slice = source.subarray(cueIn, actualCueOut);\n peaks.push(extractPeaks(slice, samplesPerPixel, bits));\n }\n\n if (isMono && peaks.length > 1) {\n peaks = makeMono(peaks, bits);\n }\n\n const numPeaks = peaks[0].length / 2;\n\n return {\n length: numPeaks,\n data: peaks,\n bits: bits,\n };\n}\n"],"mappings":";AAyBA,SAAS,WAAW,OAAmD;AACrE,MAAI,MAAM;AACV,MAAI,MAAM;AAEV,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,UAAM,OAAO,MAAM,CAAC;AACpB,QAAI,MAAM,MAAM;AACd,YAAM;AAAA,IACR;AACA,QAAI,MAAM,MAAM;AACd,YAAM;AAAA,IACR;AAAA,EACF;AAEA,SAAO,EAAE,KAAK,IAAI;A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AE,IAAI,CAAC;AACrC,aAAO,SAAS,aAAa,CAAC,EAAE,IAAI,IAAI,CAAC;AAAA,IAC3C;AAEA,UAAM,IAAI,CAAC,IAAI;AACf,UAAM,IAAI,IAAI,CAAC,IAAI;AAAA,EACrB;AAGA,SAAO,CAAC,KAAK;AACf;AAae,SAAR,uBACL,QACA,kBAA0B,KAC1B,SAAkB,MAClB,QAAgB,GAChB,QACA,OAAa,IACH;AACV,MAAI,SAAS,KAAK,SAAS,IAAI;AAC7B,UAAM,IAAI,MAAM,8DAA8D;AAAA,EAChF;AAEA,MAAI,QAAiB,CAAC;AAGtB,MAAI,oBAAoB,QAAQ;AAE9B,UAAM,UAAU,OAAO;AACvB,UAAM,eAAe,UAAU,OAAO;AAEtC,aAAS,IAAI,GAAG,IAAI,SAAS,KAAK;AAChC,YAAM,UAAU,OAAO,eAAe,CAAC;AACvC,YAAM,QAAQ,QAAQ,SAAS,OAAO,YAAY;AAClD,YAAM,KAAK,aAAa,OAAO,iBAAiB,IAAI,CAAC;AAAA,IACvD;AAAA,EACF,OAAO;AAEL,UAAM,eAAe,UAAU,OAAO;AACtC,UAAM,QAAQ,OAAO,SAAS,OAAO,YAAY;AACjD,UAAM,KAAK,aAAa,OAAO,iBAAiB,IAAI,CAAC;AAAA,EACvD;AAEA,MAAI,UAAU,MAAM,SAAS,GAAG;AAC9B,YAAQ,SAAS,OAAO,IAAI;AAAA,EAC9B;AAEA,QAAM,WAAW,MAAM,CAAC,EAAE,SAAS;AAEnC,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,MAAM;AAAA,IACN;AAAA,EACF;AACF;","names":[]}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@waveform-playlist/webaudio-peaks",
3
+ "version": "1.0.0",
4
+ "description": "Small library to extract peaks from an array of audio samples or an AudioBuffer from the WebAudio API.",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "sideEffects": false,
16
+ "keywords": [
17
+ "webaudio",
18
+ "peaks",
19
+ "waveform",
20
+ "visualization",
21
+ "waveform-playlist"
22
+ ],
23
+ "author": "Naomi Aro <naomiaro@gmail.com> (https://naomiaro.github.io/)",
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/naomiaro/waveform-playlist.git",
28
+ "directory": "packages/webaudio-peaks"
29
+ },
30
+ "homepage": "https://naomiaro.github.io/waveform-playlist",
31
+ "bugs": {
32
+ "url": "https://github.com/naomiaro/waveform-playlist/issues"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "README.md"
37
+ ],
38
+ "devDependencies": {
39
+ "tsup": "^8.0.1",
40
+ "typescript": "^5.3.3"
41
+ },
42
+ "scripts": {
43
+ "build": "tsup",
44
+ "dev": "tsup --watch",
45
+ "typecheck": "tsc --noEmit"
46
+ }
47
+ }