Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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
- // The MIDI Player function to play song
- var Player = new MidiPlayer.Player(function(event) {
- if (MPP.client.preventsPlaying()) {
- if (Player.isPlaying()) pause();
- return;
- }
- var currentEvent = event.name;
- if (!exists(currentEvent) || currentEvent == "") return;
- if (currentEvent.indexOf("Note") == 0 && (ALLOW_ALL_INTRUMENTS || event.channel != PERCUSSION_CHANNEL)) {
- var currentNote = (exists(event.noteName) ? MIDIPlayerToMPPNote[event.noteName] : null);
- if (currentEvent == "Note on" && event.velocity > 0) { // start note
- MPP.press(currentNote, (event.velocity/100));
- if (!sustainOption) MPP.release(currentNote);
- } else if (sustainOption && (currentEvent == "Note off" || event.velocity == 0)) MPP.release(currentNote); // end note
- }
- if (!ended && !Player.isPlaying()) {
- ended = true;
- paused = false;
- if (!repeatOption) {
- currentSongData = null;
- currentSongName = null;
- }
- } else {
- var timeRemaining = Player.getSongTimeRemaining();
- var timeElapsed = currentSongDuration - (timeRemaining > 0 ? timeRemaining : 0);
- // BELOW TEMP: helps mitigate duration calculation issue, but still not fully fixed, see https://github.com/grimmdude/MidiPlayerJS/issues/64
- currentSongDuration = Player.getSongTime();
- currentSongDurationFormatted = timeClearZeros(secondsToHms(currentSongDuration));
- // ABOVE TEMP
- currentSongElapsedFormatted = timeSizeFormat(secondsToHms(timeElapsed), currentSongDurationFormatted);
- }
- });
- // see https://github.com/grimmdude/MidiPlayerJS/issues/25
- 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)
- // CORS Anywhere (allows downloading files where JS can't)
- var useCorsUrl = function(url) {
- var newUrl = null; // send null back if it's already a cors url
- var cors_api_url = 'https://cors-anywhere.herokuapp.com/';
- // removes protocols before applying cors api url
- if (url.indexOf(cors_api_url) == -1) newUrl = cors_api_url + url.replace(/(^\w+:|^)\/\//, '');
- return newUrl;
- }
- // Get visual downloading progress, just enter the current progressing number (usually time elapsed in seconds)
- var getProgress = function(intProgress) {
- var progress = intProgress % 20;
- switch(progress) {
- case 0: return " █░░░░░░░░░░"; break;
- case 1: case 19: return " ░█░░░░░░░░░"; break;
- case 2: case 18: return " ░░█░░░░░░░░"; break;
- case 3: case 17: return " ░░░█░░░░░░░"; break;
- case 4: case 16: return " ░░░░█░░░░░░"; break;
- case 5: case 15: return " ░░░░░█░░░░░"; break;
- case 6: case 14: return " ░░░░░░█░░░░"; break;
- case 7: case 13: return " ░░░░░░░█░░░"; break;
- case 8: case 12: return " ░░░░░░░░█░░"; break;
- case 9: case 11: return " ░░░░░░░░░█░"; break;
- case 10: return " ░░░░░░░░░░█"; break;
- }
- }
- //this extra function to handle error, if u not wanna use then dont use, its in ur own wish
- // Checks if loading music should play
- var preventsLoadingMusic = function() {
- return !loadingMusicPrematureStop && !Player.isPlaying() && !MPP.client.preventsPlaying();
- }
- // This is used when loading a song in the midi player, if it's been turned on
- var humanMusic = function() {
- setTimeout(function() {
- if (preventsLoadingMusic()) MPP.press("c5", 1);
- if (preventsLoadingMusic()) MPP.release("c5");
- }, 200);
- setTimeout(function() {
- if (preventsLoadingMusic()) MPP.press("d5", 1);
- if (preventsLoadingMusic()) MPP.release("d5");
- }, 700);
- setTimeout(function() {
- if (preventsLoadingMusic()) MPP.press("c5", 1);
- if (preventsLoadingMusic()) MPP.release("c5");
- loadingMusicPrematureStop = false;
- }, 1200);
- }
- // Starts the loading music
- var startLoadingMusic = function() {
- if (loadingMusicLoop == null) {
- humanMusic();
- loadingMusicLoop = setInterval(function() {
- humanMusic();
- }, 2200);
- }
- }
- // Stops the loading music
- var stopLoadingMusic = function() {
- if (loadingMusicLoop != null) {
- loadingMusicPrematureStop = true;
- clearInterval(loadingMusicLoop);
- loadingMusicLoop = null;
- }
- }
- //url to download a file to play a song
- // Gets file as a blob (data URI)
- var urlToBlob = function(url, callback) {
- // show file download progress
- var downloading = null;
- mppChatSend(PRE_DOWNLOADING + ' ' + url);
- if (loadingOption) startLoadingMusic();
- else {
- var progress = 0;
- downloading = setInterval(function() {
- mppChatSend(PRE_DOWNLOADING + getProgress(progress));
- progress++;
- }, chatDelay);
- }
- fetch(url, {
- headers: {
- "Content-Disposition": "attachment" // this might not be doing anything
- }
- }).then(response => {
- stopLoadingMusic();
- clearInterval(downloading);
- if (!response.ok) {
- throw new Error("Network response was not ok");
- }
- return response.blob();
- }).then(blob => {
- stopLoadingMusic();
- clearInterval(downloading);
- callback(blob);
- }).catch(error => {
- console.error("Normal fetch couldn't get the file:", error);
- var corsUrl = useCorsUrl(url);
- if (corsUrl != null) {
- if (loadingOption) startLoadingMusic();
- fetch(corsUrl, {
- headers: {
- "Content-Disposition": "attachment" // this might not be doing anything
- }
- }).then(response => {
- stopLoadingMusic();
- clearInterval(downloading);
- if (!response.ok) {
- throw new Error("Network response was not ok");
- }
- return response.blob();
- }).then(blob => {
- stopLoadingMusic();
- clearInterval(downloading);
- callback(blob);
- }).catch(error => {
- console.error("CORS Anywhere API fetch couldn't get the file:", error);
- stopLoadingMusic();
- clearInterval(downloading);
- callback(null);
- });
- }
- // callback(null); // disabled since the second fetch already should call the call back
- });
- }
- //play song when we got correct link of song
- var fileOrBlobToBase64 = function(raw, callback) {
- if (raw == null) {
- stopLoadingMusic();
- callback(null);
- }
- // continue if we have a blob
- var reader = new FileReader();
- reader.readAsDataURL(raw);
- reader.onloadend = function() {
- var base64data = reader.result;
- callback(base64data);
- }
- }
- // Validates file or blob is a MIDI
- var isMidi = function(raw) {
- if (exists(raw)) {
- var mimetype = raw.type;
- // acceptable mimetypes for midi files
- switch(mimetype) {
- case "@file/mid": case "@file/midi":
- case "application/mid": case "application/midi":
- case "application/x-mid": case "application/x-midi":
- case "audio/mid": case "audio/midi":
- case "audio/x-mid": case "audio/x-midi":
- case "music/crescendo":
- case "x-music/mid": case "x-music/midi":
- case "x-music/x-mid": case "x-music/x-midi": return true; break;
- }
- }
- return false;
- }
- // Validates file or blob is application/octet-stream ... when using CORS
- var isOctetStream = function(raw) {
- if (exists(raw) && raw.type == "application/octet-stream") return true;
- else return false;
- }
- // Gets song from data URI and plays it.. play song when we got correct data
- var playSong = function(songFileName, songData) {
- // stop any current songs from playing
- stopSong();
- // play song if it loaded correctly
- try {
- // load song
- Player.loadDataUri(songData);
- // play song
- Player.play();
- ended = false;
- stopped = false;
- var timeoutRecorder = 0;
- var showSongName = setInterval(function() {
- if (Player.isPlaying()) {
- clearInterval(showSongName);
- // changes song
- var hasExtension = songFileName.lastIndexOf('.');
- previousSongData = currentSongData;
- previousSongName = currentSongName;
- currentSongData = songData;
- currentSongName = (hasExtension > 0) ? songFileName.substring(0, hasExtension) : songFileName;
- currentSongElapsedFormatted = timeSizeFormat(secondsToHms(0), currentSongDurationFormatted);
- currentSongDuration = Player.getSongTime();
- currentSongDurationFormatted = timeClearZeros(secondsToHms(currentSongDuration));
- mppChatSend(" Now playing " + quoteString(currentSongName));
- } else if (timeoutRecorder == SONG_NAME_TIMEOUT) {
- clearInterval(showSongName);
- } else timeoutRecorder++;
- }, 1);
- } catch(error) {
- stopLoadingMusic();
- // reload the previous working file if there is one
- if (previousSongData != null) Player.loadDataUri(previousSongData);
- mppChatSend(PRE_ERROR + " (play) " + error);
- }
- }
- //when midi file fount it just started playing
- Player.on('fileLoaded', function() {
- // Do something when file is loaded
- stopLoadingMusic();
- });
- // Plays the song from a URL if it's a MIDI
- var playURL = function(songUrl, songData) {
- currentFileLocation = songUrl;
- var songFileName = decodeURIComponent(currentFileLocation.substring(currentFileLocation.lastIndexOf('/') + 1));
- playSong(songFileName, songData);
- }
- //here is the main function to play song.. put the code into ur main command function
- function play(url) {
- var error = PRE_ERROR + " (play)";
- // URL needs to be entered to play a song
- if (!exists(url) || url == "") {
- stopLoadingMusic();
- mppChatSend(error + " No MIDI url entered... " + WHERE_TO_FIND_MIDIS);
- mppChatSend("pls enter a midi url");
- } else {
- // downloads file if possible and then plays it if it's a MIDI
- urlToBlob(url, function(blob) {
- 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);
- else if (isMidi(blob) || isOctetStream(blob)) {
- fileOrBlobToBase64(blob, function(base64data) {
- // play song only if we got data
- if (exists(base64data)) {
- if (isOctetStream(blob)) { // when download with CORS, need to replace mimetype, but it doesn't guarantee it's a MIDI file
- base64data = base64data.replace("application/octet-stream", "audio/midi");
- }
- playURL(url, base64data);
- } else mppChatSend(error + " Unexpected result, MIDI file couldn't load... " + WHERE_TO_FIND_MIDIS);
- });
- } else mppChatSend(error + " Invalid URL, this is not a MIDI file... " + WHERE_TO_FIND_MIDIS);
- });
- }
- }
- //stop the song.. put this code into stop command
- var stop = function() {
- // stops the current song
- if (ended) mppChatSend(' ' + NO_SONG);
- else {
- stopSong();
- paused = false;
- mppChatSend(" Stopped playing " + quoteString(currentSongName));
- currentFileLocation = currentSongName = null;
- }
- }
- //this is not main this just auto put "" around the string
- // Puts quotes around string
- var quoteString = function(string) {
- var newString = string;
- if (exists(string) && string != "") newString = '"' + string + '"';
- return newString
- }
Add Comment
Please, Sign In to add comment