From 82f3651f8130c4ad71cf4a5c4971accfe2619a36 Mon Sep 17 00:00:00 2001
From: Chris Robinson <chris.kcat@gmail.com>
Date: Thu, 26 Nov 2015 23:40:02 -0800
Subject: [PATCH] Treat the sound offset as the offset in seconds

---
 apps/openmw/mwbase/soundmanager.hpp     |  4 ++--
 apps/openmw/mwclass/door.cpp            | 10 ++++----
 apps/openmw/mwsound/openal_output.cpp   | 31 ++++---------------------
 apps/openmw/mwsound/openal_output.hpp   |  2 --
 apps/openmw/mwsound/sound_output.hpp    |  4 ++--
 apps/openmw/mwsound/soundmanagerimp.hpp |  8 +++----
 6 files changed, 16 insertions(+), 43 deletions(-)

diff --git a/apps/openmw/mwbase/soundmanager.hpp b/apps/openmw/mwbase/soundmanager.hpp
index 9366875e34..67203d0a41 100644
--- a/apps/openmw/mwbase/soundmanager.hpp
+++ b/apps/openmw/mwbase/soundmanager.hpp
@@ -109,13 +109,13 @@ namespace MWBase
                                        PlayType type=Play_TypeSfx, PlayMode mode=Play_Normal,
                                        float offset=0) = 0;
             ///< Play a sound, independently of 3D-position
-            ///< @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
+            ///< @param offset Number of seconds into the sound to start playback.
 
             virtual MWBase::SoundPtr playSound3D(const MWWorld::Ptr &reference, const std::string& soundId,
                                                  float volume, float pitch, PlayType type=Play_TypeSfx,
                                                  PlayMode mode=Play_Normal, float offset=0) = 0;
             ///< Play a 3D sound attached to an MWWorld::Ptr. Will be updated automatically with the Ptr's position, unless Play_NoTrack is specified.
-            ///< @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
+            ///< @param offset Number of seconds into the sound to start playback.
 
             virtual MWBase::SoundPtr playSound3D(const osg::Vec3f& initialPos, const std::string& soundId,
                                                  float volume, float pitch, PlayType type=Play_TypeSfx, PlayMode mode=Play_Normal, float offset=0) = 0;
diff --git a/apps/openmw/mwclass/door.cpp b/apps/openmw/mwclass/door.cpp
index b469dc9e2f..47219deb77 100644
--- a/apps/openmw/mwclass/door.cpp
+++ b/apps/openmw/mwclass/door.cpp
@@ -169,7 +169,9 @@ namespace MWClass
                 {
                     MWBase::Environment::get().getSoundManager()->fadeOutSound3D(ptr,
                             closeSound, 0.5f);
-                    float offset = doorRot/ 3.14159265f * 2.0f;
+                    // Doors rotate at 90 degrees per second, so start the sound at
+                    // where it would be at the current rotation.
+                    float offset = doorRot/(3.14159265f * 0.5f);
                     action->setSoundOffset(offset);
                     action->setSound(openSound);
                 }
@@ -177,10 +179,8 @@ namespace MWClass
                 {
                     MWBase::Environment::get().getSoundManager()->fadeOutSound3D(ptr,
                                                 openSound, 0.5f);
-                    float offset = 1.0f - doorRot/ 3.14159265f * 2.0f;
-                    //most if not all door have closing bang somewhere in the middle of the sound,
-                    //so we divide offset by two
-                    action->setSoundOffset(offset * 0.5f);
+                    float offset = 1.0f - doorRot/(3.14159265f * 0.5f);
+                    action->setSoundOffset(std::max(offset, 0.0f));
                     action->setSound(closeSound);
                 }
 
diff --git a/apps/openmw/mwsound/openal_output.cpp b/apps/openmw/mwsound/openal_output.cpp
index bafd272af6..a5246bde94 100644
--- a/apps/openmw/mwsound/openal_output.cpp
+++ b/apps/openmw/mwsound/openal_output.cpp
@@ -146,18 +146,6 @@ static ALenum getALFormat(ChannelConfig chans, SampleType type)
     return AL_NONE;
 }
 
-static double getBufferLength(ALuint buffer)
-{
-    ALint bufferSize, frequency, channels, bitsPerSample;
-    alGetBufferi(buffer, AL_SIZE, &bufferSize);
-    alGetBufferi(buffer, AL_FREQUENCY, &frequency);
-    alGetBufferi(buffer, AL_CHANNELS, &channels);
-    alGetBufferi(buffer, AL_BITS, &bitsPerSample);
-    throwALerror();
-
-    return (8.0*bufferSize)/(frequency*channels*bitsPerSample);
-}
-
 
 //
 // A streaming OpenAL sound.
@@ -863,14 +851,9 @@ MWBase::SoundPtr OpenAL_Output::playSound(Sound_Handle data, float vol, float ba
     }
 
     sound->updateAll(true);
-    if(offset < 0.0f)
-        offset = 0.0f;
-    if(offset > 1.0f)
-        offset = 1.0f;
+    alSourcei(src, AL_BUFFER, GET_PTRID(data));
+    alSourcef(src, AL_SEC_OFFSET, offset/pitch);
 
-    ALuint buffer = GET_PTRID(data);
-    alSourcei(src, AL_BUFFER, buffer);
-    alSourcef(src, AL_SEC_OFFSET, static_cast<ALfloat>(getBufferLength(buffer)*offset / pitch));
     alSourcePlay(src);
     throwALerror();
 
@@ -898,14 +881,8 @@ MWBase::SoundPtr OpenAL_Output::playSound3D(Sound_Handle data, const osg::Vec3f
     }
 
     sound->updateAll(false);
-    if(offset < 0.0f)
-        offset = 0.0f;
-    if(offset > 1.0f)
-        offset = 1.0f;
-
-    ALuint buffer = GET_PTRID(data);
-    alSourcei(src, AL_BUFFER, buffer);
-    alSourcef(src, AL_SEC_OFFSET, static_cast<ALfloat>(getBufferLength(buffer)*offset / pitch));
+    alSourcei(src, AL_BUFFER, GET_PTRID(data));
+    alSourcef(src, AL_SEC_OFFSET, offset/pitch);
 
     alSourcePlay(src);
     throwALerror();
diff --git a/apps/openmw/mwsound/openal_output.hpp b/apps/openmw/mwsound/openal_output.hpp
index 0f54da9b5c..fdce32c8aa 100644
--- a/apps/openmw/mwsound/openal_output.hpp
+++ b/apps/openmw/mwsound/openal_output.hpp
@@ -42,9 +42,7 @@ namespace MWSound
         virtual void unloadSound(Sound_Handle data);
         virtual size_t getSoundDataSize(Sound_Handle data) const;
 
-        /// @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
         virtual MWBase::SoundPtr playSound(Sound_Handle data, float vol, float basevol, float pitch, int flags, float offset);
-        /// @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
         virtual MWBase::SoundPtr playSound3D(Sound_Handle data, const osg::Vec3f &pos,
                                              float vol, float basevol, float pitch, float min, float max, int flags, float offset);
         virtual MWBase::SoundPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags);
diff --git a/apps/openmw/mwsound/sound_output.hpp b/apps/openmw/mwsound/sound_output.hpp
index c91431f696..e358ba3447 100644
--- a/apps/openmw/mwsound/sound_output.hpp
+++ b/apps/openmw/mwsound/sound_output.hpp
@@ -30,9 +30,9 @@ namespace MWSound
         virtual void unloadSound(Sound_Handle data) = 0;
         virtual size_t getSoundDataSize(Sound_Handle data) const = 0;
 
-        /// @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
+        /// @param offset Number of seconds into the sound to start playback.
         virtual MWBase::SoundPtr playSound(Sound_Handle data, float vol, float basevol, float pitch, int flags, float offset) = 0;
-        /// @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
+        /// @param offset Number of seconds into the sound to start playback.
         virtual MWBase::SoundPtr playSound3D(Sound_Handle data, const osg::Vec3f &pos,
                                              float vol, float basevol, float pitch, float min, float max, int flags, float offset) = 0;
         virtual MWBase::SoundPtr streamSound(DecoderPtr decoder, float basevol, float pitch, int flags) = 0;
diff --git a/apps/openmw/mwsound/soundmanagerimp.hpp b/apps/openmw/mwsound/soundmanagerimp.hpp
index db0b06dc06..fa855646d6 100644
--- a/apps/openmw/mwsound/soundmanagerimp.hpp
+++ b/apps/openmw/mwsound/soundmanagerimp.hpp
@@ -166,20 +166,18 @@ namespace MWSound
 
         virtual MWBase::SoundPtr playSound(const std::string& soundId, float volume, float pitch, PlayType type=Play_TypeSfx, PlayMode mode=Play_Normal, float offset=0);
         ///< Play a sound, independently of 3D-position
-        ///< @param offset value from [0,1], when to start playback. 0 is beginning, 1 is end.
+        ///< @param offset Number of seconds into the sound to start playback.
 
         virtual MWBase::SoundPtr playSound3D(const MWWorld::Ptr &reference, const std::string& soundId,
                                              float volume, float pitch, PlayType type=Play_TypeSfx,
                                              PlayMode mode=Play_Normal, float offset=0);
         ///< Play a 3D sound attached to an MWWorld::Ptr. Will be updated automatically with the Ptr's position, unless Play_NoTrack is specified.
-        ///< @param offset Value from [0,1] meaning from which fraction the sound the playback starts.
+        ///< @param offset Number of seconds into the sound to start playback.
 
         virtual MWBase::SoundPtr playSound3D(const osg::Vec3f& initialPos, const std::string& soundId,
                                              float volume, float pitch, PlayType type, PlayMode mode, float offset=0);
         ///< Play a 3D sound at \a initialPos. If the sound should be moving, it must be updated using Sound::setPosition.
-
-        ///< Play a sound from an object
-        ///< @param offset value from [0,1], when to start playback. 0 is beginning, 1 is end.
+        ///< @param offset Number of seconds into the sound to start playback.
 
         virtual void stopSound3D(const MWWorld::Ptr &reference, const std::string& soundId);
         ///< Stop the given object from playing the given sound,