Skip to content

Commit

Permalink
safari: no more static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkaufman committed Feb 14, 2021
1 parent 16fe3b6 commit 0e8e879
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 106 deletions.
29 changes: 14 additions & 15 deletions html/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
import {check} from './lib.js';

import {ServerConnection} from './net.js';
import {AudioChunk, CompressedAudioChunk, AudioChunkBase, PlaceholderChunk, concat_chunks, ClockInterval, ClientClockReference, ServerClockReference} from './audiochunk.js';
import {
AudioChunk,
CompressedAudioChunk,
AudioChunkBase,
thaw_audio_chunk_base,
PlaceholderChunk,
thaw_placeholder_chunk,
concat_chunks,
ClockInterval,
ClientClockReference,
ServerClockReference
} from './audiochunk.js';

// Work around some issues related to caching and error reporting
// by forcing this to load up top, before we try to 'addModule' it.
Expand Down Expand Up @@ -1531,23 +1542,11 @@ function concat_typed_arrays(arrays, _constructor) {
return result;
}

/*
function rebless(o) {
if (o.type !== undefined) {
Object.setPrototypeOf(o, eval(o.type).prototype);
}
if (o.rebless) {
o.rebless();
}
return o;
}
*/

function thaw(o) {
if (o.type == "PlaceholderChunk") {
o = PlaceholderChunk.thaw(o);
o = thaw_placeholder_chunk(o);
} else if (o.type == "AudioChunk" || o.type == "CompressedAudioChunk") {
o = AudioChunkBase.thaw(o);
o = thaw_audio_chunk_base(o);
}
return o;
}
70 changes: 29 additions & 41 deletions html/audio-worklet.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,12 @@ class ClockReference {
equals(other) {
return this.side == other.side && this.sample_rate == other.sample_rate;
}
}

static thaw(o) {
var rv = new ClockReference({
sample_rate: o.sample_rate
});
return rv;
}
function thaw_clock_reference(o) {
return new ClockReference({
sample_rate: o.sample_rate
});
}

class ClockInterval {
Expand All @@ -86,20 +85,18 @@ class ClockInterval {
get start() {
return this.end - this.length;
}

static thaw(o) {
if (o === undefined) {
return o;
}
var rv = new ClockInterval({
reference: ClockReference.thaw(o.reference),
end: o.end,
length: o.length
});
return rv;
}
}

function thaw_clock_interval(o) {
if (o === undefined) {
return o;
}
return new ClockInterval({
reference: thaw_clock_reference(o.reference),
end: o.end,
length: o.length
});
}
class AudioChunk {
constructor({ data, interval }) {
check(data !== undefined && interval !== undefined, "Must provide data and interval as named arguments");
Expand All @@ -124,14 +121,12 @@ class AudioChunk {
get length_seconds() { return this.interval.length_seconds; }
get reference() { return this.interval.reference; }
get sample_rate() { return this.interval.sample_rate; }

static thaw(o) {
var rv = new AudioChunk({
data: o.data,
interval: ClockInterval.thaw(o.interval),
});
return rv;
}
}
function thaw_audio_chunk(o) {
return new AudioChunk({
data: o.data,
interval: thaw_clock_interval(o.interval),
});
}

class PlaceholderChunk {
Expand Down Expand Up @@ -161,24 +156,17 @@ class PlaceholderChunk {
get end() { return this.interval.end; }
get length_seconds() { return this.interval.length_seconds; }
get sample_rate() { return this.reference.sample_rate; }

static thaw(o) {
var rv = new PlaceholderChunk({
reference: ClockReference.thaw(o.reference),
length: o.length,
interval: ClockInterval.thaw(o.interval),
});
return rv;
}
}

// XXX end copy-pasted imports

function thaw(o) {
// This is the only type of object we will ever be sent.
return AudioChunk.thaw(o);
function thaw_placeholder_chunk(o) {
return new PlaceholderChunk({
reference: thaw_clock_reference(o.reference),
length: o.length,
interval: thaw_clock_interval(o.interval),
});
}

// XXX end copy-pasted imports
const FRAME_SIZE = 128; // by Web Audio API spec

class ClockedRingBuffer {
Expand Down Expand Up @@ -678,7 +666,7 @@ class Player extends AudioWorkletProcessor {
return;
}

var chunk = thaw(msg.chunk);
var chunk = thaw_audio_chunk(msg.chunk);
this.play_buffer.write_chunk(chunk);
// console.debug("VERYSPAM", "new play buffer:", this.play_buffer);
}
Expand Down
93 changes: 43 additions & 50 deletions html/audiochunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ export class ClockReference {
equals(other) {
return this.side == other.side && this.sample_rate == other.sample_rate;
}

static thaw(o) {
var rv;
if (o.type == "ServerClockReference") {
rv = new ServerClockReference({
sample_rate: o.sample_rate
});
} else {
rv = new ClientClockReference({
sample_rate: o.sample_rate
});
}
return rv;
}
}

function thaw_clock_reference(o) {
if (o.type == "ServerClockReference") {
return new ServerClockReference({
sample_rate: o.sample_rate
});
} else {
return new ClientClockReference({
sample_rate: o.sample_rate
});
}
}

export class ServerClockReference extends ClockReference {
get side() { return CLOCK_SERVER; }
}
Expand Down Expand Up @@ -63,18 +60,16 @@ export class ClockInterval {
get start() {
return this.end - this.length;
}

static thaw(o) {
if (o === undefined) {
return o;
}
var rv = new ClockInterval({
reference: ClockReference.thaw(o.reference),
end: o.end,
length: o.length
});
return rv;
}
}
function thaw_clock_interval(o) {
if (o === undefined) {
return o;
}
return new ClockInterval({
reference: thaw_clock_reference(o.reference),
end: o.end,
length: o.length
});
}

export class AudioChunkBase {
Expand All @@ -99,24 +94,23 @@ export class AudioChunkBase {
get length_seconds() { return this.interval.length_seconds; }
get reference() { return this.interval.reference; }
get sample_rate() { return this.interval.sample_rate; }
}

static thaw(o) {
var rv;
if (o.type == "AudioChunk") {
rv = new AudioChunk({
data: o.data,
interval: ClockInterval.thaw(o.interval),
});
} else {
rv = new CompressedAudioChunk({
data: o.data,
interval: ClockInterval.thaw(o.interval),
});
}
return rv;
}
export function thaw_audio_chunk_base(o) {
if (o.type == "AudioChunk") {
return new AudioChunk({
data: o.data,
interval: thaw_clock_interval(o.interval),
});
} else {
return new CompressedAudioChunk({
data: o.data,
interval: thaw_clock_interval(o.interval),
});
}
}


// This would more correctly be named UncompressedAudioChunk, but the shorter name is nicer.
export class AudioChunk extends AudioChunkBase {
constructor({ data, interval }) {
Expand Down Expand Up @@ -164,15 +158,14 @@ export class PlaceholderChunk {
get end() { return this.interval.end; }
get length_seconds() { return this.interval.length_seconds; }
get sample_rate() { return this.reference.sample_rate; }
}

static thaw(o) {
var rv = new PlaceholderChunk({
reference: ClockReference.thaw(o.reference),
length: o.length,
interval: ClockInterval.thaw(o.interval),
});
return rv;
}
export function thaw_placeholder_chunk(o) {
return new PlaceholderChunk({
reference: thaw_clock_reference(o.reference),
length: o.length,
interval: thaw_clock_interval(o.interval),
});
}

function concat_typed_arrays(arrays, _constructor) {
Expand Down

0 comments on commit 0e8e879

Please sign in to comment.