4
我想把电路连接到我的电脑,它使用音频输出作为交流电流,通过某些频率,然后将其整流为几个LED,所以如果我编写一个程序让您创建特定的模式和组合的LED被点亮,它会输出特定的频率声音。C++特定的声音输出?
如何使用C++播放特定频率的声音?
可能吗?
我想把电路连接到我的电脑,它使用音频输出作为交流电流,通过某些频率,然后将其整流为几个LED,所以如果我编写一个程序让您创建特定的模式和组合的LED被点亮,它会输出特定的频率声音。C++特定的声音输出?
如何使用C++播放特定频率的声音?
可能吗?
你可以用OpenAL来做到这一点。
您需要生成一个包含PCM编码数据的数组,以表示所需的输出,然后以所需的采样频率和格式在阵列上调用alBufferData()。请参阅OpenAL Programmers Guide的第21页,了解alBufferData()函数所需的格式。
例如,以下代码播放100hz音调。
#include <iostream>
#include <cmath>
#include <al.h>
#include <alc.h>
#include <AL/alut.h>
#pragma comment(lib, "OpenAL32.lib")
#pragma comment(lib, "alut.lib")
int main(int argc, char** argv)
{
alutInit(&argc, argv);
alGetError();
ALuint buffer;
alGenBuffers(1, &buffer);
{
// Creating a buffer that hold about 1.5 seconds of audio data.
char data[32 * 1024];
for (int i = 0; i < 32 * 1024; ++i)
{
// get a value in the interval [0, 1) over the length of a second
float intervalPerSecond = static_cast<float>(i % 22050)/22050.0f;
// increase the frequency to 100hz
float intervalPerHundreth = fmod(intervalPerSecond * 100.0f, 1.0f);
// translate to the interval [0, 2PI)
float x = intervalPerHundreth * 2 * 3.14159f;
// and then convert back to the interval [0, 255] for our amplitude data.
data[i] = static_cast<char>((sin(x) + 1.0f)/2.0f * 255.0f);
}
alBufferData(buffer, AL_FORMAT_MONO8, data, 32 * 1024, 22050);
}
ALuint source;
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(source);
system("pause");
alSourceStop(source);
alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
alutExit();
return 0;
}
你在使用什么操作系统?任何特定的库/框架? – Gabe 2011-04-21 01:45:46
完全取决于设备和设备驱动程序。 – 2011-04-21 01:49:23
已关闭。如果没有James和Gabe的查询答案,我们无法为您提供有意义的答案。请注意,即使有了这些信息,您仍然应该自己研究这个问题,而不要在信息卡住之前要求提供信息。尽管如果你的问题是关于你的电路的具体细节的话,你也会面临被封闭的风险。在这种情况下,我建议在讨论板上寻求关于特定电路的帮助。 – Brian 2011-04-21 17:43:04