VS2010 C # Function Using Loop

I am currently implementing my latest project as a student in software development for music. I am making software that plays a midi track that is generated earlier in the process.

There is a play button and a stop button in my interface. When I press "play", a loop is called that plays this track. In this loop, I call a function from an instance of another class.

What I would like is that when the user presses "pause", the cycle stops temporarily, and when he presses "pause" again, he returns to exactly where he was in the cycle.

While I press the play button, the track plays back what I want, but I cannot use any other element of my window until the loop is fully processed.

Here is my code.

private void playStopButton_Click(object sender, RoutedEventArgs e)
    {
        // Initialising my output midi devices
        outDevice.Send(new ChannelMessage(ChannelCommand.ProgramChange, information.bassChannel, information.bassSound));//bass
        outDevice.Send(new ChannelMessage(ChannelCommand.ProgramChange, information.guitarChannel, information.guitarSound));//guitar

        for (int barNum = 0; barNum < Globals.numberOfBars; barNum++)
        {
            // playing the first beat
            rythmicPattern.play(0, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //second beat
            rythmicPattern.play(1, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //thrid beat
            rythmicPattern.play(2, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);

            //fourth beat
            rythmicPattern.play(3, ref outDevice, information, Globals.chordProgression.Bars[barNum].Chord);
        }
    }

private void playPauseButton_Click(object sender, RoutedEventArgs e)
        {
            // test if the track is being played
            if (isPlaying)
                rythmicPattern.Pause();
            else
                // if it was already paused, playing back from where I stopped
                rythmicPattern.PlayAfterPause(beatNumber);
        }

Thank you for your help. Greg

+3
source share
1 answer

You should use streams where you can play sound in another stream so you can execute the pause method Check this link Multithreading in .NET: introduction and suggestions

+3
source

Source: https://habr.com/ru/post/1795075/


All Articles