Compose Music Directly In Your Webpage – Musique.js

Category: Javascript , Recommended | March 19, 2024
Author:musiqueJS
Views Total:64 views
Official Page:Go to website
Last Update:March 19, 2024
License:MIT

Preview:

Compose Music Directly In Your Webpage – Musique.js

Description:

Musique.js is an open-source library that allows anyone to compose music directly within their web applications by using Web Audio API.

You can whip up a simple melody or go full-on composer mode with multiple instruments and complex arrangements.

How to use it:

1. Install and import the Musique.js with NPM.

# NPM
$ npm install musiquejs
import { Partition, Note, CustomChord } from 'musiquejs';

2. Create a note with a specific pitch and duration with the Note class.

// Create a D4 note that lasts half a second.
const note = new Note('D', 4, 0.5);
// Play the note
note.play();

3. Combine the notes into harmonious partitions. You can choose the oscillator type, like sine, square, sawtooth, or triangle. This is where the magic happens.

const partition = new Partition(
  [
    new Note('A', 4, 0.18),
    new Note('B', 4, 0.36),
    new Note('C', 4, 0.54),
    new Note('D', 4, 0.72),
  ],
 'sine', // or 'square', 'sawtooth', 'triangle'
  audioContext
);
// Play the partition
partition.play();

4. Want to get fancy? Use the CustomChord to create your own chords by specifying the notes and duration.

const myChord = new CustomChord(
  [
    new Note('A', 4, 0.18),
    new Note('B', 4, 0.36),
    new Note('C', 4, 0.54),
    new Note('D', 4, 0.72),
  ],
  0.5
);
// Play the custom chord
myChord.play();

You Might Be Interested In:


Leave a Reply