import numpy as np import os import wave import math from scipy.io import savemat import warnings from gender_options import expe_options from tools.load_matrix import loadmat def expe_build_conditions(options): # Build Signal conditions options["fs"] = 44100 # options["attenuation_dB"] = 7 mat = loadmat('./calibration.mat') cal = mat["calibration"] options["attenuation_dB"] = cal["gender"] # options["attenuation_dB"] = 10 # Calibrated attenuation options["ear"] = "both" # Design specifications options["test"] = {} options["training"] = {} options["test"]["n_repeat"] = 1 options["training"]["n_repeat"] = 1 word_list = [] if options["Bert"]: options["test"]["f0s"] = np.array([0.0, -3.0, -6.0, -9.0, -12.0]) options["test"]["vtls"] = np.array([0, 0.7, 1.6, 2.4, 3.0, 3.6]) else: options["test"]["f0s"] = np.array([0.0, -6.0, -12.0]) options["test"]["vtls"] = np.array([0, 1.8, 3.6]) options["training"]["f0s"] = np.array([0.0, -12.0]) options["training"]["vtls"] = np.array([0.0, 3.6]) nF0 = len(options["test"]["f0s"]) nVTL = len(options["test"]["vtls"]) if options["phase"] == "generation": word_list = ["Bus", "Leeg", "Pen", "Vaak"] elif options["phase"] == "test" or options["phase"] == "training": if options["language"].lower() == "nl": word_list = ["Bus", "Leeg", "Pen", "Vaak"] elif options["language"].lower() == "en": word_list = ["bike", "hat", "pool", "watch"] options["test"]["word_list"] = word_list options["training"]["word_list"] = word_list nWords = len(word_list) options["sound_path_local"] = os.path.join( options["sound_path"], options["language"]) options["tmp_path_local"] = options["tmp_path"] options["sounds_for_calibration"] = [] lst = [f for f in os.listdir( options["tmp_path_local"]) if f.endswith(".wav")] for f in range(len(lst)): options["sounds_for_calibration"].append( os.path.join(options["tmp_path_local"], lst[f])) options["test"]["total_ntrials"] = nWords * nVTL * nF0 # oneSampleFile = os.path.join(options["tmp_path"], "{}_GPR{}_VTL{}.wav".format(word_list[0], # options["test"]["f0s"][0], options["test"]["vtls"][0])) # options["training"]["total_ntrials"] = len( # word_list) * len(options["training"]["f0s"]) * len(options["training"]["vtls"]) options["training"]["total_ntrials"] = 7 # if os.path.exists(oneSampleFile): # w = wave.open(oneSampleFile, 'r') # frames = w.getnframes() # rate = w.getframerate() # duration = frames/float(rate) # options["word_duration"] = duration # else: # options["word_duration"] = 850e-3 options["lowpass"] = 4000 options["force_rebuild_sylls"] = 0 # Build test block # options["test"]["faces"] = np.array(["woman_1", "woman_2", "woman_3", "woman_4", "woman_5", # "woman_6", "woman_7", "man_1", "man_2", "man_3", "man_4", # "man_5", "man_6", "man_7"]) options["test"]["face_genders"] = ['man', 'woman'] options["test"]["number_faces"] = 7 options["training"]["face_genders"] = options["test"]["face_genders"] options["training"]["number_faces"] = options["test"]["number_faces"] # nFaces = len(options["test"]["faces"]) # nRepetitions = math.ceil(options["test"]["total_ntrials"]/nFaces) # nFemales = sum([True for s in options["test"] # ["faces"] if s.startswith("woman")]) # nMales = nFaces - nFemales # indexes = np.tile(range(1, nFemales), int(nRepetitions)) # np.random.shuffle(indexes) # indexes = np.delete( # indexes, np.s_[options["test"]["total_ntrials"]/2:], None) # indexesM = np.tile(range(1+nFemales, nMales+nFemales), int(nRepetitions)) # np.random.shuffle(indexesM) # indexesM = np.delete( # indexesM, np.s_[options["test"]["total_ntrials"]/2:], None) # indexes = [indexes, indexesM] # np.random.shuffle(indexes) # np.random.shuffle(options["test"]["faces"]) # options["test"]["faces"] = options["test"]["faces"][indexes] # TODO test Hands test = {} counter = 1 for ir in range(options["test"]["n_repeat"]): for f0 in range(nF0): for vtl in range(nVTL): i_words = np.random.permutation(nWords) for i_face_gender in range(2): face_gender = options["test"]["face_genders"][i_face_gender] i_word_start = i_face_gender * nWords / 2 + 1 for i_word in np.arange(nWords/2) - 1+i_word_start: trial = {} trial["f0"] = options["test"]["f0s"][f0] trial["vtl"] = options["test"]["vtls"][vtl] trial["word"] = options["test"]["word_list"][i_words[i_word]] trial["i_repeat"] = ir trial["done"] = 0 trial["face"] = '{}_{}'.format( face_gender, np.random.randint(options["test"]["number_faces"])) trial["face_gender"] = face_gender if not "trials" in test: test["trials"] = [trial] else: test["trials"].append(trial) training = {} nWords = len(options["training"]["word_list"]) for ir in range(options["training"]["n_repeat"]): for i_v in range(len(options["training"]["f0s"])): i_f0 = i_v i_vtl = i_v i_words = np.random.permutation(nWords) for i_face_gender in range(2): face_gender = options["training"]["face_genders"][i_face_gender] i_word_start = i_face_gender*nWords/2+1 for i_word in np.arange(nWords/2) - 1+i_word_start: trial = {} trial["f0"] = options["training"]["f0s"][i_f0] trial["vtl"] = options["training"]["vtls"][i_vtl] trial["word"] = options["training"]["word_list"][i_words[i_word]] trial["i_repeat"] = ir trial["done"] = 0 trial["face"] = '{}_{}'.format( face_gender, np.random.randint(options["test"]["number_faces"])) trial["face_gender"] = face_gender if not "trials" in training: training["trials"] = [trial] else: training["trials"].append(trial) # Create expe dictionary and save expe = {} if options["phase"] == "generation": expe["test"] = {} expe["test"]["trials"] = test["trials"] else: expe["test"] = {} expe["training"] = {} i_order = np.random.permutation(len(test["trials"])) expe["test"]["trials"] = [] for i in i_order: expe["test"]["trials"].append(test["trials"][i]) i_order = np.random.permutation(len(training["trials"])) expe["training"]["trials"] = [] for i in i_order: expe["training"]["trials"].append(training["trials"][i]) if "res_filename" in options: savemat(options["res_filename"], mdict={ "expe": expe, "options": options}) else: warnings.warn("The test file was not saved: no filename provided") return expe, options if __name__ == "__main__": options = expe_options() buildConditions(options)