Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

howto do a robust simple cross platform beep

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


gelonida at gmail

Jul 13, 2012, 6:00 PM

Post #1 of 14 (1674 views)
Permalink
howto do a robust simple cross platform beep

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.











--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Jul 13, 2012, 8:19 PM

Post #2 of 14 (1627 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Sat, 14 Jul 2012 03:00:05 +0200, Gelonida N wrote:

> 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.


--
Steven
--
http://mail.python.org/mailman/listinfo/python-list


dieter at handshake

Jul 14, 2012, 10:54 AM

Post #3 of 14 (1628 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

Steven D'Aprano <steve+comp.lang.python [at] pearwood> writes:
>> 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?

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...

--
http://mail.python.org/mailman/listinfo/python-list


miki.tebeka at gmail

Jul 14, 2012, 11:43 AM

Post #4 of 14 (1627 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

> How do others handle simple beeps?
http://pymedia.org/ ?

I *think* the "big" UI frameworks (Qt, wx ...) have some sound support.
--
http://mail.python.org/mailman/listinfo/python-list


miki.tebeka at gmail

Jul 14, 2012, 11:43 AM

Post #5 of 14 (1625 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

> How do others handle simple beeps?
http://pymedia.org/ ?

I *think* the "big" UI frameworks (Qt, wx ...) have some sound support.
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Jul 14, 2012, 11:49 AM

Post #6 of 14 (1626 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Sun, Jul 15, 2012 at 3:54 AM, Dieter Maurer <dieter [at] handshake> wrote:
> 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
--
http://mail.python.org/mailman/listinfo/python-list


hansmu at xs4all

Jul 14, 2012, 5:39 PM

Post #7 of 14 (1629 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On 14/07/12 20:49:11, Chris Angelico wrote:
> On Sun, Jul 15, 2012 at 3:54 AM, Dieter Maurer <dieter [at] handshake> wrote:
>> 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.

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


--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Jul 14, 2012, 6:07 PM

Post #8 of 14 (1629 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Sun, Jul 15, 2012 at 10:39 AM, Hans Mulder <hansmu [at] xs4all> wrote:
> 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
--
http://mail.python.org/mailman/listinfo/python-list


rantingrickjohnson at gmail

Jul 14, 2012, 6:15 PM

Post #9 of 14 (1627 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Friday, July 13, 2012 8:00:05 PM UTC-5, gelonida wrote:
> 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 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.

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

--
http://mail.python.org/mailman/listinfo/python-list


rantingrickjohnson at gmail

Jul 14, 2012, 6:15 PM

Post #10 of 14 (1630 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Friday, July 13, 2012 8:00:05 PM UTC-5, gelonida wrote:
> 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 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.

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

--
http://mail.python.org/mailman/listinfo/python-list


gelonida at gmail

Jul 24, 2012, 2:39 PM

Post #11 of 14 (1577 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On 07/15/2012 03:15 AM, rantingrickjohnson [at] gmail wrote:> On Friday,
July 13, 2012 8:00:05 PM UTC-5, gelonida wrote:
>> 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.


--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Jul 24, 2012, 2:52 PM

Post #12 of 14 (1581 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Wed, Jul 25, 2012 at 7:39 AM, Gelonida N <gelonida [at] gmail> wrote:
> On 07/15/2012 03:15 AM, rantingrickjohnson [at] gmail wrote:> On Friday, July
> 13, 2012 8:00:05 PM UTC-5, gelonida wrote:
>>> 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
--
http://mail.python.org/mailman/listinfo/python-list


malaclypse2 at gmail

Jul 24, 2012, 3:03 PM

Post #13 of 14 (1578 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Fri, Jul 13, 2012 at 9:00 PM, Gelonida N <gelonida [at] gmail> wrote:
> I tried the simplest approach (just printing the BEL character '\a' chr(7)
> to the console.

That's what I do when I want to send an audible alert to the user of a
console based program. It's then up to the user's terminal to do
whatever the user wants. Printing a BEL character has the added
advantage that it plays nicely with programs that are run remotely
(through ssh sessions and the like).

Personally, I'm usually in a screen session, inside either an xterm or
over an ssh link, and have visual bells turned on so I can see the
alerts, even when they pop up in another screen.

--
Jerry
--
http://mail.python.org/mailman/listinfo/python-list


drsalists at gmail

Jul 24, 2012, 3:19 PM

Post #14 of 14 (1576 views)
Permalink
Re: howto do a robust simple cross platform beep [In reply to]

On Sat, Jul 14, 2012 at 1:00 AM, Gelonida N <gelonida [at] gmail> wrote:

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


You may want to get over that. Some software vendors/distributors don't
want you to be able to do things portably. Others have "not invented here"
syndrome. A lot of programming is adding multiple ways of doing the same
thing to program around the stupid wars ("choices") the vendors and
distributors push on us.

Or if you're really an idealist, start your own project to abstract away
these silly details.

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.