Пример #1
0
        public static void PlayPiano()
        {
            Sequencer a = new Sequencer();
            using (OutputDevice outDevice = new OutputDevice(0))
            {
                for (int i = 60; i < 100; i++)
                {
                    ChannelMessageBuilder builder = new ChannelMessageBuilder();

                    builder.Command = ChannelCommand.NoteOn;
                    builder.MidiChannel = 14;
                    builder.Data1 = i;
                    builder.Data2 = i - 20;
                    builder.Build();

                    outDevice.Send(builder.Result);

                    Thread.Sleep(500);

                    builder.Command = ChannelCommand.NoteOff;
                    builder.Data2 = 0;
                    builder.Build();

                    outDevice.Send(builder.Result);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Set Midi instrument for all channels
        /// </summary>
        /// <param name="instrument"></param>
        public void SetInstrument(MyMidi.Instrument instrument)
        {
            try
            {
                foreach (var c in Enum.GetValues(typeof(MyMidi.Channel)))
                {
                    //outputDevice.SendProgramChange((Midi.Channel)c, instrument);

                    // Change the patch while playing
                    int nChannel = (int)c;

                    int p = (int)instrument;
                    int v = 0;

                    ChannelMessageBuilder builder = new ChannelMessageBuilder()
                    {
                        Command     = ChannelCommand.ProgramChange,
                        MidiChannel = nChannel,
                        Data1       = p,
                        Data2       = v,
                    };
                    builder.Build();
                    outDevice.Send(builder.Result);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Пример #3
0
    /*private void HandleLoadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
     * {
     *      Timer.text = sequence.GetLength().ToString();
     * }*/

    private void onChannelMessagePlayed(object sender, Midi.ChannelMessageEventArgs arg)
    {
        //Debug.Log("ChannelMessagePlayed: " + arg.Message.Command.ToString());

        if (OutputToDevice)
        {
            outDevice.Send(arg.Message);
        }
    }
Пример #4
0
 void sequencer_Stopped(object sender, StoppedEventArgs e)
 {
     // send "stop" messages to the sound card
     foreach (ChannelMessage message in e.Messages)
     {
         if (!_MIDIOutDevice.IsDisposed)
         {
             _MIDIOutDevice.Send(message);
         }
     }
 }
Пример #5
0
    /// <summary>
    /// Send a NoteOn MIDI message with a given velocity
    /// Valid notes: see https://cdn-images-1.medium.com/max/1200/1*CDXHKG0-4QO9Y-DCTAcqPg.png
    /// Valid velocity: 0-127
    /// </summary>
    public void SendNoteOn(int note, int velocity)
    {
        if (output == null)
        {
            Debug.Log("sendNoteOn on unopened device");
            return;
        }

        //Debug.Log("MIDI NoteOn " + note + ", velocity: " + velocity);
        output.Send(new ChannelMessage(ChannelCommand.NoteOn, 0, note, Mathf.Clamp(velocity, 0, 127)));
    }
        private void FEventSource_MessageReceived(IMidiMessage message)
        {
            var shortMessage = message as ShortMessage;

            if (shortMessage != null)
            {
                FOutDevice.SendShort(shortMessage.Message);
                return;
            }

            var sysExMessage = message as SysExMessage;

            if (sysExMessage != null)
            {
                FOutDevice.Send(sysExMessage);
            }
        }
Пример #7
0
 public static void PlayPartOfTrack()
 {
     Sequence seq = new Sequence(@"C:\Users\armen_000\Documents\Visual Studio 2013\Projects\Improvisation\Improvisation\bin\Debug\loseyourself.mid");
     using (var device = new OutputDevice(0))
     {
         var track = seq[1];
         foreach (var item in track.Iterator())
         {
             var k = item.MidiMessage as ChannelMessage;
             if (k != null)
             {
                 device.Send(k);
                 Thread.Sleep(item.DeltaTicks);
             }
         }
     }
 }
Пример #8
0
        private void Load()
        {
            Loading = 0;
            outDevice = new OutputDevice(0);
            sequencer = new Sequencer();
            sequence = new Sequence();

            sequencer.ChannelMessagePlayed += delegate (object o, ChannelMessageEventArgs args)
            {
                ChannelCommand cmd = args.Message.Command;
                int channel = args.Message.MidiChannel;
                int data1 = args.Message.Data1;
                int data2 = args.Message.Data2;
                if (cmd == ChannelCommand.NoteOff || data2 == 0)
                {
                    if (lastPlayed[channel, data1] != null)
                    {
                        Note n = lastPlayed[channel, data1];
                        n.Playing = false;
                    }
                }
                else if (cmd == ChannelCommand.NoteOn)
                {
                    Note n = new Note()
                    {
                        Key = data1,
                        Length = 0,
                        Playing = true,
                        Position = 0,
                        Time = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond,
                        Channel = channel,
                        Velocity = data2
                    };
                    lock (notes)
                        notes.Add(n);
                    if (lastPlayed[channel, data1] != null)
                        lastPlayed[channel, data1].Playing = false;
                    lastPlayed[channel, data1] = n;
                }

                lock (backlog)
                {
                    backlog.Enqueue(new Event(delegate
                    {
                        outDevice.Send(args.Message);
                        if (cmd == ChannelCommand.NoteOff || data2 == 0)
                            if (Keyboard.KeyPressed[data1] > 0)
                                Keyboard.KeyPressed[data1]--;
                        else if (cmd == ChannelCommand.NoteOn)
                            Keyboard.KeyPressed[data1]++;
                        else if (cmd == ChannelCommand.Controller)
                            if (data1 == 0x07)
                                Keyboard.ChannelVolume[channel] = data2;
                        else if (cmd == ChannelCommand.PitchWheel)
                        {
                            int pitchValue = Get14BitValue(data1, data2);
                            Keyboard.Pitchwheel[channel] = pitchValue;
                        }
                    }, Delay));
                }
            };
            sequencer.SysExMessagePlayed += delegate (object o, SysExMessageEventArgs args)
            {
                lock (backlog)
                    backlog.Enqueue(new Event(() => outDevice.Send(args.Message), Delay));
            };
            sequencer.Chased += delegate (object o, ChasedEventArgs args)
            {
                foreach (ChannelMessage message in args.Messages)
                    lock (backlog)
                        backlog.Enqueue(new Event(() => outDevice.Send(message), Delay));
            };
            sequencer.Stopped += delegate (object o, StoppedEventArgs args)
            {
                foreach (ChannelMessage message in args.Messages)
                    lock (backlog)
                        backlog.Enqueue(new Event(() => outDevice.Send(message), Delay));
            };
            sequence.LoadCompleted += delegate (object o, AsyncCompletedEventArgs args)
            {
                Loading = -1;
                sequencer.Sequence = sequence;
                sequencer.Start();
            };
            sequence.LoadProgressChanged += delegate (object sender, ProgressChangedEventArgs args)
            {
                Loading = args.ProgressPercentage;
            };
            sequence.LoadAsync(MIDIFile);
        }
Пример #9
0
        public void Init()
        {
            // Make sure we don't initialize twice and create a disaster
            if (Initialized)
                return;
            Initialized = true;

            // Create timer for event management
            eventTimer = new Timer(10);
            eventTimer.Elapsed += delegate
            {
                lock (NoteManager.Backlog)
                {
                    while (NoteManager.Backlog.Any() && NoteManager.Backlog.First().StartTime <= Stopwatch.ElapsedMilliseconds)
                    {
                        Event ev = NoteManager.Backlog.Dequeue();
                        ev.Method();
                    }
                }
            };

            Loading = 0;
            // Create handles to MIDI devices
            outDevice = new OutputDevice(0);
            sequencer = new Sequencer();
            sequence = new Sequence();
            Loading = -1;
            // Set custom event handlers for sequencer
            sequencer.ChannelMessagePlayed += delegate (object o, ChannelMessageEventArgs args)
            {
                ChannelCommand cmd = args.Message.Command;
                int channel = args.Message.MidiChannel;
                int data1 = args.Message.Data1;
                int data2 = args.Message.Data2;
                if (cmd == ChannelCommand.NoteOff || (cmd == ChannelCommand.NoteOn && data2 == 0))
                {
                    if (NoteManager.LastPlayed[channel, data1] != null)
                    {
                        Note n = NoteManager.LastPlayed[channel, data1];
                        n.Playing = false;
                    }
                }
                else if (cmd == ChannelCommand.NoteOn)
                {
                    Note n = new Note
                    {
                        Key = data1,
                        Length = 0,
                        Playing = true,
                        Position = 0,
                        Time = Stopwatch.ElapsedMilliseconds,
                        Channel = channel,
                        Velocity = data2
                    };
                    lock (NoteManager.Notes)
                        NoteManager.Notes.Add(n);
                    if (NoteManager.LastPlayed[channel, data1] != null)
                        NoteManager.LastPlayed[channel, data1].Playing = false;
                    NoteManager.LastPlayed[channel, data1] = n;
                }

                lock (NoteManager.Backlog)
                {
                    NoteManager.Backlog.Enqueue(new Event(delegate
                    {
                        outDevice.Send(args.Message);
                        if (cmd == ChannelCommand.NoteOff || (cmd == ChannelCommand.NoteOn && data2 == 0))
                        {
                            if (Keyboard.KeyPressed[data1] > 0)
                                Keyboard.KeyPressed[data1]--;
                        }
                        else if (cmd == ChannelCommand.NoteOn)
                        {
                            Keyboard.KeyPressed[data1]++;
                        }
                        else if (cmd == ChannelCommand.Controller)
                        {
                            if (data1 == 0x07)
                                Keyboard.ChannelVolume[channel] = data2;
                        }
                        else if (cmd == ChannelCommand.PitchWheel)
                        {
                            int pitchValue = Get14BitValue(data1, data2);
                            Keyboard.Pitchwheel[channel] = pitchValue;
                        }
                    }, Stopwatch.ElapsedMilliseconds, Delay));
                }
            };
            sequencer.SysExMessagePlayed += delegate (object o, SysExMessageEventArgs args)
            {
                lock (NoteManager.Backlog)
                    NoteManager.Backlog.Enqueue(new Event(() => outDevice.Send(args.Message), Stopwatch.ElapsedMilliseconds, Delay));
            };
            sequencer.Chased += delegate (object o, ChasedEventArgs args)
            {
                foreach (ChannelMessage message in args.Messages)
                    lock (NoteManager.Backlog)
                        NoteManager.Backlog.Enqueue(new Event(() => outDevice.Send(message), Stopwatch.ElapsedMilliseconds, Delay));
            };
            sequencer.Stopped += delegate (object o, StoppedEventArgs args)
            {
                foreach (ChannelMessage message in args.Messages)
                    lock (NoteManager.Backlog)
                        NoteManager.Backlog.Enqueue(new Event(() => outDevice.Send(message), Stopwatch.ElapsedMilliseconds, Delay));
                // Stop everything when the MIDI is done playing
                Stop();
            };
            sequence.LoadCompleted += delegate (object o, AsyncCompletedEventArgs args)
            {
                Loading = -1;
                if (args.Cancelled)
                {
                    MessageBox.Show("The operation was cancelled.", "MIDITrailer - Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return;
                }
                sequencer.Sequence = sequence;
            };
            sequence.LoadProgressChanged += delegate (object sender, ProgressChangedEventArgs args)
            {
                Loading = args.ProgressPercentage;
            };
            
        }