如何将您在 python 中创建的 .wav 文件保存到指定目录?

How to save a .wav file you created in python to a specify directory?

我制作了一个程序,可以记录我家里的人谈话 1 分钟。我认为我的代码已经成功(虽然很乱)能够保存 *.wav 文件并对录音进行性别分类。男性录音应该保存在male_voices文件夹中,女性录音应该保存在female_voices文件夹中。

我的问题是:我已经搜索过但似乎无法找到将这些录音保存到特定文件路径的方法。如您所见,我尝试使用

os.join(path, "son.wav")

但是没用。

我的代码如下:

# import required libraries
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv
import os
male_members ={'s':'son', 'b':'brother', 'u':'uncle', 'f':'father', 'n':'nephew', 'mc':'male cousin', 'o':'other'}
female_members ={'d':'daughter', 's':'sister', 'a':'aunt', 'm':'mother', 'n':'niece', 'fc':'female cousin', 'o':'other'}
# Sampling frequency
freq = 44100

# Recording duration
duration = 60

while True:
    user = str(input("Do you want to record a female [f] or male [m] voice or exit[e]? "))
    if user.lower() == 'm':
        path = r"C:\Users\core i5\Desktop\GitHub\DataSci\Data Analysis and Tools\Dataset\male_voices"
        male = str(input("Are you recording your son[s], brother[b], uncle[u], father[f], nephew[n], male cousin[mc], or other[o]? "))
        recording = sd.rec(int(duration * freq), samplerate=freq, channels=2)
        sd.wait(60)
        sound_name = f"{male_members[f'{male}']}.wav"
        wv.write(sound_name, recording, freq, sampwidth=2)
        os.path.join(path, sound_name)

    elif user.lower() == 'f':
        path = r"C:\Users\core i5\Desktop\GitHub\DataSci\Data Analysis and Tools\Dataset\female_voices"
        female = str(input("Are you recording your daughter[d], sister[s], aunt[a], mother[m], niece[n], female cousin[fc], or other[o]? "))
        recording = sd.rec(int(duration * freq), samplerate=freq, channels=2)
        sd.wait(60)
        sound_name = f"{female_members[f'{female}']}.wav"
        wv.write(sound_name, recording, freq, sampwidth=2)
        os.path.join(path, sound_name)

    elif user.lower() == 'e':
        print("exiting program....")
        break

    else:
        print("Unrecognized command. Try again\n")
        continue

如有任何帮助,我们将不胜感激

正如 Justin 所说,您没有在任何地方分配 os.path.join 的 return 值。这将创建一条路径,但如果您不对其进行任何操作,则什么也不会发生。

您必须使用 .write() 函数将文件写入 os.path.join return 值。

此代码应该有效。

# import required libraries
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv
import os
male_members ={'s':'son', 'b':'brother', 'u':'uncle', 'f':'father', 'n':'nephew', 'mc':'male cousin', 'o':'other'}
female_members ={'d':'daughter', 's':'sister', 'a':'aunt', 'm':'mother', 'n':'niece', 'fc':'female cousin', 'o':'other'}
# Sampling frequency
freq = 44100

# Recording duration
duration = 60

while True:
    user = str(input("Do you want to record a female [f] or male [m] voice or exit[e]? "))
    if user.lower() == 'm':
        path = r"C:\Users\core i5\Desktop\GitHub\DataSci\Data Analysis and Tools\Dataset\male_voices"
        male = str(input("Are you recording your son[s], brother[b], uncle[u], father[f], nephew[n], male cousin[mc], or other[o]? "))
        recording = sd.rec(int(duration * freq), samplerate=freq, channels=2)
        sd.wait(60)
        sound_name = f"{male_members[f'{male}']}.wav"
        wv.write(os.path.join(path, sound_name), recording, freq, sampwidth=2)
        
    elif user.lower() == 'f':
        path = r"C:\Users\core i5\Desktop\GitHub\DataSci\Data Analysis and Tools\Dataset\female_voices"
        female = str(input("Are you recording your daughter[d], sister[s], aunt[a], mother[m], niece[n], female cousin[fc], or other[o]? "))
        recording = sd.rec(int(duration * freq), samplerate=freq, channels=2)
        sd.wait(60)
        sound_name = f"{female_members[f'{female}']}.wav"
        wv.write(os.path.join(path, sound_name), recording, freq, sampwidth=2)

    elif user.lower() == 'e':
        print("exiting program....")
        break

    else:
        print("Unrecognized command. Try again\n")
        continue

os.path.join(path, sound_name) 会 return “C:\Users\core i5\Desktop\GitHub\DataSci\Data 解析和Tools\Dataset\male_voices\son.wav”(例如)。因此,通过编写这段代码,您所做的只是本质上构建一个字符串,然后 return 将其放置在任何地方。

你还可以尝试一个技巧

Import os
curr_dir = os.getcwd()
os.chdir(path)
# perform the write operation with path as filename
os.chdir(curr_dir)

这比其他方法要长一些
不过这也是你可以尝试的方法