音频 – C特定声音输出?
发布时间:2020-12-16 06:56:11 所属栏目:百科 来源:网络整理
导读:我想把一个电路连接到我的计算机,它使用音频输出作为交流电流,通过某些频率,然后将其整流为几个LED,所以如果我写一个程序,让你创建一个特定的模式和LED的组合要点亮,它会输出特定的频率声音. 如何使用C播放特定频率的声音? 可能? 解决方法 你可以用OpenAL
我想把一个电路连接到我的计算机,它使用音频输出作为交流电流,通过某些频率,然后将其整流为几个LED,所以如果我写一个程序,让你创建一个特定的模式和LED的组合要点亮,它会输出特定的频率声音.
如何使用C播放特定频率的声音? 可能? 解决方法
你可以用OpenAL做到这一点.
您需要生成一个包含代表所需输出的PCM编码数据的数组,然后使用所需的采样频率和格式调用阵列上的a??lBufferData().有关alBufferData()函数所需的格式,请参见OpenAL Programmers Guide的第21页. 例如,以下代码播放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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |