Allow DataView as AllowSharedBufferSource

This commit is contained in:
Vanilagy
2025-07-07 10:40:28 +02:00
parent 39df195ada
commit f78fc697cd
4 changed files with 11 additions and 12 deletions
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "mediabunny", "name": "mediabunny",
"version": "1.0.0", "version": "1.0.1",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "mediabunny", "name": "mediabunny",
"version": "1.0.0", "version": "1.0.1",
"license": "MPL-2.0", "license": "MPL-2.0",
"dependencies": { "dependencies": {
"@types/dom-mediacapture-transform": "^0.1.11", "@types/dom-mediacapture-transform": "^0.1.11",
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "mediabunny", "name": "mediabunny",
"author": "Vanilagy", "author": "Vanilagy",
"version": "1.0.0", "version": "1.0.1",
"description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.", "description": "Pure TypeScript media toolkit for reading, writing, and converting media files, directly in the browser.",
"type": "module", "type": "module",
"main": "./dist/mediabunny.js", "main": "./dist/mediabunny.js",
+7 -4
View File
@@ -134,7 +134,9 @@ export const writeBits = (bytes: Uint8Array, start: number, end: number, value:
}; };
export const toUint8Array = (source: AllowSharedBufferSource): Uint8Array => { export const toUint8Array = (source: AllowSharedBufferSource): Uint8Array => {
if (source instanceof ArrayBuffer) { if (source instanceof Uint8Array) {
return source;
} else if (source instanceof ArrayBuffer) {
return new Uint8Array(source); return new Uint8Array(source);
} else { } else {
return new Uint8Array(source.buffer, source.byteOffset, source.byteLength); return new Uint8Array(source.buffer, source.byteOffset, source.byteLength);
@@ -142,7 +144,9 @@ export const toUint8Array = (source: AllowSharedBufferSource): Uint8Array => {
}; };
export const toDataView = (source: AllowSharedBufferSource) => { export const toDataView = (source: AllowSharedBufferSource) => {
if (source instanceof ArrayBuffer) { if (source instanceof DataView) {
return source;
} else if (source instanceof ArrayBuffer) {
return new DataView(source); return new DataView(source);
} else { } else {
return new DataView(source.buffer, source.byteOffset, source.byteLength); return new DataView(source.buffer, source.byteOffset, source.byteLength);
@@ -202,11 +206,10 @@ export const colorSpaceIsComplete = (
}; };
export const isAllowSharedBufferSource = (x: unknown) => { export const isAllowSharedBufferSource = (x: unknown) => {
// Quite a mouthful:
return ( return (
x instanceof ArrayBuffer x instanceof ArrayBuffer
|| (typeof SharedArrayBuffer !== 'undefined' && x instanceof SharedArrayBuffer) || (typeof SharedArrayBuffer !== 'undefined' && x instanceof SharedArrayBuffer)
|| (ArrayBuffer.isView(x) && !(x instanceof DataView)) || ArrayBuffer.isView(x)
); );
}; };
+1 -5
View File
@@ -890,7 +890,6 @@ export class AudioSample {
} }
return new AudioData({ return new AudioData({
format: this.format, format: this.format,
sampleRate: this.sampleRate, sampleRate: this.sampleRate,
numberOfFrames: this.numberOfFrames, numberOfFrames: this.numberOfFrames,
@@ -900,11 +899,9 @@ export class AudioSample {
}); });
} else { } else {
const data = new ArrayBuffer(this.allocationSize({ planeIndex: 0, format: this.format })); const data = new ArrayBuffer(this.allocationSize({ planeIndex: 0, format: this.format }));
this.copyTo(data, { planeIndex: 0, format: this.format });
this.copyTo(new DataView(data), { planeIndex: 0, format: this.format });
return new AudioData({ return new AudioData({
format: this.format, format: this.format,
sampleRate: this.sampleRate, sampleRate: this.sampleRate,
numberOfFrames: this.numberOfFrames, numberOfFrames: this.numberOfFrames,
@@ -916,7 +913,6 @@ export class AudioSample {
} }
} else { } else {
return new AudioData({ return new AudioData({
format: this.format, format: this.format,
sampleRate: this.sampleRate, sampleRate: this.sampleRate,
numberOfFrames: this.numberOfFrames, numberOfFrames: this.numberOfFrames,