繁体   English   中英

如何将您在 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

任何帮助,将不胜感激

正如贾斯汀所说,您没有在任何地方分配 os.path.join 的返回值。 这将创建一条路径,但如果您不对其进行任何操作,则不会发生任何事情。

您必须使用.write() function 将文件写入 os.path.join 返回值。

这段代码应该可以工作。

# 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)将返回“C:\Users\core i5\Desktop\GitHub\DataSci\Data Analysis and Tools\Dataset\male_voices\son.wav”(例如)。 因此,通过编写此代码,您所做的只是构建一个字符串并在任何地方都不返回它。

您可以尝试的另一种技巧

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

这比其他方法长一点
但这也是您可以尝试的一种方式

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM