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.