3

I'm trying to generate sound via piping the output for python to aplay like this:

python test.py | aplay

My python code looks like this:

for i in range (0, 1000):
    for j in range (0, 256):
        print(chr(j), end="")

Because the way aplay works (turning raw data to unsigned 8bit, 8000Hz audio) I assumed that it would play a sawtooth wave for 1000 period on (8000/256=) 31.25 Mhz.
However when I meassured the output frequency with a tune, it gave me around 20Mhz and I have absolutely no idea where I went wrong with this.

Balázs Sáros
  • 495
  • 6
  • 12

1 Answers1

2

You're definitely on the right track. That code should create a sawtooth wave with 31.25 hz base frequency. I can think of a couple things might be off.

One: your tuner might be hearing the wrong frequency; sawtooths are pretty loud in their higher frequencies, so your tuner might mis-identify and report hearing a higher tone. Try generating a sawtooth in Audacity of the right frequency and measure it with the same tuner. You might see the same result?

Two: Performance. If your for loops are running slow enough that it can't output at 8000 samples/second, likely aplay will still accept your data and play it, but it will play slower than you expect because the samples just aren't making it to your speakers quick enough. This is probably not what's happening - I think most computers are fast enough to pull this off, but I'm not sure.

Robert Mock
  • 332
  • 2
  • 11