Click here to Skip to main content
15,921,793 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Hi,
i have built a metronome using the SoundPlayer class to instigate the various beats.

The wav files used are individual drum/bass/snaredrum beats...one beat per file.

On the metronome each button (one button each for the different types of beat) is also a single beat that plays a beat onClick of the button

What i would like to do is link the individual beats together so that it plays like a metronome and you can either have a bass drum, snare drum, woodblock or bass guitar sound to practice guitar to.

I have it working with the PlayLooping()(temporarily) C# method at the moment but that just loops through each beat randomly and i cannot control the speed of the beats, which obviously i need to do as i just want it to play in a 4/4 timing (Common Timing)formation as per a metronome.

I have a scrollbar on the bottom of the metronome with which i was hoping to control the speed of the beats with. Any ideas at all?


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Media;
using System.Timers;

namespace WindowsFormsMetromusic
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
            
        }
        

        private void Form1_Load(object sender, EventArgs e)
        {
             
        }

        private void trackBarSpeed_Scroll(object sender, EventArgs e)
        {
              
        }

        private void buttonON_Click(object sender, EventArgs e)
        {
            
          
        }

        private void buttonOFF_Click(object sender, EventArgs e)
        {
           
        }

        private void buttonBassGuitar_Click(object sender, EventArgs e)
        {
           
            SoundPlayer myPlayerLoop = new
              SoundPlayer(WindowsFormsMetromusic.Properties.Resources.Electric_Bass_Low_C_Staccato);
            myPlayerLoop.PlayLooping(); 
          
        }

        private void buttonBassDrum_Click(object sender, EventArgs e)
        {
            SoundPlayer myPlayerLoop = new
             SoundPlayer(WindowsFormsMetromusic.Properties.Resources.bassdr05);
            myPlayerLoop.PlayLooping();

        }

        private void buttonSnareDrum_Click(object sender, EventArgs e)
        {
            SoundPlayer myPlayerLoop = new
              SoundPlayer(WindowsFormsMetromusic.Properties.Resources.snare05);
            myPlayerLoop.PlayLooping();
            
        }

        private void buttonWoodBlock_click(object sender, EventArgs e)
        {
            SoundPlayer myPlayerLoop = new
               SoundPlayer(WindowsFormsMetromusic.Properties.Resources.Kick_Drum_6);
            myPlayerLoop.PlayLooping();
        }

        private void trackBarSpeed_ValueChanged(object sender, EventArgs e)
        {
          
                
        }
    }
Posted
Updated 11-May-11 1:06am
v2
Comments
Wayne Gaylard 11-May-11 7:06am    
Removed extraneous code tags

You just need to use a Timer to call a method that plays the sound something like this

C#
private void buttonBassDrum_Click(object sender, EventArgs e)
        {
            SoundPlayer soundToPlay = new SoundPlayer(Properties.Settings.Default.BassGuitar);
            timer = new System.Threading.Timer(new TimerCallback(PlayBeat), soundToPlay, 0, 60000 / beatsPerMinute);
        }

        private void buttonSnareDrum_Click(object sender, EventArgs e)
        {
            SoundPlayer soundToPlay = new SoundPlayer(Properties.Settings.Default.BassDrum);
            timer = new System.Threading.Timer(new TimerCallback(PlayBeat), soundToPlay, 0, 60000 / beatsPerMinute);
        }


C#
private void PlayBeat(Object soundToPlay)
        {
            //Play wav sound
            SoundPlayer currentSound = (SoundPlayer)soundToPlay;
            currentSound.Play();
        }


this assumes that your ScrollBar value represents Beats Per Minute, and is stored in an int beatsPerMinute declared at form level, and that timer is declared as a System.Threading.Timer at your form level. I also used Application Settings to store the wav file location thus Properties.Settings.Default.BassDrum represents a wav file.

to change the beats per minute you could do this in your scrollBar.Scroll method

C#
private void scrollBar1_Scroll(object sender, EventArgs e)
        {
            beatsPerMinute = scrollBar1.Value;
            if(timer != null)
                timer.Change(0, 60000 / beatsPerMinute);
        }


Hope this helps
 
Share this answer
 
Firstly thankyou very much for this, hugely appreciated!

I have adapted my code but am getting an exception error: "AttemptingToDivideByZero Exception" would a try/catch be sufficient here? (i am a newbie)

<code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Media;
using System.Timers;
using System.Threading;


namespace WindowsFormsMetromusic
{
public partial class Form1 : Form
{


int beatsPerMinute;
System.Threading.Timer timer;

public Form1()
{
InitializeComponent();

}


private void Form1_Load(object sender, EventArgs e)
{

}

private void trackBarSpeed_Scroll(object sender, EventArgs e)
{
beatsPerMinute = trackBarSpeed.Value;
if (timer != null)
timer.Change(0, 60000 / beatsPerMinute); //AttemptingToDivideByZero Exception message here?
}

private void buttonON_Click(object sender, EventArgs e)
{


}

private void buttonOFF_Click(object sender, EventArgs e)
{

}

private void PlayBeat(Object soundToPlay)
{
//Play wav sound
SoundPlayer currentSound = (SoundPlayer)soundToPlay;
currentSound.Play();
}

private void buttonBassGuitar_Click(object sender, EventArgs e)
{

SoundPlayer soundToPlay = new
SoundPlayer(WindowsFormsMetromusic.Properties.Resources.Electric_Bass_Low_C_Staccato);
soundToPlay.PlayLooping();
timer = new System.Threading.Timer(new TimerCallback(PlayBeat), soundToPlay, 0, 60000 / beatsPerMinute);
}

private void buttonBassDrum_Click(object sender, EventArgs e)
{
SoundPlayer soundToPlay = new
SoundPlayer(WindowsFormsMetromusic.Properties.Resources.bassdr05);
soundToPlay.PlayLooping();
timer = new System.Threading.Timer(new TimerCallback(PlayBeat), soundToPlay, 0, 60000 / beatsPerMinute);
}

private void buttonSnareDrum_Click(object sender, EventArgs e)
{
SoundPlayer soundToPlay = new
SoundPlayer(WindowsFormsMetromusic.Properties.Resources.snare05);
soundToPlay.PlayLooping();
timer = new System.Threading.Timer(new TimerCallback(PlayBeat), soundToPlay, 0, 60000 / beatsPerMinute);
}

private void buttonWoodBlock_click(object sender, EventArgs e)
{


SoundPlayer soundToPlay = new
SoundPlayer(WindowsFormsMetromusic.Properties.Resources.Kick_Drum_6);
soundToPlay.PlayLooping();
timer = new System.Threading.Timer(new TimerCallback(PlayBeat), soundToPlay, 0, 60000 / beatsPerMinute);

}

private void trackBarSpeed_ValueChanged(object sender, EventArgs e)
{

}

</code>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900