的Python:什么是winsound.Beep的最近的Linux和OSX等价物? [英] Python: what are the nearest Linux and OSX equivalents of winsound.Beep?
问题描述
如果一个人希望哔Windows上的扬声器,巨蟒显然2提供了一个有用的功能: winsound.Beep()
。关于此功能的妙处是,它需要的参数指定嘟嘟的精确频率和持续时间。这正是我想做的事情,但我不使用Windows。所以......
If one wishes to beep the speaker on Windows, Python 2 apparently provides a useful function: winsound.Beep()
. The neat thing about this function is that it takes arguments specifying the exact frequency and duration of the beep. This is exactly what I want to do, except that I don't use Windows. So...
什么是 winsound.Beep()
Linux和OSX,带来尽可能少的依赖关系可能吗?
What are the nearest equivalents of winsound.Beep()
for Linux and OSX, bringing in as few dependencies as possible?
请注意,我希望能够直接发出蜂鸣声扬声器,不播放声音文件。另外,我需要能够控制蜂鸣声的频率和持续时间,因此 curses.beep()
和打印'\\一个
不会做。最后,我知道pygame的提供了大量的音功能,但考虑到我不需要任何的pygame的的其他功能,这似乎就像使用大锤牛刀(反正,我试图与依赖客场做尽可能)
Please note that I want to be able to beep the speaker directly, not to play a sound file. Also, I need to be able to control the frequency and duration of the beep, so curses.beep()
and print '\a'
won't do. Lastly, I am aware that PyGame provides extensive sound capabilities, but given that I don't require any of PyGame's other functionality, that would seem like using a sledgehammer to crack a nut (and anyway, I'm trying to do away with dependencies as far as possible).
推荐答案
我发现了一个潜在的解决方案在这里:
<一href=\"http://bytes.com/topic/python/answers/25217-beeping-under-linux\">http://bytes.com/topic/python/answers/25217-beeping-under-linux
I found a potential solution here: http://bytes.com/topic/python/answers/25217-beeping-under-linux
它涉及直接写入到/ dev /音频。不知道怎么的便携式它或者如果它甚至还可以在所有 - 我不是在Linux机器ATM
It involves writing directly to /dev/audio. Not sure how portable it is or if it even works at all - i'm not on a linux machine atm.
def beep(frequency, amplitude, duration):
sample = 8000
half_period = int(sample/frequency/2)
beep = chr(amplitude)*half_period+chr(0)*half_period
beep *= int(duration*frequency)
audio = file('/dev/audio', 'wb')
audio.write(beep)
audio.close()
这篇关于的Python:什么是winsound.Beep的最近的Linux和OSX等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!