Skip to content

Commit

Permalink
Automatically calculate song base volume
Browse files Browse the repository at this point in the history
  • Loading branch information
Kermalis committed Sep 29, 2018
1 parent 74a288b commit 6dd3d17
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions GBAMusicStudio/Core/Song.cs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,14 @@ public override void SaveAsMIDI(string fileName, MIDISaveArgs args)
if (NumTracks == 0)
throw new InvalidDataException(Strings.ErrorNoTracks);

int baseVolume = 0x7F;
// Find highest volume
if (args.ReverseVolume)
{
baseVolume = Commands.UniteAll().Where(e => e.Command is VolumeCommand).Select(e => ((VolumeCommand)e.Command).Volume).Max();
Console.WriteLine("Reversing volume back to {0}.", baseVolume);
}

CalculateTicks();
var midi = new Sequence(24) { Format = 1 };
var metaTrack = new Sanford.Multimedia.Midi.Track();
Expand Down Expand Up @@ -492,11 +500,10 @@ public override void SaveAsMIDI(string fileName, MIDISaveArgs args)
track.Insert(ticks, new ChannelMessage(ChannelCommand.ProgramChange, i, voice.Voice));
break;
case VolumeCommand vol:
// If we want to match a BaseVolume then we need to reverse calculate
double d = args.BaseVolume / (double)0x7F;
double d = baseVolume / (double)0x7F;
int volume = (int)(vol.Volume / d);
// If there are rounding errors, fix them (happens if BaseVolume is not 127 and BaseVolume is not vol.Volume)
if (volume * args.BaseVolume / 0x7F == vol.Volume - 1)
// If there are rounding errors, fix them (happens if baseVolume is not 127 and baseVolume is not vol.Volume)
if (volume * baseVolume / 0x7F == vol.Volume - 1)
volume++;
track.Insert(ticks, new ChannelMessage(ChannelCommand.Controller, i, (int)ControllerType.Volume, volume));
break;
Expand Down
2 changes: 1 addition & 1 deletion GBAMusicStudio/Core/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Note
class MIDISaveArgs
{
public bool SaveBeforeKeysh; // M4A
public int BaseVolume = 127;
public bool ReverseVolume = false;
public List<Pair<int, Pair<byte, byte>>> TimeSignatures; // {AbsoluteTick, {Numerator, Denominator}}
}
}
2 changes: 1 addition & 1 deletion GBAMusicStudio/UI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ void ExportMIDI(object sender, EventArgs e)
var args = new MIDISaveArgs
{
SaveBeforeKeysh = false,
//BaseVolume = 80,
ReverseVolume = false,
TimeSignatures = new List<Pair<int, Pair<byte, byte>>>
{
new Pair<int, Pair<byte, byte>>(0, new Pair<byte, byte>(4, 4)),
Expand Down

0 comments on commit 6dd3d17

Please sign in to comment.