0

So I'm working on this project programmed in python that requires audio output. It generates square wave audio, so you can hear a click from the speaker from when the audio changes from high to low or visa versa. I tested the concept in Windows using PyAudio and it worked, but when I use Python-AlsaAudio in Ubuntu I can't hear the clicks. The strange thing is that if I set the audio output to a sine wave, then I can hear a tone. Because of this, I am confused about why I can hear the sine wave, but not the square wave.

Here is an example of the code in which I was able to replicate the problem:

import math
import alsaaudio as aa

#set up audio
dev = aa.PCM(aa.PCM_PLAYBACK, aa.PCM_NONBLOCK)
RATE = 8000
dev.setchannels(1)
dev.setrate(RATE)

on = ''.join(chr(255) for x in range(RATE/10))
off = ''.join(chr(0) for x in range(RATE/10))
data = ''
for x in range(100): data += on+off #create the square wave
#uncomment the below line to hear the sine tone
#data = ''.join([chr(int(math.sin(x)*127+128)) for x in range(RATE)]) #create the sine wave

dev.write(data) #output audio

The audio rate is 8000 and that works with the sine wave. Running the code as-is is supposed to play a square wave, which I can't get, not even on other computers running Ubuntu. If you uncomment the data line right below where it says to uncomment, then it will play the sine wave which I can hear.

If anyone knows what's wrong and/or how to fix it, I would greatly appreciate it. Thanks.

---Edit---
I managed to get across the problem by installing and using PyAudio. Now it works perfectly.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • You're frequency is really low like 5 Hz or something. I'm surprised you can hear anything at all. The sine otoh is above 1kHz (unusual method of generating a sine btw). Try increasing the frequency of the square wave and see if that helps. – jaket May 21 '15 at 21:59
  • That's not the problem because that frequency can be heard on Windows as a series of clicks. I would like to hear those clicks on Ubuntu, which I can't. Square wave signals will always sound like clicks at a high enough amplitude regardless of frequency. – Koppany Horvath May 22 '15 at 01:01
  • You're second sentence is completely wrong. A square wave is a sine at the fundamental frequency and all of the odd harmonics, each decreasing in level. What ever clicks you are hearing are likely some artifact of the speaker excursion followed by the ac coupling filter draining off. As you've demonstrated you can't rely on it. – jaket May 22 '15 at 01:21
  • But then how does that explain why it works on Windows but not on Ubuntu? The same audio signals are sent to the audio device. – Koppany Horvath May 22 '15 at 02:52
  • Are you running both on the same pc? – jaket May 22 '15 at 02:53
  • Yes, I tried both on the same pc and tried 2 different pc's. Only the Windows version seems to work, the Ubuntu version looks like it's running ok with the exception that I can't hear anything happening. – Koppany Horvath May 22 '15 at 03:02

0 Answers0