将PlayLooping()与节拍器一起使用 [英] Using PlayLooping() with a Metronome

查看:121
本文介绍了将PlayLooping()与节拍器一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我使用SoundPlayer类构建了一个节拍器,以激发各种节拍.

使用的wav文件是单独的鼓/贝斯/发情节拍...每个文件一个节拍.

在节拍器上,每个按钮(每个按钮用于不同类型的节拍)也是一个单一的节拍,在按一下该按钮时播放节拍

我想做的是将各个节拍链接在一起,使其像节拍器一样演奏,您可以拥有低音鼓,军鼓,木刻或低音吉他声音来练习吉他.

我现在使用PlayLooping()(临时)C#方法工作,但是它只是随机循环每个节拍,并且我无法控制节拍的速度,显然我需要做,因为我只想让它在节拍器的4/4定时(通用定时)格式.

我在节拍器的底部有一个滚动条,希望以此来控制节拍的速度.有什么想法吗?


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?


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)
        {
          
                
        }
    }

推荐答案

您只需要使用Timer来调用播放类似声音的方法

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

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);
        }





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



这假定您的ScrollBar值代表每分钟节拍数,并存储在窗体级别声明的int beatsPerMinute中,并且计时器在窗体级别声明为System.Threading.Timer.我还使用应用程序设置"存储了wav文件的位置,因此Properties.Settings.Default.BassDrum代表了wav文件.

要更改每分钟的节拍,您可以在scrollBar.Scroll方法中执行此操作



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

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



希望对您有帮助



Hope this helps


首先非常感谢您,深表感谢!

我已经调整了代码,但收到异常错误:"AttemptingToDivideByZero异常"在这里尝试/捕获就足够了吗? (我是新手)

< code>
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Drawing.Drawing2D;
使用System.Media;
使用System.Timers;
使用System.Threading;


命名空间WindowsFormsMetromusic
{
公共局部类Form1:Form
{


int beatsPerMinute;
System.Threading.Timer计时器;

公共Form1()
{
InitializeComponent();

}


私有void Form1_Load(对象发送者,EventArgs e)
{

}

私有void trackBarSpeed_Scroll(对象发送者,EventArgs e)
{
beatsPerMinute = trackBarSpeed.Value;
if(timer!= null)
timer.Change(0,60000/beatsPerMinute); //这里有AttemptingToDivideByZero异常消息吗?
}

private void buttonON_Click(对象发送者,EventArgs e)
{


}

私人无效按钮OFF_Click(对象发送者,EventArgs e)
{

}

私人void PlayBeat(Object soundToPlay)
{
//播放wav声音
SoundPlayer currentSound =(SoundPlayer)soundToPlay;
currentSound.Play();
}

私有void buttonBassGuitar_Click(对象发送者,EventArgs e)
{

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

私有void buttonBassDrum_Click(对象发送者,EventArgs e)
{
SoundPlayer soundToPlay =新
SoundPlayer(WindowsFormsMetromusic.Properties.Resources.bassdr05);
soundToPlay.PlayLooping();
timer =新的System.Threading.Timer(新的TimerCallback(PlayBeat),soundToPlay,0,60000/beatsPerMinute);
}

private void buttonSnareDrum_Click(对象发送者,EventArgs e)
{
SoundPlayer soundToPlay =新
SoundPlayer(WindowsFormsMetromusic.Properties.Resources.snare05);
soundToPlay.PlayLooping();
timer =新的System.Threading.Timer(新的TimerCallback(PlayBeat),soundToPlay,0,60000/beatsPerMinute);
}

private void buttonWoodBlock_click(object sender,EventArgs e)
{


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

}

私有void trackBarSpeed_ValueChanged(对象发送者,EventArgs e)
{

}

</code>
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>


这篇关于将PlayLooping()与节拍器一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆