0

I am writing a program for me to better understand the PyAudio module, in trying to understand how best to manipulate the audio stream, I convert the last 10 values of the first chunk into integers and compare it with the corresponding original bytes data, but the format and style of the byte stream don't make sense to me.

import pyaudio

p = pyaudio.PyAudio()
stream = p.open(44100,1,pyaudio.paInt16,True,frames_per_buffer=1024)

frames = []
arrNum = []
try:
    while True:
        data = stream.read(1024)
        frames.append(data)

except KeyboardInterrupt:
    pass

stream.stop_stream()
stream.close()
p.terminate()

for i in range (len(frames[0])-10,len(frames[0])):
    arrNum.append(frames[0][i])

print(frames[0][-10:])
print(arrNum)

I ran this code for 2 different sounds and got these outputs

b'y\x0b\xdc\x0b\xaf\x0b.\n"\t'
[121, 11, 220, 11, 175, 11, 46, 10, 34, 9]
b'\xfe\xff\r\x00\x0c\x00\x00\x00\xfe\xff'
[254, 255, 13, 0, 12, 0, 0, 0, 254, 255]

firstly, how does \r\ convert to 13 or \x0b.\ convert to 11 ect, this doesn't seem to be hex is it something else? secondly, the first byte output doesn't contain the tail end of numbers that the array contains, why is this ?

yib
  • 1

1 Answers1

0

Someone who's more knowledgeable can jump in and correct me, but seemingly, byte strings in Python will only display ASCII characters for bytes within the range 32 - 127. Anything below that range are non-printable ASCII characters, and anything above that is part of the extended ASCII table.

Anything outside this range therefore is presented as a hex-encoded byte.

firstly, how does \r\ convert to 13 or \x0b.\ convert to 11 ect, this doesn't seem to be hex is it something else?

Look at the ASCII table. The carriage return character \r maps to 13 (ord('\r') == 13).

\x0b is a hex-encoded byte. The value is 0x0b, unsurprisingly. In binary, this would be 0b0000_1011 (the first nybble 0 -> 0000, second nybble b -> 1011), which is 11 in base-10.

secondly, the first byte output doesn't contain the tail end of numbers that the array contains, why is this ?

Not sure what you mean. All your numbers are there:

y -> 121

\x0b -> 11

\xdc -> 220

\x0b -> 11

\xaf -> 175

\x0b -> 11

. -> 46

\n -> 10

" -> 34

\t -> 9

Again, look at the ASCII table.

Paul M.
  • 10,481
  • 2
  • 9
  • 15
  • my misunderstanding was with how ascii characters work. I thought they would all be in the hex form (\x[][]) I didn't realize ("y.) counted. should the special characters also be preceded by \ as in (\y) ? thanks – yib Mar 15 '23 at 17:12
  • '\' is the character that's used to escape characters, so it's only needed in this case for the hex-encoded bytes. If you're unfamiliar with this concept, I would look up "Python character escape sequences". – Paul M. Mar 15 '23 at 17:16