Skip to content

Commit

Permalink
feat(noise.js):Add webaudio noise class
Browse files Browse the repository at this point in the history
  • Loading branch information
196Ikuchil committed Mar 17, 2020
1 parent 2a74ef9 commit 49a7ab4
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
7 changes: 5 additions & 2 deletions main.js
@@ -1,4 +1,5 @@
import Oscillator from './src/nes/webaudio/oscillator.js'
import Noise from './src/nes/webaudio/noise.js'
let buf = null

const convertKeyCode = (key) => {
Expand Down Expand Up @@ -39,13 +40,15 @@ const startArrayBuf = (arrayBuf) => {
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
if (Module.NES) {
Module.NES.oscs.forEach(o => o.close());
Module.NES.oscs.forEach(o => o.close())
Module.NES.noise.close()
}
Module.NES = {
ctx,
canvas,
image: ctx.createImageData(256, 240),
oscs: [new Oscillator(), new Oscillator()],
noise: new Noise(),
}
canvas.width = 256
canvas.height = 240
Expand All @@ -62,7 +65,7 @@ const startArrayBuf = (arrayBuf) => {
}

// called from html
export const start = async (rom = './roms/apu/square_timer_div2.nes') => {
export const start = async (rom = './roms/apu/sweep_cutoff.nes') => {
const res = await fetch(rom);
const arrayBuf = await res.arrayBuffer();
startArrayBuf(arrayBuf);
Expand Down
12 changes: 12 additions & 0 deletions src/externs/lib.js
Expand Up @@ -21,5 +21,17 @@ mergeInto(LibraryManager.library, {
},
set_oscillator_duty: function (index, duty) {
Module.NES.oscs[index].setDuty(duty)
},
set_noise_frequency: function(freq) {
Module.NES.noise.setFrequency(freq)
},
set_noise_volume: function (volume) {
Module.NES.noise.setVolume(volume)
},
start_noise: function () {
Module.NES.noise.start()
},
stop_noise: function () {
Module.NES.noise.stop()
}
});
55 changes: 55 additions & 0 deletions src/nes/webaudio/noise.js
@@ -0,0 +1,55 @@
export default class Noise {
constructor () {
try {
const AudioContext = window.AudioContext || window.webkitAudioContext
this.context = new AudioContext();
} catch (e) {
throw new Error('Web Audio isn\'t supported in this browser!');
}
this.createSource()
this.playing = false
}

createSource() {
const node = this.context.createBufferSource()
const buffer = this.context.createBuffer(1, this.context.sampleRate, this.context.sampleRate)
const data = buffer.getChannelData(0)
for (let i = 0; i < this.context.sampleRate; i++) {
data[i] = Math.random();
}
node.buffer = buffer
node.loop = true
this.gain = this.context.createGain()
this.gain.gain.value = 0.01
node.connect(this.gain)
this.bandpass = this.context.createBiquadFilter()
this.gain.connect(this.bandpass)
this.bandpass.connect(this.context.destination)
this.source = node
this.setVolume(0)
}

setVolume (v) {
v = Math.max(0, Math.min(1, v))
this.gain.gain.value = v
}

setFrequency (freq) {
this.bandpass.frequency.value = freq > 22050 ? 22050 : freq
}

start () {
if (!this.playing) {
this.playing = true
this.source.start(0)
}
}

stop() {
if (this.playing) {
this.playing = false
this.source.stop()
this.createSource()
}
}
}

0 comments on commit 49a7ab4

Please sign in to comment.