如何使用线程在两个或三个外部声音声场中同时播放声音 [英] how to use thread to playback sound simultaneously in two or three external sound sards

查看:71
本文介绍了如何使用线程在两个或三个外部声音声场中同时播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我想同时在两个或三个外部声卡中播放声音文件,我认为使用线程是解决方案,但我真的不知道如何在播放代码中使用它.这是按一下按钮播放的事件

Hello
I want to playback a sound file in two or three external sound cards at the same time and I think that using threads is the solution but I really didn''t know how to use it in the playback code. This is the event makes on button play

<br />
    public partial class PlaybackForm : Form<br />
    {<br />
    IWavePlayer waveOut;<br />
    string fileName = null;<br />
    WaveStream mainOutputStream;<br />
    WaveChannel32 volumeStream;<br />
    int _deviceNum;<br />
    int _deviceNum1;<br />
    Thread t1;<br />
    Thread t2;<br />
    public PlaybackForm(int deviceNum,int deviceNum1)<br />
    {<br />
        InitializeComponent();<br />
        _deviceNum = deviceNum;<br />
        _deviceNum1 = deviceNum1;<br />
    }<br />
    private void buttonPlay_Click(object sender, EventArgs e)<br />
    {<br />
        if (waveOut != null)<br />
        {<br />
            if (waveOut.PlaybackState == PlaybackState.Playing)<br />
            {<br />
              return;<br />
            }<br />
            else if (waveOut.PlaybackState == PlaybackState.Paused)<br />
            {<br />
                waveOut.Play();<br />
                return;<br />
            }<br />
        }<br />
        // we are in a stopped state<br />
        // TODO: only re-initialise if necessary<br />
        if (String.IsNullOrEmpty(fileName))<br />
        {<br />
            toolStripButtonOpenFile_Click(sender, e);<br />
        }<br />
        if (String.IsNullOrEmpty(fileName))<br />
        {<br />
            return;<br />
        }<br />
        try<br />
        {<br />
            CreateWaveOut();<br />
        }<br />
        catch (Exception driverCreateException)<br />
        {<br />
            MessageBox.Show(String.Format("{0}", driverCreateException.Message));<br />
            return;<br />
        }<br />
        mainOutputStream = CreateInputStream(fileName);<br />
        trackBarPosition.Maximum = (int)mainOutputStream.TotalTime.TotalSeconds;<br />
        labelTotalTime.Text = String.Format("{0:00}:{1:00}", (int)mainOutputStream.TotalTime.TotalMinutes,<br />
            mainOutputStream.TotalTime.Seconds);<br />
        trackBarPosition.TickFrequency = trackBarPosition.Maximum / 30;<br />
        try<br />
        {<br />
            waveOut.Init(mainOutputStream);<br />
        }<br />
        catch (Exception initException)<br />
        {<br />
            MessageBox.Show(String.Format("{0}", initException.Message), "Error Initializing Output");<br />
            return;<br />
        }<br />
        // not doing Volume on IWavePlayer any more<br />
        volumeStream.Volume = volumeSlider1.Volume;<br />
        waveOut.Play();<br />
    }<br />


这就是创建waveout的方法


And this is how to create the waveout

<br />
  private void CreateWaveOut()<br />
    {<br />
        CloseWaveOut();<br />
        int latency = (int)comboBoxLatency.SelectedItem;<br />
        //if (radioButtonWaveOut.Checked)<br />
        {<br />
            //WaveCallbackInfo callbackInfo = checkBoxWaveOutWindow.Checked ?<br />
            WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();<br />
            // WaveCallbackInfo callbackInfo = WaveCallbackInfo.FunctionCallback();<br />
            // WaveCallbackInfo.NewWindow(): WaveCallbackInfo.FunctionCallback();<br />
            WaveOut outputDevice = new WaveOut(callbackInfo);<br />
            outputDevice.DesiredLatency = latency;<br />
            outputDevice.DeviceNumber = _deviceNum;<br />
            waveOut = outputDevice;<br />
        }<br />
    }<br />


我声明了两个deviceNum,但是直到现在我只能在一个设备上播放声音,这就是为什么要使用线程的原因.您能帮我吗,请提前谢谢您.


I declared two deviceNum but until now I can playsound only in one device,that''s why I want to use thread. Can you help me please Thank you in advance

推荐答案

对不起,我不知道解决方案,但正在尝试回答.我只想指出,您可能没有朝正确的方向看.

这就是为什么:即使您实现了这一点(我希望这是完全有可能的),但我认为您将无法同步声音.非实时系统(我知道可以运行.NET或Mono的所有系统都不是实时系统)中的线程同步永远不能保证线程的详细"同步.只能保证执行代码中一些离散点的顺序.对于音频,同步至关重要.

最好将多个音频源混合成一个,然后在单个音频卡上播放.我不知道你的最终目标.也许您会将声音传递给几个独立的人类听众;则我的建议不适用.

最好,
—SA
Sorry, I don''t know the solution but trying to answer. I only want to point out that you might not looking in a right direction.

Here is why: even if you achieve that (I hope this is quite possible), I don''t think you will be able to synchronize the sounds. Thread synchronization in a non-realtime system (all systems I know which can run .NET or Mono are not real-time systems) can never guarantee "detailed" synchronization of threads. Only the order of execution of few discreet points of the code could be guaranteed. For audio, synchronization is critically important.

Probably you would be better of mixing several audio sources into one and playing it on a single audio card. I don''t know your ultimate goal though. Perhaps you will deliver the sound to several independent human listeners; then my advice is not applicable.

Best,
—SA


这篇关于如何使用线程在两个或三个外部声音声场中同时播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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