Python Forum

Full Version: Play fixed frequency sound in python 3
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would just like to play a fixed frequency for a fixed period of time to the default speakers (HDMI or other) on a RP3 running wheezy with eventual porting to RPI 4 on Buster. This is from within a python 3 program. I am not playing anything from a file - just outputting a tone to the speaker.

Because the development work is not done directly on a Pi, I'd prefer a solution that is not restricted to a Pi but could run on other Linux systems (I have PCs running Ubuntu 18 and Debian Buster). Running on Windows is NOT a requirement.

There seems to be lots of different libraries and many of them are really designed to output files rather than "play this tone for a fixed period of time". Starting a tone unblocked, then sleeping for the required time, then stopping the tone (like audiere - now obsolete) seems to be one simple way, given there is no single call to output for a fixed time.

Can someone suggest a library that has such simple features and point me in the right direction?
I have not researched this on python 3,
but micropython (eg. on the micro:bit) has a built-in music module.
It does exactly what you want:
music.pitch(440, 1000) means play 440 herz for 1 second.

paul
(Feb-07-2021, 06:20 PM)DPaul Wrote: [ -> ]I have not researched this on python 3,
but micropython (eg. on the micro:bit) has a built-in music module.
It does exactly what you want:
music.pitch(440, 1000) means play 440 herz for 1 second.

paul
Choice of device is not an option. I have already written a bunch of software for this and another application that run on the same Pi.

However, what showed up among related topics is a question like mine and the solution to it was to use pysine, an equally efficient option.

I imported pysine but even though I did not add any code to use it, I got this when running the program:
Error:
ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side ALSA lib pcm_route.c:869:(find_matching_chmap) Found no matching channel map ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port ALSA lib pcm_oss.c:377:(_snd_pcm_oss_open) Unknown field port ALSA lib pcm_a52.c:823:(_snd_pcm_a52_open) a52 is only for playback ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card ALSA lib pcm_usb_stream.c:486:(_snd_pcm_usb_stream_open) Invalid type for card ALSA lib pcm_dmix.c:1108:(snd_pcm_dmix_open) unable to open slave