简体   繁体   English

如何用C ++的一个扬声器播放声音?

[英]How can I play sound out of just one speaker in C++?

I have a audio card that supports multiple speakers (Creative SB X-Fi). 我有一个支持多个扬声器的音频卡(Creative SB X-Fi)。 What I am trying to do is write a program in C++ (Windows 7 Environment, Visual Studio IDE) that will play a given mono wav file to one of the 4 speakers I have hooked up. 我想做的是用C ++(Windows 7环境,Visual Studio IDE)编写一个程序,该程序将向我连接的4个扬声器之一播放给定的单声道wav文件。 Can anybody point me in the correct direction as to specific functions that will accomplish this aim? 有人能为实现这一目标的特定功能指明正确的方向吗? My attempt to google a solution has not yielded fruit. 我尝试用谷歌解决方案并没有取得成果。 Thanks. 谢谢。

You can play sound through a set of surround sound speakers using DirectSound. 您可以使用DirectSound通过一组环绕声扬声器播放声音。 See the MSDN page "Sounds in 3-D" . 请参阅MSDN页面“ 3-D声音”

I suggest trying Ben Voigt's answer first, since if that works out it will probably be easier and more flexible. 我建议先尝试Ben Voigt的答案,因为如果解决了,它可能会更容易,更灵活。

But if for some reason it doesn't, another alternative is as follows. 但是,如果由于某种原因而不是,则另一种替代方法如下。

Basically, when you want to play the mono .wav out of a particular speaker, convert it to a 6-channel .wav that's silent on five of the channels, but contains the data from the mono .wav on the channel for the speaker you want it to play on. 基本上,当您要在特定扬声器上播放单声道.wav时,请将其转换为6声道.wav,该声道在五个声道中均保持静音,但会在您所用扬声器的声道中包含来自单声道.wav的数据希望它继续播放。

This should be fairly easy to do, because the .wav file format is very simple. 这应该很容易做到,因为.wav文件格式非常简单。 The basic idea is that the sound data in your mono .wav file will consist of (typically) two-byte pairs for each audio sample, for example: 基本思想是,单声道.wav文件中的声音数据将由(通常)每个音频样本的两字节对组成,例如:

1234 2ab3 def0 ce18 ....

To convert that data to multi-channel you need to insert silence (zero) for the other channels. 要将数据转换为多通道,您需要为其他通道插入静默(零)。 For example, for channel 0 (front-left) you'd use: 例如,对于频道0(左前),您将使用:

1234 0000 0000 0000 0000 0000 2ab3 0000 0000 0000 0000 0000 def0 0000 0000 0000 0000 0000 (etc)

whereas for front-right you'd use: 而对于右前方,您将使用:

0000 1234 0000 0000 0000 0000 0000 2ab3 0000 0000 0000 0000 0000 def0 0000 0000 0000 0000 (etc)

(Consult this list of speaker assignments for the order.) (请咨询此发言人分配列表 。)

You also need to deal with the file's header, but the wave file header format is very simple; 您还需要处理文件的标头,但是wave文件的标头格式非常简单。 you'd need to change NumChannels from 1 to 6, ByteRate from whatever it is to the same value times 6, similarly for BlockAlign and Subchunk2Size . 您需要将NumChannels从1更改为6,将ByteRate从它的值更改为相同值乘以6,这与BlockAlignSubchunk2Size相似。

NB I haven't actually tested any of the above, but it shouldn't take too much time to experiment. 注意:我实际上没有测试任何上述内容,但是应该花太多时间进行实验。 If it doesn't work, it may be necessary to add the newer WAVE_FORMAT_EXTENSIBLE data to the file - more fiddly but still not too hard; 如果不起作用,则可能有必要将较新的WAVE_FORMAT_EXTENSIBLE数据添加到文件中-更轻松但又不太困难; here is a useful page describing these extensions to the .wav format . 这是一个有用的页面,描述了.wav格式的这些扩展名

Have you been trying BASS library? 您是否一直在尝试BASS库?

http://www.un4seen.com/ http://www.un4seen.com/

Multiple outputs Simultaneously use multiple soundcards, and move channels between them 多个输出同时使用多个声卡,并在它们之间移动通道

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM