예제 #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
        public MIDIPlayback()
        {
            _MIDISequencer = new Midi.Sequencer();
            _MIDISequence  = new Midi.Sequence();

            // event handlers for various message types
            _MIDISequencer.ChannelMessagePlayed += sequencer_ChannelMessagePlayed;
            _MIDISequencer.Chased  += sequencer_Chased;
            _MIDISequencer.Stopped += sequencer_Stopped;
        }
예제 #3
0
            public Player(Game game)
                : base(game)
            {
                sequencer1 = new Sanford.Multimedia.Midi.Sequencer();
                sequencer1.clock.Tick += onTick;

                outDevice = new OutputDevice(outDeviceID);

                
                this.sequencer1.PlayingCompleted += new System.EventHandler(this.HandlePlayingCompleted);
                this.sequencer1.ChannelMessagePlayed += new System.EventHandler<Sanford.Multimedia.Midi.ChannelMessageEventArgs>(this.HandleChannelMessagePlayed);
                this.sequencer1.SysExMessagePlayed += new System.EventHandler<Sanford.Multimedia.Midi.SysExMessageEventArgs>(this.HandleSysExMessagePlayed);
                this.sequencer1.Chased += new System.EventHandler<Sanford.Multimedia.Midi.ChasedEventArgs>(this.HandleChased);
                this.sequencer1.Stopped += new System.EventHandler<Sanford.Multimedia.Midi.StoppedEventArgs>(this.HandleStopped);
                

            }
예제 #4
0
파일: UCEditor.cs 프로젝트: puthutp/Waver
 public UCEditor()
 {
     InitializeComponent();
     TickMultiplier = SEQUENCE_TEMPO / 10000000D / SEQUENCE_DIVISION * Program.AudioRate;
     sequence = new Sequence(SEQUENCE_DIVISION);
     sequence.Format = 1;
     sequencer = new Sequencer();
     sequencer.Sequence = sequence;
     sequencer.Position = 0;
     sequencer.ChannelMessagePlayed += new EventHandler<ChannelMessageEventArgs>(sequencer_ChannelMessagePlayed);
     sequencer.Stopped += new EventHandler<StoppedEventArgs>(sequencer_Stopped);
     sequencer.Chased += new EventHandler<ChasedEventArgs>(sequencer_Chased);
     bufferedGraphicsContext = BufferedGraphicsManager.Current;
     bufferedGraphics = bufferedGraphicsContext.Allocate(CreateGraphics(), ClientRectangle);
     SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
     MouseWheel += new MouseEventHandler(UCEditor_MouseWheel);
     OpenNew();
     toolStripComboBoxFrom.SelectedIndex = PLAY_FROM_START_POINT;
 }
예제 #5
0
 public FormImportMIDI()
 {
     InitializeComponent();
     tempo = 500000;
     isDoneParse = false;
     isClosing = false;
     isPlaying = false;
     startFromZero = true;
     outputDevice = Program.MIDIPlayer.OutputDevice;
     sequence = new Sequence();
     sequence.Format = 1;
     sequence.LoadCompleted += new EventHandler<AsyncCompletedEventArgs>(sequence_LoadCompleted);
     sequencer = new Sequencer();
     sequencer.Position = 0;
     sequencer.Sequence = this.sequence;
     sequencer.PlayingCompleted += new EventHandler(sequencer_PlayingCompleted);
     sequencer.ChannelMessagePlayed += new EventHandler<ChannelMessageEventArgs>(sequencer_ChannelMessagePlayed);
     sequencer.Stopped += new EventHandler<StoppedEventArgs>(sequencer_Stopped);
     sequencer.SysExMessagePlayed += new EventHandler<SysExMessageEventArgs>(sequencer_SysExMessagePlayed);
     sequencer.Chased += new EventHandler<ChasedEventArgs>(sequencer_Chased);
     timerLocation.Start();
 }
예제 #6
0
        /// <summary>
        /// Init method
        /// </summary>
        private void Init()
        {
            IsRecording = false;
            this.Clock = new MidiInternalClock();
            m_Session = new RecordingSession(this.Clock);
            this.Sequencer = new Sequencer();

            Sequencer.ChannelMessagePlayed += HandlePlayerMessage;
            Sequencer.PlayingCompleted += HandlePlayingCompleted;

            //If midi input isn't initialised, try and initiailise it.
            if (!_midiInput.IsInitialised) _midiInput.Initialise();
            if (_midiInput.IsInitialised)
            {
                CanRecord = true;
            }
        }
예제 #7
0
        /// <summary>
        /// Init method
        /// </summary>
        public void Init()
        {
            m_context = SynchronizationContext.Current;
            IsRecording = false;
            this.Clock = new MidiInternalClock();
            m_Session = new RecordingSession(this.Clock);
            this.Sequencer = new Sequencer();

            //If midi input isn't initialised, try and initiailise it.
            if (!_midiInput.IsInitialised) _midiInput.Initialise();
        }
예제 #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;
            };
            
        }