Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bend2/effs/audio_open.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function audio_open(rate) {
if (rate < 8000 || rate > 192000) {
return io_fail(22);
}
return io_done({ rate, queued: 0, at: Date.now() });
return io_done({ rate, queued: 0, at: performance.now() });
}

io_eff(CID(Audio.open), audio_open);
2 changes: 1 addition & 1 deletion bend2/effs/audio_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function audio_write(audio, samples) {
for (let xs = samples; xs.$ === CID(Con); xs = xs.tail) {
n += 1;
}
const now = Date.now();
const now = performance.now();
audio.queued = Math.max(0,
audio.queued - (now - audio.at) * audio.rate / 1000);
audio.at = now;
Expand Down
22 changes: 22 additions & 0 deletions tests/io/audio_monotonic_clock.bend
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# A backward wall clock must not add frames to the silent audio ring.
# JS only: the native ring drains by the device, without a wall clock.
import Base

def wall_clock_backwards(reset: Bool) -> IO(Unit):
import "./audio_monotonic_clock.js"

def report(pair: Audio & U32) -> IO(Unit):
(audio, queued) = pair
do IO<Unit>:
Unit <- Audio.close(audio)
IO.print(U32.show(queued))

def main() -> IO(Unit):
do IO<Unit>:
Unit <- wall_clock_backwards(False{})
a : Audio <- IO.try(Audio, Audio.open(48000))
pair : Audio & U32 <- Audio.write(a, [])
Unit <- wall_clock_backwards(True{})
report(pair)

#|0
20 changes: 20 additions & 0 deletions tests/io/audio_monotonic_clock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// One second backwards between opening the ring and its empty write.
const wall_now = Date.now;

function wall_clock_backwards(reset) {
if (reset) {
Date.now = wall_now;
} else {
let first = true;
Date.now = () => {
if (first) {
first = false;
return 1000;
}
return 0;
};
}
return { $: CID(Unit) };
}

io_eff(CID(wall_clock_backwards), wall_clock_backwards);