gpt4 book ai didi

C++ : AL/altypes. h 没有那个文件或目录

转载 作者:太空宇宙 更新时间:2023-11-04 11:41:26 25 4
gpt4 key购买 nike

我正在尝试开始使用 OpenAL。

我正在尝试编译我从 here 获得的这个程序使用:

g++ main.cpp -lOpenAL

此时出现错误"AL/altypes.h"No such file or directory.。有谁知道我该如何解决这个问题?我已经安装了 libopenal-dev。 (我认为我在 Ubuntu 13.04 上?)

#include <iostream>
#include <cstdlib>
#include <AL/altypes.h>
#include <AL/al.h>
#include <AL/alu.h>
#include <AL/alut.h>


#define NUM_BUFFERS 1
#define NUM_SOURCES 1
#define NUM_ENVIRONMENTS 1

ALfloat listenerPos[] = {0.0, 0.0, 4.0};
ALfloat listenerVel[] = {0.0, 0.0, 0.0};
ALfloat listenerOri[] = {0.0, 0.0, 1.0, 0.0, 1.0, 0.0};

ALfloat source0Pos[] = {0.0, 0.0, 0.0};
ALfloat source0Vel[] = {0.0, 0.0, 0.0};

ALuint buffer[NUM_BUFFERS];
ALuint source[NUM_SOURCES];
ALuint environment[NUM_ENVIRONMENTS];

ALsizei size, freq;
ALenum format;
ALvoid* data;

void init(void)
{
alListenerfv(AL_POSITION,listenerPos);
alListenerfv(AL_VELOCITY,listenerVel);
alListenerfv(AL_ORIENTATION,listenerOri);

alGetError(); // clear any error messages

// Generate buffers, or else no sound will happen!
alGenBuffers(NUM_BUFFERS, buffer);

if(alGetError() != AL_NO_ERROR)
{
std::cout << "- Error creating buffers !!" << std::endl;
exit(1);
}
else
{
std::cout << "init() - No errors yet." << std::endl;
}

alutLoadWAVFile((Albyte *) "c.wav", &format, &data, size,&freq, &al_bool);
alBufferData(buffer[0],format,data,size,freq);
alutUnloadWAV(format,data,size,freq);

alGetError(); /* clear error */
alGenSources(NUM_SOURCES, source);

if(alGetError() != AL_NO_ERROR)
{
std::cout << "- Error creating sources !!" << std::endl;
exit(2);
}
else
{
std::cout << "init - no errors after alGenSources" << std::endl;
}

alSourcef(source[0], AL_PITCH, 1.0f);
alSourcef(source[0], AL_GAIN, 1.0f);
alSourcefv(source[0], AL_POSITION, source0Pos);
alSourcefv(source[0], AL_VELOCITY, source0Vel);
alSourcei(source[0], AL_BUFFER,buffer[0]);
alSourcei(source[0], AL_LOOPING, AL_TRUE);
}

int main()
{

//initialise openAL
alutInit(&argc, argv) ;

init();

return 0;
}

顺便说一句,这个程序目前实际上不会做我想做的事。

我想声明一个 double/int 数组,用数据填充它并播放该数据。有谁知道我该怎么做?

最佳答案

有一个包含实用程序的包 libalut-dev,但是请查看用于音频文件 I/O 的 libsndfile。推出您自己的 WAV/PCM 解析器始终是一个 chalice ,它可以教给您很多东西,而且并不难。 WRT 你在一边......以下开放代码将数据合成到一个开放缓冲区中,然后将其呈现为音频。下面的另一页代码将为您提供一个流媒体音频播放器,这很方便。编译它使用

gcc -o gen_tone_08  gen_tone_08.c -lopenal -lm

告诉我们你过得怎么样

#include <stdio.h>
#include <stdlib.h> // gives malloc
#include <math.h>


#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#elif __linux
#include <AL/al.h>
#include <AL/alc.h>
#endif

ALCdevice * openal_output_device;
ALCcontext * openal_output_context;

ALuint internal_buffer;
ALuint streaming_source[1];

int al_check_error(const char * given_label) {

ALenum al_error;
al_error = alGetError();

if(AL_NO_ERROR != al_error) {

printf("ERROR - %s (%s)\n", alGetString(al_error), given_label);
return al_error;
}
return 0;
}

void MM_init_al() {

const char * defname = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);

openal_output_device = alcOpenDevice(defname);
openal_output_context = alcCreateContext(openal_output_device, NULL);
alcMakeContextCurrent(openal_output_context);

// setup buffer and source

alGenBuffers(1, & internal_buffer);
al_check_error("failed call to alGenBuffers");
}

void MM_exit_al() {

ALenum errorCode = 0;

// Stop the sources
alSourceStopv(1, & streaming_source[0]); // streaming_source
int ii;
for (ii = 0; ii < 1; ++ii) {
alSourcei(streaming_source[ii], AL_BUFFER, 0);
}
// Clean-up
alDeleteSources(1, &streaming_source[0]);
alDeleteBuffers(16, &streaming_source[0]);
errorCode = alGetError();
alcMakeContextCurrent(NULL);
errorCode = alGetError();
alcDestroyContext(openal_output_context);
alcCloseDevice(openal_output_device);
}

void MM_render_one_buffer() {

/* Fill buffer with Sine-Wave */
// float freq = 440.f;
float freq = 100.f;
float incr_freq = 0.1f;

int seconds = 4;
// unsigned sample_rate = 22050;
unsigned sample_rate = 44100;
double my_pi = 3.14159;
size_t buf_size = seconds * sample_rate;

short * samples = malloc(sizeof(short) * buf_size);

printf("\nhere is freq %f\n", freq);
int i=0;
for(; i<buf_size; ++i) {
samples[i] = 32760 * sin( (2.f * my_pi * freq)/sample_rate * i );

freq += incr_freq;
// incr_freq += incr_freq;
// freq *= factor_freq;

if (100.0 > freq || freq > 5000.0) {

incr_freq *= -1.0f;
}
}

/* upload buffer to OpenAL */
alBufferData( internal_buffer, AL_FORMAT_MONO16, samples, buf_size, sample_rate);
al_check_error("populating alBufferData");

free(samples);

/* Set-up sound source and play buffer */
// ALuint src = 0;
// alGenSources(1, &src);
// alSourcei(src, AL_BUFFER, internal_buffer);
alGenSources(1, & streaming_source[0]);
alSourcei(streaming_source[0], AL_BUFFER, internal_buffer);
// alSourcePlay(src);
alSourcePlay(streaming_source[0]);

// ---------------------

ALenum current_playing_state;
alGetSourcei(streaming_source[0], AL_SOURCE_STATE, & current_playing_state);
al_check_error("alGetSourcei AL_SOURCE_STATE");

while (AL_PLAYING == current_playing_state) {

printf("still playing ... so sleep\n");

sleep(1); // should use a thread sleep NOT sleep() for a more responsive finish

alGetSourcei(streaming_source[0], AL_SOURCE_STATE, & current_playing_state);
al_check_error("alGetSourcei AL_SOURCE_STATE");
}

printf("end of playing\n");

/* Dealloc OpenAL */
MM_exit_al();

} // MM_render_one_buffer

int main() {

MM_init_al();

MM_render_one_buffer();
}

关于C++ : AL/altypes. h 没有那个文件或目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21238543/

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