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 🙏

© 2024 – Pkg Stats / Ryan Hefner

browser-dj-js

v1.0.2

Published

A module to play notes and melodies in the browser and calculate musical note frequencies

Downloads

14

Readme

browser-dj-js

browser-dj-js is a simple JavaScript/TypeScript library to play musical notes and melodies using the Web Audio API. This library provides functions to play individual notes and sequences of notes (melodies).

Installation

You can install the package using pnpm, npm, or yarn.

Using pnpm

pnpm add browser-dj-js

Using npm

npm add browser-dj-js

Using yarn

yarn add browser-dj-js

Usage

Importing the Functions

You can import the playNote and playMelody functions in your project:

import { playNote, playMelody } from 'browser-dj-js';

Playing a Single Note

To play a single note, use the playNote function. You need to specify the frequency of the note and the duration in seconds.

playNote({
    frequency: 440, // Frequency in Hz (e.g., 440 Hz for A4)
    duration: 1     // Duration in seconds
});

Playing a Melody

To play a melody, use the playMelody function. You need to pass an array of notes, each with a frequency and duration.

const melody = [
    { frequency: 440, duration: 1 },   // A4
    { frequency: 493.88, duration: 1 }, // B4
    { frequency: 523.25, duration: 1 }, // C5
    { frequency: 440, duration: 1 },    // A4
];

playMelody(melody);

API

playNote

Plays a single note with a given frequency and duration.

Parameters

  • note: { frequency: number, duration: number } - The note to play.
    • frequency - Frequency of the note in Hz.
    • duration - Duration of the note in seconds.

playMelody

Plays a sequence of notes.

Parameters

  • melody: { frequency: number, duration: number }[] - Array of notes to play.
    • Each note has:
      • frequency - Frequency of the note in Hz.
      • duration - Duration of the note in seconds.

Example

Here is a complete example that demonstrates how to use playNote and playMelody:

import { playNote, playMelody } from 'browser-dj-js';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioContext();

// Play a single note (A4 for 1 second)
playNote(audioContext, {
    frequency: 440,
    duration: 1
});

// Play a melody
const melody = [
    { frequency: 440, duration: 1 },   // A4
    { frequency: 493.88, duration: 1 }, // B4
    { frequency: 523.25, duration: 1 }, // C5
    { frequency: 440, duration: 1 },    // A4
];

playMelody(audioContext, melody);

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any bugs or enhancements.

License

This project is licensed under the MIT License


### Summary

This `README.md` provides a clear overview of the `browser-dj-js` package, including installation instructions, usage examples, and API documentation. This should help users understand how to integrate and use the `playNote` and `playMelody` functions in their projects.