Skip to content

Commit

Permalink
Merge pull request #397 from oxygen-dioxide/openmidi
Browse files Browse the repository at this point in the history
opening midi files from explorer or by dragging in
  • Loading branch information
stakira authored Jul 26, 2022
2 parents 72d3449 + f14eaef commit e733723
Show file tree
Hide file tree
Showing 3 changed files with 972 additions and 938 deletions.
234 changes: 120 additions & 114 deletions OpenUtau.Core/Format/Formats.cs
Original file line number Diff line number Diff line change
@@ -1,114 +1,120 @@
using System.Collections.Generic;
using System.IO;
using OpenUtau.Classic;
using OpenUtau.Core.Ustx;

namespace OpenUtau.Core.Format {
public enum ProjectFormats { Unknown, Vsq3, Vsq4, Ust, Ustx };

public static class Formats {
const string ustMatch = "[#SETTING]";
const string ustxMatchJson = "\"ustxVersion\":";
const string ustxMatchYaml = "ustx_version:";
const string vsq3Match = VSQx.vsq3NameSpace;
const string vsq4Match = VSQx.vsq4NameSpace;

public static ProjectFormats DetectProjectFormat(string file) {
var lines = new List<string>();
using (var reader = new StreamReader(file)) {
for (int i = 0; i < 10 && !reader.EndOfStream; ++i) {
lines.Add(reader.ReadLine());
}
}
string contents = string.Join("\n", lines);
if (contents.Contains(ustMatch)) {
return ProjectFormats.Ust;
} else if (contents.Contains(ustxMatchJson) || contents.Contains(ustxMatchYaml)) {
return ProjectFormats.Ustx;
} else if (contents.Contains(vsq3Match)) {
return ProjectFormats.Vsq3;
} else if (contents.Contains(vsq4Match)) {
return ProjectFormats.Vsq4;
} else {
return ProjectFormats.Unknown;
}
}

public static void LoadProject(string[] files) {
if (files.Length < 1) {
return;
}
ProjectFormats format = DetectProjectFormat(files[0]);
UProject project;
switch (format) {
case ProjectFormats.Ustx:
project = Ustx.Load(files[0]);
break;
case ProjectFormats.Vsq3:
case ProjectFormats.Vsq4:
project = VSQx.Load(files[0]);
break;
case ProjectFormats.Ust:
project = Ust.Load(files);
break;
default:
throw new FileFormatException("Unknown file format");
}
if (project != null) {
DocManager.Inst.ExecuteCmd(new LoadProjectNotification(project));
}
}

public static void ImportTracks(UProject project, string[] files) {
if (files.Length < 1) {
return;
}
int initialTracks = project.tracks.Count;
int initialParts = project.parts.Count;
foreach (string file in files) {
ProjectFormats format = DetectProjectFormat(file);
UProject loaded;
switch (format) {
case ProjectFormats.Ustx:
loaded = Ustx.Load(file);
break;
case ProjectFormats.Vsq3:
case ProjectFormats.Vsq4:
loaded = VSQx.Load(file);
break;
case ProjectFormats.Ust:
loaded = Ust.Load(new[] { file });
break;
default:
throw new FileFormatException("Unknown file format");
}
int trackCount = project.tracks.Count;
foreach (var (abbr, descriptor) in loaded.expressions) {
if (!project.expressions.ContainsKey(abbr)) {
project.expressions.Add(abbr, descriptor);
}
}
foreach (var track in loaded.tracks) {
track.TrackNo = project.tracks.Count;
project.tracks.Add(track);
}
foreach (var part in loaded.parts) {
project.parts.Add(part);
part.trackNo += trackCount;
}
project.beatPerBar = loaded.beatPerBar;
project.beatUnit = loaded.beatUnit;
project.bpm = loaded.bpm;
}
for (int i = initialTracks; i < project.tracks.Count; i++) {
project.tracks[i].AfterLoad(project);
}
for (int i = initialParts; i < project.parts.Count; i++) {
var part = project.parts[i];
part.AfterLoad(project, project.tracks[part.trackNo]);
}
project.ValidateFull();
DocManager.Inst.ExecuteCmd(new LoadProjectNotification(project));
}
}
}
using System.Collections.Generic;
using System.IO;
using OpenUtau.Classic;
using OpenUtau.Core.Ustx;

namespace OpenUtau.Core.Format {
public enum ProjectFormats { Unknown, Vsq3, Vsq4, Ust, Ustx, Midi};

public static class Formats {
const string ustMatch = "[#SETTING]";
const string ustxMatchJson = "\"ustxVersion\":";
const string ustxMatchYaml = "ustx_version:";
const string vsq3Match = VSQx.vsq3NameSpace;
const string vsq4Match = VSQx.vsq4NameSpace;
const string midiMatch = "MThd";

public static ProjectFormats DetectProjectFormat(string file) {
var lines = new List<string>();
using (var reader = new StreamReader(file)) {
for (int i = 0; i < 10 && !reader.EndOfStream; ++i) {
lines.Add(reader.ReadLine());
}
}
string contents = string.Join("\n", lines);
if (contents.Contains(ustMatch)) {
return ProjectFormats.Ust;
} else if (contents.Contains(ustxMatchJson) || contents.Contains(ustxMatchYaml)) {
return ProjectFormats.Ustx;
} else if (contents.Contains(vsq3Match)) {
return ProjectFormats.Vsq3;
} else if (contents.Contains(vsq4Match)) {
return ProjectFormats.Vsq4;
} else if (contents.Contains(midiMatch)) {
return ProjectFormats.Midi;
} else {
return ProjectFormats.Unknown;
}
}

public static void LoadProject(string[] files) {
if (files.Length < 1) {
return;
}
ProjectFormats format = DetectProjectFormat(files[0]);
UProject project;
switch (format) {
case ProjectFormats.Ustx:
project = Ustx.Load(files[0]);
break;
case ProjectFormats.Vsq3:
case ProjectFormats.Vsq4:
project = VSQx.Load(files[0]);
break;
case ProjectFormats.Ust:
project = Ust.Load(files);
break;
case ProjectFormats.Midi:
project = Midi.LoadProject(files[0]);
break;
default:
throw new FileFormatException("Unknown file format");
}
if (project != null) {
DocManager.Inst.ExecuteCmd(new LoadProjectNotification(project));
}
}

public static void ImportTracks(UProject project, string[] files) {
if (files.Length < 1) {
return;
}
int initialTracks = project.tracks.Count;
int initialParts = project.parts.Count;
foreach (string file in files) {
ProjectFormats format = DetectProjectFormat(file);
UProject loaded;
switch (format) {
case ProjectFormats.Ustx:
loaded = Ustx.Load(file);
break;
case ProjectFormats.Vsq3:
case ProjectFormats.Vsq4:
loaded = VSQx.Load(file);
break;
case ProjectFormats.Ust:
loaded = Ust.Load(new[] { file });
break;
default:
throw new FileFormatException("Unknown file format");
}
int trackCount = project.tracks.Count;
foreach (var (abbr, descriptor) in loaded.expressions) {
if (!project.expressions.ContainsKey(abbr)) {
project.expressions.Add(abbr, descriptor);
}
}
foreach (var track in loaded.tracks) {
track.TrackNo = project.tracks.Count;
project.tracks.Add(track);
}
foreach (var part in loaded.parts) {
project.parts.Add(part);
part.trackNo += trackCount;
}
project.beatPerBar = loaded.beatPerBar;
project.beatUnit = loaded.beatUnit;
project.bpm = loaded.bpm;
}
for (int i = initialTracks; i < project.tracks.Count; i++) {
project.tracks[i].AfterLoad(project);
}
for (int i = initialParts; i < project.parts.Count; i++) {
var part = project.parts[i];
part.AfterLoad(project, project.tracks[part.trackNo]);
}
project.ValidateFull();
DocManager.Inst.ExecuteCmd(new LoadProjectNotification(project));
}
}
}
17 changes: 17 additions & 0 deletions OpenUtau.Core/Format/Midi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ public void Notify(string charset) {
}
public static class Midi
{
static public UProject LoadProject(string file) {
UProject uproject = new UProject();
Ustx.AddDefaultExpressions(uproject);

uproject.tracks = new List<UTrack>();

var parts = Load(file, uproject);
foreach (var part in parts) {
var track = new UTrack();
track.TrackNo = uproject.tracks.Count;
part.trackNo = track.TrackNo;
part.AfterLoad(uproject, track);
uproject.tracks.Add(track);
uproject.parts.Add(part);
}
return uproject;
}
static public List<UVoicePart> Load(string file, UProject project)
{
List<UVoicePart> resultParts = new List<UVoicePart>();
Expand Down
Loading

0 comments on commit e733723

Please sign in to comment.