/*---------------------------------------------------------------------------* Project: Horizon File: mic.cpp Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 46365 $ *---------------------------------------------------------------------------*/ #include #include #include "mic.h" char MicDemo::m_MicBuffer[MIC_BUFFER_SIZE] NN_ATTRIBUTE_ALIGN(nn::mic::BUFFER_ALIGNMENT_ADDRESS); void MicDemo::Initialize(demo::RenderSystemDrawing* p_RenderSystem) { Device::Initialize(p_RenderSystem); // Initialize mic nn::Result result = nn::mic::Initialize(); NN_UTIL_PANIC_IF_FAILED(result); // Set buffer in mic library result = nn::mic::SetBuffer(m_MicBuffer, MIC_BUFFER_SIZE); NN_UTIL_PANIC_IF_FAILED(result); // Gets sampling buffer size size_t size; result = nn::mic::GetSamplingBufferSize(&size); NN_UTIL_PANIC_IF_FAILED(result); m_SamplingLength = size / sizeof(s16); // Sets MIC amp result = nn::mic::GetAmpGain(&m_MicGain); NN_UTIL_PANIC_IF_FAILED(result); result = nn::mic::SetAmpGain(MIC_GAIN); NN_UTIL_PANIC_IF_FAILED(result); // MIC power on result = nn::mic::SetAmp(true); NN_UTIL_PANIC_IF_FAILED(result); } void MicDemo::Finalize() { // MIC power off nn::Result result = nn::mic::SetAmp(false); NN_UTIL_PANIC_IF_FAILED(result); // Restore MIC amp result = nn::mic::SetAmpGain(m_MicGain); NN_UTIL_PANIC_IF_FAILED(result); result = nn::mic::StopSampling(); if ( result.IsFailure() ) { if ( result != nn::mic::CTR::ResultShellClose() ) { NN_UTIL_PANIC_IF_FAILED(result); } } // Does not use the buffer set in the mic library result = nn::mic::ResetBuffer(); NN_UTIL_PANIC_IF_FAILED(result); result = nn::mic::Finalize(); NN_UTIL_PANIC_IF_FAILED(result); Device::Finalize(); } void MicDemo::Start() { Device::Start(); // Start mic sampling nn::Result result; result = nn::mic::StartSampling(nn::mic::SAMPLING_TYPE_SIGNED_16BIT, nn::mic::SAMPLING_RATE_8180, 0, // offset m_SamplingLength * sizeof(s16), // Size true); if ( result.IsFailure() ) { if ( result != nn::mic::CTR::ResultShellClose() ) { NN_UTIL_PANIC_IF_FAILED(result); } // 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 // m_RetryStart = true; } else { m_RetryStart = false; } } void MicDemo::End() { // End mic sampling nn::Result result; // Even if stopping the sample fails here, it stops automatically with mic::Finalize result = nn::mic::StopSampling(); if ( result.IsFailure() ) { if ( result != nn::mic::CTR::ResultShellClose() ) { NN_UTIL_PANIC_IF_FAILED(result); } } Device::End(); } void MicDemo::GetMicData(s16* pDest, size_t size) { uptr src; nn::Result result = nn::mic::GetLastSamplingAddress(&src); NN_UTIL_PANIC_IF_FAILED(result); s16* data = reinterpret_cast(m_MicBuffer); u32 offs = src - reinterpret_cast(m_MicBuffer); for (int i = 0; i < size; i++) { *(pDest + i) = data[(offs - i) % m_SamplingLength]; } } void MicDemo::CopyMicData(s16* pDest, size_t size) { s16* data = reinterpret_cast(m_MicBuffer); for (int i = 0; i < size; i++) { pDest[i] = data[i]; } } void MicDemo::DrawFrame(void) { // Try again if it fails to start sampling if ( m_RetryStart ) { Start(); } s16 micData[NN_GX_DISPLAY1_WIDTH]; GetMicData(micData, NN_GX_DISPLAY1_WIDTH); for (int i = 0; i < NN_GX_DISPLAY1_WIDTH - 1; i++) { mp_RenderSystem->DrawLine(static_cast(micData[i] / 256 + NN_GX_DISPLAY1_HEIGHT / 2), static_cast(i), static_cast(micData[i + 1] / 256 + NN_GX_DISPLAY1_HEIGHT / 2), static_cast(i + 1)); } } /*---------------------------------------------------------------------------* End of file *---------------------------------------------------------------------------*/