Example #1
0
        private static int ConvertHMPToMIDI(ProgramCall prog)
        {
            if (prog.HmqMode != FMMode.None)
            {
                PrintHelp();
                return(2);
            }

            MIDISequence midi;

            try
            {
                midi = MIDISequence.LoadHMP(ReadFile(prog.SourceFile));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Could not load HMP!");
                Console.Error.WriteLine(ex.ToString());
                return(2);
            }

            midi.Convert(MIDIFormat.Type1);
            midi.PulsesPerQuarter = 60;
            midi.AdjustPPQ(480);

            if (prog.Flags.HasFlag(ProgramFlags.FaithfulConversionToMIDI))
            {
                // add track volume 0 to all tracks without them
                // (this is how the original SOS engine works)
                foreach (MIDITrack trk in midi.Tracks)
                {
                    IEnumerable <MIDIMessage> messages = trk.GetAllEvents().Select(e => e.Data);
                    if (!messages.OfType <MIDIControlChangeMessage>().Where(m => m.Controller == MIDIControl.ChannelVolumeMSB).Any())
                    {
                        MIDIMessage chm = messages.Where(m => !m.IsExtendedEvent).FirstOrDefault();
                        if (chm != null)
                        {
                            trk.AddEvent(new MIDIEvent(0, new MIDIControlChangeMessage(chm.Channel, MIDIControl.ChannelVolumeMSB, 0)), true);
                        }
                    }
                }
            }

            try
            {
                if (!Overwrite(prog.Flags, prog.TargetFile))
                {
                    return(0);
                }
                WriteFile(midi, prog.TargetFile);
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine("Could not save MIDI!");
                Console.Error.WriteLine(ex.ToString());
                return(2);
            }
            Console.Out.WriteLine("Successfully converted and saved MIDI file");
            return(0);
        }