# -------------------------------------------------------------------------- # Authors: # ------ MATlAB ------ # Paolo Toffanin , RuG, UMCG, Groningen, NL # Jacqueline Libert # Leanne Nagels < leanne.nagels@rug.nl > # Etienne Gaudrain - 2017-12-05 # CNRS, CRNL, Lyon, FR | RuG, UMCG, Groningen, NL # ------ Python ------ # 2020-04-13: Luke Meyer < l.meyer.5@student.rug.nl > # Copyright MRC-CBSU(UK), CNRS(FR), UMCG(NL) and the author(s). # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY # without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see < http: // www.gnu.org/licenses/>. # -------------------------------------------------------------------------- import os import numpy as np import soundfile as sf import datetime import time from Robot.RobotEmotion import RobotEmotion from tools.load_matrix import loadmat from scipy import io as sio def robot(ip, port, lang, options, phase): # -- Start an instance of SAM sam = RobotEmotion(ip, port, lang) # -- Set initial volume # NAO simulateSubj = False if options["subject_name"] == "test": simulateSubj = True # Create the result dictionary and load the subject's results/options file results = {} mat_contents = loadmat(options["res_filename"]) expe = mat_contents['expe'] options = mat_contents['options'] track = loadmat(options["res_tracker"]) exp = track["tracker"] if not "gain" in options: options["gain"] = 0 # === Main Loop === ladderstep = 1 starting = 1 itrial_session = 0 done = [] # === Main Loop that runs experiment === while np.mean(done) != 1: # Reintialises the done array and reappend for when a task has been completed done = [] for i in range(len(expe[phase]["trials"])): done.append(expe[phase]["trials"][i]["done"]) try: i_trial = done.index(0) itrial_session = itrial_session + 1 except ValueError: break print "Trial {} of {}".format(itrial_session, len(done) + 1) if exp["experiment_begin"]: # NAO sam.intro() exp["experiment_begin"] = 0 sio.savemat(options["res_tracker"], mdict={"tracker": exp}) if starting: if phase == "test": if lang == "EN": sam.simple_say("Welcome to the testing phase of the game.") elif lang == "NL": sam.simple_say( "Welkom bij de testronde van dit spelletje.") else: # NAO sam.emotionIntro() pass sam.game_break() starting = 0 trial = expe[phase]["trials"][i_trial] # y, fs = sf.read(os.path.join(options["sound_folder"], trial["file"])) os.chdir(options["sound_folder"]) x, fs = sf.read(trial["file"]) x = x * 10.0 ** options["attenuation_dB"]/20 sf.write('./gen_stimulus.wav', x, fs) # NAO sam.aad.setOutputVolume(sam.stimulus_volume) sam.play_sounds('gen_stimulus.wav', lang) sam.aad.setOutputVolume(sam.speaking_volume) os.remove('./gen_stimulus.wav') os.chdir('../../') tic = time.time() response = {} response["trial"] = trial if simulateSubj: # response["timestamp"] = datetime.datetime.now() response["response_time(ms)"] = round((time.time() - tic)*100, 2) response["emotion"] = options[phase]["emotions"][np.random.randint( len(options[phase]["emotions"]))] response["correct"] = response["emotion"] == trial["emotion"] else: response["timestamp"] = datetime.datetime.now() response["emotion"] = sam.answer(trial["emotion"], 1) response["response_time"] = round((time.time() - tic)*100, 2) response["correct"] = response["emotion"] == trial["emotion"] print("Clicked button: {}\n".format(response["emotion"])) print("Response time: {} ms\n".format( round(response["response_time"] * 1000))) print("Response correct: {} \n\n".format(response["correct"])) expe[phase]["trials"][i_trial]["done"] = 1 if not phase in results: results[phase] = {} results[phase]["responses"] = [response] else: results[phase]["responses"].append(response) sio.savemat(options["res_filename"], mdict={ "options": options, "expe": expe, "results": results}) if phase == "training": if lang == "EN": sam.simple_say("That's the end of the training phase") elif lang == "NL": sam.simple_say("Dit is het einde van de oefenronde ") else: exp["experiments"]["emotion"] = 1 sio.savemat(options["res_tracker"], mdict={"tracker": exp}) if lang == "EN": sam.simple_say( "That's the end of the testing phase, and the end of this game.") elif lang == "NL": sam.simple_say( "Dit is het einde van de test, en het einde van dit spelletje.") exp_tracker = np.array([exp["experiments"][key] for key in exp["experiments"].keys()]) if np.mean(exp_tracker) == 1: sam.ending()