Python库用于播放固定频率声音

Python库用于播放固定频率声音

本文介绍了Python库用于播放固定频率声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我家蚊子的问题。这通常不会关心一个程序员社区;不过,我已经看到了声称被打了音调17Khz,以防止这些讨厌的生物的一些设备。我想做到这一点使用我的笔记本电脑。

I have a mosquito problem in my house. This wouldn't usually concern a programmers' community; However, I've seen some devices that claim to deter these nasty creatures by playing a 17Khz tone. I would like to do this using my laptop.

的一种方法将创建具有一个单一,固定频率音调一个MP3(<一href=\"http://www.google.co.il/search?q=audacity%2Bgenerate%2Btone&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a\">This可以很容易地完成大言不惭),打开它;玩它反复。

One method would be creating an MP3 with a a single, fixed-frequency tone (This can easily done by audacity), opening it with a python library and playing it repeatedly.

第二将播放使用内置扬声器计算机中的声音。我在寻找类似于QBasic中的声音的东西:

The second would be playing a sound using the computer built-in speaker. I'm looking for something similar to QBasic Sound:

SOUND 17000, 100

有一个Python库是什么?

Is there a python library for that?

推荐答案

是一个对这个问题简单的跨平台的解决方案:

PyAudiere is a simple cross-platform solution for the problem:

>>> import audiere
>>> d = audiere.open_device()
>>> t = d.create_tone(17000) # 17 KHz
>>> t.play() # non-blocking call
>>> import time
>>> time.sleep(5)
>>> t.stop()

pyaudiere.org已经一去不复返了。 和二进制安装的Python 2(Debian的,窗户)通过自由之路机如<一可href=\"https://web.archive.org/web/20120221041148/http://pyaudiere.org/download.php?get=pyaudiere-0.2.tar.gz\"相对=nofollow>这里的源$ C ​​$ C pyaudiere-0.2.tar.gz

pyaudiere.org is gone. The site and binary installers for Python 2 (debian, windows) are available via the wayback machine e.g., here's source code pyaudiere-0.2.tar.gz.

要支持在Linux,Windows,OSX,可以用来代替:

To support both Python 2 and 3 on Linux, Windows, OSX, pyaudio module could be used instead:

#!/usr/bin/env python
"""Play a fixed frequency sound."""
from __future__ import division
import math

from pyaudio import PyAudio # sudo apt-get install python{,3}-pyaudio

try:
    from itertools import izip
except ImportError: # Python 3
    izip = zip
    xrange = range

def sine_tone(frequency, duration, volume=1, sample_rate=22050):
    n_samples = int(sample_rate * duration)
    restframes = n_samples % sample_rate

    p = PyAudio()
    stream = p.open(format=p.get_format_from_width(1), # 8bit
                    channels=1, # mono
                    rate=sample_rate,
                    output=True)
    s = lambda t: volume * math.sin(2 * math.pi * frequency * t / sample_rate)
    samples = (int(s(t) * 0x7f + 0x80) for t in xrange(n_samples))
    for buf in izip(*[samples]*sample_rate): # write several samples at a time
        stream.write(bytes(bytearray(buf)))

    # fill remainder of frameset with silence
    stream.write(b'\x80' * restframes)

    stream.stop_stream()
    stream.close()
    p.terminate()

例如:

sine_tone(
    # see http://www.phy.mtu.edu/~suits/notefreqs.html
    frequency=440.00, # Hz, waves per second A4
    duration=3.21, # seconds to play sound
    volume=.01, # 0..1 how loud it is
    # see http://en.wikipedia.org/wiki/Bit_rate#Audio
    sample_rate=22050 # number of samples per second
)

这是一个修改(支持Python 3中)。

这篇关于Python库用于播放固定频率声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:42