Classes
Partition

Partition

What is a partition?

A partition is a collection of notes. It is the most basic element of music.

How to create a partition?

A partition can be created in two ways.

Partition Class

You can create a partition by using the Partition class and passing an array of notes or chords, the oscillator type and the audio context.

index.js
import { Partition, Note, Chord } from 'musiquejs';
 
const partition = new Partition(
  [
    new Note('D', 4, 0.18),
    new Note('D', 4, 0.18),
    new Note('D', 5, 0.36),
    new Chord([new Note('A', 4, 0.54), new Note('C', 5, 0.54)])
  ],
  'sine',
  audioContext
);

JSONPartition Class

You can also create a partition by using the JSONPartition class and passing a JSON object.

// partition.json
[
    {
      "note": "D",
      "octave": 4,
      "duration": 0.18
    },
    {
      "note": "D",
      "octave": 4,
      "duration": 0.18
    },
    {
      "note": "D",
      "octave": 5,
      "duration": 0.36
    },
    [
      {
        "note": "A",
        "octave": 4,
        "duration": 0.54
      },
      {
        "note": "C",
        "octave": 5,
        "duration": 0.54
      }
    ]
]
index.js
import { JSONPartition } from 'musiquejs';
 
const json = require('./partition.json');
 
const partition = new JSONPartition(
  json,
  'sine',
  audioContext
);

Importing a JSON might seem longer, but it is useful when you want to create a partition from a file or share your creations!

No matter which way you choose, this will create a partition with the following notes:

  • D4 with a duration of 0.18 seconds
  • D4 with a duration of 0.18 seconds
  • D5 with a duration of 0.36 seconds
  • A chord containing A4 with a duration of 0.54 seconds and C5 with a duration of 0.54 seconds

You can choose the OscillatorType of the partition. The default value is 'sine'.

List of OscillatorType:

  • sine
  • square
  • sawtooth
  • triangle
  • piano
  • guitar
⚠️

Piano and Guitar can for now only be used in notes lasting over one second.

Both Piano and Guitar are WiP but Piano should be usable in most cases without sounding too weird. Feel free to give feedback on what should be changed or wich instruments you would like next by dm to (𝕏 @alusinviii (opens in a new tab))

You can also choose the AudioContext of the partition. By default, you can use window.AudioContext.

How to play a partition?

A partition can be played with the following code:

index.js
import { Partition, Note } from 'musiquejs';
 
const partition = new Partition(
[
  new Note('D', 4, 0.18),
  new Note('D', 4, 0.18),
  new Note('D', 5, 0.36),
  new Note('A', 4, 0.54),
],
'sine',
audioContext
);
 
partition.play();