不韋 呂 / Mbed 2 deprecated UIT2_VowelSynthesizer

Dependencies:   UITDSP_ADDA UIT_ACM1602NI UIT_AQM1602 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //------------------------------------------------------------
00002 //  ディジタル共振器を使った母音発生器
00003 //      sw: 1, 2, 3, 4, 5 => /a/, /i/, /u/, /e/, /o/
00004 //      sw: 0 => Without resonator
00005 //  スイッチを切り替えてから Nucleo ボードのユーザボタン(青色)を
00006 //  押すと違う合成母音が発生する.
00007 //  母音の基本周波数は VR で変えられる(合成母音の音が出ている最中
00008 //  でも変えられる)
00009 //
00010 //  2015/07/25, Copyright (c) 2015 MIKAMI, Naoki
00011 //------------------------------------------------------------
00012 
00013 #include "ADC_BuiltIn.hpp"      // for ADC not using interrupt
00014 #include "DAC_MCP4921.hpp"      // for DAC MCP4921, MCP4922
00015 #include "ACM1602NI.hpp"        // for LCD display
00016 #include "Rosenberg.hpp"        // Glottal source
00017 #include "Resonator.hpp"        // Resonator corresponding to vocal tract
00018 #include "Radiator.hpp"         // Radiator from mouth
00019 using namespace Mikami;
00020 
00021 // ACM1602Ni を使う場合は次の define 文をコメントにすること
00022 #define AQM1602
00023 
00024 #ifdef AQM1602
00025 #include "AQM1602.hpp"
00026 Aqm1602 Lcd_;
00027 #else
00028 #include "ACM1602NI.hpp"
00029 Acm1602Ni Lcd_;
00030 #endif
00031 
00032 const int FS_ = 8000;           // Sampling frequency: 8 kHz
00033 ADC_BuiltIn adc_(A2, FS_);      // for AD
00034 DAC_MCP4921 myDac_;             // for DA
00035 
00036 DigitalIn uButton_(USER_BUTTON);
00037 BusIn busSw_(D2, D3, D4, D5);
00038 
00039 const int N_VWL_ = 5;   // 母音の種類の数
00040 const int N_RSN_ = 3;   // 共振器の数
00041 
00042 // フォルマント周波数,帯域幅のデータ:{周波数, 帯域幅}
00043 const Resonator::FrBw c_[N_VWL_][N_RSN_] =
00044     {{ {654, 50}, {1060, 55}, {2375, 60} },     // ア
00045      { {302, 40}, {2057, 60}, {3034, 65} },     // イ
00046      { {375, 45}, {1208, 55}, {2165, 60} },     // ウ
00047      { {541, 50}, {1784, 60}, {2452, 60} },     // エ
00048      { {458, 45}, { 807, 50}, {2379, 60} }};    // オ
00049 
00050 int main()
00051 {
00052     // 基本周波数の可変範囲を決める定数(100 ~ 180 Hz の範囲)
00053     const float F0_COEF = 50.0f/4095.0f;    // 範囲を決める定数
00054     const float F0_MIN = 100.0f;            // 最低の基本周波数
00055 
00056     Rosenberg g(100, FS_, 2.5f);    // 声帯波発生用オブジェクト初期化
00057                                     // 基本周波数:100 Hz
00058     Resonator rs[N_RSN_];           // 共振器用オブジェクト配列の宣言
00059     Radiator rd;                    // 放射の効果用オブジェクトの初期化
00060     
00061     myDac_.ScfClockTim3(336000);    // cutoff frequency: 3.36 kHz
00062 
00063     busSw_.mode(PullDown);
00064     Lcd_.Clear();
00065     Lcd_.WriteStringXY("Synthetic Vowel", 0, 0);
00066 
00067     while (true)
00068     {
00069         int sw = busSw_.read();
00070         if (sw > 5) sw = 5;
00071 
00072         char str[4] = {'/', 0xB0+sw, '/', NULL};
00073         Lcd_.WriteStringXY(str, 0, 1);   // -アイウエオ
00074                 
00075         // 共振器の準備
00076         if (sw > 0)
00077             for (int k=0; k<N_RSN_; k++)
00078                 rs[k] = Resonator(c_[sw-1][k], FS_);
00079 
00080         for (int n=0; n<4000; n++)
00081         {
00082             uint16_t adVal = adc_.Read_u16();   // Read from A2
00083             float f0 = adVal*F0_COEF + F0_MIN;
00084             g.SetPeriod(f0);                // 基本周波数を設定
00085            
00086             //-----------------------------------------------
00087             // ここで合成母音を発生している
00088             float vn = g.Execute();         // 合成声帯波発生
00089             if (sw != 0)
00090                 for (int k=0; k<N_RSN_; k++)
00091                     vn = rs[k].Execute(vn); // 声道の効果
00092             float yn = rd.Execute(vn);      // 放射の効果
00093             //-----------------------------------------------
00094 
00095             myDac_.Write(yn);               // Write to DAC
00096         }
00097         
00098         // ユーザボタンが押されるまで待つ
00099         while (uButton_ == 1) {}
00100         Lcd_.ClearLine(1);
00101     }   
00102 }