How to print the values ​​of a line full of "chaos question marks",

I am debugging with python audio, having a hard time coding audio.

Here I have a line full of audio data, say, [10, 20, 100].

However, the data is stored in a string variable,

data = "                " 

I want to check the values ​​of this string.

Below are the things I tried

Print as int

I tried using print "%i" % data[0] ended up with

  Traceback (most recent call last): File "wire.py", line 28, in <module> print "%i" % data[i] TypeError: %d format: a number is required, not str 

Convert to int

int(data[0]) finished with

 Traceback (most recent call last): File "wire.py", line 27, in <module> print int(data[0]) ValueError: invalid literal for int() with base 10: '\xd1' 

Any idea on this? I want to print the string numerically since the string is actually an array of sound wave.

EDIT

All your answers have been really helpful.

The string is actually generated from the microphone, so I believe this is a raw waveform or vibration data. Further, this should be attributed to the audio API document, PortAudio .

Having studied PortAudio, I will find this useful example.

 ** This routine will be called by the PortAudio engine when audio is needed. ** It may called at interrupt level on some machines so don't do anything ** that could mess up the system like calling malloc() or free(). static int patestCallback( const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData ) { paTestData *data = (paTestData*)userData; float *out = (float*)outputBuffer; unsigned long i; (void) timeInfo; /* Prevent unused variable warnings. */ (void) statusFlags; (void) inputBuffer; for( i=0; i<framesPerBuffer; i++ ) { *out++ = data->sine[data->left_phase]; /* left */ *out++ = data->sine[data->right_phase]; /* right */ data->left_phase += 1; if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; data->right_phase += 3; /* higher pitch so we can distinguish left and right. */ if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; } return paContinue; } 

This indicates that I can interpret the data as a float

+2
source share
4 answers

To be clear, your audio data is a byte string. A byte string is a representation of the bytes stored in an audio file. You cannot simply convert these bytes to meaningful values ​​without knowing what is in binary format.

As an example, the mp3 specification states that each mp3 contains header frames (described here: http://en.wikipedia.org/wiki/MP3 ). To read the header, you either need to use something like bitstring , or if you are comfortable doing bitwise manipulation yourself, then you just need to unpack the integer (4 bytes) and do some math to figure out the values ​​of 32 individual bits.

In fact, it all depends on what you are trying to read, and how the data was generated. If you have integer byte numbers, the structure will serve you well.

+3
source

If you are ok with \xd1 mentioned above:

 for item in data: print repr(item), 

Note that for x in data will iterate over each value in the list, not its location. If you want to specify a location, you can use for i in range(len(data)): ...

If you want them digitally, replace repr(item) with ord(item) .

+1
source

Better use the new {}.format :

 data = "                " print '{0}'.format(data[3]) 
0
source

You can use ord to match each byte with its numeric value between 0-255:

 print map(ord, data) 

Or, for compatibility with Python 3, do:

 print([ord(c) for c in data]) 

It will also work with Unicode glyphs, which may not be what you want, so make sure Python 2 has a bytearray file or an actual str or bytes object.

0
source

All Articles