Archived
195 lines
6.0 KiB
Python
195 lines
6.0 KiB
Python
import numpy as np
|
|
import importlib
|
|
import os
|
|
import soundfile as sf
|
|
import sounddevice as sd
|
|
import scipy.signal as sis
|
|
from picka_definition import main as pickaDef
|
|
from tools.to_noise import noise
|
|
from gender.cosgate import cosgate
|
|
from tools.load_matrix import loadmat
|
|
from scipy.io import savemat
|
|
from Robot.Robot import Robot
|
|
from naoqi import ALBroker
|
|
from tools.exit_condition import ExitExperimentModule
|
|
|
|
|
|
def main(language, game, att, interface):
|
|
# TODO Add interface part
|
|
# gain_field_colour = np.array([6.3, 74.5, 41.6]) / 100 * .4 + .6
|
|
|
|
calibration = {}
|
|
att = float(att)
|
|
|
|
if not os.path.exists('./calibration.mat'):
|
|
calibration["fishy"] = 0
|
|
calibration["gender"] = 0
|
|
calibration["emotion"] = 0
|
|
calibration["crm"] = 0
|
|
else:
|
|
mat = loadmat('./calibration.mat')
|
|
calibration = mat["calibration"]
|
|
|
|
options = {}
|
|
options["subject_name"] = ""
|
|
options["language"] = language
|
|
|
|
if game == "fishy":
|
|
folder = "./fishy"
|
|
fx_prefix = "expe"
|
|
calibration["fishy"] = att
|
|
elif game == "gender":
|
|
folder = "./gender"
|
|
fx_prefix = "gender"
|
|
options["phase"] = "generation"
|
|
options["language"] = language
|
|
calibration["gender"] = att
|
|
elif game == "emotion":
|
|
folder = "./emotion"
|
|
fx_prefix = "emotion"
|
|
calibration["emotion"] = att
|
|
elif game == "crm":
|
|
folder = "./crm"
|
|
fx_prefix = "crm"
|
|
calibration["crm"] = att
|
|
|
|
scripts = []
|
|
PICKA = pickaDef()
|
|
|
|
for i in range(len(PICKA)):
|
|
scripts.append(os.path.join(
|
|
PICKA["def" + str(i)]["folder"], PICKA["def" + str(i)]["prefix"]))
|
|
|
|
length_sample = 30
|
|
spectrum_max = -np.inf
|
|
|
|
experiments = {}
|
|
|
|
# for i in range(len(scripts)):
|
|
# script = scripts[i]
|
|
# folder, fx_prefix = os.path.split(script)
|
|
experiments["scr" + str(i)] = {}
|
|
experiments["scr" + str(i)]["folder"] = folder
|
|
experiments["scr" + str(i)]["fx_prefix"] = fx_prefix
|
|
|
|
# os.chdir(folder)
|
|
# print os.getcwd()
|
|
# try:
|
|
|
|
package = folder.split("/")[1]
|
|
fh_options = importlib.import_module(
|
|
"..{}_options".format(fx_prefix), package="{}.{}_options".format(package, fx_prefix))
|
|
fh_build = importlib.import_module(
|
|
"..{}_build_conditions".format(fx_prefix), package="{}.{}_build_conditions".format(package, fx_prefix))
|
|
|
|
experiments["scr" + str(i)]["fh_options"] = fh_options
|
|
experiments["scr" + str(i)]["fh_build"] = fh_build
|
|
|
|
options = fh_options.expe_options(options)
|
|
_, options = fh_build.expe_build_conditions(options)
|
|
|
|
if not "sounds_for_calibration" in options:
|
|
raise ValueError(
|
|
"Options does not contain sounds_for_calibration")
|
|
|
|
if len(options["sounds_for_calibration"]) == 0:
|
|
raise ValueError(
|
|
"There are no sounds for calibration. Try preprocessing first.")
|
|
|
|
print "Checking if RMS of calibration sounds are equal."
|
|
|
|
x = np.array([])
|
|
|
|
RMSs = np.zeros(len(options["sounds_for_calibration"]))
|
|
|
|
previous_fs = []
|
|
|
|
for k in range(len(options["sounds_for_calibration"])):
|
|
y, fs = sf.read(options["sounds_for_calibration"][k])
|
|
|
|
if previous_fs and previous_fs != fs:
|
|
raise ValueError("File {} does not have the same sampling frequency ({} Hz) as the previous one ({} Hz).".format(
|
|
options["sounds_for_calibration"][k], fs, previous_fs))
|
|
previous_fs = fs
|
|
|
|
RMSs[k] = np.sqrt(np.mean(y**2))
|
|
x = np.hstack((x, y))
|
|
|
|
mRMS = np.mean(RMSs)
|
|
|
|
print "RMS: min=%.3f, avg=%.3f, max=%.3f" % (
|
|
np.min(RMSs), mRMS, np.max(RMSs))
|
|
|
|
print "The sampling frequency is %d Hz." % fs
|
|
print "Converting to noise..."
|
|
print np.size(x)
|
|
|
|
noisy_x = noise(x, fs)
|
|
|
|
if len(noisy_x) < fs*length_sample:
|
|
noisy_x = np.tile(noisy_x, int(np.ceil(
|
|
fs*length_sample/len(noisy_x))))
|
|
|
|
noisy_x = noisy_x[:np.min(len(noisy_x))]
|
|
|
|
NOISY_X = 20 * np.log10(np.abs(np.fft.fft(noisy_x)) /
|
|
np.sqrt(len(noisy_x)))
|
|
|
|
w = sis.hann(256)
|
|
w = w / np.sum(w)
|
|
|
|
NOISY_X = np.convolve(NOISY_X, w, mode='same')
|
|
|
|
f = (np.arange(len(noisy_x)))/(len(noisy_x)) * fs
|
|
|
|
spectrum_max = np.max(np.array([spectrum_max, int(max(NOISY_X))]))
|
|
|
|
experiments["scr" + str(i)]["fs"] = fs
|
|
experiments["scr" + str(i)]["noise"] = cosgate(noisy_x, fs, 50e-3)
|
|
|
|
experiments["scr" + str(i)]["noise"] = experiments["scr" +
|
|
str(i)]["noise"] * 10.0 ** att/20
|
|
|
|
# experiments["scr" + str(i)]["gain"] = fh_gain
|
|
if interface == "laptop":
|
|
sd.play(experiments["scr" + str(i)]["noise"], fs)
|
|
elif interface.lower() == "sam":
|
|
|
|
sam = Robot("sam.local", 9559, language)
|
|
sam.aad.setOutputVolume(sam.stimulus_volume)
|
|
sf.write("calibration_{}.wav".format(game),
|
|
experiments["scr" + str(i)]["noise"], fs)
|
|
sam.play_audio("calibration_{}.wav".format(game))
|
|
|
|
r = raw_input("Save this attenuation value?: ")
|
|
|
|
if r.lower() == "y":
|
|
savemat('./calibration.mat', mdict={"calibration": calibration})
|
|
elif r.lower() == 'n':
|
|
pass
|
|
else:
|
|
raise ValueError("That is not a valid option.")
|
|
|
|
print "done"
|
|
|
|
# except:
|
|
# raise Exception("Error")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import argparse
|
|
myBroker = ALBroker("myBroker", "0.0.0.0", 0, "sam.local", 9559)
|
|
stop_audio = ExitExperimentModule("stop_audio", myBroker)
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument(
|
|
"--language", "-l", help="Language of sound files to calibrate.", choices=["EN", "NL"])
|
|
parser.add_argument("--game", "-g", help="Game for calibration",
|
|
choices=["fishy", "gender", "emotion", "crm"])
|
|
parser.add_argument("--attenuation", "-a",
|
|
help="Attenuation to be applied.")
|
|
parser.add_argument(
|
|
"--interface", "-i", help="Interface that calibration must be perfored for.", choices=["sam", "laptop"])
|
|
args = parser.parse_args()
|
|
main(args.language, args.game, args.attenuation, args.interface)
|