使用openal播放WAV音频

来源:互联网 发布:linux就该这么学视频 编辑:程序博客网 时间:2024/05/14 05:57

本文转载自博客:http://blog.csdn.net/u011417605/article/details/49666465

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

下载openal sdk,alut源文件:

http://download.csdn.net/detail/u011417605/9244991

不使用alut,只使用openal播放WAV文件:

[cpp] view plain copy print?
  1. #include <al.h>  
  2. #include <alc.h>  
[cpp] view plain copy print?
  1. struct WAVE_Data {  
  2.     char subChunkID[4]; //should contain the word data  
  3.     long subChunk2Size; //Stores the size of the data block  
  4. };  
  5.   
  6. struct WAVE_Format {  
  7.     char subChunkID[4];  
  8.     long subChunkSize;  
  9.     short audioFormat;  
  10.     short numChannels;  
  11.     long sampleRate;  
  12.     long byteRate;  
  13.     short blockAlign;  
  14.     short bitsPerSample;  
  15. };  
  16.   
  17. struct RIFF_Header {  
  18.     char chunkID[4];  
  19.     long chunkSize;//size not including chunkSize or chunkID  
  20.     char format[4];  
  21. };  
  22.   
  23. bool loadWavFile(const std::string filename, ALuint* buffer,  
  24.     ALsizei* size, ALsizei* frequency,  
  25.     ALenum* format) {  
  26.     //Local Declarations  
  27.     FILE* soundFile = NULL;  
  28.     WAVE_Format wave_format;  
  29.     RIFF_Header riff_header;  
  30.     WAVE_Data wave_data;  
  31.     unsigned char* data;  
  32.   
  33.     try {  
  34.         soundFile = fopen(filename.c_str(), "rb");  
  35.         if (!soundFile)  
  36.             throw (filename);  
  37.   
  38.         // Read in the first chunk into the struct  
  39.         fread(&riff_header, sizeof(RIFF_Header), 1, soundFile);  
  40.   
  41.         //check for RIFF and WAVE tag in memeory  
  42.         if ((riff_header.chunkID[0] != 'R' ||  
  43.             riff_header.chunkID[1] != 'I' ||  
  44.             riff_header.chunkID[2] != 'F' ||  
  45.             riff_header.chunkID[3] != 'F') ||  
  46.             (riff_header.format[0] != 'W' ||  
  47.             riff_header.format[1] != 'A' ||  
  48.             riff_header.format[2] != 'V' ||  
  49.             riff_header.format[3] != 'E'))  
  50.             throw ("Invalid RIFF or WAVE Header");  
  51.   
  52.         //Read in the 2nd chunk for the wave info  
  53.         fread(&wave_format, sizeof(WAVE_Format), 1, soundFile);  
  54.         //check for fmt tag in memory  
  55.         if (wave_format.subChunkID[0] != 'f' ||  
  56.             wave_format.subChunkID[1] != 'm' ||  
  57.             wave_format.subChunkID[2] != 't' ||  
  58.             wave_format.subChunkID[3] != ' ')  
  59.             throw ("Invalid Wave Format");  
  60.   
  61.         //check for extra parameters;  
  62.         if (wave_format.subChunkSize > 16)  
  63.             fseek(soundFile, sizeof(short), SEEK_CUR);  
  64.   
  65.         //Read in the the last byte of data before the sound file  
  66.         fread(&wave_data, sizeof(WAVE_Data), 1, soundFile);  
  67.         //check for data tag in memory  
  68.         if (wave_data.subChunkID[0] != 'd' ||  
  69.             wave_data.subChunkID[1] != 'a' ||  
  70.             wave_data.subChunkID[2] != 't' ||  
  71.             wave_data.subChunkID[3] != 'a')  
  72.             throw ("Invalid data header");  
  73.   
  74.         //Allocate memory for data  
  75.         data = new unsigned char[wave_data.subChunk2Size];  
  76.   
  77.         // Read in the sound data into the soundData variable  
  78.         if (!fread(data, wave_data.subChunk2Size, 1, soundFile))  
  79.             throw ("error loading WAVE data into struct!");  
  80.   
  81.         //Now we set the variables that we passed in with the  
  82.         //data from the structs  
  83.         *size = wave_data.subChunk2Size;  
  84.         *frequency = wave_format.sampleRate;  
  85.         //The format is worked out by looking at the number of  
  86.         //channels and the bits per sample.  
  87.         if (wave_format.numChannels == 1) {  
  88.             if (wave_format.bitsPerSample == 8)  
  89.                 *format = AL_FORMAT_MONO8;  
  90.             else if (wave_format.bitsPerSample == 16)  
  91.                 *format = AL_FORMAT_MONO16;  
  92.         }  
  93.         else if (wave_format.numChannels == 2) {  
  94.             if (wave_format.bitsPerSample == 8)  
  95.                 *format = AL_FORMAT_STEREO8;  
  96.             else if (wave_format.bitsPerSample == 16)  
  97.                 *format = AL_FORMAT_STEREO16;  
  98.         }  
  99.         //create our openAL buffer and check for success  
  100.         alGenBuffers(1, buffer);  
  101.         //errorCheck();  
  102.         //now we put our data into the openAL buffer and  
  103.         //check for success  
  104.         alBufferData(*buffer, *format, (void*)data,  
  105.             *size, *frequency);  
  106.         //errorCheck();  
  107.         //clean up and return true if successful  
  108.         fclose(soundFile);  
  109.         return true;  
  110.     }  
  111.     catch (std::string error) {  
  112.         //our catch statement for if we throw a string  
  113.         std::cerr << error << " : trying to load "  
  114.             << filename << std::endl;  
  115.         //clean up memory if wave loading fails  
  116.         if (soundFile != NULL)  
  117.             fclose(soundFile);  
  118.         //return false to indicate the failure to load wave  
  119.         return false;  
  120.     }  
  121. }  
[cpp] view plain copy print?
  1. ALuint alSource;  
  2. ALuint source;  
  3. ALsizei size2;  
  4. ALsizei frequent;  
  5. ALenum format;  

[cpp] view plain copy print?
  1. void main()  
  2. {  
  3.     alcMakeContextCurrent(alcCreateContext(alcOpenDevice(NULL), NULL));  
  4.   
  5.     //openal播放音频1  
  6.     loadWavFile("shengdao.wav", &alSource, &size2, &frequent, &format);  
  7.     alGenSources(1, &source);  
  8.     alSourcei(source, AL_BUFFER, alSource);  
  9.     alSourcePlay(source);