Use Openal to play audio in Linux

Source: Internet
Author: User

Header file

/** Soundplay. H * 1: You need to download and develop the openal Development Kit (software implementation of the openal API (Devlopment files) and ALUT Development Kit * 2: add the header file path:/usr/include/Al * 3: add libraries: openal and ALUT */# ifndef soundplay_h _ # define soundplay_h _ # include <Al. h> # include <Alb. h> # include <ALUT. h> # include <iostream> # include <stdlib. h> # include <stdio. h> # define numbuffer 4 // four sounds can be played simultaneously # define numsource 4 // four sounds can be played simultaneously using namespace STD; Class soundplay { Public: soundplay (); Virtual ~ Soundplay (); void playsound (string filename, int index, float volume); // when Index = 0: loop playback; index! = 0: play once 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 File

/** 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, directsound3ddev = alcopendevice (devicename ); cc = alccreatecontext (Dev, null); alcmakecontextcurrent (CC);} void soundplay: closedevice () {alccontext * context = alcgetcurrentcontext (); alcdevice * Device = alcgetcontextsdevice (context ); alcmakecontextcurrent (null); alcdestroycontext (context); ALB Closedevice (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); albuffe RDATA (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]); // use the audio source to associate the buffer 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 (INDE X); playlongsound (filename. data (), index); alsourcef (sources [Index], al_gain, volume);} void soundplay: setvolume (INT index, float volume) // volume value range (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 ;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.