I made a program that's supposed to record people in my household talking for 1 minute. I think my code has successfully (though messily) been able to save the *.wav file and classifying the recording on gender. The male recordings are supposed to be saved in the male_voices folder and the female recording are supposed to be saved in the female_voices folder.
My question is: I have searched and couldn't seem to find a way to save these recordings to a specific file path. As you can see I tried using
os.join(path, "son.wav")
but it doesn't work.
My code is as follows:
# 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
Any help would be appreciated
As Justin said you aren't assigning the return value of os.path.join anywhere. This will create a path, but if you aren't doing anything with it, nothing happens.
You have to use the .write()
function to write the file to the os.path.join return value.
This code should work.
# 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)
would return "C:\Users\core i5\Desktop\GitHub\DataSci\Data Analysis and Tools\Dataset\male_voices\son.wav" (for example). So by writing this code, all you're doing is building a string essentially and returning it no where.
One more trick you can try
Import os
curr_dir = os.getcwd()
os.chdir(path)
# perform the write operation with path as filename
os.chdir(curr_dir)
This is a bit longer than other methods
But this is also a way you can try
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.