Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
BGM_Tools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GovanifY
BGM_Tools
Commits
56e19a28
Commit
56e19a28
authored
7 years ago
by
GovanifY
Committed by
GitHub
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Figured I'd fix this since Xad linked it
parent
ee2d51f4
Branches
master
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
MIDI2BGM/Program.cs
+55
-39
55 additions, 39 deletions
MIDI2BGM/Program.cs
with
55 additions
and
39 deletions
MIDI2BGM/Program.cs
+
55
−
39
View file @
56e19a28
/* This is a very early MIDI to BGM converter. There is still a lot unknown. */
//I remember having fucked up something here, but don't have the time to fix it for now.
/* This is a very early MIDI to BGM converter. There is still a lot unknown. */
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
...
...
@@ -16,19 +15,22 @@ public static void WriteBytes(byte[] i, BinaryWriter writer)
writer
.
Write
(
i
);
}
public
static
void
WriteDelta
(
in
t
i
,
BinaryWriter
writer
)
public
static
void
WriteDelta
(
floa
t
i
,
BinaryWriter
writer
)
{
if
(
i
>
0xFFFFFFF
)
{
throw
new
Exception
(
"
D
elta
is too long!"
);
}
//Welp, never know(?
)
if
((
i
%
1
)
!=
0
)
{
i
=
(
int
)
Math
.
Round
((
double
)
i
,
MidpointRounding
.
AwayFromZero
);
}
// Console.WriteLine
("
d
elta
_rounded: {0}", i
);
while
(
i
>
0xFFFFFFF
)
{
WriteDummy
(
0xFFFFFFF
,
writer
);
i
-=
0xFFFFFFF
;
}
//Console.WriteLine("delta_rounded_2: {0}", i);
UInt32
b
=
((
UInt32
)
i
&
0x7F
);
while
(
Convert
.
ToBoolean
(
i
>>=
7
))
//Console.WriteLine("delta_rounded_3: {0}", b);
int
tmp
=
(
int
)
i
;
while
(
Convert
.
ToBoolean
(
tmp
>>=
7
))
{
b
<<=
8
;
b
|=
(
(
uint
)(
i
&
0x7F
)
|
0x80
);
b
|=
(
uint
)(
(
tmp
&
0x7F
)
|
0x80
);
}
// Console.WriteLine("delta_rounded_4: {0}", b);
do
{
writer
.
Write
((
byte
)
b
);
...
...
@@ -44,12 +46,11 @@ public static void WriteDummy(int i, BinaryWriter writer)
{
if
(
i
==
0
)
{
return
;
}
WriteDelta
(
i
,
writer
);
byte
[]
buffer1
=
{
0xff
,
6
,
0
};
byte
[]
buffer1
=
{
0x5d
,
0
};
WriteBytes
(
buffer1
,
writer
);
}
}
static
void
Main
(
string
[]
args
)
...
...
@@ -80,7 +81,7 @@ public static void GetBGM(string nme)
byte
trackC
;
ushort
ppqn
;
long
ActualPos
;
in
t
delta
;
floa
t
delta
;
int
deltaMod
=
1
;
FileStream
midS
=
File
.
Open
(
nme
,
FileMode
.
Open
,
FileAccess
.
Read
);
BinaryReader
mid
=
new
BinaryReader
(
midS
);
...
...
@@ -91,7 +92,7 @@ public static void GetBGM(string nme)
byte
track
=
0
;
byte
channel
=
0
;
byte
[]
command
;
byte
[]
towrite
;
byte
[]
towrite
=
new
byte
[
0
]
;
byte
SubCommand
;
long
trackLenOffset
;
UInt16
temp
;
...
...
@@ -198,14 +199,15 @@ public static void GetBGM(string nme)
for
(
ActualPos
+=
midS
.
Position
;
midS
.
Position
<
ActualPos
-
1
;)
{
do
{
byte
tmptbyte
;
t
=
tmptbyte
=
mid
.
ReadByte
();
delta
=
(
delta
<<
7
)
+
(
tmptbyte
&
0x7f
);
}
while
((
t
&
0x80
)
!=
0
);
delta
=
(
(
int
)
delta
<<
7
)
+
(
tmptbyte
&
0x7f
);
}
while
(
Convert
.
ToBoolean
(
t
&
0x80
));
delta
/=
deltaMod
;
//Console.WriteLine("delta: {0}", delta);
/*if(track>2){//skip track
midS.Position=tSzT;
...
...
@@ -219,13 +221,12 @@ public static void GetBGM(string nme)
}*/
cmd
=
mid
.
ReadByte
();
//Console.WriteLine("Current command: {0:x2}",cmd);
if
(
cmd
==
0xFF
)
{
command
=
new
byte
[]
{
mid
.
ReadByte
(),
mid
.
ReadByte
()
};
Console
.
WriteLine
(
"Current command: {0:x2} {1:x2} {2:x2}"
,
cmd
,
command
[
0
],
command
[
1
]);
//
Console.WriteLine("Current command: {0:x2} {1:x2} {2:x2}", cmd, command[0], command[1]);
switch
(
command
[
0
])
{
case
0x2F
:
...
...
@@ -302,6 +303,7 @@ public static void GetBGM(string nme)
default
:
midS
.
Position
+=
command
[
1
];
Console
.
WriteLine
(
"Unknown command1: 0x{0:x2} 0x{1:x2}"
,
cmd
,
command
[
0
]);
towrite
=
new
byte
[
0
];
break
;
}
...
...
@@ -312,6 +314,7 @@ public static void GetBGM(string nme)
{
cmd
=
mid
.
ReadByte
();
SubCommand
=
mid
.
ReadByte
();
// Console.WriteLine("Current command: {0:x2} {1:x2}", cmd, SubCommand);
switch
(
cmd
)
{
case
7
:
...
...
@@ -350,6 +353,7 @@ public static void GetBGM(string nme)
}
else
{
// Console.WriteLine("Current command: {0:x2}", cmd);
if
((
cmd
&
0xC0
)
==
0xC0
)
{
WriteDelta
(
delta
,
bgm
);
...
...
@@ -373,7 +377,8 @@ public static void GetBGM(string nme)
}
else
{
towrite
=
new
byte
[]
{
0x13
,
lVelocity
=
command
[
1
]
};
lVelocity
=
command
[
1
];
towrite
=
new
byte
[]
{
0x13
,
lVelocity
};
WriteBytes
(
towrite
,
bgm
);
}
}
...
...
@@ -395,32 +400,43 @@ public static void GetBGM(string nme)
}
}
if
((
cmd
&
0x80
)
==
0x80
)
else
{
command
=
new
byte
[]
{
mid
.
ReadByte
(),
mid
.
ReadByte
()
};
WriteDelta
(
delta
,
bgm
);
delta
=
0
;
if
(
command
[
0
]
==
lKey
)
{
towrite
=
new
byte
[]
{
0x18
};
WriteBytes
(
towrite
,
bgm
);
}
else
if
((
cmd
&
0x80
)
==
0x80
)
{
lKey
=
command
[
0
];
towrite
=
new
byte
[]
{
0x1A
,
lKey
};
WriteBytes
(
towrite
,
bgm
);
command
=
new
byte
[]
{
mid
.
ReadByte
(),
mid
.
ReadByte
()
};
WriteDelta
(
delta
,
bgm
);
delta
=
0
;
//Console.WriteLine("wtf: {0:x2}, {1:x2}", command[0], lKey);
if
(
command
[
0
]
==
lKey
)
{
towrite
=
new
byte
[]
{
0x18
};
WriteBytes
(
towrite
,
bgm
);
}
else
{
lKey
=
command
[
0
];
towrite
=
new
byte
[]
{
0x1A
,
lKey
};
WriteBytes
(
towrite
,
bgm
);
}
}
}
else
{
Console
.
WriteLine
(
"Unknown command3: 0x{0:x2}"
,
cmd
);
else
{
Console
.
WriteLine
(
"Unknown command3: 0x{0:x2}"
,
cmd
);
}
}
}
}
}
/* Console.Write("{0}: [ ", (bgm.BaseStream.Position - towrite.Length).ToString("X"));
int y = 0;
foreach (var item in towrite)
{
if (y > 0) { Console.Write(", "); }
Console.Write(item.ToString("X"));
y++;
}
Console.WriteLine("]");*/
}
if
(
midS
.
Position
!=
ActualPos
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment