How to play sound in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bill Dandreta

    #1

    How to play sound in Python?

    I posted a message ("Help with my 1st Tkinter program") a few days ago
    complaining that Python did not have any built in basic cross platform
    sound capability. I was wrong (at least partly). Python comes with some
    sound playing ability for some platforms.

    Since no one responded to my post, I assume the capability is not well
    known so I will post a code snippet from my practice program that
    demonstrates how I play a sound file with Python on both Windows and
    Linux. It may save someone else some time.

    The winsound module lets you play .wav files under windows. The
    ossaudiodev module in combination with the wave module lets you play
    ..wav files under Linux (and some other nix's as well).

    =============== ============
    file='tada.wav'
    if platform.starts with('win'):
    from winsound import PlaySound, SND_FILENAME, SND_ASYNC
    PlaySound(file, SND_FILENAME|SN D_ASYNC)
    elif platform.find(' linux')>-1:
    from wave import open as waveOpen
    from ossaudiodev import open as ossOpen
    s = waveOpen('tada. wav','rb')
    (nc,sw,fr,nf,co mptype, compname) = s.getparams( )
    dsp = ossOpen('/dev/dsp','w')
    try:
    from ossaudiodev import AFMT_S16_NE
    except ImportError:
    if byteorder == "little":
    AFMT_S16_NE = ossaudiodev.AFM T_S16_LE
    else:
    AFMT_S16_NE = ossaudiodev.AFM T_S16_BE
    dsp.setparamete rs(AFMT_S16_NE, nc, fr)
    data = s.readframes(nf )
    s.close()
    dsp.write(data)
    dsp.close()
    =============== =============== =

    Bill
  • Dmitry Borisov

    #2
    Re: How to play sound in Python?

    "Bill Dandreta" <wjdandreta@att .net> wrote in message
    news:Msved.2168 9$OD2.7465@bgtn sc05-news.ops.worldn et.att.net...[color=blue]
    > I posted a message ("Help with my 1st Tkinter program") a few days ago
    > complaining that Python did not have any built in basic cross platform
    > sound capability. I was wrong (at least partly). Python comes with some
    > sound playing ability for some platforms.[/color]

    Take a look: http://pymedia.org/
    Dmitry/


    Comment

    • bearophile

      #3
      Re: How to play sound in Python?

      Bill Dandreta:[color=blue]
      >The winsound module lets you play .wav files under windows.[/color]

      Thank you.
      You can also create "on the fly" memory-mapped wav files (with wave
      and mmap modules, I think), and play them.

      How to use the flag SND_PURGE for WAVs loaded from filename or memory
      mapped?

      Bear hugs,
      Bearophile

      Comment

      • Johannes Nix

        #4
        Re: How to play sound in Python?


        just a quick note....

        Bill Dandreta <wjdandreta@att .net> writes:
        [color=blue]
        > I posted a message ("Help with my 1st Tkinter program") a few days ago
        > complaining that Python did not have any built in basic cross platform
        > sound capability. I was wrong (at least partly). Python comes with
        > some sound playing ability for some platforms.[/color]

        I remember that somebody has implemented Python bindings for the
        portaudio library, which is a cross-platform library for real-time
        audio.


        Johannes

        Comment

        Working...