howto do a robust simple cross platform beep

G

Gelonida N

Hi,


I just want to use a beep command that works cross platform.


I tried the simplest approach (just printing the BEL character '\a'
chr(7) to the console.


This fails on my Ubuntu 12.04 host, as the pcspkr is in the list of the
blacklisted kernel modules.

I found another snippet trying to push a sine wave directly to /dev/audio

but I don't have write permissions to /dev/audio.

Other solutions seem to suggest to play a wav file, but of course first
I had to write code creating me a wav file.

How do others handle simple beeps?


I just want to use them as alert, when certain events occur within a
very long running non GUI application.


Thanks for any info.


What I do at the moment is:

For Windows I use winsound.Beep

For Linux I create some raw data and pipe it into sox's
'play' command.

I don't consider this very elegant.
 
S

Steven D'Aprano

How do others handle simple beeps?

I just want to use them as alert, when certain events occur within a
very long running non GUI application.

Why? Do you hate your users?

What I do at the moment is:

For Windows I use winsound.Beep

For Linux I create some raw data and pipe it into sox's 'play' command.

I don't consider this very elegant.

There is no cross-platform way to play a beep.

Every few years, people complain that Python doesn't have a standard way
to play a simple alert sound. Why ask for volunteers to write and
maintain the code, and suddenly they go silent.
 
D

Dieter Maurer

Steven D'Aprano said:
Why? Do you hate your users?

I, too, would find it useful -- for me (although I do not hate myself).

Surely, you know an alarm clock. Usually, it gives an audible signal
when it is time to do something. A computer can in principle be used
as a flexible alarm clock - but it is not so easy with the audible signal...
An audible signal has the advantage (over a visual one) that you can
recognize it even when you are not looking at the screen (because you
are thinking).

Unfortunately, I had to give up. My new computer lacks a working
speaker...
 
C

Chris Angelico

I, too, would find it useful -- for me (although I do not hate myself).

Surely, you know an alarm clock. Usually, it gives an audible signal
when it is time to do something. A computer can in principle be used
as a flexible alarm clock - but it is not so easy with the audible signal...
An audible signal has the advantage (over a visual one) that you can
recognize it even when you are not looking at the screen (because you
are thinking).

Unfortunately, I had to give up. My new computer lacks a working
speaker...

There's a simple cheat you can do. Just invoke some other application
to produce the sound! My current alarm clock comes in two modes: it
either picks a random MIDI file from Gilbert and Sullivan's
"Ruddigore", or it plays the "Alice: Madness Returns" theme; in each
case it just invokes the file with its default association (see the
"start" command in Windows, or "gnome-open" in, well, GNOME).

Of course, working speaker IS a prerequisite.

ChrisA
 
H

Hans Mulder

There's a simple cheat you can do. Just invoke some other application
to produce the sound! My current alarm clock comes in two modes: it
either picks a random MIDI file from Gilbert and Sullivan's
"Ruddigore", or it plays the "Alice: Madness Returns" theme; in each
case it just invokes the file with its default association (see the
"start" command in Windows, or "gnome-open" in, well, GNOME).

Of course, working speaker IS a prerequisite.

The other prerequisite is that the use is physically near the
compueter where your Python process is running.

If, for exmple, I'm ssh'ed into my webserver, then sending a sound
file to the server's speaker may startle someone in the data centre,
but it won't attract my attention. If, OTOH, you do:

print "\7"

, then an ASCII bell will be sent across the network, and my
terminal emulator will beep.

It all depends.


-- HansM
 
C

Chris Angelico

The other prerequisite is that the use is physically near the
compueter where your Python process is running.

If, for exmple, I'm ssh'ed into my webserver, then sending a sound
file to the server's speaker may startle someone in the data centre,
but it won't attract my attention. If, OTOH, you do:

print "\7"

, then an ASCII bell will be sent across the network, and my
terminal emulator will beep.

Sure, though other of the OP's ideas preclude that too. But you could
use any network protocol that acknowledges sound (MUDs use \7
following the terminal).

ChrisA
 
R

rantingrickjohnson

I just want to use a beep command that works cross platform. [...] I
just want to use them as alert, when certain events occur within a
very long running non GUI application.

I can see a need for this when facing a non GUI interface. But even "IF" you do manage to play a sound in a cross platform manner; if the speaker volume is too low, or the speakers are turned off, or the computer does not have speakers connected, etc... your user will never hear the alert! In this case, beeping the built-in speaker has the fail-safe advantage.

Why not wrap up the functionality and release a module yourself? If you arenot sure how to access the speaker on one or more OSs then ask on the list.. I would love to see some community effort behind this.

PS: Better make sure this module does not exist though ;-)
 
R

rantingrickjohnson

I just want to use a beep command that works cross platform. [...] I
just want to use them as alert, when certain events occur within a
very long running non GUI application.

I can see a need for this when facing a non GUI interface. But even "IF" you do manage to play a sound in a cross platform manner; if the speaker volume is too low, or the speakers are turned off, or the computer does not have speakers connected, etc... your user will never hear the alert! In this case, beeping the built-in speaker has the fail-safe advantage.

Why not wrap up the functionality and release a module yourself? If you arenot sure how to access the speaker on one or more OSs then ask on the list.. I would love to see some community effort behind this.

PS: Better make sure this module does not exist though ;-)
 
G

Gelonida N

On 07/15/2012 03:15 AM, (e-mail address removed) wrote:> On Friday,
I just want to use a beep command that works cross platform. [...] I
just want to use them as alert, when certain events occur within a
very long running non GUI application.

I can see a need for this when facing a non GUI interface.
That's exactly my usecase.
A rather tiny script running for hours and telling the users when
results are ready.

But even "IF" you do manage to play a sound in a cross platform manner;
if the speaker volume is too low, or the speakers are turned off, or the
computer does not have speakers connected, etc... your user will never
hear the alert! In this case, beeping the built-in speaker has the
fail-safe advantage.
Well the user starts the script, because he wants to get immediate
notification while being able mimimize the window, work on a different a
machine or doing some paperwork or discussions.
So it would be up to him to configure the volume appropraitely.
Why not wrap up the functionality and release a module yourself? If
you are not sure how to access the speaker on one or more OSs then ask
on the list. I would love to see some community effort behind this.
I'm having no some ugly code, that is working on the platforms, that I
am using.


I'm rather busy, and have no experience in publishing coe, that's good
enough for the community.


I could try to use this a test case for learning to create communicty
modules.

Is there any decent getting started guide.

Could I use github (as O know git already)?

Assuming, the module would achieve a state, where it could be usable by
others. How would one register on Pypi?
PS: Better make sure this module does not exist though ;-)

I didn't find one, that's why I asked here.
 
C

Chris Angelico

On 07/15/2012 03:15 AM, (e-mail address removed) wrote:> On Friday, July
I just want to use a beep command that works cross platform. [...] I
just want to use them as alert, when certain events occur within a
very long running non GUI application.

I can see a need for this when facing a non GUI interface.
That's exactly my usecase.
A rather tiny script running for hours and telling the users when results
are ready.

Sounds reasonable. I'd be inclined to solve just my own problem,
though; work it out for the platforms you use, and don't worry too
much about the rest. But that's because I'm lazy :)
Could I use github (as O know git already)?

You certainly could, though that doesn't help with the whole "building
a module" part. (And I can't help there either, never done it. Sorry.)

There are quite a few ways of creating an alert such as you describe.
The easiest way may be to play a .WAV file rather than a system beep;
there are a number of different options on different platforms, so
your module could be written in pure Python and basically wrap the
whole lot up into a huge bunch of "except ImportError" checks. Here's
a few ways:

http://stackoverflow.com/questions/307305/play-a-sound-with-python

Burying all that complexity behind a simple "play_sound()" function
would be handy, and you could easily start with just 2-3 options and
add more later.

ChrisA
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top