magenta / magenta-js

Magenta.js: Music and Art Generation with Machine Learning in the browser
https://magenta.tensorflow.org
Apache License 2.0
1.99k stars 314 forks source link

How to have visualizer redraw with different noteSequence #252

Closed agreyfield91 closed 5 years ago

agreyfield91 commented 5 years ago

In the docs, there is only one method listed for visualizers - redraw(). It looks like its only purpose is to update the visualizer's active note. Is it possible to have it "redraw" with a different ns (so I could perhaps create a visualizer, expand upon the ns with musicRNN, and update the visualizer with the new ns)?

Or would I have to destroy the instance and create a new visualizer? If that's the case, how would I destroy?

Thank you!

notwaldorf commented 5 years ago

Yeah, destroying the instance and creating the new one is the way to go. Most of the work is done in resizing the canvas anyway, so the constructor isn't that expensive. You don't have to "destroy" it per se, you just have to re-create it. This should work (and it's how we do it in the Visualizer demo, for example: https://github.com/tensorflow/magenta-js/blob/master/music/demos/visualizer.ts#L69)

let visualizer = new mm.Visualizer(someNoteSequence, aCanvasElement);
// ...
visualizer = new mm.Visualizer(aDifferentNoteSequences, aCanvasElement);