头文件
/*
* SoundPlay.h
* 1:需要下载开发openal开发包(Software implementation of the OpenAL API(devlopment files))和alut开发包
* 2:添加头文件路径:/usr/include/AL
* 3:添加库:openal和alut
*/
#ifndef SOUNDPLAY_H_
#define SOUNDPLAY_H_
#include <al.h>
#include <alc.h>
#include <alut.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define numBuffer 4 //可以同时播放4个声音
#define numSource 4 //可以同时播放4个声音
using namespace std;
class SoundPlay {
public:
SoundPlay();
virtual ~SoundPlay();
void PlaySound(string fileName,int index,float volume);//当index=0:循环播放;index!=0:播放1次
void SetVolume(int index,float volume);
void ReleaseSound(int index);
void MusicPause(int index);
void MusicContinue(int index);
bool CheckPlayState(int i);
private:
void PlayLongSound(const char* fileName,int index);
void OpenDevice();
void CloseDevice();
string GetALCErrorString(ALenum err);
private:
ALuint buffers[numBuffer];
ALuint sources[numSource];
ALCcontext* cc;
ALCdevice* dev;
bool checkstate;
ALenum result;
};
#endif /* SOUNDPLAY_H_ */
cpp文件
/*
* SoundPlay.cpp
*/
#include "SoundPlay.h"
SoundPlay::SoundPlay()
{
alutInit(NULL,0);
if ((result=alGetError()) != AL_NO_ERROR)
cout<<"alutInit--"<<result<<endl;
OpenDevice();
//PlaySound("Media/Music/BGSound1.wav",0,100);
}
SoundPlay::~SoundPlay()
{
CloseDevice();
}
void SoundPlay::OpenDevice()
{
ALCchar DeviceName[] = "ALSA Software";//ALSA Software,DirectSound3D
dev=alcOpenDevice(DeviceName);
cc=alcCreateContext(dev,NULL);
alcMakeContextCurrent(cc);
}
void SoundPlay::CloseDevice()
{
ALCcontext* context = alcGetCurrentContext();
ALCdevice* device = alcGetContextsDevice( context );
alcMakeContextCurrent( NULL );
alcDestroyContext( context );
alcCloseDevice( device );
}
void SoundPlay::ReleaseSound(int index)
{
alDeleteSources(1,&sources[index]);
alDeleteBuffers(1,&buffers[index]);
}
void SoundPlay::PlayLongSound(const char* fileName,int index)
{
alGenSources(1,&sources[index]);
alGenBuffers(1,&buffers[index]);
ALvoid *data;
ALsizei size=0,freq=0;
ALenum format;
ALboolean loop;
alutLoadWAVFile((ALbyte *)fileName,&format,&data,&size,&freq,&loop);
alBufferData(buffers[index],format,data,size,freq);
if ((result=alGetError()) != AL_NO_ERROR)
cout<<"PlayLongSound alBufferData errno"<<result<<endl;
alutUnloadWAV(format,data,size,freq);
alSourcei(sources[index],AL_BUFFER,buffers[index]);//用音源关联缓冲器
if(index==0)
alSourcei(sources[index],AL_LOOPING,AL_TRUE);
alSourcePlay(sources[index]);
}
void SoundPlay::PlaySound(string fileName,int index,float volume)
{
alGenSources(1,&sources[index]);
alGenBuffers(1,&buffers[index]);
ReleaseSound(index);
PlayLongSound(fileName.data(),index);
alSourcef(sources[index],AL_GAIN,volume);
}
void SoundPlay::SetVolume(int index,float volume)//volume取值范围(0~1)
{
alSourcef(sources[index],AL_GAIN,volume);
}
void SoundPlay::MusicPause(int index)
{
alSourcePause(sources[index]);
alSourceStop(sources[index]);
}
void SoundPlay::MusicContinue(int index)
{
alSourcePlay(sources[index]);
}
bool SoundPlay::CheckPlayState(int i)
{
ALint state;
alGetSourcei(sources[i], AL_SOURCE_STATE, &state);
if(state != AL_PLAYING)
{
checkstate=false;
return true;
}
return false;
}
int main()
{
SoundPlay sp;
sp.PlaySound("Media/Music/BGSound1.wav",0,100);
while(1){}
return 0;
}
分享到:
相关推荐
openal 声音播放类,C++,Linux
构建您需要在系统上安装OpenAL和libsndfile:Linux Fedora:sudo dnf安装openal-soft-devel libsndfile-devel Debian或Ubuntu:sudo apt安装libopenal-dev libsndfile1-dev Mac brew安装openal-soft libsndfile ...
OpenAL(Open Audio Library)是自由软件界的跨平台音效API,由Loki Software,使用在Windows、Linux 系统上,用来音效缓冲和收听者中编码。 OpenAL设计给多通道三维位置音效的特效表现。其 API 风格模仿自OpenGL。 ...
这是用于C ++应用程序的跨平台声音播放程序包,可以异步启动和停止音乐循环... 它在Windows,OSX和linux平台上通过OpenAL播放WAV文件。 它适用于需要音乐,声音循环或瞬态声音效果的任何Cpp应用程序; 例如。 游戏。
该插件使用openal提供与node-mumble兼容的捕获和回放设备。 建造: $ npm install electron-build-tools -g $ elb configure 去做: mac / linux支持(需要构建openal-soft并将二进制文件放入deps中) 公开用于...
它在Windows,OSX和linux平台上通过OpenAL播放WAV文件。 它适用于需要音乐,声音循环或瞬态声音效果的任何Ada应用程序; 例如。 游戏。 提取归档文件并维护目录结构的正确命令是“ 7z x filename”。
该软件包提供io.WriteCloser来播放PCM声音。 平台类 视窗 苹果系统 Linux FreeBSD OpenBSD的 安卓 的iOS Web浏览器( 和WebAssembly) 先决条件 苹果系统 Oto需要AudioToolbox.framework ,但这是自动链接的。 ...
海浪Wave 是 OpenAL 的轻量级 3D 声音包装器。 用 C++11 编写。 Wave 可解码 OGG (.ogg) 和 MusePack (.mpc) 文件格式。 包括所有依赖项:OpenAL Soft、MusePack、STB_Vorbis。 跨平台。 在 Windows/Linux 上构建。 ...
单击所有源和二进制文件的发行版下的大7z文件,或尝试以下链接:带OpenAL声音的Reliquarium 版本1.8.7-20feb21 升级到OpenAL声音。在文件末尾查看较旧的修订历史记录Reliquarium游戏说明Reliquarium是一组非常独特的...
与C的结合导致扩展库丰富,3D方面支持SDL、openGL、D3D等,声音方面支持OpenAL、FMOD等,数据库支持Mysql、SQLite等,图形窗口界面支持GTK、Wx-C、WinAPI,网络方面支持Socket等等,扩展库也在增加中。
在发行版下的tar.gz文件中获取完整的源代码和二进制文件,或尝试以下链接:RufaSok使用GLFW3,OpenAL音频什么是新的: 版本2.5.1-20feb2021 修复了用户设置的窗口大小/位置烦人的更改。 升级到OpenAL声音。 更新了...
使用Ogre或Irrlicht进行图形处理,使用Bullet进行物理处理,使用OpenAL进行声音处理 OgreKit是最活跃的开发版本,在svn / trunk中,已暂停的Irrlicht版本在svn / branches / IrrKit中。 引擎是用C ++编写的,游戏...
它是用C ++ 17编写的,使用SDL2,WildMIDI进行音乐播放,并使用OpenAL Soft进行声音和混合。 支持Windows,Linux和macOS。 版本:0.12.0 执照:麻省理工学院 不和谐: : 当前状态 尚无实际的游戏玩法,但可以...
3D 声音(可选,使用 OpenAL)。 使用 OpenCL(可选)的 GPU 加速物理和粒子系统。 系统要求 引擎本身需要支持 OpenGL 3.3 或更新版本的 GPU。 其他要求主要取决于您尝试运行的游戏类型。 建造 构建这个包至少需要...
OpenAL声音API支持(多个扬声器支持和更好的音质) 全面的x86_64支持 VoIP支持,包括通过Mumble提供的游戏内和外部支持。 Windows上的MinGW编译支持和Linux上的交叉编译支持 演示的AVI视频捕获 控制台自动补全功能...
似乎有许多将某些库组合在一起的常用方法,并且对于使用它们进行自我射击的开发人员来说,还有许多常用方法。 我们正在遵循所有提供此软件的社区的建议和最佳实践,以尝试提供一个单一的干净API。 这里提到的许多...
用于EAX混响效果的OpenAL EFX(阅读:在所有平台/硬件上都类似于EAX的声音效果) 更好地支持宽屏(和任意显示分辨率) 基于CMake的便携式构建系统 MinGW-w64的(交叉)编译 一般注意事项 游戏数据和补丁 此源发行...
Windows,Linux和macOS均支持游戏引擎和编辑器,并使用.NET Core构建。 有关如何使用引擎的更完整视图,请参阅存储库。 特征 图形 完整的3D渲染系统,支持Direct3D 11和OpenGL后端 前向渲染管道,支持实时定向阴影...
EAX / OpenAL声音已修复 支持现代屏幕分辨率 修复了所有已知的安全错误 最小化器:在全屏模式下按Windows键/命令键可最小化 改进的伽玛校正 引擎中的其他修复和改进功能,请参阅更改日志以获取详细信息 开源(GPLv2...
目前,由于我处理控制器和声音的方式,我们仅支持Linux。 如果您想修补其他平台上的支持,请给我发电子邮件。 您可以通过Racket软件包安装它: raco pkg install get-bonus 运行它时,您将运行racket -l gb/main ...