SPKT
SAI_InOut.hpp@0:f255629eada1, 2019-06-07 (annotated)
- Committer:
- phungductung
- Date:
- Fri Jun 07 05:10:10 2019 +0000
- Revision:
- 0:f255629eada1
spkt; ;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phungductung | 0:f255629eada1 | 1 | //----------------------------------------------------------- |
phungductung | 0:f255629eada1 | 2 | // SaiIO class (Header) |
phungductung | 0:f255629eada1 | 3 | // 2017/03/17, Copyright (c) 2017 MIKAMI, Naoki |
phungductung | 0:f255629eada1 | 4 | //----------------------------------------------------------- |
phungductung | 0:f255629eada1 | 5 | |
phungductung | 0:f255629eada1 | 6 | #ifndef F746_SAI_IO_HPP |
phungductung | 0:f255629eada1 | 7 | #define F746_SAI_IO_HPP |
phungductung | 0:f255629eada1 | 8 | |
phungductung | 0:f255629eada1 | 9 | #include "mbed.h" |
phungductung | 0:f255629eada1 | 10 | #include "stm32746g_discovery_audio.h" |
phungductung | 0:f255629eada1 | 11 | #include "BSP_AudioIn_Overwrite.hpp" |
phungductung | 0:f255629eada1 | 12 | #include "BSP_AudioOut_Overwrite.hpp" |
phungductung | 0:f255629eada1 | 13 | #include "Array.hpp" |
phungductung | 0:f255629eada1 | 14 | |
phungductung | 0:f255629eada1 | 15 | namespace Mikami |
phungductung | 0:f255629eada1 | 16 | { |
phungductung | 0:f255629eada1 | 17 | class SaiIO |
phungductung | 0:f255629eada1 | 18 | { |
phungductung | 0:f255629eada1 | 19 | public: |
phungductung | 0:f255629eada1 | 20 | enum InOutBoth { INPUT, // input only |
phungductung | 0:f255629eada1 | 21 | OUTPUT, // output only |
phungductung | 0:f255629eada1 | 22 | BOTH }; // input and output |
phungductung | 0:f255629eada1 | 23 | // Constructor |
phungductung | 0:f255629eada1 | 24 | // inputDevice: INPUT_DEVICE_DIGITAL_MICROPHONE_2 or |
phungductung | 0:f255629eada1 | 25 | // INPUT_DEVICE_INPUT_LINE_1 |
phungductung | 0:f255629eada1 | 26 | // inputDevice == 0 : not use input device |
phungductung | 0:f255629eada1 | 27 | SaiIO(InOutBoth ioBoth, int size, int fs, |
phungductung | 0:f255629eada1 | 28 | uint16_t inputDevice = 0); |
phungductung | 0:f255629eada1 | 29 | virtual ~SaiIO() {} |
phungductung | 0:f255629eada1 | 30 | |
phungductung | 0:f255629eada1 | 31 | int32_t GetLength() { return nData_; } |
phungductung | 0:f255629eada1 | 32 | |
phungductung | 0:f255629eada1 | 33 | void RecordIn(); |
phungductung | 0:f255629eada1 | 34 | // sw = 0: DIGITAL_MICROPHONE_2 |
phungductung | 0:f255629eada1 | 35 | // 1: LINE_1 |
phungductung | 0:f255629eada1 | 36 | void SwitchInputDevice(int sw); |
phungductung | 0:f255629eada1 | 37 | |
phungductung | 0:f255629eada1 | 38 | bool IsCaptured(); |
phungductung | 0:f255629eada1 | 39 | // Input using SAI |
phungductung | 0:f255629eada1 | 40 | void Input(int16_t &xL, int16_t &xR) |
phungductung | 0:f255629eada1 | 41 | { (this->*InputFp)(xL, xR); } |
phungductung | 0:f255629eada1 | 42 | |
phungductung | 0:f255629eada1 | 43 | void StopIn() { BSP_AUDIO_IN_Stop(CODEC_PDWN_SW); } |
phungductung | 0:f255629eada1 | 44 | void PauseIn() { BSP_AUDIO_IN_Pause(); } |
phungductung | 0:f255629eada1 | 45 | void ResumeIn() { BSP_AUDIO_IN_Resume(); } |
phungductung | 0:f255629eada1 | 46 | |
phungductung | 0:f255629eada1 | 47 | void PlayOut(); |
phungductung | 0:f255629eada1 | 48 | bool IsXferred(); |
phungductung | 0:f255629eada1 | 49 | // Output using SAI |
phungductung | 0:f255629eada1 | 50 | void Output(int16_t xL, int16_t xR); |
phungductung | 0:f255629eada1 | 51 | |
phungductung | 0:f255629eada1 | 52 | void StopOut() { BSP_AUDIO_OUT_Stop(CODEC_PDWN_SW); } |
phungductung | 0:f255629eada1 | 53 | void PauseOut() { BSP_AUDIO_OUT_Pause(); } |
phungductung | 0:f255629eada1 | 54 | void ResumeOut() { BSP_AUDIO_OUT_Resume(); } |
phungductung | 0:f255629eada1 | 55 | |
phungductung | 0:f255629eada1 | 56 | // IF you use both input and output of SAI, |
phungductung | 0:f255629eada1 | 57 | // you can use following function |
phungductung | 0:f255629eada1 | 58 | bool IsCompleted() |
phungductung | 0:f255629eada1 | 59 | { return IsCaptured() && IsXferred(); } |
phungductung | 0:f255629eada1 | 60 | |
phungductung | 0:f255629eada1 | 61 | // Following two member functions are called from |
phungductung | 0:f255629eada1 | 62 | // callback functions in "BSP_AudioIn_Overwrite.cpp" |
phungductung | 0:f255629eada1 | 63 | static void Captured1st() { Captured(0); } |
phungductung | 0:f255629eada1 | 64 | static void Captured2nd() { Captured(bufferSize_/2); } |
phungductung | 0:f255629eada1 | 65 | |
phungductung | 0:f255629eada1 | 66 | // Following two member functions are called from |
phungductung | 0:f255629eada1 | 67 | // callback functions in "BSP_AudioOut_Overwrite.cpp" |
phungductung | 0:f255629eada1 | 68 | static void FillBuffer1st() { FillBuffer(0); } |
phungductung | 0:f255629eada1 | 69 | static void FillBuffer2nd() { FillBuffer(bufferSize_/2); } |
phungductung | 0:f255629eada1 | 70 | |
phungductung | 0:f255629eada1 | 71 | // Called form the functions in "BSP_AudioIn_Overwrite.cpp" |
phungductung | 0:f255629eada1 | 72 | // and "BSP_AudioOut_Overwrite.cpp" |
phungductung | 0:f255629eada1 | 73 | static void ErrorTrap(); |
phungductung | 0:f255629eada1 | 74 | |
phungductung | 0:f255629eada1 | 75 | private: |
phungductung | 0:f255629eada1 | 76 | const int FS_; |
phungductung | 0:f255629eada1 | 77 | const InOutBoth IOBOTH_; |
phungductung | 0:f255629eada1 | 78 | |
phungductung | 0:f255629eada1 | 79 | Array<uint16_t> inBuffer_; |
phungductung | 0:f255629eada1 | 80 | static Array<uint16_t> outBuffer_; |
phungductung | 0:f255629eada1 | 81 | static Array<uint16_t> tmp_; |
phungductung | 0:f255629eada1 | 82 | |
phungductung | 0:f255629eada1 | 83 | __IO int32_t inIndex_; |
phungductung | 0:f255629eada1 | 84 | __IO int32_t tmpIndex_; |
phungductung | 0:f255629eada1 | 85 | |
phungductung | 0:f255629eada1 | 86 | static int32_t nData_; |
phungductung | 0:f255629eada1 | 87 | static int32_t bufferSize_; |
phungductung | 0:f255629eada1 | 88 | static __IO bool captured_; |
phungductung | 0:f255629eada1 | 89 | static __IO int32_t inOffset_; |
phungductung | 0:f255629eada1 | 90 | static __IO bool xferred_; |
phungductung | 0:f255629eada1 | 91 | |
phungductung | 0:f255629eada1 | 92 | void InitCodec(uint16_t inputDevice); |
phungductung | 0:f255629eada1 | 93 | void InitInput(uint16_t inputDevice); |
phungductung | 0:f255629eada1 | 94 | void SetInput(); |
phungductung | 0:f255629eada1 | 95 | void SetOutput(); |
phungductung | 0:f255629eada1 | 96 | void ClearBuffer(); |
phungductung | 0:f255629eada1 | 97 | |
phungductung | 0:f255629eada1 | 98 | // This function pointer is assigned by |
phungductung | 0:f255629eada1 | 99 | // InputNormal() or InputReversal() |
phungductung | 0:f255629eada1 | 100 | void (SaiIO::*InputFp)(int16_t &, int16_t &); |
phungductung | 0:f255629eada1 | 101 | // For line input |
phungductung | 0:f255629eada1 | 102 | void InputNormal(int16_t &xL, int16_t &xR); |
phungductung | 0:f255629eada1 | 103 | // For MEMS microphone input |
phungductung | 0:f255629eada1 | 104 | void InputReversal(int16_t &xL, int16_t &xR); |
phungductung | 0:f255629eada1 | 105 | |
phungductung | 0:f255629eada1 | 106 | static void Captured(int32_t offset); |
phungductung | 0:f255629eada1 | 107 | static void FillBuffer(uint32_t offset); |
phungductung | 0:f255629eada1 | 108 | }; |
phungductung | 0:f255629eada1 | 109 | } |
phungductung | 0:f255629eada1 | 110 | #endif // F746_SAI_IO_HPP |