osg.AudioStream
是OpenSceneGraph库中用于处理音频流的类。
osg.AudioStream
是一个抽象类,提供了处理音频流的接口,它可以被用来处理不同的音频流格式。该类提供了以下核心功能:
为了使用 osg.AudioStream
,需要继承该类,并实现以下方法:
virtual void play() = 0;
virtual void pause() = 0;
virtual void stop() = 0;
virtual bool isPlaying() const = 0;
virtual bool isPaused() const = 0;
virtual bool eos() const = 0;
virtual void setVolume(float volume) = 0;
virtual void setBalance(float balance) = 0;
virtual bool open(const std::string& fileName) = 0;
virtual bool open(const osgAudio::AudioStreamReader& audioStreamReader) = 0;
virtual bool close() = 0;
virtual unsigned int getNumChannels() const = 0;
virtual unsigned int getNumSamples() const = 0;
virtual unsigned int getSampleRate() const = 0;
virtual unsigned int getNumBitsPerSample() const = 0;
virtual TimeValue getLength() const = 0;
virtual unsigned int read(unsigned int numSamples, void* buffer) = 0;
上述方法是音频流必须实现的方法。其中,一些核心函数的功能如下:
play
:播放音频流pause
:暂停音频流stop
:停止音频流isPlaying
:检测音频是否正在播放isPaused
:检测音频是否已经暂停eos
:检测音频流是否结束setVolume
:设置音量大小open
:打开音频流close
:关闭音频流getNumChannels
:获取音频通道数getNumSamples
:获取总采样数getSampleRate
:获取采样频率getNumBitsPerSample
:获取每个采样值所占的字节数getLength
:获取音频流长度read
:读取指定数量的采样数据到缓冲区假设我们有一个wav格式的音频文件 "audio.wav",我们将展示如何使用 osg.AudioStream
来播放文件中的音频流。
#include <osg/Notify>
#include <osgDB/Registry>
#include <osgAudio/AudioStream>
#include <osgAudio/SoundState>
#include <osgViewer/Viewer>
class SimpleAudioStream : public osgAudio::AudioStream
{
public:
SimpleAudioStream()
: _audioStreamReader()
{}
void play() override {}
void pause() override {}
void stop() override {}
bool isPlaying() const override { return false; }
bool isPaused() const override { return false; }
bool eos() const override { return false; }
void setVolume(float volume) override {}
void setBalance(float balance) override {}
bool open(const std::string& fileName) override
{
std::string ext = osgDB::getFileExtension(fileName);
osgDB::Registry::instance()->setReadFileCallback(&_audioStreamReader);
osgDB::ReaderWriter::ReadResult result = osgDB::readFile(fileName);
osgDB::Registry::instance()->setReadFileCallback(nullptr);
return result.valid();
}
bool open(const osgAudio::AudioStreamReader& audioStreamReader) override
{
_audioStreamReader = audioStreamReader;
return true;
}
bool close() override { return true; }
unsigned int getNumChannels() const override { return _audioStreamReader.getNumChannels(); }
unsigned int getNumSamples() const override { return _audioStreamReader.getNumSamples(); }
unsigned int getSampleRate() const override { return _audioStreamReader.getSampleRate(); }
unsigned int getNumBitsPerSample() const override { return _audioStreamReader.getNumBitsPerSample(); }
TimeValue getLength() const override { return _audioStreamReader.getTime(); }
unsigned int read(unsigned int numSamples, void* buffer) override { return 0; }
private:
osgAudio::AudioStreamReader _audioStreamReader;
};
int main()
{
osg::ArgumentParser arguments(&argc, argv);
osg::ref_ptr<SimpleAudioStream> audioStream = new SimpleAudioStream();
audioStream->open("audio.wav");
osg::ref_ptr<osgAudio::SoundState> soundState = new osgAudio::SoundState();
soundState->setAudioStream(audioStream.get());
soundState->setLooping(true);
soundState->play();
osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer();
viewer->setSceneData(soundState.get());
return viewer->run();
}
在上述代码中,我们通过 osgDB
来读取音频文件,实现了 open
方法。我们还创建了一个 osgAudio::SoundState
对象,将 SimpleAudioStream
对象设置为其音频流。最后,我们通过 osgViewer::Viewer
类来展示这个音频文件。