Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将wav音频文件格式(采样宽度)转换为8位格式?_Python_Audio_Pyaudio_Wave_Playsound - Fatal编程技术网

Python 如何将wav音频文件格式(采样宽度)转换为8位格式?

Python 如何将wav音频文件格式(采样宽度)转换为8位格式?,python,audio,pyaudio,wave,playsound,Python,Audio,Pyaudio,Wave,Playsound,我正在使用一个记录语音的python gui,我需要将wav文件的宽度格式设置为8位。当我运行录音时,只有噪音 import tkinter as tk import threading import pyaudio import wave from tkinter import * import tkinter.font as font from tkinter.filedialog import asksaveasfilename class App(): chunk = 102

我正在使用一个记录语音的python gui,我需要将wav文件的宽度格式设置为8位。当我运行录音时,只有噪音

import tkinter as tk
import threading
import pyaudio
import wave
from tkinter import *
import tkinter.font as font
from tkinter.filedialog import asksaveasfilename


class App():
    chunk = 1024
    sample_format = pyaudio.paUInt8
    channels = 2
    fs =44100 

frames = []

def __init__(self, master):
    self.isrecording = False
    myFont = font.Font(weight="bold")
    self.button1 = tk.Button(audio_window, text='Record', command=self.startrecording,
                             height=2, width=20, bg='#0052cc', fg='#ffffff')
    self.button2 = tk.Button(audio_window, text='stop', command=self.stoprecording,
                             height=2, width=20, bg='#0052cc', fg='#ffffff')
    self.button1['font'] = myFont
    self.button2['font'] = myFont
    self.button1.place(x=30, y=30)
    self.button2.place(x=280, y=30)

def startrecording(self):
    self.p = pyaudio.PyAudio()
    self.stream = self.p.open(format=self.sample_format, channels=self.channels,
        rate=self.fs, frames_per_buffer=self.chunk, input=True)
    self.isrecording = True

    print('Recording')
    t = threading.Thread(target=self.record)
    t.start()

def stoprecording(self):
    self.isrecording = False
    print('recording complete')

    self.filename = asksaveasfilename(initialdir="/", title="Save as",
        filetypes=(("audio file", "*.wav"), ("all files", "*.*")),
        defaultextension=".wav")

    wf = wave.open(self.filename, 'wb')
    wf.setnchannels(self.channels)
    wf.setsampwidth(self.p.get_sample_size(self.sample_format))
    wf.setframerate(self.fs)
    wf.writeframes(b''.join(self.frames))


def record(self):
    while self.isrecording:
        data = self.stream.read(self.chunk)
        self.frames.append(data)
        print("does it")

audio_window= tk.Tk()
audio_window.title('recorder')
app=App(audio_window)
audio_window.geometry('520x120')
audio_window.mainloop()
这是我使用的录制GUI。救命!!我需要8位的录音,因为我以后需要对其进行加密

您可以这样使用:

import librosa  # just to demo, not necessary, as you already have the data
import soundfile

# read some wave file, so that y is the date and sr the sample rate
y, sr = librosa.load('some.wav')

# write to a new wave file with sample rate sr and format 'unsigned 8bit'
soundfile.write('your.wav', y, sr, subtype='PCM_U8')
要获取特定格式的可用子类型,请使用:

>>> soundfile.available_subtypes('WAV')
{'PCM_16': 'Signed 16 bit PCM', 'PCM_24': 'Signed 24 bit PCM', 'PCM_32': 'Signed 32 bit PCM', 'PCM_U8': 'Unsigned 8 bit PCM', 'FLOAT': '32 bit float', 'DOUBLE': '64 bit float', 'ULAW': 'U-Law', 'ALAW': 'A-Law', 'IMA_ADPCM': 'IMA ADPCM', 'MS_ADPCM': 'Microsoft ADPCM', 'GSM610': 'GSM 6.10', 'G721_32': '32kbs G721 ADPCM'}

您的问题与
tkinter
无关,因此您应该提供一个不使用它的问题。答案很可能与使用
pyaudio
wave
模块有关。首先尝试在不使用
tkinter
线程的情况下执行,因为它们与您的问题无关。你们将有最少的工作代码可以在新问题中显示,人们可能会尝试运行它来解决问题。顺便说一句:你们不能在开始时使用
pyaudio.paInt8
?谢谢大家。。我是一个新用户。我的问题有什么想法吗?@furas我试过了,但是录音变成了噪音。请将你使用的代码添加到你的原始问题中,以便它可读。它会抖动吗?是的@pipll我添加了我在question@bipll好的,Python声音文件是基于的,它看起来没有抖动。但我可能错了。