使装ubuntu的笔记本可以beep警报

大多数电脑都有beep,我们可以利用这一点,很方便的利用电脑可以发声起来进行各种DIY。
首先安装
apt-get install beep

在ubuntu中,pcspkr这个驱动应该被加入黑名单了
vim /etc/modprobe.d/blacklist.conf

所以需要手动加载驱动
modprobe pcspkr
lsmod |grep pcspkr


加载之后调节beep的音量,使用下面这个软件命令,按m键取消静音
alsamixer


确保命令行打开了beep
xset b on
xset -q |grep bell



使用beep测试一下
beep
beep -f 300.7 -r 2 -d 100 -l 400


监听某个日志,出现passwd时发出警告
tail -f /var/log/xferlog | grep --line-buffered passwd | beep -f 1000 -r 5 -s


自己写的一个根据不同字符流产生不同声音的python脚本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
    beep.py
    ~~~~~~~~~~~~~~

    A brief description goes here.
"""
import subprocess
import sys

def beep(s):
    MINTONE = 261.6
    MAXTONE = 523.2
    frequencies = []
    for c in s:
        try:
            if ord(c) > ord('z') or ord(c) < ord('A'):
                f = 20
                l = 50
            else:
                f = MINTONE + float((ord(c) - ord('A'))) / (ord('z') - ord('A')) * (MAXTONE - MINTONE)
                l = 100
        except TypeError as e:
            continue
        frequencies.append((f, l))
    cmd = "beep %s" % " -n ".join(["-f %s -l %s" % (i[0], i[1]) for i in frequencies])
    print cmd
    result = subprocess.check_output(cmd, shell=True)
    print result

def _main(argv):
    if len(argv) > 1:
        string = argv[1]
        beep(string)
    else:
        while True:
            #string = sys.stdin.readlines()[0]
            string = sys.stdin.readline()
            beep(string)


if __name__ == '__main__':
    import sys
    _main(sys.argv)

猜你喜欢

转载自luozhaoyu.iteye.com/blog/1959212