Python 没有终端的 windows 或 Mac 的铃音

Python Bell sound for windows or Mac without terminal

我有一个程序,我想使用内部铃声“\a”,因为它可以在 linux、windows 和 mac 上运行,无需额外的文件。当程序 运行 为 .py 时效果很好,但是,我也希望我的程序 运行 没有打开的终端 window,所以当程序 运行 时它不起作用]s 作为 .pyw ,因为程序没有连接到标准输出。

现在我使用sys.stdout.write('\a') 来发出警报,但是没有程序连接到终端,声音不会响起(最终导致程序崩溃) .

同样,我想做一些所有终端系统默认的事情,所以我尽量避免包含声音文件并播放它。

假设platform.system()给你的名字是OS,你可以在Linux和Windows上使用下面的方法。它使您可以自由选择特定的声音。

if platform.system () == 'Linux':
    import pyaudio
else:
    import winsound

class BeepBase:
    def __init__ (self):
        self.normalPars = (1000, 0.1)
        self.specialPars = (2100, 0.1)
        self.attentionPars = (400, 0.5)

if platform.system () == 'Linux':
    class Beep (BeepBase):
        def __init__ (self):
            BeepBase.__init__ (self)

            self.sampleFreq = 16000
            self.sampleTime = 1. / self.sampleFreq
            self.attackTime = 0.001
            self.decayTime = 0.015

            self.normalWave = self.getWave (*self.normalPars)
            self.specialWave = self.getWave (*self.specialPars) 
            self.attentionWave = self.getWave (*self.attentionPars)

            # self.pyAudio = pyaudio.PyAudio () # Gives errors

        def done (self):    # Never called, may leak resources
            self.pyAudio.terminate ()

        def getWave (self, frequency, duration):
            wave = ''.join ([
                chr (int (128 + 127 * math.sin (2 * math.pi * frequency * time) * max (0, min (1, min (time/self.attackTime, (duration - time)/self.decayTime)))))
                for time in [self.sampleTime * iSample for iSample in xrange (int (duration / self.sampleTime))]
            ])

            return wave

        def any (self, wave):
            self.stream = self.pyAudio.open (
                format = self.pyAudio.get_format_from_width (1),
                channels = 1,
                rate = self.sampleFreq,
                output = True
            )
            sleep (0.025)
            self.stream.write (wave)
            sleep (0.15)
            self.stream.stop_stream ()
            self.stream.close ()

        def normal (self):
            if main.settings.sound:
                self.any (self.normalWave)

        def special (self):
            if main.settings.sound:
                self.any (self.specialWave)

        def attention (self):
            if main.settings.sound:
                self.any (self.attentionWave)
else:
    class Beep (BeepBase):
        def normal (self):
            if main.settings.sound:
                winsound.Beep (self.normalPars [0], int (self.normalPars [1] * 1000))

        def special (self):
            if main.settings.sound:
                winsound.Beep (self.specialPars [0], int (self.specialPars [1] * 1000))     

        def attention (self):
            if main.settings.sound:
                winsound.Beep (self.attentionPars [0], int (self.attentionPars [1] * 1000))

beep = Beep ()

我发现 forum post 说下面的代码片段可以工作:

import Tkinter
Tkinter.Tk().bell()

我不确定它是否有效。我在 Linux 上,但我无法在我的机器上执行你的命令 运行(可能它被管理员禁用或其他原因)。

请注意,上面的代码片段打开了一个 Tkinter window。我认为隐藏 window 并在播放哔声后关闭它不会是一个大问题(等待一秒钟或 bell() 有一种事件监听器)。

抱歉不够详细。我只是希望我能给你一个提示,因为我无法自行测试。祝你好运! :)