Browse Source

Squashed commit of the following:

commit 2542705105c840d7507893d24af70ca4673532bd
Author: meshonline <[email protected]>
Date:   Fri May 12 18:46:45 2017 +0800

    add seek method.
Lasse Öörni 8 years ago
parent
commit
e6b9b9557b

+ 1 - 0
Source/Urho3D/AngelScript/AudioAPI.cpp

@@ -56,6 +56,7 @@ void RegisterSoundSources(asIScriptEngine* engine)
     RegisterSoundSource<SoundSource3D>(engine, "SoundSource3D");
     // Allow creation of sound sources also outside scene
     RegisterObjectConstructor<SoundSource>(engine, "SoundSource");
+    engine->RegisterObjectMethod("SoundSource", "void Seek(float)", asMETHOD(SoundSource, Seek), asCALL_THISCALL);
     engine->RegisterObjectMethod("SoundSource3D", "void SetDistanceAttenuation(float, float, float)", asMETHOD(SoundSource3D, SetDistanceAttenuation), asCALL_THISCALL);
     engine->RegisterObjectMethod("SoundSource3D", "void SetAngleAttenuation(float, float)", asMETHOD(SoundSource3D, SetAngleAttenuation), asCALL_THISCALL);
     engine->RegisterObjectMethod("SoundSource3D", "void set_nearDistance(float)", asMETHOD(SoundSource3D, SetNearDistance), asCALL_THISCALL);

+ 10 - 0
Source/Urho3D/Audio/OggVorbisSoundStream.cpp

@@ -59,6 +59,16 @@ OggVorbisSoundStream::~OggVorbisSoundStream()
     }
 }
 
+bool OggVorbisSoundStream::Seek(unsigned int sample_number)
+{
+    if (!decoder_)
+        return false;
+    
+    stb_vorbis* vorbis = static_cast<stb_vorbis*>(decoder_);
+    
+    return (stb_vorbis_seek(vorbis, sample_number) == 1) ? true : false;
+}
+
 unsigned OggVorbisSoundStream::GetData(signed char* dest, unsigned numBytes)
 {
     if (!decoder_)

+ 3 - 0
Source/Urho3D/Audio/OggVorbisSoundStream.h

@@ -39,6 +39,9 @@ public:
     /// Destruct.
     ~OggVorbisSoundStream();
 
+    /// Seek to sample number
+    virtual bool Seek(unsigned int sample_number);
+
     /// Produce sound data into destination. Return number of bytes produced. Called by SoundSource from the mixing thread.
     virtual unsigned GetData(signed char* dest, unsigned numBytes);
 

+ 24 - 0
Source/Urho3D/Audio/SoundSource.cpp

@@ -144,6 +144,30 @@ void SoundSource::RegisterObject(Context* context)
     URHO3D_ACCESSOR_ATTRIBUTE("Play Position", GetPositionAttr, SetPositionAttr, int, 0, AM_FILE);
 }
 
+void SoundSource::Seek(float seekTime)
+{
+    // ignore buffered sound stream
+    if (!audio_ || !sound_ || (soundStream_ && !sound_->IsCompressed()))
+        return;
+
+    // set to valid range
+    seekTime = Clamp(seekTime, 0.0f, sound_->GetLength());
+
+    if (!soundStream_)
+    {
+        // raw or wav format
+        SetPositionAttr((int)(seekTime * (sound_->GetSampleSize() * sound_->GetFrequency())));
+    }
+    else
+    {
+        // ogg format
+        if (soundStream_->Seek((unsigned int)(seekTime * soundStream_->GetFrequency())))
+        {
+            timePosition_ = seekTime;
+        }
+    }
+}
+
 void SoundSource::Play(Sound* sound)
 {
     if (!audio_)

+ 2 - 0
Source/Urho3D/Audio/SoundSource.h

@@ -48,6 +48,8 @@ public:
     /// Register object factory.
     static void RegisterObject(Context* context);
 
+    /// Seek to time.
+    void Seek(float seekTime);
     /// Play a sound.
     void Play(Sound* sound);
     /// Play a sound with specified frequency.

+ 5 - 0
Source/Urho3D/Audio/SoundStream.cpp

@@ -39,6 +39,11 @@ SoundStream::~SoundStream()
 {
 }
 
+bool SoundStream::Seek(unsigned int sample_number)
+{
+    return false;
+}
+    
 void SoundStream::SetFormat(unsigned frequency, bool sixteenBit, bool stereo)
 {
     frequency_ = frequency;

+ 3 - 0
Source/Urho3D/Audio/SoundStream.h

@@ -36,6 +36,9 @@ public:
     /// Destruct.
     ~SoundStream();
 
+    /// Seek to sample number
+    virtual bool Seek(unsigned int sample_number);
+    
     /// Produce sound data into destination. Return number of bytes produced. Called by SoundSource from the mixing thread.
     virtual unsigned GetData(signed char* dest, unsigned numBytes) = 0;
 

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Audio/SoundSource.pkg

@@ -5,6 +5,7 @@ enum AutoRemoveMode {};
 
 class SoundSource : public Component
 {
+    void Seek(float seekTime);
     void Play(Sound* sound);
     void Play(Sound* sound, float frequency);
     void Play(Sound* sound, float frequency, float gain);