Esempio n. 1
0
        private static TrackChunk AddEventsOfNote(
            TrackChunk chunkito,
            Note n,
            byte channel)
        {
            var noteOn = new NoteOnEvent()
            {
                Channel    = new FourBitNumber(channel),
                DeltaTime  = n.StartSinceBeginningOfSongInTicks,
                NoteNumber = new SevenBitNumber(n.Pitch),
                Velocity   = new SevenBitNumber(n.Volume)
            };

            chunkito.Events.Add(noteOn);
            var noteOff = new NoteOffEvent()
            {
                Channel    = new FourBitNumber(channel),
                DeltaTime  = n.EndSinceBeginningOfSongInTicks,
                NoteNumber = new SevenBitNumber(n.Pitch)
            };

            chunkito.Events.Add(noteOff);
            foreach (var pb in n.PitchBending)
            {
                var p = new PitchBendEvent()
                {
                    Channel    = new FourBitNumber(channel),
                    DeltaTime  = pb.TicksSinceBeginningOfSong,
                    PitchValue = pb.Pitch
                };
                chunkito.Events.Add(p);
            }
            return(chunkito);
        }
        public void PitchBend_CreateWithPitchValue()
        {
            const ushort pitchValue = 1234;

            var pitchBendEvent = new PitchBendEvent(pitchValue);

            Assert.AreEqual(pitchValue, pitchBendEvent.PitchValue, "Pitch value is set incorrectly.");
        }
Esempio n. 3
0
        private void InitializePitchBendData(PitchBendEvent pitchBendEvent, long time)
        {
            if (pitchBendEvent == null)
            {
                return;
            }

            _pitchValueLines[pitchBendEvent.Channel].SetValue(time, pitchBendEvent.PitchValue);
        }
Esempio n. 4
0
        private void UpdateCurrentPitchBendData(PitchBendEvent pitchBendEvent)
        {
            if (pitchBendEvent == null)
            {
                return;
            }

            _currentPitchValues[pitchBendEvent.Channel] = pitchBendEvent.PitchValue;
        }
        public void PitchBend_SetPitchValue()
        {
            const ushort pitchValue = 1234;

            var pitchBendEvent = new PitchBendEvent();

            pitchBendEvent.PitchValue = pitchValue;
            Assert.AreEqual(pitchValue, pitchBendEvent.PitchValue, "Pitch value is set incorrectly.");
        }
Esempio n. 6
0
            public void PitchBendEvent()
            {
                var value = GetRandomValue();

                var x = new PitchBendEvent(5, PitchWheelStep.WholeStepDown);
                var y = ReDeserialize(x);

                Assert.That(x.UpperBits == y.UpperBits);
                Assert.That(x.LowerBits == y.LowerBits);
                Assert.That(x.Position == y.Position);
            }
Esempio n. 7
0
            private void _read()
            {
                _vTime       = new VlqBase128Be(m_io);
                _eventHeader = m_io.ReadU1();
                if (EventHeader == 255)
                {
                    _metaEventBody = new MetaEventBody(m_io, this, m_root);
                }
                if (EventHeader == 240)
                {
                    _sysexBody = new SysexEventBody(m_io, this, m_root);
                }
                switch (EventType)
                {
                case 224: {
                    _eventBody = new PitchBendEvent(m_io, this, m_root);
                    break;
                }

                case 144: {
                    _eventBody = new NoteOnEvent(m_io, this, m_root);
                    break;
                }

                case 208: {
                    _eventBody = new ChannelPressureEvent(m_io, this, m_root);
                    break;
                }

                case 192: {
                    _eventBody = new ProgramChangeEvent(m_io, this, m_root);
                    break;
                }

                case 160: {
                    _eventBody = new PolyphonicPressureEvent(m_io, this, m_root);
                    break;
                }

                case 176: {
                    _eventBody = new ControllerEvent(m_io, this, m_root);
                    break;
                }

                case 128: {
                    _eventBody = new NoteOffEvent(m_io, this, m_root);
                    break;
                }
                }
            }
        public static SongSimplification GetSimplificationZeroOfSong(string base64encodedMidiFile)
        {
            var  notesObj                     = new List <Note>();
            var  midiFile                     = MidiFile.Read(base64encodedMidiFile);
            long songDuration                 = GetSongDurationInTicks(base64encodedMidiFile);
            var  isSustainPedalOn             = false;
            var  notesOnBecauseOfSustainPedal = new List <Note>();
            var  instrumentOfChannel          = new byte[16];

            short chunkNo = -1;

            foreach (TrackChunk chunk in midiFile.Chunks)
            {
                chunkNo++;
                var  currentNotes = new List <Note>();
                long currentTick  = 0;

                foreach (MidiEvent eventito in chunk.Events)
                {
                    currentTick += eventito.DeltaTime;

                    if (eventito is ProgramChangeEvent)
                    {
                        var pg = eventito as ProgramChangeEvent;
                        instrumentOfChannel[pg.Channel] = (byte)pg.ProgramNumber.valor;
                        continue;
                    }

                    if (IsSustainPedalEventOn(eventito))
                    {
                        isSustainPedalOn = true;
                        continue;
                    }

                    if (IsSustainPedalEventOff(eventito))
                    {
                        isSustainPedalOn = false;
                        foreach (var n in notesOnBecauseOfSustainPedal)
                        {
                            ProcessNoteOff(n.Pitch, currentNotes, notesObj, currentTick,
                                           n.Instrument, (byte)chunkNo);
                        }
                        continue;
                    }
                    if (eventito is NoteOnEvent)
                    {
                        NoteOnEvent noteOnEvent = eventito as NoteOnEvent;
                        if (noteOnEvent.Velocity > 0 || isSustainPedalOn == false)
                        {
                            ProcessNoteOn(noteOnEvent.NoteNumber, noteOnEvent.Velocity,
                                          currentNotes, notesObj, currentTick,
                                          instrumentOfChannel[noteOnEvent.Channel],
                                          IsPercussionEvent(eventito), (byte)chunkNo);
                        }
                        continue;
                    }
                    if (eventito is NoteOffEvent && isSustainPedalOn == false)
                    {
                        NoteOffEvent noteOffEvent = eventito as NoteOffEvent;
                        ProcessNoteOff(noteOffEvent.NoteNumber, currentNotes, notesObj, currentTick,
                                       instrumentOfChannel[noteOffEvent.Channel], (byte)chunkNo);
                        continue;
                    }
                    if (eventito is PitchBendEvent)
                    {
                        PitchBendEvent bendito = eventito as PitchBendEvent;
                        foreach (var notita in currentNotes)
                        {
                            PitchBendEvent maldito = bendito.Clone() as PitchBendEvent;
                            maldito.DeltaTime = currentTick;
                            notita.PitchBending.Add(new PitchBendItem
                            {
                                Note  = notita,
                                Pitch = maldito.PitchValue,
                                TicksSinceBeginningOfSong = maldito.DeltaTime
                            });
                        }
                        continue;
                    }
                }
            }

            var retObj = new SongSimplification()
            {
                Notes = notesObj,
                SimplificationVersion = 0,
                NumberOfVoices        = chunkNo
            };

            return(retObj);
        }
Esempio n. 9
0
        private static Tuple <Option <MidiEvent>, int, byte> NextEvent(List <byte> trackData, int startIndex, byte lastMidiChannel)
        {
            var i = startIndex - 1;

            MidiEvent midiEvent = null;

            {
                int deltaTime;
                {
                    var lengthTemp = new List <byte>();
                    do
                    {
                        i += 1;
                        lengthTemp.Add(trackData.ElementAt(i));
                    } while (trackData.ElementAt(i) > 0x7F);

                    deltaTime = VariableLengthUtil.decode_to_int(lengthTemp);
                }

                i += 1;

                var eventTypeValue = trackData.ElementAt(i);

                // MIDI Channel Events
                if ((eventTypeValue & 0xF0) < 0xF0)
                {
                    var midiChannelEventType = (byte)(eventTypeValue & 0xF0);
                    var midiChannel          = (byte)(eventTypeValue & 0x0F);
                    i += 1;
                    var  parameter1 = trackData.ElementAt(i);
                    byte parameter2;

                    // One or two parameter type
                    switch (midiChannelEventType)
                    {
                    // One parameter types
                    case 0xC0:
                        midiEvent       = new ProgramChangeEvent(deltaTime, midiChannel, parameter1);
                        lastMidiChannel = midiChannel;
                        break;

                    case 0xD0:
                        midiEvent       = new ChannelAftertouchEvent(deltaTime, midiChannel, parameter1);
                        lastMidiChannel = midiChannel;
                        break;

                    // Two parameter types
                    case 0x80:
                        i              += 1;
                        parameter2      = trackData.ElementAt(i);
                        midiEvent       = new NoteOffEvent(deltaTime, midiChannel, parameter1, parameter2);
                        lastMidiChannel = midiChannel;
                        break;

                    case 0x90:
                        i              += 1;
                        parameter2      = trackData.ElementAt(i);
                        midiEvent       = new NoteOnEvent(deltaTime, midiChannel, parameter1, parameter2);
                        lastMidiChannel = midiChannel;
                        break;

                    case 0xA0:
                        i              += 1;
                        parameter2      = trackData.ElementAt(i);
                        midiEvent       = new NoteAftertouchEvent(deltaTime, midiChannel, parameter1, parameter2);
                        lastMidiChannel = midiChannel;
                        break;

                    case 0xB0:
                        i              += 1;
                        parameter2      = trackData.ElementAt(i);
                        midiEvent       = new ControllerEvent(deltaTime, midiChannel, parameter1, parameter2);
                        lastMidiChannel = midiChannel;
                        break;

                    case 0xE0:
                        i              += 1;
                        parameter2      = trackData.ElementAt(i);
                        midiEvent       = new PitchBendEvent(deltaTime, midiChannel, parameter1, parameter2);
                        lastMidiChannel = midiChannel;
                        break;

                    // Might be a Control Change Messages LSB
                    default:
                        midiEvent = new ControllerEvent(deltaTime, lastMidiChannel, eventTypeValue, parameter1);
                        break;
                    }

                    i += 1;
                }
                // Meta Events
                else if (eventTypeValue == 0xFF)
                {
                    i += 1;
                    var metaEventType = trackData.ElementAt(i);
                    i += 1;
                    var metaEventLength = trackData.ElementAt(i);
                    i += 1;
                    var metaEventData = Enumerable.Range(i, metaEventLength).Select(trackData.ElementAt).ToArray();

                    switch (metaEventType)
                    {
                    case 0x00:
                        midiEvent = new SequenceNumberEvent(BitConverter.ToUInt16(metaEventData.Reverse().ToArray(), 0));
                        break;

                    case 0x01:
                        midiEvent = new TextEvent(deltaTime, StringEncoder.GetString(metaEventData));
                        break;

                    case 0x02:
                        midiEvent = new CopyrightNoticeEvent(StringEncoder.GetString(metaEventData));
                        break;

                    case 0x03:
                        midiEvent = new SequenceOrTrackNameEvent(StringEncoder.GetString(metaEventData));
                        break;

                    case 0x04:
                        midiEvent = new InstrumentNameEvent(deltaTime, StringEncoder.GetString(metaEventData));
                        break;

                    case 0x05:
                        midiEvent = new LyricsEvent(deltaTime, StringEncoder.GetString(metaEventData));
                        break;

                    case 0x06:
                        midiEvent = new MarkerEvent(deltaTime, StringEncoder.GetString(metaEventData));
                        break;

                    case 0x07:
                        midiEvent = new CuePointEvent(deltaTime, StringEncoder.GetString(metaEventData));
                        break;

                    case 0x20:
                        midiEvent = new MIDIChannelPrefixEvent(deltaTime, metaEventData[0]);
                        break;

                    case 0x2F:
                        midiEvent = new EndOfTrackEvent(deltaTime);
                        break;

                    case 0x51:
                        var tempo =
                            (metaEventData[2] & 0x0F) +
                            ((metaEventData[2] & 0xF0) * 16) +
                            ((metaEventData[1] & 0x0F) * 256) +
                            ((metaEventData[1] & 0xF0) * 4096) +
                            ((metaEventData[0] & 0x0F) * 65536) +
                            ((metaEventData[0] & 0xF0) * 1048576);
                        midiEvent = new SetTempoEvent(deltaTime, tempo);
                        break;

                    case 0x54:
                        midiEvent = new SMPTEOffsetEvent(deltaTime, metaEventData[0], metaEventData[1], metaEventData[2], metaEventData[3], metaEventData[4]);
                        break;

                    case 0x58:
                        midiEvent = new TimeSignatureEvent(deltaTime, metaEventData[0], metaEventData[1], metaEventData[2], metaEventData[3]);
                        break;

                    case 0x59:
                        midiEvent = new KeySignatureEvent(deltaTime, metaEventData[0], metaEventData[1]);
                        break;

                    case 0x7F:
                        midiEvent = new SequencerSpecificEvent(deltaTime, metaEventData);
                        break;
                    }

                    i += metaEventLength;
                }
                // System Exclusive Events
                else if (eventTypeValue == 0xF0 || eventTypeValue == 0xF7)
                {
                    var lengthTemp = new List <byte>();
                    do
                    {
                        i += 1;
                        lengthTemp.Add(trackData.ElementAt(i));
                    } while (trackData.ElementAt(i) > 0x7F);

                    var eventLength = VariableLengthUtil.decode_to_int(lengthTemp);

                    i += 1;

                    var eventData = Enumerable.Range(i, eventLength).Select(trackData.ElementAt);

                    midiEvent = new SysexEvent(deltaTime, eventTypeValue, eventData);

                    i += eventLength;
                }
            }

            return(midiEvent != null
                ? new Tuple <Option <MidiEvent>, int, byte>(new Some <MidiEvent>(midiEvent), i - startIndex, lastMidiChannel)
                : new Tuple <Option <MidiEvent>, int, byte>(new None <MidiEvent>(), i - startIndex, lastMidiChannel));
        }
Esempio n. 10
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="midiEvent"></param>
        protected override void OnPitchBendEvent(PitchBendEvent midiEvent)
        {
            base.OnPitchBendEvent(midiEvent);

            soundManager.ProcessPitchBend(midiEvent);
        }
Esempio n. 11
0
 public PitchBendEventLabel(PitchBendEvent pitchBendEvent) : base(pitchBendEvent)
 {
 }
Esempio n. 12
0
        public void PitchBend_CreateDefault()
        {
            var pitchBendEvent = new PitchBendEvent();

            Assert.AreEqual(PitchBendEvent.MinPitchValue, pitchBendEvent.PitchValue, "Pitch value is invalid.");
        }
Esempio n. 13
0
        public void PitchBend_SetInvalidPitchValue()
        {
            var pitchBendEvent = new PitchBendEvent();

            Assert.Throws <ArgumentOutOfRangeException>(() => pitchBendEvent.PitchValue = PitchBendEvent.MaxPitchValue + 1);
        }