private string getChannelString(ChannelMessage channelMessage, MidiEvent midiEvent, ref StringBuilder lilyString)
        {
            string returnString = "";

            if (channelMessage.Command == ChannelCommand.NoteOn)
            {
                if (channelMessage.Data2 > 0) // Data2 = loudness
                {
                    // Append the new note.
                    lilyString.Append(MidiToLilyHelper.GetLilyNoteName(_previousMidiKey, channelMessage.Data1));
                    returnString        += MidiToLilyHelper.GetLilyNoteName(_previousMidiKey, channelMessage.Data1);
                    _previousMidiKey     = channelMessage.Data1;
                    _startedNoteIsClosed = false;
                }
                else if (!_startedNoteIsClosed)
                {
                    // Finish the previous note with the length.
                    double percentageOfBar;
                    string noteLength = MidiToLilyHelper.GetLilypondNoteLength(_previousNoteAbsoluteTicks, midiEvent.AbsoluteTicks, _division, _beatNote, _beatsPerBar, out percentageOfBar);
                    lilyString.Append(noteLength);
                    _previousNoteAbsoluteTicks = midiEvent.AbsoluteTicks;
                    lilyString.Append(" ");

                    returnString += noteLength;
                    returnString += " ";

                    _percentageOfBarReached += percentageOfBar;
                    if (_percentageOfBarReached >= 1)
                    {
                        lilyString.AppendLine("|");
                        returnString            += "|";
                        _percentageOfBarReached -= 1;
                    }
                    _startedNoteIsClosed = true;
                }
                else
                {
                    returnString += "r";
                    lilyString.Append("r");
                }
            }
            return(returnString);
        }
        private string getEndOfTrackString(MetaMessage metaMessage, MidiEvent midiEvent, ref StringBuilder lilyString)
        {
            string returnString = "";

            if (_previousNoteAbsoluteTicks > 0)
            {
                // Finish the last notelength.
                double percentageOfBar;
                string noteLength = MidiToLilyHelper.GetLilypondNoteLength(_previousNoteAbsoluteTicks, midiEvent.AbsoluteTicks, _division, _beatNote, _beatsPerBar, out percentageOfBar);
                returnString += noteLength;
                returnString += " ";
                lilyString.Append(noteLength);
                lilyString.Append(" ");

                _percentageOfBarReached += percentageOfBar;
                if (_percentageOfBarReached >= 1)
                {
                    returnString += "|";
                    lilyString.AppendLine("|");
                    percentageOfBar = percentageOfBar - 1;
                }
            }
            return(returnString);
        }
Esempio n. 3
0
        public string Load(string fileName)
        {
            Sequence sequence = new Sequence();

            sequence.Load(fileName);

            StringBuilder lilypondContent = new StringBuilder();

            lilypondContent.AppendLine("\\relative c' {");
            lilypondContent.AppendLine("\\clef treble");

            int    division                  = sequence.Division;
            int    previousMidiKey           = 60; // Central C;
            int    previousNoteAbsoluteTicks = 0;
            double percentageOfBarReached    = 0;
            bool   startedNoteIsClosed       = true;

            for (int i = 0; i < sequence.Count(); i++)
            {
                Track track = sequence[i];

                foreach (var midiEvent in track.Iterator())
                {
                    IMidiMessage midiMessage = midiEvent.MidiMessage;
                    // TODO: Split this switch statements and create separate logic.
                    // We want to split this so that we can expand our functionality later with new keywords for example.
                    // Hint: Command pattern? Strategies? Factory method?
                    switch (midiMessage.MessageType)
                    {
                    case MessageType.Meta:
                        var metaMessage = midiMessage as MetaMessage;
                        switch (metaMessage.MetaType)
                        {
                        case MetaType.TimeSignature:
                            byte[] timeSignatureBytes = metaMessage.GetBytes();
                            _beatNote    = timeSignatureBytes[0];
                            _beatsPerBar = (int)(1 / Math.Pow(timeSignatureBytes[1], -2));
                            lilypondContent.AppendLine($"\\time {_beatNote}/{_beatsPerBar}");
                            break;

                        case MetaType.Tempo:
                            byte[] tempoBytes = metaMessage.GetBytes();
                            int    tempo      = (tempoBytes[0] & 0xff) << 16 | (tempoBytes[1] & 0xff) << 8 | (tempoBytes[2] & 0xff);
                            _bpm = 60000000 / tempo;
                            lilypondContent.AppendLine($"\\tempo 4={_bpm}");
                            break;

                        case MetaType.EndOfTrack:
                            if (previousNoteAbsoluteTicks > 0)
                            {
                                // Finish the last notelength.
                                double percentageOfBar;
                                lilypondContent.Append(MidiToLilyHelper.GetLilypondNoteLength(previousNoteAbsoluteTicks, midiEvent.AbsoluteTicks, division, _beatNote, _beatsPerBar, out percentageOfBar));
                                lilypondContent.Append(" ");

                                percentageOfBarReached += percentageOfBar;
                                if (percentageOfBarReached >= 1)
                                {
                                    lilypondContent.AppendLine("|");
                                    percentageOfBar = percentageOfBar - 1;
                                }
                            }
                            break;

                        default: break;
                        }
                        break;

                    case MessageType.Channel:
                        var channelMessage = midiEvent.MidiMessage as ChannelMessage;
                        if (channelMessage.Command == ChannelCommand.NoteOn)
                        {
                            if (channelMessage.Data2 > 0)     // Data2 = loudness
                            {
                                // Append the new note.
                                lilypondContent.Append(MidiToLilyHelper.GetLilyNoteName(previousMidiKey, channelMessage.Data1));

                                previousMidiKey     = channelMessage.Data1;
                                startedNoteIsClosed = false;
                            }
                            else if (!startedNoteIsClosed)
                            {
                                // Finish the previous note with the length.
                                double percentageOfBar;
                                lilypondContent.Append(MidiToLilyHelper.GetLilypondNoteLength(previousNoteAbsoluteTicks, midiEvent.AbsoluteTicks, division, _beatNote, _beatsPerBar, out percentageOfBar));
                                previousNoteAbsoluteTicks = midiEvent.AbsoluteTicks;
                                lilypondContent.Append(" ");

                                percentageOfBarReached += percentageOfBar;
                                if (percentageOfBarReached >= 1)
                                {
                                    lilypondContent.AppendLine("|");
                                    percentageOfBarReached -= 1;
                                }
                                startedNoteIsClosed = true;
                            }
                            else
                            {
                                lilypondContent.Append("r");
                            }
                        }
                        break;
                    }
                }
            }

            lilypondContent.Append("}");

            return(lilypondContent.ToString());
        }