1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     snd_CurveLfo.cpp
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 13145 $
14  *---------------------------------------------------------------------------*/
15 
16 #include "precompiled.h"
17 
18 #include <nw/snd/snd_CurveLfo.h>
19 #include <nw/assert.h>
20 
21 namespace nw {
22 namespace snd {
23 namespace internal {
24 
25 /* ========================================================================
26         public function
27    ======================================================================== */
28 
29 /*---------------------------------------------------------------------------*
30   Name:         LfoParam::Init
31 
32   Description:  LFOパラメータ構造体を初期化します
33 
34   Arguments:    None.
35 
36   Returns:      None.
37  *---------------------------------------------------------------------------*/
Initialize()38 void CurveLfoParam::Initialize()
39 {
40     depth  = 0.0f;
41     range  = 1;
42     speed  = 6.25f;
43     delay  = 0;
44 }
45 
46 /*---------------------------------------------------------------------------*
47   Name:         Reset
48 
49   Description:  LFOをリセットします
50 
51   Arguments:    なし
52 
53   Returns:      なし
54  *---------------------------------------------------------------------------*/
Reset()55 void CurveLfo::Reset()
56 {
57     m_Counter = 0.0f;
58     m_DelayCounter = 0;
59 }
60 
61 /*---------------------------------------------------------------------------*
62   Name:         Update
63 
64   Description:  LFOを更新します
65 
66   Arguments:    msec - 更新時間[msec]
67 
68   Returns:      なし
69  *---------------------------------------------------------------------------*/
Update(int msec)70 void CurveLfo::Update( int msec )
71 {
72     if ( m_DelayCounter < m_Param.delay )
73     {
74         if ( m_DelayCounter + msec <= m_Param.delay )
75         {
76             m_DelayCounter += msec;
77             return;
78         }
79         else
80         {
81             msec -= m_Param.delay - m_DelayCounter;
82             m_DelayCounter = m_Param.delay;
83         }
84     }
85 
86     m_Counter += m_Param.speed * msec / 1000.0f;
87     m_Counter -= static_cast<int>( m_Counter );
88 }
89 
90 /*---------------------------------------------------------------------------*
91   Name:         GetValue
92 
93   Description:  現在のLFO値を取得します
94 
95   Arguments:    なし
96 
97   Returns:      現在のLFO値
98  *---------------------------------------------------------------------------*/
GetValue() const99 f32 CurveLfo::GetValue() const
100 {
101     if ( m_Param.depth == 0.0f ) return 0.0f;
102     if ( m_DelayCounter < m_Param.delay ) return 0.0f;
103 
104     f32 value = GetSinIdx( static_cast<int>( m_Counter * TABLE_SIZE * 4 ) ) / 127.0f;
105     value *= m_Param.depth;
106     value *= m_Param.range ;
107 
108     return value;
109 }
110 
GetSinIdx(int index)111 s8 CurveLfo::GetSinIdx( int index )
112 {
113     //  LFO用サイン波テーブル
114     static const s8 sinTable[ TABLE_SIZE + 1 ] =
115     {
116            0,    6,   12,   19,   25,   31,   37,   43,
117           49,   54,   60,   65,   71,   76,   81,   85,
118           90,   94,   98,  102,  106,  109,  112,  115,
119          117,  120,  122,  123,  125,  126,  126,  127,
120          127
121     };
122 
123     NW_MINMAXLT_ASSERT( index, 0, PERIOD );
124 
125     if ( index < TABLE_SIZE )
126     {
127         return sinTable[ index ];
128     }
129     if ( index < TABLE_SIZE * 2 )
130     {
131         return sinTable[ TABLE_SIZE - ( index - TABLE_SIZE ) ];
132     }
133     if ( index < TABLE_SIZE * 3 )
134     {
135         return static_cast<s8>( - sinTable[ index - TABLE_SIZE * 2 ] );
136     }
137     return static_cast<s8>( - sinTable[ TABLE_SIZE - ( index - TABLE_SIZE * 3 ) ] );
138 }
139 
140 } // namespace nw::snd::internal
141 } // namespace nw::snd
142 } // namespace nw
143 
144