0

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()
print(p.get_default_output_device_info())
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 = p.open(format=pyaudio.paFloat32,
                channels=2,
                rate=fs,
                output=True)
# play. May repeat with different volume values (if done interactively) 
stream.write(volume*samples)

stream.stop_stream()
stream.close()

p.terminate()

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.

0

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.

3
  • Hi! Thanks for the answer. I've tried changing the volume like you suggested, but still nothing. Also, the samples array is populated with oscillating values between [-1,1] as expected. I have also noticed that the program runs indefinitely, even when I set the duration to 1 second. Would you happen to have any other suggestions? – Phoenix Wright Jul 25 '20 at 11:59
  • Did you change all the numeric constants that should be float to use a decimal point? Fortran and C are famously bad at converting nothing from int to float (or vice versa), and TypeScript is very forgiving at inferring what types you want from how they're used. Python I don't know well enough to say without coding up a small experiment. – StevePoling Jul 26 '20 at 18:52
  • Yes, I have done so as you suggested in your first answer. Unfortunately, there is still nothing. – Phoenix Wright Jul 29 '20 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.