Fix T49764: Audio strips crackle when animating the volume
authorJörg Müller <nexyon@gmail.com>
Sat, 22 Oct 2016 11:38:41 +0000 (13:38 +0200)
committerJörg Müller <nexyon@gmail.com>
Sat, 22 Oct 2016 11:39:55 +0000 (13:39 +0200)
- Implemented linear interpolation for volume changes in the software
mixer.
- Using this in the software device.

intern/audaspace/intern/AUD_Mixer.cpp
intern/audaspace/intern/AUD_Mixer.h
intern/audaspace/intern/AUD_SoftwareDevice.cpp
intern/audaspace/intern/AUD_SoftwareDevice.h

index 380affa6bd1065efb23d53f1de7f8980b227418e..78dfaddd27a8a1d34e952533deb174489bce32ad 100644 (file)
@@ -95,6 +95,21 @@ void AUD_Mixer::mix(sample_t* buffer, int start, int length, float volume)
                out[i + start] += buffer[i] * volume;
 }
 
+void AUD_Mixer::mix(sample_t* buffer, int start, int length, float volume_to, float volume_from)
+{
+       sample_t* out = m_buffer.getBuffer();
+
+       length = (std::min(m_length, length + start) - start);
+
+       for(int i = 0; i < length; i++)
+       {
+               float volume = volume_from * (1.0f - i / float(length)) + volume_to * (i / float(length));
+
+               for(int c = 0; c < m_specs.channels; c++)
+                       out[(i + start) * m_specs.channels + c] += buffer[i * m_specs.channels + c] * volume;
+       }
+}
+
 void AUD_Mixer::read(data_t* buffer, float volume)
 {
        sample_t* out = m_buffer.getBuffer();
index 3dd03b0a3fe7525fa15d5c7ea733535879db998b..0735fee715b936a1feb1f554ddd5ee705c98fd70 100644 (file)
@@ -95,6 +95,8 @@ public:
         */
        void mix(sample_t* buffer, int start, int length, float volume);
 
+       void mix(sample_t* buffer, int start, int length, float volume_to, float volume_from);
+
        /**
         * Writes the mixing buffer into an output buffer.
         * \param buffer The target buffer for superposing.
index c4277a6d02e1e9246f77b8310cab653ce5ac7c8c..15594d340be949346db9edcb88e24a489d91dcc1 100644 (file)
@@ -89,7 +89,7 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::pause(bool keep)
 }
 
 AUD_SoftwareDevice::AUD_SoftwareHandle::AUD_SoftwareHandle(AUD_SoftwareDevice* device, boost::shared_ptr<AUD_IReader> reader, boost::shared_ptr<AUD_PitchReader> pitch, boost::shared_ptr<AUD_ResampleReader> resampler, boost::shared_ptr<AUD_ChannelMapperReader> mapper, bool keep) :
-       m_reader(reader), m_pitch(pitch), m_resampler(resampler), m_mapper(mapper), m_keep(keep), m_user_pitch(1.0f), m_user_volume(1.0f), m_user_pan(0.0f), m_volume(1.0f), m_loopcount(0),
+       m_reader(reader), m_pitch(pitch), m_resampler(resampler), m_mapper(mapper), m_keep(keep), m_user_pitch(1.0f), m_user_volume(1.0f), m_user_pan(0.0f), m_volume(1.0f), m_old_volume(1.0f), m_loopcount(0),
        m_relative(true), m_volume_max(1.0f), m_volume_min(0), m_distance_max(std::numeric_limits<float>::max()),
        m_distance_reference(1.0f), m_attenuation(1.0f), m_cone_angle_outer(M_PI), m_cone_angle_inner(M_PI), m_cone_volume_outer(0),
        m_flags(AUD_RENDER_CONE), m_stop(NULL), m_stop_data(NULL), m_status(AUD_STATUS_PLAYING), m_device(device)
@@ -100,6 +100,8 @@ void AUD_SoftwareDevice::AUD_SoftwareHandle::update()
 {
        int flags = 0;
 
+       m_old_volume = m_volume;
+
        AUD_Vector3 SL;
        if(m_relative)
                SL = -m_location;
@@ -404,7 +406,7 @@ bool AUD_SoftwareDevice::AUD_SoftwareHandle::setVolume(float volume)
 
        if(volume == 0)
        {
-               m_volume = volume;
+               m_old_volume = m_volume = volume;
                m_flags |= AUD_RENDER_VOLUME;
        }
        else
@@ -772,7 +774,7 @@ void AUD_SoftwareDevice::mix(data_t* buffer, int length)
                        // in case of looping
                        while(pos + len < length && sound->m_loopcount && eos)
                        {
-                               m_mixer->mix(buf, pos, len, sound->m_volume);
+                               m_mixer->mix(buf, pos, len, sound->m_volume, sound->m_old_volume);
 
                                pos += len;
 
@@ -789,7 +791,7 @@ void AUD_SoftwareDevice::mix(data_t* buffer, int length)
                                        break;
                        }
 
-                       m_mixer->mix(buf, pos, len, sound->m_volume);
+                       m_mixer->mix(buf, pos, len, sound->m_volume, sound->m_old_volume);
 
                        // in case the end of the sound is reached
                        if(eos && !sound->m_loopcount)
index 3c8c1e438a370aa24ea588cff81b5b70db722177..54e49c87b277082a1a246196fcdf34757fb0fa88 100644 (file)
@@ -84,6 +84,7 @@ protected:
 
                /// The calculated final volume of the source.
                float m_volume;
+               float m_old_volume;
 
                /// The loop count of the source.
                int m_loopcount;