Skip to content

Commit f9de3c7

Browse files
committed
Feature/Parity: Support beep
1 parent e2f4d12 commit f9de3c7

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

compiler/shared/src/main/scala/SimplePrims.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ object SimplePrims {
445445
case _: prim.etc._exportview => "ImportExportPrims.exportView"
446446
case _: prim.etc._exportworld => "ImportExportPrims.exportWorld"
447447
case _: prim.etc._wait => "Prims.wait"
448+
case _: prim.etc._beep => "Prims.beep"
448449

449450
case _: prim.etc._importdrawing => "ImportExportPrims.importDrawing"
450451

compiler/shared/src/test/scala/CompiledModelTest.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class CompiledModelTest extends AnyFunSuite {
4747
test("model with unimplemented primitives can be compiled") {
4848
val unimplemented =
4949
s"""|to go
50-
| beep
50+
| export-interface "file.png"
5151
|end""".stripMargin
5252
testModelCode(unimplemented, isInvalid, CompilerFlags(generateUnimplemented = false))
5353
testModelCode(unimplemented, isValid, CompilerFlags(generateUnimplemented = true))

engine/src/main/coffee/engine/prim/prims.coffee

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ module.exports =
8686
notImplemented('display', undefined)
8787
return
8888

89+
# () => Unit
90+
beep: () ->
91+
AudioCtx = (typeof AudioContext != 'undefined' and AudioContext) or
92+
(typeof webkitAudioContext != 'undefined' and webkitAudioContext)
93+
if AudioCtx
94+
ctx = new AudioCtx()
95+
oscillator = ctx.createOscillator()
96+
gain = ctx.createGain()
97+
oscillator.connect(gain)
98+
gain.connect(ctx.destination)
99+
oscillator.frequency.value = 880
100+
gain.gain.setValueAtTime(0.3, ctx.currentTime)
101+
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.2)
102+
oscillator.start(ctx.currentTime)
103+
oscillator.stop(ctx.currentTime + 0.2)
104+
else
105+
@_printPrims.print('BEEP')
106+
return
107+
89108
# (Any) => Boolean
90109
ifElseValueBooleanCheck: (b) ->
91110
@booleanCheck(b, "IFELSE-VALUE")

0 commit comments

Comments
 (0)