museaikit
Version:
A powerful music-focused AI toolkit
97 lines • 3.65 kB
JavaScript
import * as sr from 'staffrender';
import { BaseVisualizer } from './base_visualizer';
import { ScrollType } from './config';
export class StaffSVGVisualizer extends BaseVisualizer {
render;
instruments;
drawnNotes;
constructor(sequence, div, config = {}) {
super(sequence, config);
if (config.pixelsPerTimeStep === undefined ||
config.pixelsPerTimeStep <= 0) {
this.config.pixelsPerTimeStep = 0;
}
this.instruments = config.instruments || [];
this.render = new sr.StaffSVGRender(this.getScoreInfo(sequence), {
noteHeight: this.config.noteHeight,
noteSpacing: this.config.noteSpacing,
pixelsPerTimeStep: this.config.pixelsPerTimeStep,
noteRGB: this.config.noteRGB,
activeNoteRGB: this.config.activeNoteRGB,
defaultKey: config.defaultKey || 0,
scrollType: config.scrollType || ScrollType.PAGE,
}, div);
this.drawnNotes = sequence.notes.length;
this.clear();
this.redraw();
}
clear() {
this.render.clear();
}
redraw(activeNote, scrollIntoView) {
if (this.drawnNotes !== this.noteSequence.notes.length) {
this.render.scoreInfo = this.getScoreInfo(this.noteSequence);
this.drawnNotes = this.noteSequence.notes.length;
}
const activeNoteInfo = activeNote ? this.getNoteInfo(activeNote) : undefined;
return this.render.redraw(activeNoteInfo, scrollIntoView);
}
isNoteInInstruments(note) {
if (note.instrument === undefined || this.instruments.length === 0) {
return true;
}
else {
return this.instruments.indexOf(note.instrument) >= 0;
}
}
timeToQuarters(time) {
const qpm = (this.noteSequence.tempos && this.noteSequence.tempos.length > 0 && this.noteSequence.tempos[0].qpm) ?
this.noteSequence.tempos[0].qpm : 120;
const q = time * qpm / 60;
return Math.round(q * 16) / 16;
}
getNoteInfo(note) {
const startQ = this.timeToQuarters(note.startTime);
const endQ = this.timeToQuarters(note.endTime);
return {
start: startQ,
length: endQ - startQ,
pitch: note.pitch,
intensity: note.velocity
};
}
getScoreInfo(sequence) {
const notesInfo = [];
sequence.notes.forEach((note) => {
if (this.isNoteInInstruments(note)) {
notesInfo.push(this.getNoteInfo(note));
}
});
return {
notes: notesInfo,
tempos: sequence.tempos ?
sequence.tempos.map((t) => {
return { start: this.timeToQuarters(t.time), qpm: t.qpm };
}) :
[],
keySignatures: sequence.keySignatures ?
sequence.keySignatures.map((ks) => {
return { start: this.timeToQuarters(ks.time), key: ks.key };
}) :
[],
timeSignatures: sequence.timeSignatures ?
sequence.timeSignatures.map((ts) => {
return {
start: this.timeToQuarters(ts.time),
numerator: ts.numerator,
denominator: ts.denominator
};
}) :
[]
};
}
clearActiveNotes() {
this.redraw();
}
}
//# sourceMappingURL=staff_visualizer.js.map