简体   繁体   中英

Python sounddevice module outputs Traceback (most recent call last):

Need help

What i want:

I want to record microphone and get the duration from a txt file

Code:

import sounddevice as sd
import numpy as np
import scipy.io.wavfile as wav
import soundfile as sf
fs=44100
dur = open("duration.txt", "r")
duration = dur.read()
print (duration)
myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
print ("Recording Audio")
sd.wait()
sf.write('myfile.wav', myrecording, fs, subtype='PCM_24')

duration.txt contains just a 7

Output:

7
Traceback (most recent call last):
  File "audio.py", line 9, in <module>
    myrecording = sd.rec(duration * fs, samplerate=fs, channels=2,dtype='float64')
  File "C:\Users\User\Desktop\portfowardtesting\python\lib\site-packages\sounddevice.py", line 271, in rec
    ctx.frames = ctx.check_out(out, frames, channels, dtype, mapping)
  File "C:\Users\User\Desktop\portfowardtesting\python\lib\site-packages\sounddevice.py", line 2437, in check_out
    out = np.empty((frames, channels), dtype, order='C')
TypeError: 'str' object cannot be interpreted as an integer

You should try to do print(duration * fs) , this might give you a hint.

The problem is that the duration you read from the file is a string, you should first convert it to a number.

In other words:

duration = int(dur.read())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM