gpt4 book ai didi

c - OpenAL/OpenAL 软件

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:12 30 4
gpt4 key购买 nike

OpenAL.org && 创意开发网站已关闭。我选择替代版本 OpenAL Soft .我很担心,因为在 OpenAL Soft 的二进制安装中我找不到 alut.h header 。 alut.h header 的用途是什么?如果我有这个 header ,它会带来什么变化?

哦还有一件事。我从中获取了一个简单的代码(描述中的 ZIP)tutorial并翻译成C语言。我实际上听到了声音,但它变形了。我想知道我的代码有什么问题吗?

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <OPENAL/al.h>
#include <OPENAL/alc.h>


int main(int argc, char *argv[])
{
FILE *fp = NULL;
fp=fopen("WAVE/Sound.wav","rb");

char type[4];
DWORD size,chunkSize;
short formatType,channels;
DWORD sampleRate,avgBytesPerSec;
short bytesPerSample,bitsPerSample;
DWORD dataSize;

fread(type,sizeof(char),4,fp);
fread(&size, sizeof(DWORD),1,fp);
fread(type, sizeof(char),4,fp);
fread(type,sizeof(char),4,fp);
fread(&chunkSize,sizeof(DWORD),1,fp);
fread(&formatType,sizeof(short),1,fp);
fread(&channels,sizeof(short),1,fp);
fread(&sampleRate,sizeof(DWORD),1,fp);
fread(&avgBytesPerSec,sizeof(DWORD),1,fp);
fread(&bytesPerSample,sizeof(short),1,fp);
fread(&bitsPerSample,sizeof(short),1,fp);
fread(type,sizeof(char),4,fp);
fread(&dataSize,sizeof(DWORD),1,fp);

ALCdevice *device;
ALCcontext *context;
device = alcOpenDevice(NULL);
context = alcCreateContext(device, NULL);
alcMakeContextCurrent(context);

ALuint source;
ALuint buffer;
ALuint frequency=sampleRate;
ALenum format=0;

alGenBuffers(1, &buffer);
alGenSources(1, &source);

if(bitsPerSample == 8)
{
if(channels == 1)
format = AL_FORMAT_MONO8;
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
else if(bitsPerSample == 16)
{
if(channels == 1)
format = AL_FORMAT_MONO16;
else if(channels == 2)
format = AL_FORMAT_STEREO16;
}

alBufferData(buffer, format, "24641", dataSize, frequency);

//Sound setting variables
ALfloat SourcePos[] = { 0.0, 0.0, 0.0 }; //Position of the source sound
ALfloat SourceVel[] = { 0.0, 0.0, 0.0 }; //Velocity of the source sound
ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 }; //Position of the listener
ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 }; //Velocity of the listener
ALfloat ListenerOri[] = { 0.0, 0.0, -1.0, 0.0, 1.0, 0.0 }; //Orientation of the listener

alListenerfv(AL_POSITION, ListenerPos); //Set position of the listener
alListenerfv(AL_VELOCITY, ListenerVel); //Set velocity of the listener
alListenerfv(AL_ORIENTATION, ListenerOri); //Set orientation of the listener

alSourcei (source, AL_BUFFER, buffer); //Link the buffer to the source
alSourcef (source, AL_PITCH, 1.0f ); //Set the pitch of the source
alSourcef (source, AL_GAIN, 1.0f ); //Set the gain of the source
alSourcefv(source, AL_POSITION, SourcePos); //Set the position of the source
alSourcefv(source, AL_VELOCITY, SourceVel); //Set the velocity of the source
alSourcei (source, AL_LOOPING, AL_FALSE ); //Set if source is looping sound

//PLAY
alSourcePlay(source); //Play the sound buffer linked to the source
system("PAUSE"); //Pause to let the sound play

//Clean-up
fclose(fp); //Delete the sound data buffer
alDeleteSources(1, &source); //Delete the OpenAL Source
alDeleteBuffers(1, &buffer); //Delete the OpenAL Buffer
alcMakeContextCurrent(NULL); //Make no context current
alcDestroyContext(context); //Destroy the OpenAL Context
alcCloseDevice(device); //Close the OpenAL Device

return EXIT_SUCCESS;
}

最佳答案

alBufferData(buffer, format, "24641", dataSize, frequency);

您将“24641”字节数组作为声音传递。除了它非常非常快的声音 - dataSize 肯定要大得多,所以你实际上阅读了更多。玩这个垃圾。

我不太了解 WAV 格式,正如我所见 - 您不会从文件中读取实际的波形数据,只是它的标题。检查 WAV 文件。猜测在 header 之后您需要分配 dataSize 字节数组并从文件的其余部分读取它,然后将它传递给 alBufferData。

不,您的代码不使用 alut,因此您不需要它。它是处理上下文创建的小型实用程序库 - 您可以自己创建。

关于c - OpenAL/OpenAL 软件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18915761/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com