0

At first,I am a python beginner. I need to read my clean.wav(cut to 50 slices to haha0~haha49 .wav) and noise.wav ,doing a snr [5,10,15]. Then output the before and after's waveform and Spectrogram. But,teriminal always tell me RuntimeError: Error opening 'D:\python\pythonnoisy\add_white\haha0_white_snr5.wav': System error. However, I can see this file in my folder.

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

  • The error seems to indicate that your input wav file is not compatible with the `wave` package, which causes your program to crash. Or perhaps the file is not readable. – 9769953 Aug 07 '21 at 12:47
  • sorry, I find my mistake. The path "add_white\\haha0......" can not appear "\\" . Thanks for your helping! – Alberta li Aug 07 '21 at 13:00

0 Answers0