Git Product home page Git Product logo

tone-stream's Introduction

tone-stream

Overview

A simple node.js tone stream library.

You can specify frequencies to be played by adding items in the format:

  [NUMBER_OF_SAMPLES, FREQUENCY]

or DTMF tones:

  [NUMBER_OF_SAMPLES, 'DTMF:ID']

You can add multiple of such tones and they will be enequeued and played in order.

Installation

npm i tone-stream

Sample usage

Playing some musical notes:

const { ToneStream } = require('tone-stream')

const Speaker = require('speaker')

const sampleRate = 8000

const format = {
  sampleRate,
  bitDepth: 16,
  channels: 1
}

const ts = new ToneStream(format)

const s = new Speaker(format)

var ns = 0
ns += ts.add([1000, 261.63]) // C4
ns += ts.add([1000, 296.33]) // D4
ns += ts.add([1000, 329.63]) // E4

var duration = ns / sampleRate * 1000

ts.pipe(s)

setTimeout(() => {
  console.log("done")
  process.exit(0)
}, duration)

Playing some DTMF tones:

const { ToneStream } = require('tone-stream')

const Speaker = require('speaker')

const sampleRate = 8000

const format = {
  sampleRate,
  bitDepth: 16,
  channels: 1
}

const ts = new ToneStream(format)

const s = new Speaker(format)

var ns = 0

ns += ts.add([1000, 'DTMF:1'])
ns += ts.add([500, 's'])
ns += ts.add([1000, 'DTMF:2'])
ns += ts.add([500, 's'])
ns += ts.add([1000, 'DTMF:3'])
ns += ts.add([500, 's'])

var duration = ns / sampleRate * 1000

ts.pipe(s)

setTimeout(() => {
  console.log("done")
  process.exit(0)
}, duration)

Using some helper functions to add miscelaneous tones

const { ToneStream, utils } = require('tone-stream')

const Speaker = require('speaker')

const _ = require('lodash')

const SAMPLE_RATE = 8000

const format = {
        sampleRate: SAMPLE_RATE,
        bitDepth: 16,
        channels: 1
}

const ts = new ToneStream(format)

const s = new Speaker(format)

const tones = _.flatten([
    utils.gen_dtmf_tones("1234567890abcdef", 100, 100, SAMPLE_RATE),
    utils.gen_morse_tones("Be yourself; everyone else is already taken", 880, 70, SAMPLE_RATE),
    utils.gen_music_scale("C5 D5 E5 F5 G5 A5 B5 C6", 100, 0, SAMPLE_RATE),
    utils.gen_binary_tones_from_text("hello world", 5, 500, 2000, SAMPLE_RATE),
])

console.log("Inspecting tones:")
tones.forEach(tone => {
    console.log(tone)
})

ts.concat(tones)

ts.on('empty', () => {
    console.log("Got event 'empty'. Reversing tones and playing again.")
    tones.reverse()
    ts.concat(tones)
})

console.log("Starting playing tones")
ts.pipe(s)

Events

The stream emits events:

  • 'empty': when there are no more tones to be played in the queue (it happens when the queue of tones is found empty)
  • 'ended': when all tones in the queue were generated (it happens when the consumer tries to read data from the stream and there are no more tones to be generated)

More examples

See here.

tone-stream's People

Contributors

mayamatakeshi avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

gamekyuubi

tone-stream's Issues

Add support for option stay_alive

The default is for the stream to terminate (return without push data) when there is no more tone spec to be read.
This is fine for things like writing (ex: dtmf_to_file.js).
However, in some scenarios we might want to keep the stream alive generating silence as we might eventually inject more tones in the future.
So we should permit the client code to specify:

  const opts = {
     stay_alive: true,
  }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.