通过扬声器C ++可编程地制作和播放声音 [英] Programably make and play a sound through speakers C++

查看:175
本文介绍了通过扬声器C ++可编程地制作和播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地vc ++(不是.Net)制作游戏

I'm making a game in native vc++ (not .Net)

我在寻找一种方法来播放噪音(可能是8位或某些东西)通过真正的扬声器(不是内部)。我知道PlaySound,但我不想让我的EXE大。我想编程的声音。

I'm looking for a way to play a noise (maybe 8 bit or something) through the real speakers (not internal). I know about PlaySound, but I don't want to make my EXE big. I want to program the sound.

有一个api的方式(有点像Beep()),但通过真正的扬声器播放?

Is there an api way (kinda like Beep() ) but that plays through the real speakers?

感谢

推荐答案

你提到你知道 PlaySound 。其中一个标志( SND_MEMORY )将允许您播放已经加载到内存中的WAVE,即您自己创建的缓冲区。只要缓冲区有合适的WAVE标头,无论你放在哪里都应该通过扬声器播放。

You mention that you know about PlaySound. One of the it's flags (SND_MEMORY) will allow you to play a WAVE that is already loaded into memory, i.e. a buffer that you have created yourself. As long as the buffer has the appropriate WAVE header, whatever you you put in there should play through the speakers.

标题是一个44字节的块,是相当直接

The header is a 44 byte block that is fairly straight forward

struct WaveHeader
{
    DWORD chunkID;       // 0x52494646 "RIFF" in big endian
    DWORD chunkSize;     // 4 + (8 + subChunk1Size) + (8 + subChunk2Size)
    DWORD format;        // 0x57415645 "WAVE" in big endian

    DWORD subChunk1ID;   // 0x666d7420 "fmt " in big endian
    DWORD subChunk1Size; // 16 for PCM
    WORD  audioFormat;   // 1 for PCM
    WORD  numChannels;   // 1 for mono, 2 for stereo
    DWORD sampleRate;    // 8000, 22050, 44100, etc...
    DWORD byteRate;      // sampleRate * numChannels * bitsPerSample/8
    WORD  blockAlign;    // numChannels * bitsPerSample/8
    WORD  bitsPerSample; // number of bits (8 for 8 bits, etc...)

    DWORD subChunk2ID;   // 0x64617461 "data" in big endian
    DWORD subChunk2Size; // numSamples * numChannels * bitsPerSample/8 (this is the actual data size in bytes)
};

您可以使用类似于以下内容的缓冲区设置:

You'd set up your buffer with something similar to:

char *myBuffer = new char[sizeof(WaveHeader) + myDataSize];

WaveHeader *header = (WaveHeader*)myBuffer;
// fill out the header...

char *data = myBuffer + sizeof(WavHeader);
// fill out waveform data...

/ p>

So you use it something like:

PlaySound(myBuffer, NULL, SND_MEMORY | SND_ASYNC);

我假设你要使用你生成的声音。如果没有,请小心 SND_ASYNC 标志。也就是说,在调用PlaySound之后(在它仍在使用时),不要直接释放缓冲区。

I'm assuming that you're going to be using you generated sound for the lifetime of you app. If you aren't, be careful with that SND_ASYNC flag. That is, don't go freeing the buffer directly after you call PlaySound (while it is still in use).

MSDN PlaySound文档

WAV页眉上有更多细节的页面

DirectX还支持播放来自内存缓冲区的音频,一个更强大的API,但它可能overkill你需要做什么:)

DirectX also supports playing audio from in-memory buffers and is a much more powerful API, but it maybe overkill for what you need to do :)

这篇关于通过扬声器C ++可编程地制作和播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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