Esempio n. 1
0
        /// <summary>
        /// Waiting state.
        /// </summary>
        /// <param name="e">
        /// Information about the event.
        /// </param>
        private void Waiting(SysRealtimeEventArgs e)
        {
            // If this is a clock event.
            if (e.Message.Type == SysRealtimeType.Clock)
            {
                // Keep track of time stamp.
                prevTimeStamp = e.TimeStamp;

                if (MasterEnabled)
                {
                    midiSender.Send(e.Message);
                }

                // The first clock message has been received, start the tick
                // generator.
                tickGenerator.Start();

                // Transition to the running state.
                state = new SlaveModeCallback(Running);
            }
            // Else if this is a stop event.
            else if (e.Message.Type == SysRealtimeType.Stop)
            {
                if (MasterEnabled)
                {
                    midiSender.Send(e.Message);
                }

                // Transition to the initial state.
                state = new SlaveModeCallback(Initial);

                // Raise the stopping event.
                OnStopping();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initial state.
        /// </summary>
        /// <param name="e">
        /// Information about the event.
        /// </param>
        private void Initial(SysRealtimeEventArgs e)
        {
            // If this is a start event.
            if (e.Message.Type == SysRealtimeType.Start)
            {
                if (MasterEnabled)
                {
                    midiSender.Send(e.Message);
                }

                // Transition to the waiting state.
                state = new SlaveModeCallback(Waiting);

                // Raise starting event.
                OnStarting();
            }
            // Else if this is a continue event.
            else if (e.Message.Type == SysRealtimeType.Continue)
            {
                // Transition to the waiting state.
                state = new SlaveModeCallback(Waiting);

                // Raise contining event.
                OnContinuing();
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Determines the type of message received and triggers the correct
        /// event in response.
        /// </summary>
        /// <param name="message">
        /// The short Midi message received.
        /// </param>
        /// <param name="timeStamp">
        /// Number of milliseconds that have passed since the input device
        /// began recording.
        /// </param>
        private void DispatchShortMessage(int message, int timeStamp)
        {
            // Unpack status value.
            int status = ShortMessage.UnpackStatus(message);

            // If a channel message was received.
            if (ChannelMessage.IsChannelMessage(status))
            {
                // If anyone is listening for channel messages.
                if (ChannelMessageReceived != null)
                {
                    // Create channel message.
                    ChannelMessage msg = new ChannelMessage(message);

                    // Create channel message event argument.
                    ChannelMessageEventArgs e =
                        new ChannelMessageEventArgs(msg, timeStamp);

                    // Trigger channel message received event.
                    ChannelMessageReceived(this, e);
                }
            }
            // Else if a system common message was received
            else if (SysCommonMessage.IsSysCommonMessage(status))
            {
                // If anyone is listening for system common messages
                if (SysCommonReceived != null)
                {
                    // Create system common message.
                    SysCommonMessage msg = new SysCommonMessage(message);

                    // Create system common event argument.
                    SysCommonEventArgs e = new SysCommonEventArgs(msg, timeStamp);

                    // Trigger system common received event.
                    SysCommonReceived(this, e);
                }
            }
            // Else if a system realtime message was received
            else if (SysRealtimeMessage.IsSysRealtimeMessage(status))
            {
                // If anyone is listening for system realtime messages
                if (SysRealtimeReceived != null)
                {
                    // Create system realtime message.
                    SysRealtimeMessage msg = new SysRealtimeMessage(message);

                    // Create system realtime event argument.
                    SysRealtimeEventArgs e = new SysRealtimeEventArgs(msg, timeStamp);

                    // Trigger system realtime received event.
                    SysRealtimeReceived(this, e);
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Running state.
        /// </summary>
        /// <param name="e">
        /// Information about the event.
        /// </param>
        private void Running(SysRealtimeEventArgs e)
        {
            // If this is a clock event.
            if (e.Message.Type == SysRealtimeType.Clock)
            {
                // Calculate tempo based on the time that has elapsed since the
                // last clock message.
                //
                // To calculate the tempo based on clock messages, determine
                // the time in milliseconds that have elapsed since the last
                // clock message. Since there are 24 clock messages per beat,
                // multiply the elapsed time by 24 to get the milliseconds per
                // beat value. And since the tempo is measured in microseconds
                // per beat, multiply this value by 1000. The TempoScale
                // constant takes care of combining the number of clock
                // messages per beat with the microsecond scale.
                int tempo = (e.TimeStamp - prevTimeStamp) * TempoScale;

                // If the tempo has changed, change the tick generator's tempo.
                if (tempo != tickGenerator.Tempo &&
                    tempo >= TickGenerator.TempoMin &&
                    tempo <= TickGenerator.TempoMax)
                {
                    tickGenerator.Tempo = tempo;
                }

                if (MasterEnabled)
                {
                    midiSender.Send(e.Message);
                }

                // Keep track of timestamp.
                prevTimeStamp = e.TimeStamp;
            }
            // Else if this is a stop message.
            else if (e.Message.Type == SysRealtimeType.Stop)
            {
                tickGenerator.Stop();

                if (MasterEnabled)
                {
                    midiSender.Send(e.Message);
                }

                // Transition to the initial state.
                state = new SlaveModeCallback(Initial);

                // Raise stopping event.
                OnStopping();
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Handles system realtime received events.
 /// </summary>
 /// <param name="sender">
 /// The MIDI receiver responsible for the event.
 /// </param>
 /// <param name="e">
 /// Information about the event.
 /// </param>
 private void SysRealtimeReceivedHandler(object sender, SysRealtimeEventArgs e)
 {
     // Pass on the event to the current state.
     state(e);
 }
Esempio n. 6
0
        /// <summary>
        /// Determines the type of message received and triggers the correct
        /// event in response.
        /// </summary>
        /// <param name="message">
        /// The short Midi message received.
        /// </param>
        /// <param name="timeStamp">
        /// Number of milliseconds that have passed since the input device 
        /// began recording.
        /// </param>
        private void DispatchShortMessage(int message, int timeStamp)
        {
            // Unpack status value.
            int status = ShortMessage.UnpackStatus(message);

            // If a channel message was received.
            if(ChannelMessage.IsChannelMessage(status))
            {
                // If anyone is listening for channel messages.
                if(ChannelMessageReceived != null)
                {
                    // Create channel message.
                    ChannelMessage msg = new ChannelMessage(message);

                    // Create channel message event argument.
                    ChannelMessageEventArgs e =
                        new ChannelMessageEventArgs(msg, timeStamp);

                    // Trigger channel message received event.
                    ChannelMessageReceived(this, e);
                }
            }
            // Else if a system common message was received
            else if(SysCommonMessage.IsSysCommonMessage(status))
            {
                // If anyone is listening for system common messages
                if(SysCommonReceived != null)
                {
                    // Create system common message.
                    SysCommonMessage msg = new SysCommonMessage(message);

                    // Create system common event argument.
                    SysCommonEventArgs e = new SysCommonEventArgs(msg, timeStamp);

                    // Trigger system common received event.
                    SysCommonReceived(this, e);
                }
            }
            // Else if a system realtime message was received
            else if(SysRealtimeMessage.IsSysRealtimeMessage(status))
            {
                // If anyone is listening for system realtime messages
                if(SysRealtimeReceived != null)
                {
                    // Create system realtime message.
                    SysRealtimeMessage msg = new SysRealtimeMessage(message);

                    // Create system realtime event argument.
                    SysRealtimeEventArgs e = new SysRealtimeEventArgs(msg, timeStamp);

                    // Trigger system realtime received event.
                    SysRealtimeReceived(this, e);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Waiting state.
        /// </summary>
        /// <param name="e">
        /// Information about the event.
        /// </param>
        private void Waiting(SysRealtimeEventArgs e)
        {
            // If this is a clock event.
            if(e.Message.Type == SysRealtimeType.Clock)
            {
                // Keep track of time stamp.
                prevTimeStamp = e.TimeStamp;

                if(MasterEnabled)
                    midiSender.Send(e.Message);

                // The first clock message has been received, start the tick
                // generator.
                tickGenerator.Start();

                // Transition to the running state.
                state = new SlaveModeCallback(Running);
            }
            // Else if this is a stop event.
            else if(e.Message.Type == SysRealtimeType.Stop)
            {
                if(MasterEnabled)
                    midiSender.Send(e.Message);

                // Transition to the initial state.
                state = new SlaveModeCallback(Initial);

                // Raise the stopping event.
                OnStopping();
            }
        }
Esempio n. 8
0
 /// <summary>
 /// Handles system realtime received events.
 /// </summary>
 /// <param name="sender">
 /// The MIDI receiver responsible for the event.
 /// </param>
 /// <param name="e">
 /// Information about the event.
 /// </param>
 private void SysRealtimeReceivedHandler(object sender, SysRealtimeEventArgs e)
 {
     // Pass on the event to the current state.
     state(e);
 }
Esempio n. 9
0
        /// <summary>
        /// Running state.
        /// </summary>
        /// <param name="e">
        /// Information about the event.
        /// </param>
        private void Running(SysRealtimeEventArgs e)
        {
            // If this is a clock event.
            if(e.Message.Type == SysRealtimeType.Clock)
            {
                // Calculate tempo based on the time that has elapsed since the
                // last clock message.
                //
                // To calculate the tempo based on clock messages, determine
                // the time in milliseconds that have elapsed since the last
                // clock message. Since there are 24 clock messages per beat,
                // multiply the elapsed time by 24 to get the milliseconds per
                // beat value. And since the tempo is measured in microseconds
                // per beat, multiply this value by 1000. The TempoScale
                // constant takes care of combining the number of clock
                // messages per beat with the microsecond scale.
                int tempo = (e.TimeStamp - prevTimeStamp) * TempoScale;

                // If the tempo has changed, change the tick generator's tempo.
                if(tempo != tickGenerator.Tempo &&
                    tempo >= TickGenerator.TempoMin &&
                    tempo <= TickGenerator.TempoMax)
                {
                    tickGenerator.Tempo = tempo;
                }

                if(MasterEnabled)
                    midiSender.Send(e.Message);

                // Keep track of timestamp.
                prevTimeStamp = e.TimeStamp;
            }
            // Else if this is a stop message.
            else if(e.Message.Type == SysRealtimeType.Stop)
            {
                tickGenerator.Stop();

                if(MasterEnabled)
                    midiSender.Send(e.Message);

                // Transition to the initial state.
                state = new SlaveModeCallback(Initial);

                // Raise stopping event.
                OnStopping();
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Initial state.
        /// </summary>
        /// <param name="e">
        /// Information about the event.
        /// </param>
        private void Initial(SysRealtimeEventArgs e)
        {
            // If this is a start event.
            if(e.Message.Type == SysRealtimeType.Start)
            {
                if(MasterEnabled)
                    midiSender.Send(e.Message);

                // Transition to the waiting state.
                state = new SlaveModeCallback(Waiting);

                // Raise starting event.
                OnStarting();
            }
            // Else if this is a continue event.
            else if(e.Message.Type == SysRealtimeType.Continue)
            {
                // Transition to the waiting state.
                state = new SlaveModeCallback(Waiting);

                // Raise contining event.
                OnContinuing();
            }
        }