代码之家  ›  专栏  ›  技术社区  ›  Dutchie432

生成DTMF铃声

  •  7
  • Dutchie432  · 技术社区  · 16 年前

    我想知道是否有人在iPhone SDK中找到了一种生成音调的方法。我正在尝试生成双音多频音,但似乎找不到任何实质性内容。我希望能够指定播放音调的时间(即模拟按住按钮,而不是简单地按一下)。

    我发现了一个叫做iphreak的开源应用程序。据说它会生成DTMF音来愚弄付费电话(我向你保证这不是我的意图——我的公司处理基于电话的对讲系统)。该应用程序的唯一问题是开源项目中缺少文件。也许以前有人让这个项目工作过?

    如果有人知道我会在哪里找这样的东西,我会非常感激我的投票:)

    3 回复  |  直到 7 年前
        1
  •  5
  •   Toad    16 年前

    应该足够容易产生你自己。 考虑到硬件可以以44.1 kHz的频率播放一个PCM缓冲区(16位采样)(在某些库功能或其他功能下,它肯定可以播放),您只剩下计算波形了:

     const int PLAYBACKFREQ = 44100;
     const float PI2 = 3.14159265359f * 2;
    
     void generateDTMF(short *buffer, int length, float freq1, float freq2)
     {
          int i;
          short *dest = buffer;
          for(i=0; i<length; i++)
          {
               *(dest++) = (sin(i*(PI2*(PLAYBACKFREQ/freq1))) + sin(i (PI2*(PLAYBACKFREQ/freq2)))) * 16383;
          }
     }
    

    16383是在我使用加法合成(只是把正弦波加在一起)之后完成的。因此,最大结果是-2.0-2.0,所以乘以16383后,我得到的最大16位结果是-32768-+32767

    编辑: 这两个频率是维基百科文章中的频率,另一个回答者链接到。两个独特的频率发出DTMF声音

        2
  •  5
  •   mahboudz    15 年前

    简单的答案是:

    soundArray = [[NSArray alloc] initWithObjects: 
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-0.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-1.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-2.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-3.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-4.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-5.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-6.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-7.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-8.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-9.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-0.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-pound.caf"] autorelease],
        [[[SoundEffect alloc] initWithContentsOfFile: @"/System/Library/Audio/UISounds/dtmf-star.caf"] autorelease],
                  nil];
    

    给你。所有的声音,一个标准的手机键盘,在一个阵列,为您的享受。

        3
  •  1
  •   Cameron Lowell Palmer    10 年前

    快速DTMF声音

    我正在尝试生成PCM数据,并用Swift提出了这个问题。此函数将生成 [Float] 哪些是音频样本。你可以和他们一起玩 AVAudio .

    每个 DTMF 由一对音调、一个标记长度(250 ms)、一个空格长度(250 ms)组成,当然,您需要指定一个采样频率(8000 Hz)。 Mark and Space 通常在250毫秒左右,我称之为标准的人工拨号。采样频率很有趣,但需要是最高频率的两倍。为了好玩,你可以把它放在下面听发生了什么。

    public static func generateDTMF(frequency1 frequency1: Float, frequency2: Float, markSpace: MarkSpaceType, sampleRate: Float) -> [Float]
    {
        let toneLengthInSamples = 10e-4 * markSpace.0 * sampleRate
        let silenceLengthInSamples = 10e-4 * markSpace.1 * sampleRate
    
        var sound = [Float](count: Int(toneLengthInSamples + silenceLengthInSamples), repeatedValue: 0)
        let twoPI = 2.0 * Float(M_PI)
    
        for i in 0 ..< Int(toneLengthInSamples) {
            // Add first tone at half volume
            let sample1 = 0.5 * sin(Float(i) * twoPI / (sampleRate / frequency1));
    
            // Add second tone at half volume
            let sample2 = 0.5 * sin(Float(i) * twoPI / (sampleRate / frequency2));
    
            sound[i] = sample1 + sample2
        }
    
        return sound
    }
    

    完整的操场可以下载到 GitHub .