GitXplorerGitXplorer
n

react-tone

public
2 stars
0 forks
1 issues

Commits

List of commits on branch master.
Verified
e5165290d3c8f0bc865103a1a4624d06cf63f813

Merge pull request #7 from newyork-anthonyng/feature/an/fix-start-error

nnewyork-anthonyng committed 7 years ago
Unverified
997b2019f0a6a767d824175821384eee7216c998

fix(oscillator): Fix oscillator crash

nnewyork-anthonyng committed 7 years ago
Verified
95013acc9090232b4cd68a68c36b660d76e845eb

Merge pull request #6 from newyork-anthonyng/feature/an/volume

nnewyork-anthonyng committed 7 years ago
Unverified
299aa8c4a87913e9c23821c1cac8b0cba07ee265

feat(volume): Add volume

nnewyork-anthonyng committed 7 years ago
Verified
b8a1e6ebc39814f3f2f6b56a4cfbe2a9996cff2d

Merge pull request #4 from newyork-anthonyng/feature/an/dummy-change

nnewyork-anthonyng committed 7 years ago
Unverified
634cb876d315e5bb9b778cb670b82e49eb39f72c

fix(npm): Dummy change to force a publish

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.