-2

The error message I am getting is:

Traceback (most recent call last):
  File "C:\Users\dabea\Downloads\3DPrintedRecord-master\wavtotext.py", line 29, in <module>
    frameInt = list(map(ord, list(frame)))#turn into array
TypeError: ord() expected string of length 1, but int found

The code is in Python but was written in 2012 so I think that's why I'm having so many issues.

import wave
import math
import struct

bitDepth = 8#target bitDepth
frate = 44100#target frame rate

fileName = "C:/Users/dabea/desktop/Spendtime Palace __ Sonora (OFFICIAL VIDEO) (320 kbps).wav"#file to be imported (change this)

#read file and get data
w = wave.open(fileName, 'r')
numframes = w.getnframes()

frame = w.readframes(numframes)#w.getnframes()

frameInt = list(map(ord, list(frame)))#turn into array

#separate left and right channels and merge bytes
frameOneChannel = [0]*numframes#initialize list of one channel of wave
for i in range(numframes):
    frameOneChannel[i] = frameInt[4*i+1]*2**8+frameInt[4*i]#separate channels and store one channel in new list
    if frameOneChannel[i] > 2**15:
        frameOneChannel[i] = (frameOneChannel[i]-2**16)
    elif frameOneChannel[i] == 2**15:
        frameOneChannel[i] = 0
    else:
        frameOneChannel[i] = frameOneChannel[i]

#convert to string
audioStr = ''
for i in range(numframes):
    audioStr += str(frameOneChannel[i])
    audioStr += ","#separate elements with comma

fileName = fileName[:-3]#remove .wav extension
text_file = open(fileName+"txt", "w")
text_file.write("%s"%audioStr)
text_file.close()
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
helpmelol
  • 1
  • 1
  • looks like you are trying to convert to int, since you are storing the results in a variable called frameInt. Try changing `ord` for `int` –  Jul 20 '22 at 09:02

1 Answers1

1

You are bitten by the Python 2 to 3 transition. readframes() returns a bytes object in Python 3, whereas in Python 2, it returns a string. Since the ord function expects a (1-character) string, and not a bytes object, it fails at that point in Python 3. That is the error you are seeing.

Try using

frameInt = list(map(ord, frame.decode('utf-8')))

or per Sembei Norimaki's comment

frameInt = list(map(int, frame))

instead. (The latter may be even better, since a .wav file is data, not strings, so decoding bytes to a string doesn't really make sense.) The result of both operations should be the same.

In fact, I think you can simply do

frameInt = list(frame)

or even

frameInt = frame

because a bytes object is effectively already a list of integers, and thus a conversion is not needed.

Here's what the manual says about it:

While bytes literals and representations are based on ASCII text, bytes objects actually behave like immutable sequences of integers, with each value in the sequence restricted such that 0 <= x < 256

I'm not saying this is the solution, but is a solution, which may be a proper work-around (I don't know the further context) or may be a problem later (but doesn't look like it will be).

9769953
  • 10,344
  • 3
  • 26
  • 37