Demo program of SAI_IO class for audio signal input and output. DISCO-F746 搭載の CODEC (WM8994) を使ってオーディオ信号の入出力を行うための SAI_IO クラスの使用例.

Dependencies:   BSP_DISCO_F746NG F746_GUI F746_SAI_IO LCD_DISCO_F746NG TS_DISCO_F746NG mbed

main.cpp

Committer:
MikamiUitOpen
Date:
2016-05-09
Revision:
1:7400dd732dbc
Parent:
0:089c5e022aa8
Child:
2:034168531ce0

File content as of revision 1:7400dd732dbc:

//----------------------------------------------------------------------
//  CODEC を使い,MEMS マイクまたはライン (CN11) から入力した信号をそのまま
//  リアルタイムでヘッドフォン出力 (CN10) へ出力する
//      ディジタルフィルタなどの雛形として使える    
//
//  "stm32746g_discovery_audio.c" の BSP_AUDIO_IN_OUT_Init() 関数は
//  INPUT_LINE_1 からの入力を禁止しているため,これを禁止しない関数として,
//  BSP_AUDIO_IN_OUT_Init() の一部を修正し,MyBSP_AUDIO_IN_OUT_Init() 
//  という関数を定義した.
//  この MyBSP_AUDIO_IN_OUT_Init() は "MyBSP_AUDIO_IN_OUT_Init.hpp" で
//  定義されている.
//
//  2016/05/09, Copyright (c) 2016 MIKAMI, Naoki
//----------------------------------------------------------------------

#include "ButtonGroup.hpp"
#include "Label.hpp"
#include "WaveformDisplay.hpp"

#include "SAI_InOut.hpp"
using namespace Mikami;

int main()
{
    const uint32_t BACK_COLOR = GuiBase::ENUM_BACK;
    const uint32_t AXIS_COLOR = 0xFFCCFFFF;
    const uint32_t LINE_COLOR = LCD_COLOR_CYAN;

    const uint32_t N_DATA = 400;
    SaiIO mySai(SaiIO::BOTH, N_DATA, I2S_AUDIOFREQ_16K, INPUT_DEVICE_INPUT_LINE_1);
    
    LCD_DISCO_F746NG *lcd = GuiBase::GetLcdPtr();
    lcd->Clear(BACK_COLOR);

    const uint16_t BG_LEFT = 380;
    const uint16_t BG_WIDTH = 100;
    const uint16_t BG_HEIGHT = 40;

    const string INPUT[2] = {"MIC", "LINE"};
    ButtonGroup inSw(BG_LEFT, 180, BG_WIDTH/2, BG_HEIGHT,
                     2, INPUT, 0, 0, 2, 1);

    const string ON_OFF[2] = {"ON", "OFF"};
    ButtonGroup onOff(BG_LEFT, 230, BG_WIDTH/2, BG_HEIGHT,
                      2, ON_OFF, 0, 0, 2, 0);

    lcd->SetTextColor(LCD_COLOR_WHITE);
    lcd->SetFont(&Font16);

    const int X_WAV = 30;   // Origin for x axis of waveform
    const int Y_WAV = 60;   // Origin for y axis of waveform
    WaveformDisplay waveDispL(lcd, X_WAV, Y_WAV, N_DATA, 7,
                              AXIS_COLOR, LINE_COLOR, BACK_COLOR);
    WaveformDisplay waveDispR(lcd, X_WAV, Y_WAV+60, N_DATA, 7,
                              AXIS_COLOR, LINE_COLOR, BACK_COLOR);
    Label labelL(445, Y_WAV- 8, "L", Label::LEFT, Font16);
    Label labelR(445, Y_WAV+52, "R", Label::LEFT, Font16);

    int16_t snL[N_DATA];
    int16_t snR[N_DATA];
    bool on = true;
    int inSelect = 1;   // input: line

    mySai.RecordIn();
    mySai.PlayOut();

    while(1)
    {
        int sw = 0;
        if (onOff.GetTouchedNumber(sw))
            on = (sw == 0) ? true : false;

        int swNow;
        if (inSw.GetTouchedNumber(swNow) && (swNow != inSelect))
        {
            mySai.SwitchInputDevice(swNow);
            mySai.RecordIn();
            mySai.PlayOut();
            inSelect = swNow;
        }

        if (mySai.IsCompleted())
        {
            for (int n=0; n<mySai.GetLength(); n++)
            {
                int16_t xL;
                int16_t xR;
                mySai.Input(xL, xR);
                snL[n] = xL;    // 入力波形のモニタのため
                snR[n] = xR;    // 入力波形のモニタのため

                //----------------------------------------
                // ここにディジタルフィルタなどの処理を書く
                int16_t yL = xL;    // through の場合
                int16_t yR = xR;    // through の場合
                //----------------------------------------

                mySai.Output(yL, yR);
            }
            if (on)
            {
                waveDispL.Execute(snL);
                waveDispR.Execute(snR);
            }
            mySai.Reset();
        }    
    }
}