beeping under linux

A

Alexander Schmolck

This is only partly a python question, but what is the easiest way to get
python to (reliably) beep under linux? By reliably I mean that ``print "\b"``
won't do because it depends on the terminal settings -- so I guess I'm looking
for some simple way to more or less directly access the internal speaker
(maybe writing to something in /dev/).

[The usage scenario is simply to have an effective way of signalling that a
long running-process finshed, without me having to constantly look at the
screen]

'as
 
C

Christophe Delord

Hello,

you can for instance write to /dev/audio
Here is a simple and ugly beep function (not really portable). It builds
a string with a signal having the expected frequency, amplitude (between
0 and 255) and duration (in seconds) :

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()

beep(440, 63, 1)

There must be more smarter solutions...

Christophe.
 
B

Ben Finney

This is only partly a python question, but what is the easiest way to
get python to (reliably) beep under linux? By reliably I mean that
``print "\b"`` won't do because it depends on the terminal settings

Assuming you mean 'print "\a"' (ASCII BEL) rather than 'print "\b"'
(ASCII BS), this is the most portable way to sound the terminal bell.

Most responses have been talking about /dev/audio, which is unrelated to
the terminal bell and is not portable -- many systems, especially
headless ones, do not have any sound card.
[The usage scenario is simply to have an effective way of signalling
that a long running-process finshed, without me having to constantly
look at the screen]

The correct way to do this is to send the ASCII BEL ("\a") character to
the terminal. If the terminal's bell has been disabled somehow, it's
not the job of your program to revert that decision.
 
D

David M. Wilson

Alexander Schmolck said:
This is only partly a python question, but what is the easiest way to get
python to (reliably) beep under linux? By reliably I mean that ``print "\b"``
won't do because it depends on the terminal settings -- so I guess I'm looking
for some simple way to more or less directly access the internal speaker
(maybe writing to something in /dev/).

[cape:1:dw]$ setterm -blen 1234 -bfreq 4321 | xxd
0000000: 1b5b 3131 3b31 3233 345d 1b5b 3130 3b34 .[11;1234].[10;4
0000010: 3332 315d 321]
[cape:2:dw]$ echo $TERM
linux

If by 'reliably', you mean 'override the users choice of having the
speaker on or off', then I think you need a rethink. :)

There are several utilities about that can do the beeping (and music)
for you, and IIRC even ones that work across platforms.

[The usage scenario is simply to have an effective way of signalling that a
long running-process finshed, without me having to constantly look at the
screen]

Suggestion: allow a configurable command to be called, so that users
can choose their own notification method, eg. /usr/bin/play (a part of
'sox').


David.
 
A

Alexander Schmolck

Ben Finney said:
Assuming you mean 'print "\a"' (ASCII BEL) rather than 'print "\b"'
(ASCII BS), this is the most portable way to sound the terminal bell.

Anything that works under a normal linux box will do.
Most responses have been talking about /dev/audio, which is unrelated to
the terminal bell and is not portable -- many systems, especially
headless ones, do not have any sound card.

Yep -- and although mine does have a soundcard it has no speakers.
The correct way to do this is to send the ASCII BEL ("\a") character to
the terminal. If the terminal's bell has been disabled somehow, it's
not the job of your program to revert that decision.

Well, I happen to see myself as the ultimate authority on what my program's
job (especially when I'm the sole user) and I also fail to discern an obvious
relationship between my user-preference for visible/audible bells when working
with (i.e. in front of) a certain terminal and my desire to receive some
notification when a long-running process finishes that doesn't force me to
constantly monitor the monitor (because chances are I might be reading a
paper).

I could live with a solution that requires me to change comint's (viz Emacs's
py-shell's) and kterm's beeping behavior before and after my program runs, but
I have no idea to get either of them to beep audibly (kterms bell settings
e.g. don't seem much of a help).

The best reason I can see for trying to find a way to make '\a' work is that
it would be desirable to have beeping behavior also for remotely running
processes (so directly writing to /dev/* or some such wouldn't be ideal).

'as
 
A

Alexander Schmolck

If by 'reliably', you mean 'override the users choice of having the
speaker on or off', then I think you need a rethink. :)

Being the (sole) user (and not suffering from multiple personality disorder),
such an override seems unlikely.

As I mentioned in my reply to Ben Finney, apart from the fact that I simply
have no idea how to configure comint (or kterm for that matter -- cerainly not
via settings-bell) for (audible) beeping, for this particular scenario
(notification of myself when I'm not necessarily in front of my computer) the
standard terminal bell preference settings are not really that relevant
anyway.
There are several utilities about that can do the beeping (and music)
for you, and IIRC even ones that work across platforms.

Great, do you know one for linux (that uses the internal speaker and that
ideally would be preinstalled or come as a Mandrake rpm)?
[The usage scenario is simply to have an effective way of signalling that a
long running-process finshed, without me having to constantly look at the
screen]

Suggestion: allow a configurable command to be called, so that users
can choose their own notification method, eg. /usr/bin/play (a part of
'sox').

Sure, I just don't know any suitable command/program.

'as
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top