diff options
author | Ricardo Garcia <rago@google.com> | 2015-04-18 14:47:04 -0700 |
---|---|---|
committer | Ricardo Garcia <rago@google.com> | 2015-04-22 10:57:24 -0700 |
commit | 5a8a95de6dad1a3bcf3da5a37b35766e89086e13 (patch) | |
tree | 64b82016eaef26f51b75470714172c06287c9c56 /include | |
parent | ea44f41bb142555e747cc11382296e94af99d312 (diff) | |
download | frameworks_av-5a8a95de6dad1a3bcf3da5a37b35766e89086e13.zip frameworks_av-5a8a95de6dad1a3bcf3da5a37b35766e89086e13.tar.gz frameworks_av-5a8a95de6dad1a3bcf3da5a37b35766e89086e13.tar.bz2 |
Use AudioPlaybackRate to hold TimestretchBufferProvider parameters
Use this struct to handle the parameters for TimestretchBufferProvider all
across the system.
Add stretch mode and fallback mode to TimestretchBuffer Provider.
Change-Id: I19099924a7003c62e48bb6ead56c785cb129fba2
Diffstat (limited to 'include')
-rw-r--r-- | include/media/AudioResamplerPublic.h | 78 | ||||
-rw-r--r-- | include/media/AudioTrack.h | 8 | ||||
-rw-r--r-- | include/private/media/AudioTrackShared.h | 26 |
3 files changed, 86 insertions, 26 deletions
diff --git a/include/media/AudioResamplerPublic.h b/include/media/AudioResamplerPublic.h index 07d946d..53b8c13 100644 --- a/include/media/AudioResamplerPublic.h +++ b/include/media/AudioResamplerPublic.h @@ -18,6 +18,9 @@ #define ANDROID_AUDIO_RESAMPLER_PUBLIC_H #include <stdint.h> +#include <math.h> + +namespace android { // AUDIO_RESAMPLER_DOWN_RATIO_MAX is the maximum ratio between the original // audio sample rate and the target rate when downsampling, @@ -34,13 +37,76 @@ // an int32_t of the phase increments, making the resulting sample rate inexact. #define AUDIO_RESAMPLER_UP_RATIO_MAX 65536 -#define AUDIO_TIMESTRETCH_SPEED_MIN 0.5f -#define AUDIO_TIMESTRETCH_SPEED_MAX 2.0f +// AUDIO_TIMESTRETCH_SPEED_MIN and AUDIO_TIMESTRETCH_SPEED_MAX define the min and max time stretch +// speeds supported by the system. These are enforced by the system and values outside this range +// will result in a runtime error. +// Depending on the AudioPlaybackRate::mStretchMode, the effective limits might be narrower than +// the ones specified here +// AUDIO_TIMESTRETCH_SPEED_MIN_DELTA is the minimum absolute speed difference that might trigger a +// parameter update +#define AUDIO_TIMESTRETCH_SPEED_MIN 0.01f +#define AUDIO_TIMESTRETCH_SPEED_MAX 20.0f #define AUDIO_TIMESTRETCH_SPEED_NORMAL 1.0f +#define AUDIO_TIMESTRETCH_SPEED_MIN_DELTA 0.0001f -#define AUDIO_TIMESTRETCH_PITCH_MIN 0.5f -#define AUDIO_TIMESTRETCH_PITCH_MAX 2.0f +// AUDIO_TIMESTRETCH_PITCH_MIN and AUDIO_TIMESTRETCH_PITCH_MAX define the min and max time stretch +// pitch shifting supported by the system. These are not enforced by the system and values +// outside this range might result in a pitch different than the one requested. +// Depending on the AudioPlaybackRate::mStretchMode, the effective limits might be narrower than +// the ones specified here. +// AUDIO_TIMESTRETCH_PITCH_MIN_DELTA is the minimum absolute pitch difference that might trigger a +// parameter update +#define AUDIO_TIMESTRETCH_PITCH_MIN 0.25f +#define AUDIO_TIMESTRETCH_PITCH_MAX 4.0f #define AUDIO_TIMESTRETCH_PITCH_NORMAL 1.0f +#define AUDIO_TIMESTRETCH_PITCH_MIN_DELTA 0.0001f + + +//Determines the current algorithm used for stretching +enum AudioTimestretchStretchMode : int32_t { + AUDIO_TIMESTRETCH_STRETCH_DEFAULT = 0, + AUDIO_TIMESTRETCH_STRETCH_SPEECH = 1, + //TODO: add more stretch modes/algorithms +}; + +//Limits for AUDIO_TIMESTRETCH_STRETCH_SPEECH mode +#define TIMESTRETCH_SONIC_SPEED_MIN 0.1f +#define TIMESTRETCH_SONIC_SPEED_MAX 6.0f + +//Determines behavior of Timestretch if current algorithm can't perform +//with current parameters. +// FALLBACK_CUT_REPEAT: (internal only) for speed <1.0 will truncate frames +// for speed > 1.0 will repeat frames +// FALLBACK_MUTE: will set all processed frames to zero +// FALLBACK_FAIL: will stop program execution and log a fatal error +enum AudioTimestretchFallbackMode : int32_t { + AUDIO_TIMESTRETCH_FALLBACK_CUT_REPEAT = -1, + AUDIO_TIMESTRETCH_FALLBACK_DEFAULT = 0, + AUDIO_TIMESTRETCH_FALLBACK_MUTE = 1, + AUDIO_TIMESTRETCH_FALLBACK_FAIL = 2, +}; + +struct AudioPlaybackRate { + float mSpeed; + float mPitch; + enum AudioTimestretchStretchMode mStretchMode; + enum AudioTimestretchFallbackMode mFallbackMode; +}; + +static const AudioPlaybackRate AUDIO_PLAYBACK_RATE_DEFAULT = { + AUDIO_TIMESTRETCH_SPEED_NORMAL, + AUDIO_TIMESTRETCH_PITCH_NORMAL, + AUDIO_TIMESTRETCH_STRETCH_DEFAULT, + AUDIO_TIMESTRETCH_FALLBACK_DEFAULT +}; + +static inline bool isAudioPlaybackRateEqual(const AudioPlaybackRate &pr1, + const AudioPlaybackRate &pr2) { + return fabs(pr1.mSpeed - pr2.mSpeed) < AUDIO_TIMESTRETCH_SPEED_MIN_DELTA && + fabs(pr1.mPitch - pr2.mPitch) < AUDIO_TIMESTRETCH_PITCH_MIN_DELTA && + pr2.mStretchMode == pr2.mStretchMode && + pr2.mFallbackMode == pr2.mFallbackMode; +} // TODO: Consider putting these inlines into a class scope @@ -77,4 +143,8 @@ static inline size_t sourceFramesNeededWithTimestretch( return required * (double)speed + 1 + 1; // accounting for rounding dependencies } +} // namespace android + +// --------------------------------------------------------------------------- + #endif // ANDROID_AUDIO_RESAMPLER_PUBLIC_H diff --git a/include/media/AudioTrack.h b/include/media/AudioTrack.h index a06197f..2d34c02 100644 --- a/include/media/AudioTrack.h +++ b/include/media/AudioTrack.h @@ -21,6 +21,7 @@ #include <media/AudioSystem.h> #include <media/AudioTimestamp.h> #include <media/IAudioTrack.h> +#include <media/AudioResamplerPublic.h> #include <utils/threads.h> namespace android { @@ -369,10 +370,10 @@ public: * Speed increases the playback rate of media, but does not alter pitch. * Pitch increases the "tonal frequency" of media, but does not affect the playback rate. */ - status_t setPlaybackRate(float speed, float pitch); + status_t setPlaybackRate(const AudioPlaybackRate &playbackRate); /* Return current playback rate */ - void getPlaybackRate(float *speed, float *pitch) const; + const AudioPlaybackRate& getPlaybackRate() const; /* Enables looping and sets the start and end points of looping. * Only supported for static buffer mode. @@ -748,8 +749,7 @@ protected: float mVolume[2]; float mSendLevel; mutable uint32_t mSampleRate; // mutable because getSampleRate() can update it - float mSpeed; // timestretch: 1.0f for normal speed. - float mPitch; // timestretch: 1.0f for normal pitch. + AudioPlaybackRate mPlaybackRate; size_t mFrameCount; // corresponds to current IAudioTrack, value is // reported back by AudioFlinger to the client size_t mReqFrameCount; // frame count to request the first or next time diff --git a/include/private/media/AudioTrackShared.h b/include/private/media/AudioTrackShared.h index 6cc2e2b..1e5064f 100644 --- a/include/private/media/AudioTrackShared.h +++ b/include/private/media/AudioTrackShared.h @@ -114,13 +114,7 @@ struct AudioTrackSharedStatic { mPosLoopQueue; }; - -struct AudioTrackPlaybackRate { - float mSpeed; - float mPitch; -}; - -typedef SingleStateQueue<AudioTrackPlaybackRate> AudioTrackPlaybackRateQueue; +typedef SingleStateQueue<AudioPlaybackRate> PlaybackRateQueue; // ---------------------------------------------------------------------------- @@ -168,7 +162,7 @@ private: uint32_t mSampleRate; // AudioTrack only: client's requested sample rate in Hz // or 0 == default. Write-only client, read-only server. - AudioTrackPlaybackRateQueue::Shared mPlaybackRateQueue; + PlaybackRateQueue::Shared mPlaybackRateQueue; // client write-only, server read-only uint16_t mSendLevel; // Fixed point U4.12 so 0x1000 means 1.0 @@ -345,10 +339,7 @@ public: mCblk->mSampleRate = sampleRate; } - void setPlaybackRate(float speed, float pitch) { - AudioTrackPlaybackRate playbackRate; - playbackRate.mSpeed = speed; - playbackRate.mPitch = pitch; + void setPlaybackRate(const AudioPlaybackRate& playbackRate) { mPlaybackRateMutator.push(playbackRate); } @@ -365,7 +356,7 @@ public: status_t waitStreamEndDone(const struct timespec *requested); private: - AudioTrackPlaybackRateQueue::Mutator mPlaybackRateMutator; + PlaybackRateQueue::Mutator mPlaybackRateMutator; }; class StaticAudioTrackClientProxy : public AudioTrackClientProxy { @@ -483,8 +474,7 @@ public: : ServerProxy(cblk, buffers, frameCount, frameSize, true /*isOut*/, clientInServer), mPlaybackRateObserver(&cblk->mPlaybackRateQueue) { mCblk->mSampleRate = sampleRate; - mPlaybackRate.mSpeed = AUDIO_TIMESTRETCH_SPEED_NORMAL; - mPlaybackRate.mPitch = AUDIO_TIMESTRETCH_PITCH_NORMAL; + mPlaybackRate = AUDIO_PLAYBACK_RATE_DEFAULT; } protected: virtual ~AudioTrackServerProxy() { } @@ -520,11 +510,11 @@ public: virtual size_t framesReleased() const { return mCblk->mServer; } // Return the playback speed and pitch read atomically. Not multi-thread safe on server side. - void getPlaybackRate(float *speed, float *pitch); + AudioPlaybackRate getPlaybackRate(); private: - AudioTrackPlaybackRate mPlaybackRate; // last observed playback rate - AudioTrackPlaybackRateQueue::Observer mPlaybackRateObserver; + AudioPlaybackRate mPlaybackRate; // last observed playback rate + PlaybackRateQueue::Observer mPlaybackRateObserver; }; class StaticAudioTrackServerProxy : public AudioTrackServerProxy { |