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.h>
17 #include <nn/mic.h>
18 #include "mic.h"
19
20 char MicDemo::m_MicBuffer[MIC_BUFFER_SIZE] NN_ATTRIBUTE_ALIGN(nn::mic::BUFFER_ALIGNMENT_ADDRESS);
21
Initialize(demo::RenderSystemDrawing * p_RenderSystem)22 void MicDemo::Initialize(demo::RenderSystemDrawing* p_RenderSystem)
23 {
24 Device::Initialize(p_RenderSystem);
25
26 // Initialize mic
27 nn::Result result = nn::mic::Initialize();
28 NN_UTIL_PANIC_IF_FAILED(result);
29
30 // Set buffer in mic library
31 result = nn::mic::SetBuffer(m_MicBuffer, MIC_BUFFER_SIZE);
32 NN_UTIL_PANIC_IF_FAILED(result);
33
34 // Gets sampling buffer size
35 size_t size;
36 result = nn::mic::GetSamplingBufferSize(&size);
37 NN_UTIL_PANIC_IF_FAILED(result);
38 m_SamplingLength = size / sizeof(s16);
39
40 // Sets MIC amp
41 result = nn::mic::GetAmpGain(&m_MicGain);
42 NN_UTIL_PANIC_IF_FAILED(result);
43
44 result = nn::mic::SetAmpGain(MIC_GAIN);
45 NN_UTIL_PANIC_IF_FAILED(result);
46
47 // MIC power on
48 result = nn::mic::SetAmp(true);
49 NN_UTIL_PANIC_IF_FAILED(result);
50 }
51
Finalize()52 void MicDemo::Finalize()
53 {
54 // MIC power off
55 nn::Result result = nn::mic::SetAmp(false);
56 NN_UTIL_PANIC_IF_FAILED(result);
57
58 // Restore MIC amp
59 result = nn::mic::SetAmpGain(m_MicGain);
60 NN_UTIL_PANIC_IF_FAILED(result);
61
62 result = nn::mic::StopSampling();
63 if ( result.IsFailure() )
64 {
65 if ( result != nn::mic::CTR::ResultShellClose() )
66 {
67 NN_UTIL_PANIC_IF_FAILED(result);
68 }
69 }
70
71 // Does not use the buffer set in the mic library
72 result = nn::mic::ResetBuffer();
73 NN_UTIL_PANIC_IF_FAILED(result);
74
75 result = nn::mic::Finalize();
76 NN_UTIL_PANIC_IF_FAILED(result);
77
78 Device::Finalize();
79 }
80
Start()81 void MicDemo::Start()
82 {
83 Device::Start();
84
85 // Start mic sampling
86 nn::Result result;
87 result = nn::mic::StartSampling(nn::mic::SAMPLING_TYPE_SIGNED_16BIT,
88 nn::mic::SAMPLING_RATE_8180,
89 0, // offset
90 m_SamplingLength * sizeof(s16), // Size
91 true);
92 if ( result.IsFailure() )
93 {
94 if ( result != nn::mic::CTR::ResultShellClose() )
95 {
96 NN_UTIL_PANIC_IF_FAILED(result);
97 }
98 // If the system is closed, the mic library will retry. This is because the mic library cannot enter a system open state if the application does not return a response to the applet
99 //
100 m_RetryStart = true;
101 }
102 else
103 {
104 m_RetryStart = false;
105 }
106 }
107
End()108 void MicDemo::End()
109 {
110 // End mic sampling
111 nn::Result result;
112 // Even if stopping the sample fails here, it stops automatically with mic::Finalize
113 result = nn::mic::StopSampling();
114 if ( result.IsFailure() )
115 {
116 if ( result != nn::mic::CTR::ResultShellClose() )
117 {
118 NN_UTIL_PANIC_IF_FAILED(result);
119 }
120 }
121
122 Device::End();
123 }
124
GetMicData(s16 * pDest,size_t size)125 void MicDemo::GetMicData(s16* pDest, size_t size)
126 {
127 uptr src;
128 nn::Result result = nn::mic::GetLastSamplingAddress(&src);
129 NN_UTIL_PANIC_IF_FAILED(result);
130
131 s16* data = reinterpret_cast<s16*>(m_MicBuffer);
132 u32 offs = src - reinterpret_cast<uptr>(m_MicBuffer);
133
134 for (int i = 0; i < size; i++)
135 {
136 *(pDest + i) = data[(offs - i) % m_SamplingLength];
137 }
138 }
139
CopyMicData(s16 * pDest,size_t size)140 void MicDemo::CopyMicData(s16* pDest, size_t size)
141 {
142 s16* data = reinterpret_cast<s16*>(m_MicBuffer);
143
144 for (int i = 0; i < size; i++)
145 {
146 pDest[i] = data[i];
147 }
148 }
149
DrawFrame(void)150 void MicDemo::DrawFrame(void)
151 {
152 // Try again if it fails to start sampling
153 if ( m_RetryStart )
154 {
155 Start();
156 }
157
158 s16 micData[NN_GX_DISPLAY1_WIDTH];
159 GetMicData(micData, NN_GX_DISPLAY1_WIDTH);
160 for (int i = 0; i < NN_GX_DISPLAY1_WIDTH - 1; i++)
161 {
162 mp_RenderSystem->DrawLine(static_cast<f32>(micData[i] / 256 + NN_GX_DISPLAY1_HEIGHT / 2), static_cast<f32>(i),
163 static_cast<f32>(micData[i + 1] / 256 + NN_GX_DISPLAY1_HEIGHT / 2), static_cast<f32>(i + 1));
164 }
165 }
166
167 /*---------------------------------------------------------------------------*
168 End of file
169 *---------------------------------------------------------------------------*/
170