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).
You can install the package using pnpm
, npm
, or yarn
.
pnpm add browser-dj-js
npm add browser-dj-js
yarn add browser-dj-js
You can import the playNote and playMelody functions in your project:
import { playNote, playMelody } from 'browser-dj-js';
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
});
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);
playNote
Plays a single note with a given frequency and duration.
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.
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.
- Each note has:
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);
Contributions are welcome! Please open an issue or submit a pull request for any bugs or enhancements.
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.