https://github.com/ftsf/nim-webaudio
Web Audio API for Nim Javascript
https://github.com/ftsf/nim-webaudio
Last synced: over 1 year ago
JSON representation
Web Audio API for Nim Javascript
- Host: GitHub
- URL: https://github.com/ftsf/nim-webaudio
- Owner: ftsf
- Created: 2017-05-15T03:42:21.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-02-25T21:07:30.000Z (over 3 years ago)
- Last Synced: 2025-03-23T18:50:56.254Z (over 1 year ago)
- Language: JavaScript
- Size: 48.8 KB
- Stars: 15
- Watchers: 2
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Nim Wrapper for the Web Audio API
Example code for creating an oscillator that responds to clicks on the screen and fades and changes pitch over time.
[Try it!](https://cdn.rawgit.com/ftsf/nim-webaudio/4588fab0/tests/test.html)
```nim
import webaudio
import dom
var ctx = newAudioContext()
var gain = ctx.createGain()
gain.gain.value = 0.5
gain.connect(ctx.destination)
var osc = ctx.createOscillator()
osc.type = "square"
osc.frequency.value = 440.0
osc.connect(gain)
osc.start()
discard window.setInterval(
proc() =
if gain.gain.value > 0.0:
gain.gain.value -= 0.01
if gain.gain.value < 0.0:
gain.gain.value = 0.0
, 30)
discard window.setInterval(
proc() =
osc.frequency.value *= 0.5
if osc.frequency.value < 20.0:
gain.gain.value = 0.0
, 60)
window.addEventListener("mousedown") do(e: Event):
osc.stop()
osc = ctx.createOscillator()
osc.type = "square"
osc.frequency.value = 880.0
osc.connect(gain)
gain.gain.value = 0.5
osc.start()
```