AEAEAEAEarray

Untitled

Aug 25th, 2021
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //tip: just put all codes in ur random location of coding just add the main function to play in command just read all command carefully
  2.  
  3.  
  4.  
  5. // The MIDI Player function to play song
  6. var Player = new MidiPlayer.Player(function(event) {
  7.     if (MPP.client.preventsPlaying()) {
  8.         if (Player.isPlaying()) pause();
  9.         return;
  10.     }
  11.     var currentEvent = event.name;
  12.     if (!exists(currentEvent) || currentEvent == "") return;
  13.     if (currentEvent.indexOf("Note") == 0 && (ALLOW_ALL_INTRUMENTS || event.channel != PERCUSSION_CHANNEL)) {
  14.         var currentNote = (exists(event.noteName) ? MIDIPlayerToMPPNote[event.noteName] : null);
  15.         if (currentEvent == "Note on" && event.velocity > 0) { // start note
  16.             MPP.press(currentNote, (event.velocity/100));
  17.             if (!sustainOption) MPP.release(currentNote);
  18.         } else if (sustainOption && (currentEvent == "Note off" || event.velocity == 0)) MPP.release(currentNote); // end note
  19.     }
  20.     if (!ended && !Player.isPlaying()) {
  21.         ended = true;
  22.         paused = false;
  23.         if (!repeatOption) {
  24.             currentSongData = null;
  25.             currentSongName = null;
  26.         }
  27.     } else {
  28.         var timeRemaining = Player.getSongTimeRemaining();
  29.         var timeElapsed = currentSongDuration - (timeRemaining > 0 ? timeRemaining : 0);
  30.         // BELOW TEMP: helps mitigate duration calculation issue, but still not fully fixed, see https://github.com/grimmdude/MidiPlayerJS/issues/64
  31.         currentSongDuration = Player.getSongTime();
  32.         currentSongDurationFormatted = timeClearZeros(secondsToHms(currentSongDuration));
  33.         // ABOVE TEMP
  34.         currentSongElapsedFormatted = timeSizeFormat(secondsToHms(timeElapsed), currentSongDurationFormatted);
  35.     }
  36. });
  37. // see https://github.com/grimmdude/MidiPlayerJS/issues/25
  38. Player.sampleRate = 0; // this allows sequential notes that are supposed to play at the same time, do so when using fast MIDIs (e.g. some black MIDIs)
  39.  
  40.  
  41. // CORS Anywhere (allows downloading files where JS can't)
  42. var useCorsUrl = function(url) {
  43.     var newUrl = null; // send null back if it's already a cors url
  44.     var cors_api_url = 'https://cors-anywhere.herokuapp.com/';
  45.     // removes protocols before applying cors api url
  46.     if (url.indexOf(cors_api_url) == -1) newUrl = cors_api_url + url.replace(/(^\w+:|^)\/\//, '');
  47.     return newUrl;
  48. }
  49.  
  50.  
  51. // Get visual downloading progress, just enter the current progressing number (usually time elapsed in seconds)
  52. var getProgress = function(intProgress) {
  53.     var progress = intProgress % 20;
  54.     switch(progress) {
  55.         case 0: return " █░░░░░░░░░░"; break;
  56.         case 1: case 19: return " ░█░░░░░░░░░"; break;
  57.         case 2: case 18: return " ░░█░░░░░░░░"; break;
  58.         case 3: case 17: return " ░░░█░░░░░░░"; break;
  59.         case 4: case 16: return " ░░░░█░░░░░░"; break;
  60.         case 5: case 15: return " ░░░░░█░░░░░"; break;
  61.         case 6: case 14: return " ░░░░░░█░░░░"; break;
  62.         case 7: case 13: return " ░░░░░░░█░░░"; break;
  63.         case 8: case 12: return " ░░░░░░░░█░░"; break;
  64.         case 9: case 11: return " ░░░░░░░░░█░"; break;
  65.         case 10: return " ░░░░░░░░░░█"; break;
  66.     }
  67. }
  68.  
  69.  
  70. //this extra function to handle error, if u not wanna use then dont use, its in ur own wish
  71. // Checks if loading music should play
  72. var preventsLoadingMusic = function() {
  73.     return !loadingMusicPrematureStop && !Player.isPlaying() && !MPP.client.preventsPlaying();
  74. }
  75.  
  76. // This is used when loading a song in the midi player, if it's been turned on
  77. var humanMusic = function() {
  78.     setTimeout(function() {
  79.         if (preventsLoadingMusic()) MPP.press("c5", 1);
  80.         if (preventsLoadingMusic()) MPP.release("c5");
  81.     }, 200);
  82.     setTimeout(function() {
  83.         if (preventsLoadingMusic()) MPP.press("d5", 1);
  84.         if (preventsLoadingMusic()) MPP.release("d5");
  85.     }, 700);
  86.     setTimeout(function() {
  87.         if (preventsLoadingMusic()) MPP.press("c5", 1);
  88.         if (preventsLoadingMusic()) MPP.release("c5");
  89.         loadingMusicPrematureStop = false;
  90.     }, 1200);
  91. }
  92.  
  93. // Starts the loading music
  94. var startLoadingMusic = function() {
  95.     if (loadingMusicLoop == null) {
  96.         humanMusic();
  97.         loadingMusicLoop = setInterval(function() {
  98.             humanMusic();
  99.         }, 2200);
  100.     }
  101. }
  102.  
  103. // Stops the loading music
  104. var stopLoadingMusic = function() {
  105.     if (loadingMusicLoop != null) {
  106.         loadingMusicPrematureStop = true;
  107.         clearInterval(loadingMusicLoop);
  108.         loadingMusicLoop = null;
  109.     }
  110. }
  111.  
  112.  
  113. //url to download a file to play a song
  114. // Gets file as a blob (data URI)
  115. var urlToBlob = function(url, callback) {
  116.     // show file download progress
  117.     var downloading = null;
  118.     mppChatSend(PRE_DOWNLOADING + ' ' + url);
  119.     if (loadingOption) startLoadingMusic();
  120.     else {
  121.         var progress = 0;
  122.         downloading = setInterval(function() {
  123.             mppChatSend(PRE_DOWNLOADING + getProgress(progress));
  124.             progress++;
  125.         }, chatDelay);
  126.     }
  127.  
  128.     fetch(url, {
  129.         headers: {
  130.             "Content-Disposition": "attachment" // this might not be doing anything
  131.         }
  132.     }).then(response => {
  133.         stopLoadingMusic();
  134.         clearInterval(downloading);
  135.         if (!response.ok) {
  136.             throw new Error("Network response was not ok");
  137.         }
  138.         return response.blob();
  139.     }).then(blob => {
  140.         stopLoadingMusic();
  141.         clearInterval(downloading);
  142.         callback(blob);
  143.     }).catch(error => {
  144.         console.error("Normal fetch couldn't get the file:", error);
  145.         var corsUrl = useCorsUrl(url);
  146.         if (corsUrl != null) {
  147.             if (loadingOption) startLoadingMusic();
  148.  
  149.             fetch(corsUrl, {
  150.                 headers: {
  151.                     "Content-Disposition": "attachment" // this might not be doing anything
  152.                 }
  153.             }).then(response => {
  154.                 stopLoadingMusic();
  155.                 clearInterval(downloading);
  156.                 if (!response.ok) {
  157.                     throw new Error("Network response was not ok");
  158.                 }
  159.                 return response.blob();
  160.             }).then(blob => {
  161.                 stopLoadingMusic();
  162.                 clearInterval(downloading);
  163.                 callback(blob);
  164.             }).catch(error => {
  165.                 console.error("CORS Anywhere API fetch couldn't get the file:", error);
  166.                 stopLoadingMusic();
  167.                 clearInterval(downloading);
  168.                 callback(null);
  169.             });
  170.         }
  171.         // callback(null); // disabled since the second fetch already should call the call back
  172.     });
  173. }
  174.  
  175.  
  176. //play song when we got correct link of song
  177. var fileOrBlobToBase64 = function(raw, callback) {
  178.     if (raw == null) {
  179.         stopLoadingMusic();
  180.         callback(null);
  181.     }
  182.  
  183.     // continue if we have a blob
  184.     var reader = new FileReader();
  185.     reader.readAsDataURL(raw);
  186.     reader.onloadend = function() {
  187.         var base64data = reader.result;
  188.         callback(base64data);
  189.     }
  190. }
  191.  
  192.  
  193. // Validates file or blob is a MIDI
  194. var isMidi = function(raw) {
  195.     if (exists(raw)) {
  196.         var mimetype = raw.type;
  197.         // acceptable mimetypes for midi files
  198.         switch(mimetype) {
  199.             case "@file/mid": case "@file/midi":
  200.             case "application/mid": case "application/midi":
  201.             case "application/x-mid": case "application/x-midi":
  202.             case "audio/mid": case "audio/midi":
  203.             case "audio/x-mid": case "audio/x-midi":
  204.             case "music/crescendo":
  205.             case "x-music/mid": case "x-music/midi":
  206.             case "x-music/x-mid": case "x-music/x-midi": return true; break;
  207.         }
  208.     }
  209.     return false;
  210. }
  211.  
  212. // Validates file or blob is application/octet-stream ... when using CORS
  213. var isOctetStream = function(raw) {
  214.     if (exists(raw) && raw.type == "application/octet-stream") return true;
  215.     else return false;
  216. }
  217.  
  218. // Gets song from data URI and plays it.. play song when we got correct data
  219. var playSong = function(songFileName, songData) {
  220.     // stop any current songs from playing
  221.     stopSong();
  222.     // play song if it loaded correctly
  223.     try {
  224.         // load song
  225.         Player.loadDataUri(songData);
  226.         // play song
  227.         Player.play();
  228.         ended = false;
  229.         stopped = false;
  230.         var timeoutRecorder = 0;
  231.         var showSongName = setInterval(function() {
  232.             if (Player.isPlaying()) {
  233.                 clearInterval(showSongName);
  234.  
  235.                 // changes song
  236.                 var hasExtension = songFileName.lastIndexOf('.');
  237.                 previousSongData = currentSongData;
  238.                 previousSongName = currentSongName;
  239.                 currentSongData = songData;
  240.                 currentSongName = (hasExtension > 0) ? songFileName.substring(0, hasExtension) : songFileName;
  241.                 currentSongElapsedFormatted = timeSizeFormat(secondsToHms(0), currentSongDurationFormatted);
  242.                 currentSongDuration = Player.getSongTime();
  243.                 currentSongDurationFormatted = timeClearZeros(secondsToHms(currentSongDuration));
  244.  
  245.                 mppChatSend(" Now playing " + quoteString(currentSongName));
  246.             } else if (timeoutRecorder == SONG_NAME_TIMEOUT) {
  247.                 clearInterval(showSongName);
  248.             } else timeoutRecorder++;
  249.         }, 1);
  250.     } catch(error) {
  251.         stopLoadingMusic();
  252.         // reload the previous working file if there is one
  253.         if (previousSongData != null) Player.loadDataUri(previousSongData);
  254.         mppChatSend(PRE_ERROR + " (play) " + error);
  255.     }
  256. }
  257.  
  258. //when midi file fount it just started playing
  259. Player.on('fileLoaded', function() {
  260.     // Do something when file is loaded
  261.     stopLoadingMusic();
  262. });
  263. // Plays the song from a URL if it's a MIDI
  264. var playURL = function(songUrl, songData) {
  265.     currentFileLocation = songUrl;
  266.     var songFileName = decodeURIComponent(currentFileLocation.substring(currentFileLocation.lastIndexOf('/') + 1));
  267.     playSong(songFileName, songData);
  268. }
  269.  
  270.  
  271.  
  272. //here is the main function to play song.. put the code into ur main command function
  273. function play(url) {
  274.     var error = PRE_ERROR + " (play)";
  275.     // URL needs to be entered to play a song
  276.     if (!exists(url) || url == "") {
  277.         stopLoadingMusic();
  278.         mppChatSend(error + " No MIDI url entered... " + WHERE_TO_FIND_MIDIS);
  279.         mppChatSend("pls enter a midi url");
  280.     } else {
  281.         // downloads file if possible and then plays it if it's a MIDI
  282.         urlToBlob(url, function(blob) {
  283.             if (blob == null) mppChatSend(" Invalid URL, this is not a MIDI file, or the file requires a manual download from " + quoteString(url) + ". " + WHERE_TO_FIND_MIDIS);
  284.             else if (isMidi(blob) || isOctetStream(blob)) {
  285.                 fileOrBlobToBase64(blob, function(base64data) {
  286.                     // play song only if we got data
  287.                     if (exists(base64data)) {
  288.                         if (isOctetStream(blob)) { // when download with CORS, need to replace mimetype, but it doesn't guarantee it's a MIDI file
  289.                             base64data = base64data.replace("application/octet-stream", "audio/midi");
  290.                         }
  291.                         playURL(url, base64data);
  292.                     } else mppChatSend(error + " Unexpected result, MIDI file couldn't load... " + WHERE_TO_FIND_MIDIS);
  293.                 });
  294.             } else mppChatSend(error + " Invalid URL, this is not a MIDI file... " + WHERE_TO_FIND_MIDIS);
  295.         });
  296.     }
  297. }
  298.  
  299. //stop the song.. put this code into stop command
  300. var stop = function() {
  301.     // stops the current song
  302.     if (ended) mppChatSend(' ' + NO_SONG);
  303.     else {
  304.         stopSong();
  305.         paused = false;
  306.         mppChatSend(" Stopped playing " + quoteString(currentSongName));
  307.         currentFileLocation = currentSongName = null;
  308.     }
  309. }
  310.  
  311. //this is not main this just auto put "" around the string
  312. // Puts quotes around string
  313. var quoteString = function(string) {
  314.     var newString = string;
  315.     if (exists(string) && string != "") newString = '"' + string + '"';
  316.     return newString
  317. }
Add Comment
Please, Sign In to add comment