Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: F746_SD_WavPlayer F746_SD_GraphicEqualizer_ren0620 Joerg_turbo_ede CW_Decoder_using_FFT_on_DiscoF746NG ... more
SAI_InOut.cpp
00001 //----------------------------------------------------------- 00002 // SaiIO class 00003 // BSP_DISCO_F746NG Rev.9 に合わせて変更 00004 // InitInput() の内容を一部変更(変更前の部分はコメントアウト) 00005 // 00006 // 2017/09/17, Copyright (c) 2017 MIKAMI, Naoki 00007 //----------------------------------------------------------- 00008 00009 #include "SAI_InOut.hpp" 00010 00011 namespace Mikami 00012 { 00013 // Constructor 00014 SaiIO::SaiIO(InOutBoth ioBoth, int size, int fs, 00015 uint16_t inputDevice) 00016 : FS_(fs), IOBOTH_(ioBoth) 00017 { 00018 nData_ = size; 00019 bufferSize_ = (size*2)*2; 00020 00021 if (ioBoth != OUTPUT) 00022 { 00023 inBuffer_.SetSize((size*2)*2); 00024 inOffset_ = 0; 00025 captured_ = false; 00026 } 00027 if (ioBoth != INPUT) 00028 { 00029 outBuffer_.SetSize((size*2)*2); 00030 tmp_.SetSize(size*2); 00031 tmpIndex_ = 0; 00032 xferred_ = false; 00033 ClearBuffer(); 00034 } 00035 InitCodec(inputDevice); 00036 } 00037 00038 // Input start 00039 void SaiIO::RecordIn() 00040 { 00041 if (BSP_AUDIO_IN_Record(inBuffer_, bufferSize_) == AUDIO_ERROR) 00042 ErrorTrap(); 00043 } 00044 00045 // Switching input device and run 00046 void SaiIO::SwitchInputDevice(int sw) 00047 { 00048 uint16_t dev = (sw == 0) ? 00049 INPUT_DEVICE_DIGITAL_MICROPHONE_2 00050 : INPUT_DEVICE_INPUT_LINE_1; 00051 InitInput(dev); 00052 ClearBuffer(); 00053 RecordIn(); 00054 if (IOBOTH_ == BOTH) PlayOut(); 00055 } 00056 00057 // If captured, return true 00058 bool SaiIO::IsCaptured() 00059 { 00060 if (!captured_) return false; 00061 00062 inIndex_ = inOffset_; 00063 captured_ = false; 00064 return true; 00065 } 00066 00067 void SaiIO::PlayOut() 00068 { 00069 ClearBuffer(); 00070 BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02); 00071 if (BSP_AUDIO_OUT_Play(outBuffer_, bufferSize_*AUDIODATA_SIZE) 00072 == AUDIO_ERROR) 00073 ErrorTrap(); 00074 } 00075 00076 // Return true if transfer completion to output 00077 bool SaiIO::IsXferred() 00078 { 00079 if (!xferred_) return false; 00080 00081 tmpIndex_ = 0; 00082 xferred_ = false; 00083 return true; 00084 } 00085 00086 void SaiIO::Output(int16_t xL, int16_t xR) 00087 { 00088 tmp_[tmpIndex_++] = xL; // Left 00089 tmp_[tmpIndex_++] = xR; // Right 00090 } 00091 00092 void SaiIO::ErrorTrap() 00093 { 00094 DigitalOut led1(LED1); 00095 fprintf(stderr, "\r\n#### ERROR ####\r\n"); 00096 while(true) 00097 { 00098 led1 = !led1; 00099 wait_ms(250); 00100 } 00101 } 00102 00103 // Initialize audio input and output 00104 void SaiIO::InitCodec(uint16_t inputDevice) 00105 { 00106 if (inputDevice != 0) InitInput(inputDevice); 00107 00108 if (IOBOTH_ == OUTPUT) 00109 if (BSP_AUDIO_OUT_Init(OUTPUT_DEVICE_HEADPHONE, 00110 100, FS_) == AUDIO_ERROR) 00111 ErrorTrap(); 00112 00113 if (IOBOTH_ != OUTPUT) SetInput(); 00114 if (IOBOTH_ != INPUT) SetOutput(); 00115 } 00116 00117 void SaiIO::InitInput(uint16_t inputDevice) 00118 { 00119 int audioInVolume = (inputDevice == INPUT_DEVICE_INPUT_LINE_1) ? 00120 70 : 90; 00121 InputFp = (inputDevice == INPUT_DEVICE_INPUT_LINE_1) ? 00122 &SaiIO::InputNormal : &SaiIO::InputReversal; 00123 if (IOBOTH_ == BOTH) 00124 /* 00125 if (BSP_AUDIO_IN_OUT_Init(inputDevice, OUTPUT_DEVICE_HEADPHONE, 00126 audioInVolume, FS_) == AUDIO_ERROR) 00127 */ 00128 if (BSP_AUDIO_IN_OUT_Init(inputDevice, OUTPUT_DEVICE_HEADPHONE, 00129 FS_, DEFAULT_AUDIO_IN_BIT_RESOLUTION, 00130 DEFAULT_AUDIO_IN_CHANNEL_NBR) == AUDIO_ERROR) 00131 ErrorTrap(); 00132 00133 if (IOBOTH_ == INPUT) 00134 if (BSP_AUDIO_IN_Init(inputDevice, 00135 audioInVolume, FS_) == AUDIO_ERROR) 00136 ErrorTrap(); 00137 } 00138 00139 void SaiIO::SetInput() 00140 { 00141 NVIC_SetVector(AUDIO_IN_SAIx_DMAx_IRQ, 00142 (uint32_t)AUDIO_IN_SAIx_DMAx_IRQHandler); 00143 } 00144 00145 void SaiIO::SetOutput() 00146 { 00147 NVIC_SetVector(AUDIO_OUT_SAIx_DMAx_IRQ, 00148 (uint32_t)AUDIO_OUT_SAIx_DMAx_IRQHandler); 00149 BSP_AUDIO_OUT_SetAudioFrameSlot(CODEC_AUDIOFRAME_SLOT_02); 00150 } 00151 00152 void SaiIO::ClearBuffer() 00153 { 00154 if (IOBOTH_ != OUTPUT) 00155 for (int n=0; n<bufferSize_; n++) inBuffer_[n] = 0; 00156 for (int n=0; n<bufferSize_; n++) outBuffer_[n] = 0; 00157 for (int n=0; n<nData_*2; n++) tmp_[n] = 0; 00158 } 00159 00160 // For line input 00161 void SaiIO::InputNormal(int16_t &xL, int16_t &xR) 00162 { 00163 xL = inBuffer_[inIndex_++]; 00164 xR = inBuffer_[inIndex_++]; 00165 } 00166 00167 // For MEMS microphone input 00168 void SaiIO::InputReversal(int16_t &xL, int16_t &xR) 00169 { 00170 xR = inBuffer_[inIndex_++]; 00171 xL = inBuffer_[inIndex_++]; 00172 } 00173 00174 void SaiIO::Captured(int32_t offset) 00175 { 00176 captured_ = true; 00177 inOffset_ = offset; 00178 } 00179 00180 void SaiIO::FillBuffer(uint32_t offset) 00181 { 00182 int k = offset; 00183 for (int n=0; n<nData_*2; n++) 00184 outBuffer_[k++] = tmp_[n]; 00185 xferred_ = true; 00186 } 00187 00188 int32_t SaiIO::nData_; 00189 int32_t SaiIO::bufferSize_; 00190 00191 __IO bool SaiIO::captured_; 00192 __IO int32_t SaiIO::inOffset_; 00193 00194 Array<uint16_t> SaiIO::outBuffer_; 00195 Array<uint16_t> SaiIO::tmp_; 00196 __IO bool SaiIO::xferred_; 00197 }
Generated on Mon Jul 18 2022 21:01:12 by
1.7.2