Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2010 C# function using a loop

I am currently implementing my final year project as a Music Software Development student. I am making a software that plays a midi track that is generated earlier in the process.

In my interface, there is a button "play" and a button "stop". When I click "play", a loop is called that plays this track. In this loop, I call a function from an instance of an other class.

What I would like is that when the user press "pause", the loop is temporary stopped, and when he press "pause" again, it goes back exactly where it was in the loop.

So far, when I press play, the track plays exactly how I want to, but I can't use any other element of my window until the loop is not completly 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

like image 296
Greg Avatar asked Sep 25 '26 17:09

Greg


1 Answers

You must use threading where you can play the sound in another thread, so you can execute pause method Check this link Multi-threading in .NET: Introduction and suggestions

like image 186
M.Ramadan Avatar answered Sep 28 '26 07:09

M.Ramadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!