本文主要是介绍OpenAL学习笔记(二)---播放音频(*.wav),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
接上一章学习,补充:
- 播放控制
- 音量调节
// TEAudio.h
//! Created 2013/04/22;//! c++;
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
#include <algorithm>//! OpenAl;
#include "al/alut.h"using namespace std;
namespace TE
{class TEAudio{private:static TEAudio* m_pAudio;//! audio of info;struct AudioInfo{ALuint uiBuffer;ALuint uiSource;ALenum eError;ALint iStatus;std::string strAudioName;//! Audio's name;};typedef std::map<ALuint, AudioInfo > MAP_AUDIOINFO;MAP_AUDIOINFO m_mapAudioInfo;public:TEAudio(void);~TEAudio(void);//! Singleton. Return the single instance;static TEAudio* sharedAudio(void);//! Load file and play the media(only *.wav...now)ALuint loadFile(const string strFilePath);//! Update play state every 0.3s;void update(void);//! Play audio by file's generate source;void play(ALuint uiSource);//! Pause audio;void pause(ALuint uiSource);//! Stop audio;void stop(ALuint uiSource);//! Rewind a Source (set playback postiton to beginning);void rewind(ALuint uiSource);//! Return audio's name;std::string getAudioName(ALuint uiSource);//! Return audio's status;std::string getAudioStatus(ALuint uiSource);//! Volume control;void setVolume(ALuint uiSid, ALenum eParam, ALfloat fValue);};
}
//TEAudio.cpp#include "TEAudio.h"
#include "TELog.h"
#include "TETools.h"using namespace TE;TEAudio* TEAudio::m_pAudio = NULL;TE::TEAudio::TEAudio( void )
{int argc = 1;char* argv = "TEApp";if (!alutInit (&argc, &argv)){ALenum error = alutGetError ();fprintf (stderr, "%s\n", alutGetErrorString (error));exit (EXIT_FAILURE);}
}TE::TEAudio::~TEAudio( void )
{m_mapAudioInfo.clear();
}TEAudio* TE::TEAudio::sharedAudio( void )
{if (NULL == m_pAudio){m_pAudio = new TEAudio();}return m_pAudio;
}ALuint TE::TEAudio::loadFile(const string strFilePath )
{AudioInfo audioInfoTemp;audioInfoTemp.strAudioName = strFilePath;/* Create an AL buffer from the given sound file. */audioInfoTemp.uiBuffer = alutCreateBufferFromFile ((char*)strFilePath.c_str());if (audioInfoTemp.uiBuffer == AL_NONE){audioInfoTemp.eError = alutGetError ();fprintf (stderr, "Error loading file: '%s'\n",alutGetErrorString (audioInfoTemp.eError));alutExit ();exit (EXIT_FAILURE);}/* Generate a single source, attach the buffer to it and start playing. */alGenSources (1, &audioInfoTemp.uiSource);alSourcei (audioInfoTemp.uiSource, AL_BUFFER, audioInfoTemp.uiBuffer);//alSourcePlay (source);/* Normally nothing should go wrong above, but one never knows... */audioInfoTemp.eError = alGetError ();if (audioInfoTemp.eError != ALUT_ERROR_NO_ERROR){fprintf (stderr, "%s\n", alGetString (audioInfoTemp.eError));alutExit ();exit (EXIT_FAILURE);}m_mapAudioInfo.insert(make_pair(audioInfoTemp.uiSource, audioInfoTemp));return (audioInfoTemp.uiSource);
}void TE::TEAudio::update( void )
{/* Check every 0.3 seconds if the sound is still playing. */alutSleep (0.3f);for (MAP_AUDIOINFO::iterator it = m_mapAudioInfo.begin(); it != m_mapAudioInfo.end(); ++it){if (it->second.iStatus == AL_PLAYING){alGetSourcei (it->second.uiSource, AL_SOURCE_STATE, &it->second.iStatus);}std::string strLog = it->second.strAudioName;strLog += " : status = ";strLog += getAudioStatus(it->second.uiSource);log(strLog);}}void TE::TEAudio::play(ALuint uiSource)
{alSourcePlay(uiSource);MAP_AUDIOINFO::iterator it = m_mapAudioInfo.find(uiSource);it->second.iStatus = AL_PLAYING;
}void TE::TEAudio::pause(ALuint uiSource)
{alSourcePause(uiSource);
}void TE::TEAudio::stop(ALuint uiSource)
{alSourceStop(uiSource);
}void TE::TEAudio::rewind(ALuint uiSource)
{alSourceRewind(uiSource);alSourcePlay(uiSource);
}std::string TE::TEAudio::getAudioName( ALuint uiSource )
{MAP_AUDIOINFO::iterator it = m_mapAudioInfo.find(uiSource);return (it->second.strAudioName);
}std::string TE::TEAudio::getAudioStatus( ALuint uiSource )
{MAP_AUDIOINFO::iterator it = m_mapAudioInfo.find(uiSource);std::string strStatus = "AL_SOURCE_STATE";switch (it->second.iStatus){case AL_INITIAL:strStatus = "AL_INITIAL";break;case AL_PLAYING:strStatus = "AL_PLAYING";break;case AL_PAUSED:strStatus = "AL_PAUSED";break;case AL_STOPPED:strStatus = "AL_STOPPED";break;default:break;}return strStatus;
}void TE::TEAudio::setVolume(ALuint uiSid, ALenum eParam, ALfloat fValue)
{MAP_AUDIOINFO::iterator it = m_mapAudioInfo.find(uiSid);if (it != m_mapAudioInfo.end()){alSourcef(it->first, AL_GAIN, fValue);}
}
这篇关于OpenAL学习笔记(二)---播放音频(*.wav)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!