-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathopen_gp_file.cs
executable file
·137 lines (116 loc) · 4.58 KB
/
open_gp_file.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using SFB;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using UnityEngine;
public class open_gp_file : MonoBehaviour {
public string Title = "";
public string FileName = "";
public string Directory = "";
public string Extension = "";
public bool Multiselect = false;
GPFile gpfile;
public void open_file_dialog()
{
var paths = StandaloneFileBrowser.OpenFilePanel(Title, Directory, Extension, Multiselect);
if (paths.Length > 0)
{
StartCoroutine(OutputRoutine(new System.Uri(paths[0]).AbsoluteUri));
}
}
private IEnumerator OutputRoutine(string url)
{
var loader = new WWW(url);
yield return loader;
//Detect Version by Filename
int version = 7;
string fileEnding = Path.GetExtension(url);
if (fileEnding.Equals(".gp3")) version = 3;
if (fileEnding.Equals(".gp4")) version = 4;
if (fileEnding.Equals(".gp5")) version = 5;
if (fileEnding.Equals(".gpx")) version = 6;
if (fileEnding.Equals(".gp")) version = 7;
switch (version)
{
case 3:
gpfile = new GP3File(loader.bytes);
gpfile.readSong();
break;
case 4:
gpfile = new GP4File(loader.bytes);
gpfile.readSong();
break;
case 5:
gpfile = new GP5File(loader.bytes);
gpfile.readSong();
break;
case 6:
gpfile = new GP6File(loader.bytes);
gpfile.readSong();
gpfile = gpfile.self; //Replace with transferred GP5 file
break;
case 7:
string archiveName = url.Substring(8).Replace("%20"," ");
byte[] buffer = new byte[8200000];
MemoryStream stream = new MemoryStream(buffer);
using (var unzip = new Unzip(archiveName))
{
//Console.WriteLine("Listing files in the archive:");
ListFiles(unzip);
unzip.Extract("Content/score.gpif", stream);
stream.Position = 0;
var sr = new StreamReader(stream);
string gp7xml = sr.ReadToEnd();
gpfile = new GP7File(gp7xml);
gpfile.readSong();
gpfile = gpfile.self; //Replace with transferred GP5 file
}
break;
default:
Debug.Log("Unknown File Format");
break;
}
Debug.Log("Done");
var song = new Native.NativeFormat(gpfile);
var midi = song.toMidi();
List<byte> data = midi.createBytes();
var dataArray = data.ToArray();
using (var fs = new FileStream("output.mid", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
fs.Write(dataArray, 0, dataArray.Length);
}
//Create Example Track (delete)
/*
MidiExport.MidiExport midi = new MidiExport.MidiExport();
midi.fileType = 2;
midi.ticksPerBeat = 960;
MidiExport.MidiTrack track = new MidiExport.MidiTrack();
track.messages.Add(new MidiExport.MidiMessage("track_name",new string[] { "untitled" },0));
track.messages.Add(new MidiExport.MidiMessage("time_signature", new string[] {"4","4","24","8" }, 0));
track.messages.Add(new MidiExport.MidiMessage("control_change", new string[] { "0","7", "100" }, 0));
track.messages.Add(new MidiExport.MidiMessage("program_change", new string[] { "0", "48" }, 0));
track.messages.Add(new MidiExport.MidiMessage("note_on", new string[] { "0", "55","91" }, 0));
track.messages.Add(new MidiExport.MidiMessage("note_on", new string[] { "0", "55", "0" }, 256));
track.messages.Add(new MidiExport.MidiMessage("end_of_track", new string[] { }, 0));
midi.midiTracks.Add(track);
midi.createBytes();
*/
}
private static void ListFiles(Unzip unzip)
{
var tab = unzip.Entries.Any(e => e.IsDirectory) ? "\t" : string.Empty;
foreach (var entry in unzip.Entries.OrderBy(e => e.Name))
{
if (entry.IsFile)
{
//Debug.Log(tab + entry.Name+": "+ entry.CompressedSize + " -> "+ entry.OriginalSize);
continue;
}
//Debug.Log(entry.Name);
}
//Debug.Log("");
}
}