npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@hoge1e3/oscillator

v1.0.1

Published

oscillator using WebAudio, with wave sampling

Downloads

2

Readme

@hoge1e3/oscillator

A JavaScript library for creating and manipulating audio oscillators using the Web Audio API, with support for standard waveforms and custom wave sampling.

Installation

npm install @hoge1e3/oscillator

Features

  • Create standard waveform oscillators (sine, square, triangle, sawtooth)
  • Support for custom/buffered waveforms
  • Control frequency, volume, and ADSR envelope
  • Join multiple sound sources together
  • Create mute/silent notes

Usage

Basic Example

import { createNote, joinSource } from '@hoge1e3/oscillator';

// Create an audio context
const audioCtx = new AudioContext();

// Create a note with sine waveform
const note = createNote(
  1,                // duration in seconds
  440,              // frequency in Hz (A4)
  0.5,              // volume (0-1)
  'sine',           // waveform type
  {                 // ADSR envelope
    attack: 0.1,    // attack time in seconds
    decay: 0.2,     // decay time in seconds
    sustain: 0.7,   // sustain level (0-1) 
    release: 0.2    // release time in seconds
  }
);

// Play the note
const playback = note.play(audioCtx);

// Stop playback after 2 seconds
setTimeout(() => {
  playback.stop();
}, 2000);

Playing a Sequence of Notes

import { createNote, joinSource } from '@hoge1e3/oscillator';

const audioCtx = new AudioContext();

// Create a C major scale
const notes = [];
for (let i = 0; i < 8; i++) {
  const note = createNote(
    0.5,                        // 0.5 second notes
    261.63 * Math.pow(2, i/12), // C4 to C5
    0.5,                        // volume
    'sawtooth',                 // waveform
    {
      attack: 0.05,
      decay: 0.1,
      sustain: 0.8,
      release: 0.1
    }
  );
  notes.push(note);
}

// Join all notes into a sequence
const sequence = joinSource(...notes);

// Play the sequence
const playback = sequence.play(audioCtx);

Using Custom Waveforms

import { createNote, bufferedWaveform } from '@hoge1e3/oscillator';

const audioCtx = new AudioContext();

// Create a custom waveform
const waveData = [];
const s = (x) => Math.sin(x * (Math.PI * 2));
for (let i = 0; i < 1024; i++) {
  // Custom formula to create a complex waveform
  waveData.push(s(i / 1024 + 1 * s(i * 3 / 1024)));
}

// Create a buffered waveform
const customWave = bufferedWaveform(audioCtx, waveData, { lambda: 1024 });

// Create and play a note with the custom waveform
const note = createNote(
  2,       // duration
  440,     // frequency
  0.7,     // volume
  customWave, // custom waveform
  {
    attack: 0.1,
    decay: 0.2,
    sustain: 0.6,
    release: 0.3
  }
);

// Play the note
const playback = note.play(audioCtx);

API Reference

Main Functions

createNote(duration, freq, vol, waveform, envelope)

Creates a note (sound source) with specified parameters.

  • duration: Duration of the note in seconds
  • freq: Frequency in Hz
  • vol: Volume (0-1)
  • waveform: Either a string ('sine', 'square', 'sawtooth', 'triangle') or a BufferedWaveform object
  • envelope: ADSR envelope with attack, decay, sustain, and release properties

Returns a Source object with duration property and play() method.

createOscillatorNote(duration, freq, vol, waveform, envelope)

Creates a note using standard oscillator types ('sine', 'square', 'sawtooth', 'triangle').

Parameters are the same as createNote().

createBufferedWaveformNote(duration, freq, vol, waveform, envelope)

Creates a note using a custom buffered waveform.

  • waveform: Must be a BufferedWaveform object created with bufferedWaveform()

Other parameters are the same as createNote().

bufferedWaveform(ctx, array, freqParam)

Creates a custom waveform from an array of samples.

  • ctx: AudioContext instance
  • array: Array of sample values
  • freqParam: (Optional) Either { lambda: number } or { sampleRate: number, baseFreq: number }.
    • It is recommended to specify { lambda: number } for program-generated waveform data and { sampleRate: number, baseFreq: number } for recorded data.
    • lambda is the length of the fundamental frequency in the waveform data. This value should be 1/n (n is a positive integer) of the total length of the waveform data.
    • The sampleRate and baseFreq specify the sampling frequency of the recorded data and the base frequency of the sounds in the data. Returns a BufferedWaveform object that can be used with createNote().

joinSource(...sources)

Combines multiple sound sources into a single sequence.

  • sources: Array of Source objects

Returns a new Source object that plays the sources in sequence.

createMuteNote(duration)

Creates a silent note of specified duration.

  • duration: Duration in seconds

Playback Control

When you call play() on a Source object, it returns a Playback object with:

  • ctx: AudioContext instance
  • dest: AudioDestinationNode
  • start: Start time
  • end: End time
  • stop(): Method to stop playback

Waveform Types

  • 'sine': Standard sine wave
  • 'square': Square wave
  • 'sawtooth': Sawtooth wave
  • 'triangle': Triangle wave
  • Custom waveforms created with bufferedWaveform()

ADSR Envelope

The ADSR (Attack, Decay, Sustain, Release) envelope controls the volume shape of a note:

  • attack: Time in seconds for the volume to reach its maximum
  • decay: Time in seconds for the volume to fall to the sustain level
  • sustain: Volume level during the sustain phase (0-1)
  • release: Time in seconds for the volume to fade out

Browser Support

This library works in all modern browsers that support the Web Audio API:

  • Chrome 34+
  • Firefox 25+
  • Safari 7.1+
  • Edge 12+

License

ISC

Links