Version using MEMS microphone and CODEC for the program "F746_RealtimeSpectrumAnalyzer". "F746_RealtimeSpectrumAnalyzer" の入力を MEMS のマイクと CODEC に変更.このプログラムは Tomona Nanase さんが作成し DISCO-F746NG_Oscilloscope の名前で登録しているプログラムで, CODEC を使って入力する部分を参考にして作成.このプログラムの説明は,CQ出版社のインターフェース誌,2016年4月号に掲載.

Dependencies:   BSP_DISCO_F746NG BUTTON_GROUP LCD_DISCO_F746NG TS_DISCO_F746NG UIT_FFT_Real mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers functions_for_audio.cpp Source File

functions_for_audio.cpp

00001 //-------------------------------------------------------
00002 //  Functions for audio device on DISCO-F746NG and etc.
00003 //  2016/01/06, Copyright (c) 2016 MIKAMI, Naoki
00004 //-------------------------------------------------------
00005 
00006 #include "main.h"
00007 #include "functions_for_audio.hpp"
00008 
00009 // Initialize audio input and run 
00010 //      InputDevice: INPUT_DEVICE_DIGITAL_MICROPHONE_2 or INPUT_DEVICE_INPUT_LINE_1
00011 void InitRunAudioIn(uint16_t inputDevice, int samplingFreq)
00012 {
00013     init_audio_in(inputDevice, samplingFreq);
00014     record_audio_in();
00015 }
00016 
00017 // Stop audio input
00018 void StopAudioIn()
00019 {
00020     if (BSP_AUDIO_IN_Stop(CODEC_PDWN_SW) == AUDIO_ERROR)
00021         error_trap();    
00022 }
00023 
00024 // Defined in BSP_overwrite.cpp by Nanase
00025 void AUDIO_IN_SAIx_DMAx_IRQHandler();
00026 
00027 // Initialize audio-in for DISCO-F746NG
00028 static void init_audio_in(uint16_t InputDevice, int SamplingFreq)
00029 {
00030     int audioInVolume = (InputDevice == INPUT_DEVICE_INPUT_LINE_1) ? 86 : 100;
00031 
00032     if (BSP_AUDIO_IN_Init(InputDevice, audioInVolume, SamplingFreq) == AUDIO_ERROR)
00033             error_trap();
00034     NVIC_SetVector(AUDIO_IN_SAIx_DMAx_IRQ, (uint32_t)AUDIO_IN_SAIx_DMAx_IRQHandler);
00035 }
00036 
00037 // Start sampling of audio input signal
00038 static void record_audio_in()
00039 {
00040     if (BSP_AUDIO_IN_Record((uint16_t*)audio_in_buffer, BufferSize) == AUDIO_ERROR)
00041         error_trap();
00042 }
00043 
00044 void error_trap()
00045 {
00046     DigitalOut led1(LED1);
00047     fprintf(stderr, "\r\n### ERROR\r\n");
00048     while(true)
00049     {
00050         led1 = !led1;
00051         wait_ms(250);
00052     }
00053 }
00054 
00055 // Decimator by a factor of 2 using 3-tap FIR filter
00056 //      This function is used for downsampling 16 kHz to 8 kHz
00057 namespace Mikami
00058 {
00059     void Decimate(int dataNum, int index, const int16_t xn[], int16_t yn[])
00060     {
00061         static const float h0 = 5.443840E-01f;
00062         static const float h1 = 2.928932E-01f;
00063 
00064         for (int n=0; n<dataNum; n++)
00065         {
00066             // Execute FIR filter with 3 taps
00067             yn[n] = h0*xn[index+2] + h1*(xn[index] + xn[index+4]);
00068             
00069             index += 4; // Corresponding to decimation by a factor of 2
00070         }
00071     }   
00072 }