469,366 Members | 1,805 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

beeping under linux

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
Jul 18 '05 #1
5 23803
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.

On 19 Nov 2003 19:41:28 +0000, Alexander Schmolck wrote:
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

Jul 18 '05 #2
On 19 Nov 2003 19:41:28 +0000, Alexander Schmolck wrote:
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.

--
\ "I always wanted to be somebody. I see now that I should have |
`\ been more specific." -- Lily Tomlin |
_o__) |
Ben Finney <http://bignose.squidly.org/>
Jul 18 '05 #3
Alexander Schmolck <a.********@gmx.net> wrote in message news:<yf*************@black132.ex.ac.uk>...
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.
Jul 18 '05 #4
dw***********@botanicus.net (David M. Wilson) writes:
Alexander Schmolck <a.********@gmx.net> wrote in message news:<yf*************@black132.ex.ac.uk>...
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/).
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
Jul 18 '05 #5
Ben Finney <bi****************@and-benfinney-does-too.id.au> writes:
On 19 Nov 2003 19:41:28 +0000, Alexander Schmolck wrote:
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.


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
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jhuola Hoptire | last post: by
1 post views Thread by Herman Geldenhuys | last post: by
4 posts views Thread by Jim | last post: by
5 posts views Thread by cranium.2003 | last post: by
reply views Thread by adrian | last post: by
Subsciber123
9 posts views Thread by Subsciber123 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.