Initial commit

This commit is contained in:
Kasiaaaaaa committed 2026-09-22 11:52:03 +02:00
commit d89b82be4a
103 files changed
+23110

No files matched your search

+327
View File
@@ -0,0 +1,327 @@
# --------------------------------------------------------------------------
# Authors:
# ------ MATlAB ------
# 2010-03-15: Etienne Gaudrain < etienne.gaudrain@mrc-cbu.cam.ac.uk >
# 2011-10-20: Etienne Gaudrain < etienne.gaudrain@mrc-cbu.cam.ac.uk >
# 2013-04-22: Etienne Gaudrain < e.p.c.gaudrain@rug.nl >
# 2017-03-27: Etienne Gaudrain < etienne.gaudrain@cnrs.fr >
# ------ Python ------
# 2020-01-28: 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/>.
# --------------------------------------------------------------------------
from expe_options import expe_options
from collections import OrderedDict
import warnings
import scipy.io as sio
import cPickle
import random
import numpy as np
import glob
import os
from tools.load_matrix import loadmat
def expe_build_conditions(options):
'''
This function creates the list of conditions that will be tested in the
experiment.
Without argument, it just returns the `expe` structure as well as the
list of `options`.
With an option argument that contains a `res_filename` field, it saves
them in a result file.
Modify the "Build test block" section to change which conditions are to
be stested.
WARNING: Once saved in a result file, the conditions are not modified
anymore at runtime even if the code is modified. This is done on purpose
so that we always know what were the options used for a specific
experimental session.
'''
# If all syllables need to be rebuilt (overwrites cached files)
FORCE_REBUIL_SYLLS = 0
# --- GUI options ---
options["instructions"] = {}
options["instructions"]["training"] = "You are going to hear three triplets of different syllables. \n" +\
"One of the triplets is said with a different voice. \n" +\
"Your task is to click on the button that corresponds to the different voice. \n"
options["instructions"]["test"] = options["instructions"]["training"]
# --- Signal options ---
options["fs"] = 44100
# options["attenuation_dB"] = 27 # General attenuation
mat = loadmat('./calibration.mat')
cal = mat["calibration"]
options["attenuation_dB"] = cal["fishy"] # Calibrated attenuation
options["ear"] = "both" # right, left, or both
# --- Design specifications ---
options["test"] = {}
options["test"]["n_repeat"] = 1 # Number of repetition per condition
options["test"]["step_size_modifier"] = 1/np.sqrt(2)
# When difference <= to this times the step-size, decrease the step size
options["test"]["change_step_size_condition"] = 2
# Change step-size every n trials
options["test"]["change_step_size_n_trials"] = 15
options["test"]["initial_step_size"] = 2 # Semitones
options["test"]["starting_difference"] = 12 # Semitones
options["test"]["down_up"] = np.array([2, 1]) # 2-down, 1-up => 70.7%
options["test"]["terminate_on_n_turns"] = 8
options["test"]["terminate_on_n_trials"] = 100
options["test"]["retry"] = 1 # number of retries if measure failed
options["test"]["threshold_on_last_n_trials"] = 6
options["training"] = {}
options["training"]["n_repeat"] = 1 # Number of repetition per condition
options["training"]["step_size_modifier"] = 1
# When difference <= to this times the step-size, decrease the step size
options["training"]["change_step_size_condition"] = 1
# Change step-size every n trials
options["training"]["change_step_size_n_trials"] = 15
options["training"]["initial_step_size"] = 3 # Semitones
options["training"]["starting_difference"] = 6 # Semitones
options["training"]["down_up"] = np.array([2, 1]) # 2-down, 1-up => 70.7%
options["training"]["terminate_on_n_turns"] = 0
options["training"]["terminate_on_n_trials"] = 6
options["training"]["retry"] = 0 # number of retries if measure failed
options["training"]["threshold_on_last_n_trials"] = 5
# --- Stimuli options ---
ref_f0 = 242.0 # This is the average pitch of the original voice
options["ref_f0"] = ref_f0
options["test"]["voices"] = {}
options["test"]["voices"]["v1"] = {}
options["test"]["voices"]["v1"]["label"] = 'female'
options["test"]["voices"]["v1"]["f0"] = ref_f0
options["test"]["voices"]["v1"]["ser"] = 1.0
options["test"]["voices"]["v2"] = {}
options["test"]["voices"]["v2"]["label"] = 'male'
options["test"]["voices"]["v2"]["f0"] = ref_f0/2.0
options["test"]["voices"]["v2"]["ser"] = 1 * np.power(2, -3.8/12)
options["test"]["voices"]["v3"] = {}
options["test"]["voices"]["v3"]["label"] = 'child'
options["test"]["voices"]["v3"]["f0"] = round(ref_f0*np.power(2, 5.0/12))
options["test"]["voices"]["v3"]["ser"] = 1 * np.power(2, 7.0/12)
options["test"]["voices"]["v4"] = {}
options["test"]["voices"]["v4"]["label"] = 'male-gpr'
options["test"]["voices"]["v4"]["f0"] = ref_f0/2.0
options["test"]["voices"]["v4"]["ser"] = 1.0
options["test"]["voices"]["v5"] = {}
options["test"]["voices"]["v5"]["label"] = 'male-vtl'
options["test"]["voices"]["v5"]["f0"] = options["test"]["voices"]["v1"]["f0"]
options["test"]["voices"]["v5"]["ser"] = 1 * np.power(2, -3.8/12)
options["test"]["voices"]["v6"] = {}
options["test"]["voices"]["v6"]["label"] = 'child-gpr'
options["test"]["voices"]["v6"]["f0"] = round(ref_f0*np.power(2, 5.0/12))
options["test"]["voices"]["v6"]["ser"] = 1.0
options["test"]["voices"]["v7"] = {}
options["test"]["voices"]["v7"]["label"] = 'child-vtl'
options["test"]["voices"]["v7"]["f0"] = options["test"]["voices"]["v1"]["f0"]
options["test"]["voices"]["v7"]["ser"] = 1 * np.power(2, 7.0/12)
options["training"]["voices"] = options["test"]["voices"]
# -- Voice pairing choices
# 1 2 : female -> Male
# 1 4 : Female -> Male GPR
# 1 5 : Female -> Male VTL
# 1 3 : Female -> Child
# 1 6 : Female -> Child GPR
# 1 7 : Female -> Child VTL
options["test"]["voice_pairs"] = np.matrix('1 4; 1 5; 1 6; 1 7')
options["training"]["voice_pairs"] = np.matrix('1 2; 1 3')
if options["language"] == "EN":
options["sound_path"] = './stimuli/fishy/en'
options["sound_path_store"] = './stimuli_store/en'
options["tmp_path"] = './tmp/en'
elif options["language"] == "NL":
options["sound_path"] = './stimuli/fishy/nl'
options["sound_path_store"] = './stimuli_store/nl'
options["tmp_path"] = './tmp/nl'
if not os.path.exists(options["tmp_path"]):
os.mkdir(options["tmp_path"])
if not os.path.exists(options["sound_path_store"]):
os.mkdir(options["sound_path_store"])
syllable_list = []
dir_waves = [f for f in os.listdir(
options["sound_path"]) if f.endswith(".wav")]
for file in dir_waves:
if file.endswith(".wav"):
syllable_list.append(file)
for i in range(len(syllable_list)):
syllable_list[i] = syllable_list[i].replace('.wav', '')
options["syllables"] = np.array(syllable_list)
options["sounds_for_calibration"] = []
for f in range(len(dir_waves)):
options["sounds_for_calibration"].append(
os.path.join(options["sound_path"], dir_waves[f]))
options["n_syll"] = 3
options["inter_syllable_silence"] = 50.0e-3
options["syllable_duration"] = 200.0e-3
options["f0_contour_step_size"] = 1.0/3.0 # semitones
# options["f0_contours"] = np.array([[-1, 0, +1], [+1, 0, -1], np.add([-1.0, 1.0, -1.0], 1.0/3), np.subtract([1, -1, 1], 1.0/3), np.add(
# [-1, -1, 1], 1.0/3), np.subtract([1, 1, -1], 1.0/3), np.subtract([-1, 1, 1], 1.0/3), np.add([1, -1, -1], 1.0/3)])
options["f0_contours"] = np.array([[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [
1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])
options["inter_triplet_interval"] = 250.0e-3
options["force_rebuild_sylls"] = FORCE_REBUIL_SYLLS
# --- Vocoder options ---
# Base Parameters
p = {}
p["envelope"] = {}
p["envelope"]["method"] = 'low-pass'
p["envelope"]["rectify"] = 'half-wave'
p["envelope"]["order"] = 2
p["synth"] = {}
p["synth"]["carrier"] = 'noise'
p["synth"]["filter_before"] = False
p["synth"]["filter_after"] = True
p["synth"]["f0"] = 1.0
options["vocoder"] = {}
options["vocoder"]['v1'] = {}
options["vocoder"]['v1']["label"] = 'noise'
options["vocoder"]['v1']["description"] = 'Noise-band vocoder, 6 bands from 1250 to 7000, envelope cutoff defined per band.'
options["vocoder"]['v1']["parameters"] = p
options["vocoder"]['v2'] = {}
options["vocoder"]['v2']["label"] = 'sin'
options["vocoder"]['v2']["description"] = 'Sinewave vocoder, 6 bands from 1250 to 7000, envelope cutoff defined per band, not refiltered after modulation.'
options["vocoder"]['v2']["parameters"] = p
options["vocoder"]['v2']["parameters"]["synth"]["carrier"] = 'sin'
options["vocoder"]['v2']["parameters"]["synth"]["filter_after"] = False
options["vocoder"]['v3'] = {}
options["vocoder"]['v3']["label"] = 'lownoise'
options["vocoder"]['v3']["description"] = 'Low-noise-noise-band vocoder, 6 bands from 1250 to 7000, envelope cutoff defined per band.'
options["vocoder"]['v3']["parameters"] = p
options["vocoder"]['v3']["parameters"]["synth"]["carrier"] = 'low-noise'
options["vocoder"]["v4"] = {}
options["vocoder"]['v4']["label"] = 'pshc'
options["vocoder"]['v4']["description"] = 'PSHC vocoder, 6 bands from 1250 to 7000, envelope cutoff defined per band.'
options["vocoder"]['v4']["parameters"] = p
options["vocoder"]['v4']["parameters"]["synth"]["carrier"] = 'pshc'
# === Build test block ===
test = {}
# def update(d1, d2):
# for k, v in d2.items():
# if k in d1 and isinstance(v, dict) and isinstance(d1[k], dict):
# update(d1[k], v)
# else:
# d1[k] = v
m, _ = options["test"]["voice_pairs"].shape
for ir in range(options["test"]["n_repeat"]):
for i_vp in range(m): # iterate for each row in voice_pairs
condition = {}
condition["ref_voice"] = options["test"]["voice_pairs"][i_vp, 0]
condition["dir_voice"] = options["test"]["voice_pairs"][i_vp, 1]
condition["visual_feedback"] = 1.0
condition["sound_feedback"] = 1.0
# Should not be removed
condition["i_repeat"] = ir
condition["done"] = 0
condition["attempts"] = 0
if not "conditions" in test:
test["conditions"] = [condition]
else:
test["conditions"].append(condition)
# items = test["conditions"].items()
test["conditions"] = random.sample(
test["conditions"], len(test["conditions"]))
# === Build training block ===
training = {}
m, _ = options["training"]["voice_pairs"].shape
for ir in range(options["training"]["n_repeat"]):
for i_vp in range(m): # iterate for each row in voice_pairs
condition = {}
condition["ref_voice"] = options["training"]["voice_pairs"][i_vp, 0]
condition["dir_voice"] = options["training"]["voice_pairs"][i_vp, 1]
condition["visual_feedback"] = 1.0
condition["sound_feedback"] = 1.0
# Should not be removed
condition["i_repeat"] = ir
condition["done"] = 0
condition["attempts"] = 0
if not "conditions" in training:
training["conditions"] = [condition]
else:
training["conditions"].append(condition)
# items = test["conditions"].items()
# Randomise the order of the conditions
training["conditions"] = random.sample(
training["conditions"], len(training["conditions"]))
# === Create expe dictionary and save .mat file ===
expe = {}
expe["test"] = test
expe["training"] = training
if "res_filename" in options:
# with open(options["res_filename"], 'w') as f:
# cPickle.dump([options, expe, p], f)
sio.savemat(options["res_filename"], mdict={
'expe': expe, 'options': options, 'p': p})
else:
warnings.warn("The test file was not saved: no filename provided.")
return expe, options