|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +beep.py - Make a beep sound |
| 5 | +
|
| 6 | +Copyright (c) 2006-2012 sqlmap developers (http://sqlmap.org/) |
| 7 | +See the file 'doc/COPYING' for copying permission |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import wave |
| 14 | + |
| 15 | +BEEP_WAV_FILENAME = os.path.join(os.path.dirname(__file__), "beep.wav") |
| 16 | + |
| 17 | +def beep(): |
| 18 | + try: |
| 19 | + if subprocess.mswindows: |
| 20 | + _win_wav_play(BEEP_WAV_FILENAME) |
| 21 | + elif sys.platform == "darwin": |
| 22 | + _mac_beep() |
| 23 | + elif sys.platform == "linux2": |
| 24 | + _linux_wav_play(BEEP_WAV_FILENAME) |
| 25 | + else: |
| 26 | + _speaker_beep() |
| 27 | + except Exception: |
| 28 | + _speaker_beep() |
| 29 | + |
| 30 | +def _speaker_beep(): |
| 31 | + sys.stdout.write('\a') # doesn't work on modern Linux systems |
| 32 | + |
| 33 | + try: |
| 34 | + sys.stdout.flush() |
| 35 | + except IOError: |
| 36 | + pass |
| 37 | + |
| 38 | +def _mac_beep(): |
| 39 | + import Carbon.Snd |
| 40 | + Carbon.Snd.SysBeep(1) |
| 41 | + |
| 42 | +def _win_wav_play(filename): |
| 43 | + import winsound |
| 44 | + |
| 45 | + winsound.PlaySound(filename, winsound.SND_FILENAME) |
| 46 | + |
| 47 | +def _linux_wav_play(filename): |
| 48 | + import ctypes |
| 49 | + |
| 50 | + PA_STREAM_PLAYBACK = 1 |
| 51 | + PA_SAMPLE_S16LE = 3 |
| 52 | + BUFFSIZE = 1024 |
| 53 | + |
| 54 | + class struct_pa_sample_spec(ctypes.Structure): |
| 55 | + _fields_ = [("format", ctypes.c_int), ("rate", ctypes.c_uint32), ("channels", ctypes.c_uint8)] |
| 56 | + |
| 57 | + pa = ctypes.cdll.LoadLibrary("libpulse-simple.so.0") |
| 58 | + |
| 59 | + wave_file = wave.open(filename, "rb") |
| 60 | + |
| 61 | + pa_sample_spec = struct_pa_sample_spec() |
| 62 | + pa_sample_spec.rate = wave_file.getframerate() |
| 63 | + pa_sample_spec.channels = wave_file.getnchannels() |
| 64 | + pa_sample_spec.format = PA_SAMPLE_S16LE |
| 65 | + |
| 66 | + error = ctypes.c_int(0) |
| 67 | + |
| 68 | + pa_stream = pa.pa_simple_new(None, filename, PA_STREAM_PLAYBACK, None, "playback", ctypes.byref(pa_sample_spec), None, None, ctypes.byref(error)) |
| 69 | + if not pa_stream: |
| 70 | + raise Exception("Could not create pulse audio stream: %s" % pa.strerror(ctypes.byref(error))) |
| 71 | + |
| 72 | + while True: |
| 73 | + latency = pa.pa_simple_get_latency(pa_stream, error) |
| 74 | + if latency == -1: |
| 75 | + raise Exception("Getting latency failed") |
| 76 | + |
| 77 | + buf = wave_file.readframes(BUFFSIZE) |
| 78 | + if not buf: |
| 79 | + break |
| 80 | + |
| 81 | + if pa.pa_simple_write(pa_stream, buf, len(buf), error): |
| 82 | + raise Exception("Could not play file") |
| 83 | + |
| 84 | + wave_file.close() |
| 85 | + |
| 86 | + if pa.pa_simple_drain(pa_stream, error): |
| 87 | + raise Exception("Could not simple drain") |
| 88 | + |
| 89 | + pa.pa_simple_free(pa_stream) |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + beep() |
0 commit comments