howto do a robust simple cross platform beep

Discussion in 'Python' started by Gelonida N, Jul 14, 2012.

  1. Gelonida N

    Gelonida N Guest

    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.
     
    Gelonida N, Jul 14, 2012
    #1
    1. Advertising

  2. 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
     
    Steven D'Aprano, Jul 14, 2012
    #2
    1. Advertising

  3. Steven D'Aprano <> 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...
     
    Dieter Maurer, Jul 14, 2012
    #3
  4. Gelonida N

    Miki Tebeka Guest

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

    I *think* the "big" UI frameworks (Qt, wx ...) have some sound support.
     
    Miki Tebeka, Jul 14, 2012
    #4
  5. Gelonida N

    Miki Tebeka Guest

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

    I *think* the "big" UI frameworks (Qt, wx ...) have some sound support.
     
    Miki Tebeka, Jul 14, 2012
    #5
  6. Re: howto do a robust simple cross platform beep

    On Sun, Jul 15, 2012 at 3:54 AM, Dieter Maurer <> 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
     
    Chris Angelico, Jul 14, 2012
    #6
  7. Gelonida N

    Hans Mulder Guest

    Re: howto do a robust simple cross platform beep

    On 14/07/12 20:49:11, Chris Angelico wrote:
    > On Sun, Jul 15, 2012 at 3:54 AM, Dieter Maurer <> 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
     
    Hans Mulder, Jul 15, 2012
    #7
  8. Re: howto do a robust simple cross platform beep

    On Sun, Jul 15, 2012 at 10:39 AM, Hans Mulder <> 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
     
    Chris Angelico, Jul 15, 2012
    #8
  9. Gelonida N

    Guest

    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 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 ;-)
     
    , Jul 15, 2012
    #9
  10. Gelonida N

    Guest

    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 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 ;-)
     
    , Jul 15, 2012
    #10
  11. Gelonida N

    Gelonida N Guest

    On 07/15/2012 03:15 AM, 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.
     
    Gelonida N, Jul 24, 2012
    #11
  12. Re: howto do a robust simple cross platform beep

    On Wed, Jul 25, 2012 at 7:39 AM, Gelonida N <> wrote:
    > On 07/15/2012 03:15 AM, 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
     
    Chris Angelico, Jul 24, 2012
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arsalan
    Replies:
    0
    Views:
    347
    Arsalan
    Nov 2, 2004
  2. Greg Hauptmann
    Replies:
    3
    Views:
    235
    David Masover
    Sep 16, 2009
  3. Matt Kruse
    Replies:
    0
    Views:
    138
    Matt Kruse
    Aug 15, 2006
  4. Martin Rinehart

    Robust, Cross-Browser setAttribute()?

    Martin Rinehart, Nov 23, 2008, in forum: Javascript
    Replies:
    10
    Views:
    221
    Martin Rinehart
    Nov 25, 2008
  5. Jerry Hill
    Replies:
    0
    Views:
    206
    Jerry Hill
    Jul 24, 2012
Loading...

Share This Page