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

问题描述 投票:1回答:1

我正在使用记录声音的python gui,我需要将wav文件的Width格式设置为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位,因为我以后需要对其进行加密

python audio record pyaudio wave
1个回答
0
投票

您可以像这样使用soundfile.write()

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'}
© www.soinside.com 2019 - 2024. All rights reserved.