Updated to support midi

This commit is contained in:
hhhzzzsss 2020-08-09 22:39:47 -05:00 committed by GitHub
parent d6ce6a59c3
commit 3857f79059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 74 additions and 36 deletions

Binary file not shown.

Binary file not shown.

View file

@ -109,6 +109,11 @@ public class CommandProcessor {
SongPlayer.addChatMessage("§cCould not find song §4" + args);
return true;
}
catch (Exception e) {
SongPlayer.addChatMessage("§cError getting song: " + e.getMessage());
e.printStackTrace();
return true;
}
SongPlayer.stage = new Stage();
SongPlayer.stage.movePlayerToStagePosition();

View file

@ -1,5 +1,9 @@
package com.github.hhhzzzsss.songplayer.song;
import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import com.github.hhhzzzsss.songplayer.SongPlayer;
import com.github.hhhzzzsss.songplayer.noteblocks.BuildingThread;

View file

@ -52,28 +52,9 @@ public class MidiConverter {
public static String fileName = "moskau";
public static TreeMap<Long, ArrayList<Integer>> noteMap;
public static TreeMap<Long, ArrayList<Integer>> getMidi(String url) throws Exception {
public static TreeMap<Long, ArrayList<Integer>> getMidi(BufferedInputStream downloadStream) throws Exception {
noteMap = new TreeMap<>();
//SocketAddress addr = new InetSocketAddress("34.94.90.93", 3128);
//Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
URLConnection conn = (new URL(url)).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
BufferedInputStream downloadStream = new BufferedInputStream(conn.getInputStream());
boolean fileTooLarge = true;
byte tempbuf[] = new byte[10240];
for (int i=0; i<1024; i++) {
if (downloadStream.read(tempbuf, 0, 1024) == -1) {
fileTooLarge = false;
break;
}
}
if (fileTooLarge) {
throw new IOException("File too large");
}
downloadStream.close();
downloadStream = new BufferedInputStream(new URL(url).openStream());
Sequence sequence = MidiSystem.getSequence(downloadStream);
long tpq = sequence.getResolution();

View file

@ -1,10 +1,14 @@
package com.github.hhhzzzsss.songplayer.song;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Map;
import java.util.TreeMap;
@ -33,31 +37,67 @@ public class Song {
return notes.size();
}
public static Song getSongFromFile(String file) throws IOException {
public static Song getSongFromFile(String file) throws Exception {
Song song = new Song();
if (file.contains("/") || file.contains("\\")) throw new FileNotFoundException();
File songPath = new File(SongPlayer.SONG_DIR, file);
if (!songPath.exists()) songPath = new File(SongPlayer.SONG_DIR, file + ".txt");
if (!songPath.exists()) throw new FileNotFoundException();
song.notes.clear();
BufferedReader br = new BufferedReader(new FileReader(songPath));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" ");
long time = Long.parseLong(split[0]);
int note = Integer.parseInt(split[1]);
song.requiredNotes[note] = true;
song.notes.add(new NoteEvent(note, time));
if (!songPath.exists()) {
songPath = new File(SongPlayer.SONG_DIR, file + ".txt");
}
if (!songPath.exists()) {
songPath = new File(SongPlayer.SONG_DIR, file + ".mid");
System.out.println(file + ".mid");
}
if (!songPath.exists()) {
songPath = new File(SongPlayer.SONG_DIR, file + ".midi");
}
if (!songPath.exists()) throw new FileNotFoundException();
boolean isMidi = false;
String extension = getExtension(songPath);
if (extension.equalsIgnoreCase("mid") || extension.equalsIgnoreCase("midi")) isMidi = true;
System.out.println(songPath.getName());
System.out.println(extension);
System.out.println(isMidi);
if (isMidi) {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(songPath));
song.notes.clear();
TreeMap<Long, ArrayList<Integer>> noteMap = MidiConverter.getMidi(bis);
System.out.println(noteMap.size());
for (int i=0; i<400; i++) song.requiredNotes[i] = false;
for (Map.Entry<Long, ArrayList<Integer>> entry : noteMap.entrySet()) {
for (int note : entry.getValue()) {
long time = entry.getKey();
song.requiredNotes[note] = true;
song.add(new NoteEvent(note, time));
}
}
song.name = songPath.getName();
return song;
}
else {
song.notes.clear();
BufferedReader br = new BufferedReader(new FileReader(songPath));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" ");
long time = Long.parseLong(split[0]);
int note = Integer.parseInt(split[1]);
song.requiredNotes[note] = true;
song.notes.add(new NoteEvent(note, time));
}
br.close();
song.name = songPath.getName();
return song;
}
br.close();
song.name = songPath.getName();
return song;
}
public static Song getSongFromUrl(String url) throws Exception {
URLConnection conn = (new URL(url)).openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
BufferedInputStream downloadStream = new BufferedInputStream(conn.getInputStream());
Song song = new Song();
song.notes.clear();
TreeMap<Long, ArrayList<Integer>> noteMap = MidiConverter.getMidi(url);
TreeMap<Long, ArrayList<Integer>> noteMap = MidiConverter.getMidi(downloadStream);
System.out.println(noteMap.size());
for (int i=0; i<400; i++) song.requiredNotes[i] = false;
for (Map.Entry<Long, ArrayList<Integer>> entry : noteMap.entrySet()) {
@ -71,4 +111,12 @@ public class Song {
return song;
}
private static String getExtension(File file) {
String name = file.getName();
if(name.lastIndexOf(".") != -1 && name.lastIndexOf(".") != 0)
return name.substring(name.lastIndexOf(".") + 1);
else
return "";
}
}