GitXplorerGitXplorer
n

react-tone

public
2 stars
0 forks
1 issues

Commits

List of commits on branch master.
Verified
13b765e2a3a08dc92367ca9b5371c9df9dea423e

Merge pull request #3 from newyork-anthonyng/feature/an/peer-dependencies

nnewyork-anthonyng committed 7 years ago
Unverified
ffde47346995ab7043e4a4ea90bfe400d9bd025f

build(webpack): Remove React from bundle

nnewyork-anthonyng committed 7 years ago
Unverified
c2a7f88480d5794b81cdf014289bebb35aa4d36a

chore(semantic-release): Add semantic-release

nnewyork-anthonyng committed 7 years ago
Unverified
9a690c7b61a8561c9ef7d8e846fab76d64c174ab

Republish package

nnewyork-anthonyng committed 7 years ago
Verified
45cd95d062282e829d382e3c52095f5544afa2d3

Merge pull request #2 from newyork-anthonyng/feature/an/add-code-sandbox

nnewyork-anthonyng committed 7 years ago
Unverified
5fc84ac0f7fda84e3e5f22992f32e049a133966b

Update README.md with CodeSandbox demo

nnewyork-anthonyng committed 7 years ago

README

The README file for this repository.

Travis build status Codecov branch npm downloads MIT License

gzip size size

Maintainability PRs Welcome

react-tone 🎶

A React component that plays a tone from an oscillator.

Getting started

npm install --save react-tone

Why?

react-tone is a component that uses a OscillatorNode to play a sound at a given frequency and length.

You can pass react-tone an instance of AudioContext. This solves an issue playing sounds on iOS devices. On iOS devices, a sound is muted until it's created from an interaction event. Using an AudioContext to play a dummy buffer fixes this. See this issue for more information.

API

Props Description Default
play (Boolean) Play a tone false
length (Number) How long the tone should play (in seconds) 0.05
frequency (Number) The frequency of the tone 400
volume (Number) The volume of the tone (0 being silent, and 1 being the loudest) 1
audioContext (Object) An instance of AudioContext new AudioContext()
onStop (Function) Callback function to be called when the tone has finished playing () => {}
import React, { Component } from "react";
import Tone from "react-tone";

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      isTonePlaying: false
    };

    this.audioContext = undefined;
    this.iosAudioContextUnlocked = false;
  }

  componentDidMount() {
    this.audioContext = new AudioContext();
  }

  handleClick = () => {
    if (!this.iosAudioContextUnlocked) this.playEmptyBuffer();

    this.setState({ isTonePlaying: true });
  }

  playEmptyBuffer = () => {
    // start an empty buffer with an instance of AudioContext
    const buffer = this.audioContext.createBuffer(1, 1, 22050);
    const node = this.audioContext.createBufferSource();
    node.buffer = buffer;
    node.start(0);
    this.iosAudioContextUnlocked = true;
  }

  handleToneStop = () => {
    this.setState({ isTonePlaying: false });
  }

  render() {
    // Pass the same instance of AudioContext that played an empty buffer to <Tone />
    return (
      <div>
        <button onClick={this.handleClick}>Play Tone</button>
        <Tone
          audioContext={this.audioContext}
          play={this.state.isTonePlaying}
          frequency={500}
          volume={0.8}
          length={2}
          onStop={this.handleToneStop}
        />
      </div>
    );
  }
}

Demo

See this CodeSandbox demo.