openal播放ogg的例子

转自

https://blog.csdn.net/ccsdu2004/article/details/4264751


OpenAL 播放 ogg

#include <AL/al.h>
#include <AL/alut.h>
#include <al/alc.h>
#include <vorbis/vorbisfile.h>
#include <cstdio>
#include <iostream>
#include <vector>

#define BUFFER_SIZE     32768       // 32 KB buffers

#pragma comment(lib,"vorbisfile_static_d.lib")
#pragma comment(lib,"ogg_d.lib")
#pragma comment(lib,"vorbisenc_static_d.lib") 
#pragma comment(lib,"vorbis_d.lib")

typedef struct ALCdevice_struct ALCdevice;
typedef struct ALCcontext_struct ALCcontext;

using namespace std;

bool LoadOGG(char *name, vector<char> &buffer, ALenum &format, ALsizei &freq)
{
    int endian = 0;                         // 0 for Little-Endian, 1 for Big-Endian
    int bitStream;
    long bytes;
    char array[BUFFER_SIZE];                // Local fixed size array
    FILE *f;
 
    f = fopen(name, "rb");

    if (f == NULL)
        return false; 

    vorbis_info *pInfo;
    OggVorbis_File oggFile;

    // Try opening the given file
    if (ov_open(f, &oggFile, NULL, 0) != 0)
        return false; 
 
    pInfo = ov_info(&oggFile, -1);
 
    if (pInfo->channels == 1)
        format = AL_FORMAT_MONO16;
    else
        format = AL_FORMAT_STEREO16;
  
    freq = pInfo->rate;
 
    do
    { 
        bytes = ov_read(&oggFile, array, BUFFER_SIZE, endian, 2, 1, &bitStream);

        if (bytes < 0)
            {
            ov_clear(&oggFile);
            cerr << "Error decoding " << fileName << "..." << endl;
            exit(-1);
            }
 
        buffer.insert(buffer.end(), array, array + bytes);
    }
    while (bytes > 0);
 
    ov_clear(&oggFile);
    return true; 
}
 
int main(int argc, char *argv[])
{

    ALCdevice* pDevice;
    ALCcontext* pContext;
 
    ALint state;                            // The state of the sound source
    ALuint bufferID;                        // The OpenAL sound buffer ID
    ALuint sourceID;                        // The OpenAL sound source
    ALenum format;                          // The sound data format
    ALsizei freq;                           // The frequency of the sound data
    vector<char> bufferData;                // The sound buffer data from file
     
	 ALCdevice *device;
     ALCcontext *context; 

       device = alcOpenDevice(0);
       context = alcCreateContext(device,0);
	   ALboolean initStatus = alcMakeContextCurrent(context);    

    // Create sound buffer and source
    alGenBuffers(1, &bufferID);
    alGenSources(1, &sourceID);

    // Set the source and listener to the same location
    alListener3f(AL_POSITION, 0.0f, 0.0f, 0.0f);
    alSource3f(sourceID, AL_POSITION, 0.0f, 0.0f, 0.0f);

    // Load the OGG file into memory
    LoadOGG("Bomb.ogg", bufferData, format, freq);

    // Upload sound data to buffer
    alBufferData(bufferID, format, &bufferData[0], static_cast<ALsizei>(bufferData.size()), freq);

    // Attach sound buffer to source
    alSourcei(sourceID, AL_BUFFER, bufferID);
	
	alSourcef (sourceID, AL_GAIN, 1.0 );

    // Finally, play the sound!!!
    alSourcePlay(sourceID);
 
    do
    {
        // Query the state of the souce
        alGetSourcei(sourceID, AL_SOURCE_STATE, &state);
    }
    while (state != AL_STOPPED);

    // Clean up sound buffer and source
    alDeleteBuffers(1, &bufferID);
    alDeleteSources(1, &sourceID);
 
    alcDestroyContext(context);
	alcCloseDevice(device);   

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值