简体   繁体   English

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

[英]How to convert a wav audio file format (sample width) into 8 bits format?

I am using a Python gui that records the voice and I need to set the Width format of the wav file into 8 bits.我正在使用 Python 录制语音的 gui,我需要将 wav 文件的宽度格式设置为 8 位。 When I run the recording has nothing but noise!当我运行录音时,除了噪音什么都没有!

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()

This is the recording GUI I used.这是我使用的录制 GUI。 I need the recording to be in 8 bits because I need to encrypt it later.我需要录音是 8 位的,因为我需要稍后对其进行加密。

You can use soundfile.write() like this:您可以像这样使用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')

To get available subtypes for a certain format, use:要获取特定格式的可用子类型,请使用:

>>> 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'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中将音频文件(wav格式)拼接成1秒拼接? - How to splice an audio file (wav format) into 1 sec splices in python? 如何在不写入文件的情况下将pyaudio帧转换为wav格式? - How to convert pyaudio frames into wav format without writing to a file? 如何将wav文件的内容放入数组,以便剪切所需的段并将其转换为使用python的wav格式? - How to get the contents of the wav file into array so as to cut the required segment and convert it back to wav format using python? Python中如何将base64格式的音频文件转换成.wav文件而不存储在当前目录下? - How to convert base64-format audio files into .wav files without storage them on current directory in Python? 如何在 Linux 环境下解码 Silk 文件并将其转换为 PCM/WAV 格式 - How do I decode a silk file in a Linux environment and convert it to a PCM / WAV format 如何从梅尔频谱图转换 wav(音频)文件? - How to convert wav (audio) file from mel spectrogram? 如何获取numpy arrays output的.wav文件格式 - How to get numpy arrays output of .wav file format 如何转换。 按关键字转换为不同格式的示例字典? - How to convert. a sample dictionary to a different format by key word? 如何使用 Python 将音频文件(.mp3 或 .wav 或任何其他文件)转换为唯一的音频 ID? - How to convert an audio file (.mp3 or .wav or any other) to an unique audio id using Python? 如何在WAV文件中查找和绘制最大样本 - How to find and plot the largest sample in a wav file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM