0

I am working on the following python code:

import wave
from bitstring import BitArray


w = wave.open('file.wav','rb')

totalFrames = w.getnframes()      #Total number of samples
bytesData = w.readframes(totalFrames)
binData = BitArray(bytesData)
bin2Data = (binData.bin)

The file.wav has 88200 samples at a sampling rate of 44.1KHz. My goal is to be able to get the 2's compliment of the binary data I obtain from file.wav. 'binData.bin' gives me the binary form of the bytes (\x00\x00N\x00n\xff..) obtained through w.readframes but in a string format.

I was using this to obtain 2'scompliment:

2comp = ~(bin2Data) + 0b1

but in vain. It would show the following error:

Traceback (most recent call last):
  File "speaker_bin.py", line 16, in <module>
    bin2Data = ~((binData.bin)) + 0b1
TypeError: bad operand type for unary ~: 'str'

I tried int(bin2Data) to convert it but it would not work (It would not print anything at all. I guess because of the size of the data.)

What am I doing wrong?

I would really appreciate any feedback. (even a simple nudge in the right direction)

Vasfed
  • 18,013
  • 10
  • 47
  • 53
cr0ssb0w
  • 9
  • 3
  • 1
    I think `binData.bin` returns a string, so you might need to convert it to integer before applying that operator on it. Something like `int(binData.bin, 2)` – smac89 Jun 30 '16 at 16:31
  • Related possible duplicate: http://stackoverflow.com/questions/17067813/convert-ascii-character-to-signed-8-bit-integer-python – Tom Myddeltyn Jun 30 '16 at 17:10

1 Answers1

0

You need to use

int(binData.bin, 2)

To create an int, you can specify the base as a second parameter, otherwise it will just assume the value is in base 10. As you can see from the docs, the default base is 10, which is why you need to specify a different base other than 10

Also do the same with 0b1

smac89
  • 39,374
  • 15
  • 132
  • 179
  • But doesn't the `0b` prefix imply that this is a binary number? –  Jun 30 '16 at 16:35
  • @LPK Yes it does, but the int class does not want deal with all variations of strings passed to it as that will be too expensive. The string can be a number in base 16, 24, 36, etc, therefore it needs that second parameter to determine the base of number you have given. This greatly speeds up the process of converting the number from the given base. Also not all bases have a prefix associated with them – smac89 Jun 30 '16 at 16:42
  • Thanks for your prompt response! but don't you think converting it into an `int` will render the 2's compliment statement kinda waste? The data will no longer be binary. – cr0ssb0w Jun 30 '16 at 17:09
  • @AkhileshRawat, you can always convert back to binary by calling `bin`. See [this question](http://stackoverflow.com/questions/10411085/converting-integer-to-binary-in-python) – smac89 Jun 30 '16 at 17:15
  • @khredos and with that I get back what I started with : a binary string!! The `~` operator would not again work. Shows this. `TypeError: bad operand type for unary ~: 'str'` – cr0ssb0w Jun 30 '16 at 17:29