Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sound API: beep #135

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions logo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3693,4 +3693,52 @@ function LogoInterpreter(turtle, stream, savehook)
throw new Error("Internal error: Unbound procedure");
return value;
});

//----------------------------------------------------------------------
//
// Sound supports: basic sound / basic music / web speech
//
//----------------------------------------------------------------------

if ('AudioContext' in window) {
var audioCtx = new window.AudioContext();
var soundqueue = [];

function oscillate(frequency, duration) {
soundqueue.push([frequency, duration]);
}

function soundgo() {
if (soundqueue.length > 0) {
var sound = soundqueue.shift();
var frequency = sound[0], duration = sound[1];
var oscillator = audioCtx.createOscillator();
oscillator.type = 'sine';
oscillator.frequency.value = frequency; // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.onended = function() {
oscillator.disconnect();
if (soundqueue.length > 0) {
var next = soundqueue.shift();
oscillate(next[0], next[1]);
}
}
oscillator.start();
setTimeout(
function() {
oscillator.stop();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the oscillator leak here since it's not detached? Or is "ended" guaranteed to fire?

if (soundqueue.length > 0) {
soundgo();
}
}, duration);
}
}

// Play sound with a frequency and a time interval
def("beep", function(frequency, duration) {
oscillate(aexpr(frequency), aexpr(duration));
soundgo();
});

}
}
8 changes: 8 additions & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,7 @@ QUnit.test("Arity of Primitives", function(t) {
['ashift', [2, 2, 2]],
['back', [1, 1, 1]],
['background', [0, 0, 0]],
['beep', [2, 2, 2]],
['before?', [2, 2, 2]],
['beforep', [2, 2, 2]],
['bf', [1, 1, 1]],
Expand Down Expand Up @@ -2313,3 +2314,10 @@ QUnit.test("Arity of Primitives", function(t) {
this.assert_equals('arity "' + proc, arity);
}.bind(this));
});

QUnit.test("Sound API", function(t) {
//
// Sound API
//
this.assert_equals('beep 220 1', undefined);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding tests!

Can you add documentation as well, in language.html?

});