Table of Contents

include/AK/Plugin/PluginServices/AkFXDurationHandler.h

Go to the documentation of this file.
00001 /*******************************************************************************
00002 The content of this file includes portions of the AUDIOKINETIC Wwise Technology
00003 released in source code form as part of the SDK installer package.
00004 
00005 Commercial License Usage
00006 
00007 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
00008 may use this file in accordance with the end user license agreement provided 
00009 with the software or, alternatively, in accordance with the terms contained in a
00010 written agreement between you and Audiokinetic Inc.
00011 
00012 Apache License Usage
00013 
00014 Alternatively, this file may be used under the Apache License, Version 2.0 (the 
00015 "Apache License"); you may not use this file except in compliance with the 
00016 Apache License. You may obtain a copy of the Apache License at 
00017 http://www.apache.org/licenses/LICENSE-2.0.
00018 
00019 Unless required by applicable law or agreed to in writing, software distributed
00020 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
00021 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
00022 the specific language governing permissions and limitations under the License.
00023 
00024   Version: <VERSION>  Build: <BUILDNUMBER>
00025   Copyright (c) <COPYRIGHTYEAR> Audiokinetic Inc.
00026 *******************************************************************************/
00027  
00028 #ifndef _AKFXDURATIONHANDLER_H_
00029 #define _AKFXDURATIONHANDLER_H_
00030 
00031 #include <AK/SoundEngine/Common/AkTypes.h>
00032 
00033 /// Duration handler service for source plug-in.
00034 /// Duration may change between different execution
00035 class AkFXDurationHandler
00036 {
00037 public:
00038 
00039     /// Setup duration handler
00040     inline void Setup(  
00041         AkReal32 in_fDuration,      ///< Duration (in secs)
00042         AkInt16 in_iLoopingCount,   ///< Number of loop iterations (0 == infinite)
00043         AkUInt32 in_uSampleRate     ///< Sample rate
00044         )
00045     {
00046         m_uSampleRate = in_uSampleRate;
00047         SetDuration( in_fDuration );
00048         SetLooping( in_iLoopingCount );
00049         Reset();
00050     }
00051 
00052     /// Reset looping and frame counters and start again.
00053     inline void Reset()
00054     {
00055         m_uFrameCount = 0;
00056     }
00057 
00058     /// Change number of loop iterations (0 == infinite).
00059     inline void SetLooping( AkInt16 in_iNumLoops )
00060     {
00061         m_iNumLoops = in_iNumLoops;
00062     }
00063 
00064     /// Set current duration per iteration (in secs).
00065     inline void SetDuration( AkReal32 in_fDuration )
00066     {
00067         m_uIterationFrame = (AkUInt32) (in_fDuration*m_uSampleRate);
00068         m_uIterationFrame = (m_uIterationFrame + 3) & ~3; // Align to next 4 frame boundary for SIMD alignment
00069     }
00070 
00071     /// Return current total duration (considering looping) in secs.
00072     inline AkReal32 GetDuration() const
00073     {
00074         // Note: Infinite looping will return a duration of 0 secs.
00075         return (AkReal32)(m_uIterationFrame*m_iNumLoops)/m_uSampleRate;
00076     }
00077 
00078     /// Set current number of frames to be produced (validFrames) 
00079     /// and output state of audio buffer and advance internal state.
00080     inline void ProduceBuffer( AkAudioBuffer * io_pBuffer )
00081     {
00082         io_pBuffer->eState = ProduceBuffer( io_pBuffer->MaxFrames(), io_pBuffer->uValidFrames );
00083     }
00084 
00085     /// Set current number of frames to be produced (validFrames) 
00086     /// and output state of audio buffer and advance internal state.
00087     inline AKRESULT ProduceBuffer( AkUInt16 in_uMaxFrames, AkUInt16 & out_uValidFrames )
00088     {
00089         // Infinite looping or not reached the end, always producing full capacity
00090         out_uValidFrames = in_uMaxFrames;
00091         AKRESULT eState = AK_DataReady;
00092 
00093         if ( m_iNumLoops != 0 )
00094         {
00095             // Finite looping, produce full buffer untill the end.
00096             const AkUInt32 uTotalFrames = m_iNumLoops*m_uIterationFrame;
00097             if ( m_uFrameCount < uTotalFrames )
00098             {
00099                 const AkUInt32 uFramesRemaining = uTotalFrames-m_uFrameCount;
00100                 if ( uFramesRemaining <= in_uMaxFrames )
00101                 {
00102                     out_uValidFrames = (AkUInt16)uFramesRemaining;
00103                     eState = AK_NoMoreData;
00104                 }
00105             }
00106             else
00107             {
00108                 out_uValidFrames = 0;
00109                 eState = AK_NoMoreData;
00110             }
00111         }   
00112         m_uFrameCount += out_uValidFrames;
00113 
00114         return eState;
00115     }
00116 
00117 protected:
00118 
00119     AkUInt32    m_uIterationFrame;  // Number of frames in a single loop iteration 
00120     AkUInt32    m_uFrameCount;      // Number of frames output in the current iteration
00121     AkUInt32    m_uSampleRate;      // Sample rate used to convert time to samples
00122     AkInt16     m_iNumLoops;        // Number of loop iterations (0 == infinite looping)
00123     
00124 };
00125 
00126 #endif // _AKFXDURATIONHANDLER_H_