1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - include - snd - common
3   File:     utl.h
4 
5   Copyright 2004-2008 Nintendo. 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   $Date:: 2008-09-18#$
14   $Rev: 8573 $
15   $Author: okubata_ryoma $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NITRO_SND_COMMON_UTIL_H_
19 #define NITRO_SND_COMMON_UTIL_H_
20 
21 #include <nitro/types.h>
22 
23 #if !(defined(SDK_WIN32) || defined(SDK_FROM_TOOL))
24 #include <nitro/misc.h>
25 #else
26 #define SDK_MINMAX_ASSERT(exp, min, max)           ((void) 0)
27 #endif /* SDK_FROM_TOOL */
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 /******************************************************************************
34     Macro Definitions
35  ******************************************************************************/
36 
37 #define SND_PITCH_DIVISION_BIT     6   // Half-tone resolution. (Number of bits.)
38 #define SND_PITCH_DIVISION_RANGE ( 1 << SND_PITCH_DIVISION_BIT )        // Half-tone resolution
39 
40 #define SND_PITCH_TABLE_SIZE     ( 12 * SND_PITCH_DIVISION_RANGE )      // Pitch calculation table size
41 #define SND_PITCH_TABLE_BIAS       0x10000
42 #define SND_PITCH_TABLE_SHIFT     16
43 
44 #define SND_VOLUME_DB_MIN        (-723) // -72.3.4 dB = -inf
45 #define SND_VOLUME_DB_MAX         0
46 #define SND_VOLUME_TABLE_SIZE ( SND_VOLUME_DB_MAX - SND_VOLUME_DB_MIN + 1 )
47 
48 #define SND_CALC_DECIBEL_SCALE_MAX    127
49 
50 #define SND_DECIBEL_TABLE_SIZE        128
51 #define SND_DECIBEL_SQUARE_TABLE_SIZE 128
52 
53 #define SND_SIN_TABLE_SIZE 32
54 #define SND_SIN_PERIOD ( SND_SIN_TABLE_SIZE * 4 )
55 
56 /******************************************************************************
57     Private Variable Declarations
58  ******************************************************************************/
59 
60 extern const s16 SNDi_DecibelTable[SND_DECIBEL_TABLE_SIZE];
61 extern const s16 SNDi_DecibelSquareTable[SND_DECIBEL_SQUARE_TABLE_SIZE];
62 
63 /******************************************************************************
64     Inline Function Definitions
65  ******************************************************************************/
66 
67 /*---------------------------------------------------------------------------*
68   Name:         SND_CalcDecibel
69 
70   Description:  Calculate decibels from scale factor.
71 
72   Arguments:    scale:      scale factor
73 
74   Returns:      decibel
75  *---------------------------------------------------------------------------*/
76 #ifndef _MSC_VER
77 static  inline
78 #else
79 __inline
80 #endif
SND_CalcDecibel(int scale)81 s16 SND_CalcDecibel(int scale)
82 {
83     SDK_MINMAX_ASSERT(scale, 0, SND_CALC_DECIBEL_SCALE_MAX);
84 
85     return SNDi_DecibelTable[scale];
86 }
87 
88 /*---------------------------------------------------------------------------*
89   Name:         SND_CalcDecibelSquare
90 
91   Description:  Calculate decibels from square scale factor.
92 
93   Arguments:    scale:      square scale factor
94 
95   Returns:      decibel
96  *---------------------------------------------------------------------------*/
97 #ifndef _MSC_VER
98 static  inline
99 #else
100 __inline
101 #endif
SND_CalcDecibelSquare(int scale)102 s16 SND_CalcDecibelSquare(int scale)
103 {
104     SDK_MINMAX_ASSERT(scale, 0, SND_CALC_DECIBEL_SCALE_MAX);
105 
106     return SNDi_DecibelSquareTable[scale];
107 }
108 
109 /******************************************************************************
110     Public Function Declarations
111  ******************************************************************************/
112 
113 u16     SND_CalcTimer(int orgTimer, int pitch);
114 u16     SND_CalcChannelVolume(int dB);
115 
116 #ifdef SDK_ARM7
117 
118 u16     SND_CalcRandom(void);
119 s8      SND_SinIdx(int index);
120 
121 #endif /* SDK_ARM7 */
122 
123 #ifdef __cplusplus
124 } /* extern "C" */
125 #endif
126 
127 #endif /* NITRO_SND_COMMON_UTIL_H_ */
128