Here is an example from the wxPython demo. And finally, although I don't know the limitations of pyaudio, your error message sounds more like the library is not able to find the default output device more than the device is in use by another process. You can try Simpleaudio :.
I found playsound to be the simplest. Learn more. What's a cross platform way to play a sound file in python? Ask Question. Asked 11 years, 5 months ago. Active 2 years, 2 months ago. Viewed 9k times.
Another method? Jon Seigel Gilbert Gilbert 4 4 silver badges 7 7 bronze badges. Active Oldest Votes. Sound 'sound. Play wx. Have you looked at pymedia? It looks as easy as this to play a WAV file: import time, wave, pymedia.
Erwin Mayer Erwin Mayer Sound from pygame import mixer mixer. Mauli Mauli Nae Nae 8, 4 4 gold badges 23 23 silver badges 51 51 bronze badges. Sign up or log in Sign up using Google.
I am looking for a well maintained Python library with allows me to play audio files on my Raspberry Pi using the standard audio output. So far I've tried several, but none of them seem to work. Although pyglet works on my regular computer fine, it causes an error on the Raspberry Pi.
Is there a Python library which has been proven as easy to use? I recommend the widely popular Pygame. I may be wrong, but I believe that it is pre-installed on the Pi. You can use the Pygame Mixer Music Module to play audio files. I have included some example code below. I needed a script to play a song from thirty seconds in in the background whilst responding to other user input. I then wanted it to end the song on some event.
Look at MPlayer documentation for all sorts of commands you can pass in this way to control playback. I would recommend pyglet rather than pygame if you don't need graphics. In my view it is simpler, more elegant, and better maintained. Then again, I expect it might be a matter of taste. You may use a number of external players through pipes. However, you will nearly always experience a loss of speed.
There is also a problem with playback control.Many thanks to both Blaise Potard and Matthias Schaff for discovering the issue and for their patches! Thanks as well to Timothy Port for helping to correct a docstring. Many thanks to Tony Jacobson for discovering and helping with the overflow error.
Thanks again to Bastian Bechtold for his help converting the documentation for use with Sphinx! In addition, thanks to John K. Luebs for the callback fix. Many thanks to Bastian Bechtold and Bob Jamison for their contributions! Without their patches and Bastian's careful review, this release would still be far away. Also, great thanks to Danilo J. Bellini for reporting bugs. Note: As of this update, PyAudio is compatible with Python 2.
For Python installations older than 2. The current version is PyAudio v0. Install PyAudio using pip on most platforms. For versions prior to v0. Currently, there are wheels compatible with the official distributions of Python 2.
Play sound in Python
For those versions, both bit and bit wheels are available. Notes: pip will download the PyAudio source and build it for your system. Be sure to install the portaudio library development package portaudiodev and the python development package python-all-dev beforehand. For better isolation from system packages, consider installing PyAudio in a virtualenv. To build PyAudio from source, you will also need to build PortAudio v See compilation hints for some instructions on building PyAudio for various platforms.
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi.
It only takes a minute to sign up. I am trying to get my Raspberry Pi to read some audio input through a basic USB souncard and play it back in real time for 10 seconds, and then print the output with Matplotlib after it's finished. I am using PyAudio in callback mode. Audio recording and playback works fine in Audacity. After I run my code I can see the the plotted output signal, but it only has very few samples.
I can't hear anything through the speakers either while I'm playing. Running the same code on my Windows machine works fine, using the same Python and PyAudio versions. I do not know why your audio is not working. Perhaps you need to explicitly state the output device index. Sign up to join this community. The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered. Asked 4 years, 4 months ago.
Active 3 years, 7 months ago. Viewed 46k times. Any help would be much appreciated! DrumPower DrumPower 95 1 1 gold badge 1 1 silver badge 6 6 bronze badges. When you sent them to playback, did you raise the amplitude?
I don't hear anything till the amplitude gets aroundand is good at for int16 format data. Active Oldest Votes. This is what I did to get audio out through the RPi audio jack. Your answer is supposed to help the OP get their code working.
Subscribe to RSS
You can use PyAudio. An example here on my Linux it works:. Your wav is probably 8k. So when pygame plays it, it plays roughly twice as fast. So specify your wav frequency in the init.
Pyglet has some problems correctly reading RIFF headers. If you have a very basic wav file with exactly a 16 byte fmt block with no other information in the fmt chunk like 'fact' datait works. But it makes no provision for additional data in the chunks, so it's really not adhering to the RIFF interface specification. Learn more. Ask Question. Asked 6 years, 9 months ago. Active 1 month ago. Viewed 79k times. I tried pygame for playing wav file like this: import pygame pygame.
You mean, it sounds higher pitched than it should? Active Oldest Votes.
An example here on my Linux it works:! I am getting ALSA errors stackoverflow. Only one add-on: changing while data! This problem only occurs after an update from python 3. The reason pygame changes your audio is mixer defaults to a 22k sample rate: initialize the mixer module pygame.
Gene Gene 81 1 1 silver badge 1 1 bronze badge. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name.
Email Required, but never shown. The Overflow Blog. Socializing with co-workers while social distancing.Released: Mar 19, View statistics for this project via Libraries.How to Record Sound/Speech using Python??
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Mar 19, Jan 17, Oct 20, Feb 4, Oct 28, Oct 17, Nov 3, Mar 7, Jul 6, Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Latest version Released: Mar 19, Navigation Project description Release history Download files.With PyAudio, you can easily use Python to play and record audio on a variety of platforms. PyAudio is inspired by:. To use PyAudio, first instantiate PyAudio using pyaudio. PyAudio 1which sets up the portaudio system. To record or play audio, open a stream on the desired device with the desired audio parameters using pyaudio.
This sets up a pyaudio. Stream to play or record audio. Play audio by writing audio data to the stream using pyaudio. Use pyaudio. Finally, terminate the portaudio session using pyaudio.
Note that PyAudio calls the callback function in a separate thread. Start processing the audio stream using pyaudio. A PortAudio Sample Format constant.
How to Play and Record Audio in Python
Close a stream. Typically use Stream. Return a dictionary containing the default Host API parameters. Return the default input Device parameters as a dictionary. Return the default output Device parameters as a dictionary. Returns the size in bytes for the specified sample format a PortAudio Sample Format constant. Check to see if specified device configuration is supported.