天天看點

android學習日記:撥号按鍵聲音自定義

android中的撥号鍵盤音也是可以改變的,不過不同的是,按鍵音并不是存儲在手機中的ogg格式的音頻檔案。他是通過C++代碼中的頻率設定來實作的。

具體的音頻控制在frameworks/av/media/libmedia檔案夾中的ToneGenerator.cpp中實作:

const ToneGenerator::ToneDescriptor ToneGenerator::sToneDescriptors[] = {
        { segments: {{ duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1336, 941, 0 }, 0, 0},
                     { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_0
        { segments: { { duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1209, 697, 0 }, 0, 0 },
                      { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_1
        { segments: { { duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1336, 697, 0 }, 0, 0 },
                      { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_2
        { segments: { { duration: ToneGenerator::TONEGEN_INF, waveFreq: { 1477, 697, 0 }, 0, 0 },
                      { duration: 0 , waveFreq: { 0 }, 0, 0}},
          repeatCnt: ToneGenerator::TONEGEN_INF,
          repeatSegment: 0 },                              // TONE_DTMF_3
           

我隻截取了0-3号鍵盤的音頻生成代碼,其中的函數功能在ToneGenerator.h中有詳細說明:

 - The array waveFreq[]:

    //         1 for static tone descriptors: contains the frequencies of all individual waves making the multi-tone.

    //         2 for active tone descritors: contains the indexes of the WaveGenerator objects in mWaveGens

    //        The number of sine waves varies from 1 to TONEGEN_MAX_WAVES.

    //        The first null value indicates that no more waves are needed.

    //    - The array segments[] is used to generate the tone pulses. A segment is a period of time

    //        during which the tone is ON or OFF.    Segments with even index (starting from 0)

    //        correspond to tone ON state and segments with odd index to OFF state.

    //        The data stored in segments[] is the duration of the corresponding period in ms.

    //        The first segment encountered with a 0 duration    indicates that no more segment follows.

    //    - loopCnt - Number of times to repeat a sequence of seqments after playing this

    //    - loopIndx - The segment index to go back and play is loopcnt > 0

    //    - repeatCnt indicates the number of times the sequence described by segments[] array must be repeated.

    //        When the tone generator encounters the first 0 duration segment, it will compare repeatCnt to mCurCount.

    //        If mCurCount > repeatCnt, the tone is stopped automatically. Otherwise, tone sequence will be

    //        restarted from segment repeatSegment.

    //    - repeatSegment number of the first repeated segment when repeatCnt is not null

本來想做成鋼琴音的效果,但是對于頻率波神馬的無能為力。。。随意改動出來的效果實在難聽。。。就放棄了。