Score Management
Methods for loading and exporting scores in various formats
Methods
loadFlatScore()
Load a score hosted on Flat Loads a Flat score by its unique identifier. For private scores, you must provide the sharing key obtained from a private link.
loadFlatScore(score: string | { score: string; sharingKey?: string; }): Promise<void>
Parameters:
score.score
-string
score.sharingKey?
-string
Returns: Promise<void>
Throws:
TypeError
- If the score parameter is invalidError
- If the score cannot be loaded (e.g., not found, access denied)
Examples:
// Load a public score
await embed.loadFlatScore('56ae21579a127715a02901a6');
// Load a private score with sharing key
await embed.loadFlatScore({
score: '56ae21579a127715a02901a6',
sharingKey: 'f79c3c0dd1fc76ed8b30d6f2c845c8c30f11fe88d9fc39ab96e8e407629d4885'
});
loadMusicXML()
Load a MusicXML score Loads a MusicXML score from a string or binary data. The score will be converted to Flat's internal format and displayed in the embed.
loadMusicXML(score: string | Uint8Array): Promise<void>
Parameters:
score
-string | Uint8Array
Returns: Promise<void>
Throws:
TypeError
- If the score format is invalidError
- If the MusicXML cannot be parsed or loaded
Examples:
// Load from XML string
const xmlString = '<?xml version="1.0"?>...';
await embed.loadMusicXML(xmlString);
// Load from compressed MXL file
const response = await fetch('score.mxl');
const buffer = await response.arrayBuffer();
await embed.loadMusicXML(new Uint8Array(buffer));
loadMIDI()
Load a MIDI score Loads a MIDI file and converts it to sheet music notation. Note that MIDI files contain performance data rather than notation, so the conversion may not perfectly represent the original musical intent.
loadMIDI(score: Uint8Array): Promise<void>
Parameters:
score
-Uint8Array
Returns: Promise<void>
Throws:
TypeError
- If the score format is invalidError
- If the MIDI file cannot be parsed or loaded
Examples:
// Load MIDI file from URL
const response = await fetch('song.mid');
const buffer = await response.arrayBuffer();
await embed.loadMIDI(new Uint8Array(buffer));
// Load MIDI file from file input
const file = document.getElementById('fileInput').files[0];
const buffer = await file.arrayBuffer();
await embed.loadMIDI(new Uint8Array(buffer));
loadJSON()
Load a Flat JSON score Loads a score from Flat's native JSON format. This format preserves all score data and is the most reliable way to transfer scores between Flat applications.
loadJSON(score: string | Record<string, unknown>): Promise<void>
Parameters:
score
-string | Record<string, unknown>
Returns: Promise<void>
Throws:
TypeError
- If the JSON is invalid or cannot be parsedError
- If the score data is malformed or cannot be loaded
Examples:
// Load from JSON object
const scoreData = await fetch('score.json').then(r => r.json());
await embed.loadJSON(scoreData);
// Load from JSON string
const jsonString = '{"score-partwise": {...}}';
await embed.loadJSON(jsonString);
getJSON()
Get the score in Flat JSON format Exports the currently loaded score as Flat's native JSON format. This format preserves all score data including notation, layout, and metadata.
getJSON(): Promise<Record<string, unknown>>
Returns: Promise<Record<string, unknown>>
Throws:
Error
- If no score is currently loaded
Examples:
// Export and save score data
const scoreData = await embed.getJSON();
const jsonString = JSON.stringify(scoreData);
// Save to file
const blob = new Blob([jsonString], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'score.json';
a.click();
getMusicXML()
Convert the displayed score to MusicXML Exports the currently loaded score as MusicXML, the standard format for sheet music notation exchange. Supports both uncompressed XML and compressed MXL formats.
getMusicXML(options?: { compressed?: boolean; }): Promise<string | Uint8Array>
Parameters:
options.compressed
(optional) -boolean
Returns: Promise<string | Uint8Array>
Throws:
TypeError
- If options parameter is invalidError
- If no score is loaded or conversion fails
Examples:
// Get uncompressed MusicXML
const xml = await embed.getMusicXML();
console.log(xml); // <?xml version="1.0"...
// Get compressed MusicXML (.mxl)
const mxl = await embed.getMusicXML({ compressed: true });
const blob = new Blob([mxl], { type: 'application/vnd.recordare.musicxml' });
const url = URL.createObjectURL(blob);
getPNG()
Convert the displayed score to PNG image Exports the currently loaded score as a PNG image. Supports various export options including resolution, layout mode, and output format.
getPNG(options?: { result?: 'Uint8Array' | 'dataURL'; dpi?: number; layout?: 'track' | 'page'; }): Promise<Uint8Array | string>
Parameters:
options.result
(optional) -'Uint8Array' | 'dataURL'
: Export result (either a PNG returned as Uint8Array or in dataURL)options.dpi
(optional) -number
: DPI of exported image (min: 50, max: 300, default: 150)options.layout
(optional) -'track' | 'page'
: Layout of exported image
Returns: Promise<Uint8Array | string>
Throws:
TypeError
- If options parameter is invalidError
- If no score is loaded or conversion fails
Examples:
// Get PNG as Uint8Array
const pngData = await embed.getPNG();
const blob = new Blob([pngData], { type: 'image/png' });
// Get PNG as data URL for direct display
const dataUrl = await embed.getPNG({ result: 'dataURL' });
document.getElementById('preview').src = dataUrl;
// High resolution export with page layout
const hqPng = await embed.getPNG({ dpi: 300, layout: 'page' });
getMIDI()
Convert the displayed score to MIDI Exports the currently loaded score as a MIDI file. The MIDI file will contain performance data including notes, tempo, dynamics, and instrument information. Note that some notation elements may not be represented in MIDI format.
getMIDI(): Promise<Uint8Array>
Returns: Promise<Uint8Array>
Throws:
Error
- If no score is loaded or conversion fails
Examples:
// Export score as MIDI
const midiData = await embed.getMIDI();
// Save as file
const blob = new Blob([midiData], { type: 'audio/midi' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'score.mid';
a.click();
// Play MIDI in browser (requires Web MIDI API)
const midiData = await embed.getMIDI();
// ... use with Web MIDI API or MIDI player library
getFlatScoreMetadata()
Get the metadata of the score (for scores hosted on Flat) Retrieves metadata for scores that are hosted on Flat, including title, composer, collaborators, creation date, and other information available through Flat's API. This method only works for scores loaded via loadFlatScore()
.
getFlatScoreMetadata(): Promise<ScoreDetails>
Returns: Promise<ScoreDetails>
View ScoreDetails
type definition
ScoreDetails
Complete score details including metadata and statistics
Properties:
id
:string
- The unique identifier of the scoresharingKey
(optional):string
- The private sharing key of the score (available when theprivacy
mode is set toprivateLink
)title
:string
- The title of the scoreprivacy
:ScorePrivacy
- Privacy setting of the scoreuser
:UserPublic
- User who created the scorehtmlUrl
:string
- The url where the score can be viewed in a web browsereditHtmlUrl
:string
- The url where the score can be edited in a web browsersubtitle
(optional):string
- Subtitle of the scorelyricist
(optional):string
- Lyricist of the scorearranger
(optional):string
- Arranger of the scorecomposer
(optional):string
- Composer of the scoredescription
(optional):string
- Description of the creationtags
(optional):string[]
- Tags describing the scorecreationType
(optional):ScoreCreationType
- Type of creationlicense
(optional):ScoreLicense
- License of the scorelicenseText
(optional):string
- Additional license text written on the exported/printed scoredurationTime
(optional):number
- In seconds, an approximative duration of the scorenumberMeasures
(optional):number
- The number of measures in the scoremainTempoQpm
(optional):number
- The main tempo of the score (in QPM)mainKeySignature
(optional):number
- The main key signature of the score (expressed between -7 and 7).rights
(optional):ResourceRights
- Access rights for the scorecollaborators
:ResourceCollaborator[]
- The list of the collaborators of the scorecreationDate
:string
- The date when the score was createdmodificationDate
(optional):string
- The date of the last revision of the scorepublicationDate
(optional):string
- The date when the score was published on FlatscheduledDeletionDate
(optional):string
- The date when the score will be definitively deleted. This date can be in the past if the score will be deleted at the next deletion batch.highlightedDate
(optional):string
- The date when the score was highlighted (featured) on our communityorganization
(optional):string
- If the score has been created in an organization, the identifier of this organizationparentScore
(optional):string
- If the score has been forked, the unique identifier of the parent scoreinstruments
:string[]
- An array of the instrument identifiers used in the last version of the score. The format of the strings is{instrument-group}.{instrument-id}
.instrumentsNames
:string[]
- An array of the instrument names that match the indexes from theinstruments
listsamples
:string[]
- An array of the audio samples identifiers used in the different score parts. The format of the strings is{instrument-group}.{sample-id}
.googleDriveFileId
(optional):string
- If the score exists on Google Drive, this field will contain the unique identifierlikes
(optional):ScoreLikesCounts
- Like statisticscomments
(optional):ScoreCommentsCounts
- Comment statisticsviews
(optional):ScoreViewsCounts
- View statisticsplays
(optional):ScorePlaysCounts
- Play statisticscollections
(optional):string[]
- The list of parent collections which includes all the collections this score is included in
Throws:
Error
- If no Flat score is loaded or metadata is unavailable
Example:
const embed = new Embed('container', config);
const flatScoreMetadata = await embed.getFlatScoreMetadata();
console.log(flatScoreMetadata);
See also: