读取wav文件时出现运行时错误、打开错误、系统错误

2024-10-02 02:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

起初,我是python初学者。我需要阅读我的clean.wav(剪切到50个片段,以获得haha0~haa49.wav)和noise.wav,做一个snr[5,10,15]。然后输出前后的波形和频谱图。但是,Terminal总是告诉我RuntimeError:打开“D:\python\pythonnoisy\add\u white\hahaha0\u white\u snr5.wav”时出错:系统错误。但是,我可以在我的文件夹中看到此文件

import os
import re
import sys
import wave
import librosa
import matplotlib
import numpy as np
import pylab as pl
import soundfile as sf
import matplotlib.pyplot as plt
from scipy.fftpack import fft

wavedir = r"D:\python"
noisydir = wavedir+"\\pythonnoisy"
noisedir = wavedir+"\\pythonnoise"
cleandir = wavedir+"\\pythonclean"


def add_noise(noisydir, noisedir, cleandir, snr):  # noisy
    noisewav = "white.wav"
    noise, fs = sf.read(noisedir+"\\"+noisewav)  # 讀取白雜訊.wav
    noisy_splitdir = noisydir+"\\"+"add_"+noisewav[:-4]+"\\"
    # 迴圈取原始wav檔資料夾裡所有檔案
    for cleanwav in os.listdir(cleandir):
        clean, Fs = sf.read(cleandir+"\\"+cleanwav)  # 讀取原始.wav檔
        # 取樣頻率:原始音檔==白雜訊&&時長:原始音檔<白雜訊
        if fs == Fs and len(clean) <= len(noise):
            cleanenergy = np.sum(np.power(clean, 2))  # 原始音檔Power(=振幅^2)
            # 1<隨機生成長度<noise長-clean長+1
            ind = np.random.randint(1, len(noise) - len(clean) + 1)
            noiselen = noise[ind:len(clean) + ind]
            noiseenergy = np.sum(np.power(noiselen, 2))  # 白雜訊Power
            ratio2 = np.sqrt(
                (cleanenergy / noiseenergy) / (np.power(10, snr * 0.1)))
            noisyAudio = clean + noiselen * ratio2  # 混音振幅
            # 混音路徑+檔名
            noisywavname = noisy_splitdir + \
                cleanwav[:-4]+"_"+noisewav[:-4]+"_snr"+str(snr)+".wav"
            sf.write(noisywavname, noisyAudio, 44100)  # 生成混音.wav


def draw_clean(cleandir, j):
    f = wave.open(
        r"D:\python\pythonclean\haha" + str(j) + ".wav", "rb")
    params = f.getparams()
    nchannels, sampwidth, framerate, nframes = params[:4]
    str_data = f.readframes(nframes)
    f.close()
    wave_data = np.fromstring(str_data, dtype=np.short)
    wave_data.shape = -1, 2
    wave_data = wave_data.T
    time = np.arange(0, nframes) * (1.0 / framerate)
    plt.subplot(4, 2, 7)
    plt.plot(time, wave_data[0])
    plt.xlabel("time(s)")
    plt.subplot(4, 2, 8)
    plt.specgram(wave_data[0], Fs=framerate)
    plt.xlabel("time(s)")


def draw_noisy(noisydir, j):
    noisydirr = noisydir+"\\add_white\\haha"
    for k in range(5, 20, 5):
        f = wave.open(r"D:\python\pythonnoisy\add_white\haha" +
                      str(j)+"_white_snr"+str(k)+".wav", "rb")
        params = f.getparams()
        nchannels, sampwidth, framerate, nframes = params[:4]
        str_data = f.readframes(nframes)
        wave_data = np.fromstring(str_data, dtype=np.short)
        wave_data.shape = -1, 2
        wave_data = wave_data.T
        time = np.arange(0, nframes) * (1.0 / framerate)
        plt.subplot(4, 2, k/5*2-1)
        plt.plot(time, wave_data[0])
        plt.subplot(4, 2, k/5*2)
        plt.specgram(wave_data[0], Fs=framerate)


wavedir = r"D:\python"
noisydir = wavedir+"\\pythonnoisy"
noisedir = wavedir+"\\pythonnoise"
cleandir = wavedir+"\\pythonclean"

level = [5, 10, 15]
for snr in level:
    add_noise(noisydir, noisedir, cleandir, snr)

for j in range(0, 51):
    draw_clean(cleandir, j)
    draw_noisy(noisydir, j)
    picture = noisydir+"\picture\hahawhite"+str(j)+".png"
    plt.savefig(picture)
    plt.close()

file location

terminal


Tags: importcleanadddatanppltwavewhite

热门问题