Generating single-frequency sound with Python, but no sound is produced

Raspberry Pi Asked by Phoenix Wright on January 2, 2022

I just got a new Raspberry Pi 4 and am trying to write a Python program that generates a sound that’s just one frequency. However, I am having a problem that I cannot hear any sound coming out of the program at all. Here is the code I am using.

import pyaudio
import numpy as np

p = pyaudio.PyAudio()
volume = 1     # range [0.0, 1.0]
fs = 48000       # sampling rate, Hz, must be integer
duration = 100   # in seconds, may be float
f = 440.0        # sine frequency, Hz, may be float

# generate samples, note conversion to float32 array
samples = (np.sin(2*np.pi*np.arange(fs*duration)*f/fs)).astype(np.float32)

# for paFloat32 sample values must be in range [-1.0, 1.0]
stream =,
# play. May repeat with different volume values (if done interactively) 



I took this code from another question on this site regarding generating the sound I mentioned. The only changes I made were to observe my device’s info and to change the sampling rate to my device’s default. If I did not change my sampling rate, the program would produce an error. As it is, the program simply runs indefinitely, all the while producing no sound. If I stop the program, a pop can be heard from the speakers.

Now, the Pi itself produces audio output just fine. I have tested both playing audio to HDMI and Analog, with YouTube from the browser, and playing a wav file in Python using the playsound library.

So, I am quite confused as to why the program above produces no sound. Any insight would be appreciated. Thank you very much in advance.

EDIT: I have also noticed that the program runs indefinitely, even when I set the duration to 1 second.

One Answer

The pop means you've got some control over the speakers. Good. Maybe, your program is telling the speakers to work too quietly. Look for things that would cause that: Have you tried volume=1.0 or maybe volume=0.99? Maybe Python isn't coercing the integer 1 into the float 1.0. Double check that your samples array is populated with values of the expected range.

Answered by StevePoling on January 2, 2022

Add your own answers!

Related Questions

Fade screen on sleep and brighten on wake

1  Asked on February 8, 2021 by itamar-kerbel


reading a dutch ‘slimme meter’ has USB troubles

1  Asked on February 6, 2021 by cantdutchthis


Screenly install process

1  Asked on February 6, 2021 by dirxtope


Block script excecution when fbi is displaying image

4  Asked on February 5, 2021 by jpx


Access ports with specific address

0  Asked on February 4, 2021 by dan-chaltiel


Adafruit Speaker Bonnet for Raspberry Pi – No sound

0  Asked on February 2, 2021 by emad-mokhtar


voltage issues with GPIO raspberry pi 2

1  Asked on February 1, 2021 by andrew-m


Pi loses local network connectivity

0  Asked on January 30, 2021 by boogie-man


Raspbian 8 on Raspberry Pi 3 B+

4  Asked on January 30, 2021 by medismail


Raspberry Pi Networking

2  Asked on January 19, 2021 by danny-herbert


Make ifplugd available with systemd

1  Asked on January 17, 2021 by ingo


Trying to connect pi zero over USB

1  Asked on January 16, 2021 by wabbitt


Compile a dts file (dt overlay)

1  Asked on January 13, 2021 by feroxcannibal


RPi 3 B+ set Wi-Fi country on localisation tab

1  Asked on January 12, 2021 by stilllearning


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir