Skip to content

Instantly share code, notes, and snippets.

@deveedutta
Last active February 19, 2019 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deveedutta/1ae1f0b1a668f742c90fd371fe134402 to your computer and use it in GitHub Desktop.
Save deveedutta/1ae1f0b1a668f742c90fd371fe134402 to your computer and use it in GitHub Desktop.
oscillator component
import { h, Component } from 'preact';
import style from './style';
const audioContext = new (window.AudioContext || window.webkitAudioContext);
export default class Oscillator extends Component {
play() {
if( this.oscillator ) return;
this.oscillator = audioContext.createOscillator();
this.oscillator.type = this.props.type || 'sine';
this.oscillator.frequency.value = this.props.frequency || 329.63; //E(1) is default
this.oscillator.connect(audioContext.destination);
this.oscillator.start();
}
stop() {
if(!this.oscillator) return;
this.oscillator.stop();
this.oscillator = null;
}
render() {
return (
<div class={style.home}>
<table>
<tr>
<td>-</td>
<td><strong>{this.props.note}</strong></td>
<td>{this.props.frequency}Hz</td>
<td><button onClick={this.play.bind(this)}>start</button></td>
<td><button onClick={this.stop.bind(this)}>stop</button></td>
</tr>
</table>
</div>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment