1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: mic.cpp
4
5 Copyright (C)2009-2012 Nintendo Co., Ltd. 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 $Rev: 46365 $
14 *---------------------------------------------------------------------------*/
15
16 #include <nn/mic.h>
17 #include "mic.h"
18
19 namespace
20 {
21 char s_MicBuffer[MIC_MEMORY_SIZE] NN_ATTRIBUTE_ALIGN(nn::mic::BUFFER_ALIGNMENT_ADDRESS);
22 bool s_RetryStart;
23 u8 s_MicGain;
24 u32 s_SamplingSize;
25
StartSampling()26 void StartSampling()
27 {
28 nn::Result result = nn::mic::StartSampling(nn::mic::SAMPLING_TYPE_SIGNED_8BIT, nn::mic::SAMPLING_RATE_8180, 0, s_SamplingSize, true);
29 if ( result.IsFailure() )
30 {
31 if ( result != nn::mic::CTR::ResultShellClose() )
32 {
33 NN_UTIL_PANIC_IF_FAILED(result);
34 }
35 s_RetryStart = true;
36 }
37 else
38 {
39 s_RetryStart = false;
40 }
41 }
42 }
43
StartMic(void)44 void StartMic(void)
45 {
46 // Initialize mic
47 nn::Result result = nn::mic::Initialize();
48 NN_UTIL_PANIC_IF_FAILED(result);
49
50 // Sets buffer that the mic library uses
51 result = nn::mic::SetBuffer(s_MicBuffer, sizeof(s_MicBuffer));
52 NN_UTIL_PANIC_IF_FAILED(result);
53
54 // Gets sampling buffer size
55 result = nn::mic::GetSamplingBufferSize(&s_SamplingSize);
56 NN_UTIL_PANIC_IF_FAILED(result);
57
58 // Sets MIC amp
59 result = nn::mic::GetAmpGain(&s_MicGain);
60 NN_UTIL_PANIC_IF_FAILED(result);
61
62 result = nn::mic::SetAmpGain(MIC_GAIN);
63 NN_UTIL_PANIC_IF_FAILED(result);
64
65 // MIC power on
66 result = nn::mic::SetAmp(true);
67 NN_UTIL_PANIC_IF_FAILED(result);
68
69 // Start mic sampling
70 StartSampling();
71 }
72
EndMic(void)73 void EndMic(void)
74 {
75 // MIC power off
76 nn::Result result = nn::mic::SetAmp(false);
77 NN_UTIL_PANIC_IF_FAILED(result);
78
79 // Restore MIC amp
80 result = nn::mic::SetAmpGain(s_MicGain);
81 NN_UTIL_PANIC_IF_FAILED(result);
82
83 // Even if stopping the sample fails here, it stops automatically with mic::Finalize
84 result = nn::mic::StopSampling();
85 if ( result.IsFailure() )
86 {
87 if ( result != nn::mic::CTR::ResultShellClose() )
88 {
89 NN_UTIL_PANIC_IF_FAILED(result);
90 }
91 }
92
93 // Does not use the buffer set in the mic library
94 result = nn::mic::ResetBuffer();
95 NN_UTIL_PANIC_IF_FAILED(result);
96
97 result = nn::mic::Finalize();
98 NN_UTIL_PANIC_IF_FAILED(result);
99 }
100
GetLoudness(void)101 u8 GetLoudness(void)
102 {
103 if ( s_RetryStart )
104 {
105 StartSampling();
106 }
107
108 const u32 MAX_LOUDNESS = s_SamplingSize * 128;
109 s8* buffer = reinterpret_cast<s8*>(s_MicBuffer);
110
111 u32 sum = 0;
112 for(int i = 0; i < s_SamplingSize; ++i)
113 {
114 s8 val = *(buffer + i);
115 if(val < 0) sum -= val;
116 else sum += val;
117 }
118
119 return static_cast<u8>(sum * 255 / MAX_LOUDNESS);
120 }
121
122 /*---------------------------------------------------------------------------*
123 End of file
124 *---------------------------------------------------------------------------*/
125