简体   繁体   English

Python:什么是 winsound.Beep 最接近的 Linux 和 OSX 等价物?

[英]Python: what are the nearest Linux and OSX equivalents of winsound.Beep?

If one wishes to beep the speaker on Windows, Python 2 apparently provides a useful function: winsound.Beep() .如果希望在 Windows 上发出哔哔声,Python 2 显然提供了一个有用的函数: 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.这正是我想要做的,只是我不使用 Windows。 So...所以...

What are the nearest equivalents of winsound.Beep() for Linux and OSX , bringing in as few dependencies as possible?什么是winsound.Beep() for LinuxOSX的最接近等价物,引入尽可能少的依赖项?

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.另外,我需要能够控制哔声的频率和持续时间,所以curses.beep()print '\\a'不会做。 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).最后,我知道PyGame提供了广泛的声音功能,但鉴于我不需要 PyGame 的任何其他功能,这似乎是使用大锤敲碎坚果(无论如何,我正在尝试消除依赖越远越好)。

I found a potential solution here:http://bytes.com/topic/python/answers/25217-beeping-under-linux我在这里找到了一个潜在的解决方案:http ://bytes.com/topic/python/answers/25217-beeping-under-linux

It involves writing directly to /dev/audio.它涉及直接写入/dev/audio。 Not sure how portable it is or if it even works at all - i'm not on a linux machine atm.不确定它有多便携,或者它是否可以工作 - 我不是在 linux 机器上使用 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()

winsound is only for windows and I could not find any cross platform way to do this, other than print "/a". winsound 仅适用于 Windows,除了打印“/a”之外,我找不到任何跨平台的方法来执行此操作。 However, you cannot set the frequency and duration with this.但是,您不能使用此设置频率和持续时间。

However, you can try the os.system command to do the same with the system command beep .但是,您可以尝试使用 os.system 命令对系统命令beep执行相同的操作。 Here is a snippet, which defines the function playsound in a platform independent way这是一个片段,它以独立于平台的方式定义了函数 playsound

try:
    import winsound
except ImportError:
    import os
    def playsound(frequency,duration):
        #apt-get install beep
        os.system('beep -f %s -l %s' % (frequency,duration))
else:
    def playsound(frequency,duration):
        winsound.Beep(frequency,duration)

For more info, look at this blog有关更多信息,请查看此博客

EDIT: You will need to install the beep package on linux to run the beep command.编辑:您需要在 linux 上安装 beep 包才能运行 beep 命令。 You can install by giving the command您可以通过给出命令来安装

sudo apt-get install beep

This works on mac:这适用于mac:

import numpy as np
import simpleaudio as sa

def sound(x,z):
 frequency = x # Our played note will be 440 Hz
 fs = 44100  # 44100 samples per second
 seconds = z  # Note duration of 3 seconds

 # Generate array with seconds*sample_rate steps, ranging between 0 and seconds
 t = np.linspace(0, seconds, seconds * fs, False)

 # Generate a 440 Hz sine wave
 note = np.sin(frequency * t * 2 * np.pi)

 # Ensure that highest value is in 16-bit range
 audio = note * (2**15 - 1) / np.max(np.abs(note))
 # Convert to 16-bit data
 audio = audio.astype(np.int16)

 # Start playback
 play_obj = sa.play_buffer(audio, 1, 2, fs)

 # Wait for playback to finish before exiting
 play_obj.wait_done()

sound(300,2)

sound(200,1)

The most light-weight cross-platform layer I can see is "PortAudio".我能看到的最轻量级的跨平台层是“PortAudio”。 This is used by R for instance in their package to wrap platform-specific driver calls into simple play/record of digitized waveforms as an array.这被 R 例如在他们的包中用于将特定于平台的驱动程序调用包装成简单的数字化波形播放/记录作为数组。 The good folk at MIT produce a Python binding for this, but you will need to include the compiled .dll/.so for this to work.麻省理工学院的好人为此生成了一个 Python 绑定,但您需要包含已编译的 .dll/.so 才能使其工作。 http://people.csail.mit.edu/hubert/pyaudio/ http://people.csail.mit.edu/hubert/pyaudio/

( libao is similar by Xiph the makers of Ogg/Vorbis , a wrapper pyao exists but this seems less widely used ) (libao 与 Ogg/Vorbis 的制造商 Xiph 相似,存在包装 pyao 但这似乎不太广泛使用)

SoX is an excellent set of cross-platform tools with much more functionality for format conversion and reading files etc.. SoX 是一套出色的跨平台工具,具有更多的格式转换和读取文件等功能。

Using ctypes to make calls from Python to a driver is feasible but very messy, even the simplest legacy WinMM.使用 ctypes 从 Python 调用驱动程序是可行的,但非常混乱,即使是最简单的遗留 WinMM。

I've found 3 methods for Linux:我为 Linux 找到了 3 种方法:

  • new method using the Linux evdev API, works with any user in the input group ( example source code )使用 Linux evdev API 的新方法,适用于input组中的任何用户(示例源代码
  • old method using fcntl and /dev/console (requires root priviledges) ( example source code )使用fcntl/dev/console旧方法(需要 root 权限)(示例源代码
  • invoke the beep command directly with subprocess or os.system (slower and must be installed in the system).调用beep直接命令与subprocessos.system (较慢并且必须安装在系统中)。

See also my tone() function here with all the alternatives .另请参阅 我的tone() 函数以及所有替代方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM