Global

Methods

calculateFrequency(note) → {number}

Get the frequency of a note based on its distance from A4 (440 Hz).

Parameters:
Name Type Description
note string

The note (e.g., 'C4', 'A4', 'G#5').

Source:
Returns:
  • The frequency of the note in Hz.
Type
number

createAudio() → {HTMLAudioElement}

Create a new audio input element and append it to the DOM. Maintain a reference to the created audio input.

Source:
Returns:

The created audio element

Type
HTMLAudioElement

createCanvas(options) → {HTMLCanvasElement}

Create a new canvas element and append it to the DOM. Maintain a reference to the created canvas.

Parameters:
Name Type Description
options Object

Options for the canvas (width, height, etc.)

Source:
Returns:

The created canvas element

Type
HTMLCanvasElement

createNote(noteName) → {Note|undefined}

Creates a musical note with the given note name. If no instrument is selected, it defaults to Piano.

Parameters:
Name Type Description
noteName string | number

The name or number of the musical note (e.g., "A4", "440").

Source:
Returns:
  • A new Note instance with the specified pitch, or undefined if the note is not found.
Type
Note | undefined
Example
// Select an instrument and create a note
PN.instrument('cello');    // Select piano instrument
const note = createNote('C4'); // Creates a note with frequency for C4
console.log(note);         // Logs the created note
PN.save();

createSong(songData) → {Array}

Creates a song based on the input data. The song is constructed from a series of note strings and durations. Notes are processed and encoded into a Tune instance.

Parameters:
Name Type Description
songData Array

An array of song sections and note data, in string format.

Source:
Returns:
  • The encoded song data.
Type
Array
Example
// Define song data
const songData = [
 "ch1[1.5:A4-F5]",
 "ch2[0.5:C4]",
 "ch1[2.0:G3-E4-D4]"
];

// Create a song
PN.setVolume(0.5); // Set volume level
const song = createSong(songData); // Create a song from the input data
console.log(song); // Logs the encoded song data

parseWav(buffer) → {Object}

Parses WAV file data from an ArrayBuffer.

Parameters:
Name Type Description
buffer ArrayBuffer

The WAV file buffer.

Source:
Returns:

Parsed WAV data including sample rate and audio samples.

Type
Object

processAudioData(audioData, sampleRate)

Parameters:
Name Type Description
audioData Float32Array

The audio data samples.

sampleRate number

The sample rate of the audio data.

Source:

readWavFile(url)

Reads a WAV file from the provided URL, parses it, and processes the audio data.

Parameters:
Name Type Description
url string

The URL of the WAV file to read.

Source:

setDuration(duration)

Sets the duration for notes in the PN object.

Parameters:
Name Type Description
duration number

The duration of the note in seconds.

Source:
Example
// Set the duration of notes to 1 second
setDuration(1);
console.log(PN.duration);  // Logs: 1

setHarmonic(harmonicFunc)

Sets the harmonic function for notes in the PN object.

Parameters:
Name Type Description
harmonicFunc function

The harmonic function to apply to the notes.

Source:
Example
// Set the harmonic function to 'first'
PN.setHarmonic(first);
console.log(PN.harmonic);  // Logs: [function: first]

setVolume(volume)

Sets the volume level for notes in the PN object.

Parameters:
Name Type Description
volume number

The volume level (e.g., between 0 and 1).

Source:
Example
// Set the volume of notes to 0.75 (75% of max volume)
setVolume(0.75);
console.log(PN.volume);  // Logs: 0.75

singVoice(noteName) → {File|undefined}

Creates a musical note with the given note name. If no instrument is selected, it defaults to Piano.

Parameters:
Name Type Description
noteName string

The name of audio file you wish to replicate.

Source:
Returns:
  • A new audio file instance with the specified pitch, or undefined if the note is not found.
Type
File | undefined
Example
// Select an instrument and create a note
     // Wrap everything in an async function
     async function runPNExample() {
         console.log(PN);  // This should print the PN object
          PN.instrument('Piano'); // Select the instrument
          // PN.setVolume(0.5); // Set volume (optional)

          // Wait for PN.singVoice to complete
          const song = await PN.singVoice('recording2.wav');

          console.log("Song created:", song);
          console.log(PN.volume);  // Logs the current volume
              setTimeout(() => {
                  PN.save(); // Call save after the delay
                  saveLogToFile(logMessages);

              }, 8000); // Delay in milliseconds (5000ms = 5s)
      }

      // Run the function
      runPNExample();

yin(data, sampleRate, thresholdopt) → {Object}

Enhanced YIN algorithm to estimate fundamental frequency, duration, and volume of audio signal

Parameters:
Name Type Attributes Default Description
data Float32Array

The time-domain data for the audio signal

sampleRate Number

The sample rate

threshold Number <optional>
0.07

The threshold

Source:
Returns:

An object with frequency, volume, and chunk duration

Type
Object