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