How do hex escapes correlate with audio in PyAudio?

359 Views Asked by At

I'm playing around with PyAudio in an attempt to make some microtonal music, but can't seem to get off the ground. I found this bit of code on using core audio in-program: http://code.activestate.com/recipes/578301-platform-independent-1khz-pure-audio-sinewave-gene/ And I've made some small adjustments:

import time
import pyaudio

# Open the stream
stream=pyaudio.PyAudio().open(format=pyaudio.paInt8,channels=1,rate=24000,output=True)

# Amount of time for wave to be played in seconds
length = 1

# Now generate the 1KHz signal at the speakers/headphone output
# Sine wave, to 8 bit depth
start = time.clock()
while time.clock() - start <= length:
    stream.write("\x00\x30\x5a\x76\x7f\x76\x5a\x30\x00\xd0\xa6\x8a\x80\x8a\xa6\xd0")

# Close the open _channel(s)_
stream.close()
pyaudio.PyAudio().terminate()

From what I understand, the hex escapes are the raw audio being sent to the audio output. I've put them into a hex converter and graphed their values. It certainly looks like a sin wave, but I'm still no closer to an intuitive understanding of what's going on. My eventual goal will be to write a function that can output any given frequency in a series of hex escapes to be written to the stream. How many samples are contained within one bytestring and what values do they represent (ie. what values express the crest, trough, and resting state of a sine wave at full volume)?

0

There are 0 best solutions below