1 /* global PREF_OPACPlayMusicalInscripts interface verovio */
2 $(document).ready(function() {
3 if( $(".musical_inscripts").length > 0 ){
5 // Check support for WebAssembly
6 // https://stackoverflow.com/questions/47879864/how-can-i-check-if-a-browser-supports-webassembly
7 var webassenbly_supported = (() => {
9 if (typeof WebAssembly === "object"
10 && typeof WebAssembly.instantiate === "function") {
11 const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));
12 if (module instanceof WebAssembly.Module)
13 return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;
20 if( webassenbly_supported ){
26 $.getScript( interface + "/lib/verovio/verovio-toolkit.js", function( data, textStatus, jqxhr ) {
27 $('.musical_inscripts .inscript').each(function() {
28 var vrvToolkit = new verovio.toolkit();
30 var data = "@clef:"+$t.data('clef')+"\n@keysig:"+$t.data('keysig')+"\n@timesig:"+$t.data('timesig')+"\n@data:"+$t.data('notation')+"\n";
31 var svg = vrvToolkit.renderData(data, {
32 inputFormat: $t.data('system'),
39 var base64midi = vrvToolkit.renderToMIDI();
40 var song = 'data:audio/midi;base64,' + base64midi;
41 var play_btn = $('.play_btn', $t.parent());
43 play_btn.data('song', song);
44 play_btn.data('toolkit', vrvToolkit);
47 if( PREF_OPACPlayMusicalInscripts ){
48 $(".audio_controls").show();
49 var playmusic_1 = $.getScript( interface + "/lib/verovio/000_acoustic_grand_piano.js" );
50 var playmusic_2 = $.getScript( interface + "/lib/verovio/midiplayer.js" );
51 $.when( playmusic_1, playmusic_2 ).done(function () {
56 var player = $('.inscript_audio');
58 var midiUpdate = function(time) {
59 // time needs to - 400 for adjustment
60 var vrvTime = Math.max(0, time - 400);
61 var elementsattime = currentToolkit.getElementsAtTime(vrvTime);
62 if (elementsattime.page > 0) {
63 if ((elementsattime.notes.length > 0) && (ids != elementsattime.notes)) {
64 ids.forEach(function(noteid) {
65 if ($.inArray(noteid, elementsattime.notes) == -1) {
66 $("#" + noteid).attr("fill", "#000").attr("stroke", "#000");
69 ids = elementsattime.notes;
70 ids.forEach(function(noteid) {
71 if ($.inArray(noteid, elementsattime.notes) != -1) {
72 $("#" + noteid).attr("fill", "#c00").attr("stroke", "#c00");
79 var midiStop = function() {
80 ids.forEach(function(noteid) {
81 $("#" + noteid).attr("fill", "#000").attr("stroke", "#000");
88 locateFile: function(file) {
89 return interface + '/lib/verovio/'+file;
97 $('.musical_inscripts .play_btn').click(function() {
100 currentToolkit = $t.data('toolkit')
101 player.midiPlayer.play($t.data('song'));