Use the CRM corpus#

This shows how to use the CRM corpus functions.

@author: rkmaddox

import numpy as np

from expyfun import ExperimentController, analyze, building_doc
from expyfun._utils import _TempDir
from expyfun.stimuli import (
    CRMPreload,
    add_pad,
    crm_info,
    crm_prepare_corpus,
    crm_response_menu,
    crm_sentence,
)

print(__doc__)

crm_path = _TempDir()
fs = 40000

Prepare the corpus#

For simplicity, here we prepare just two talkers at the native 40000 Hz sampling rate.

Note

For your experiment, you only need to prepare the corpus once per sampling rate, you should probably use the default path, and you should just do all the talkers at once. For the example, we are using fs=40000 and only doing two talkers so that the stimulus preparation is very fast, and a temp directory so that we don’t interfere with any other prepared corpuses. Your code will likely look like this line, and not appear in your actual experiment script:

>>> crm_prepare_corpus(24414)
crm_prepare_corpus(
    fs,
    path_out=crm_path,
    overwrite=True,
    talker_list=[dict(sex=0, talker_num=0), dict(sex=1, talker_num=0)],
)

# print the valid callsigns
print(f'Valid callsigns are {crm_info()["callsign"]}')

# read a sentence in from the hard drive
x1 = 0.5 * crm_sentence(fs, "m", "0", "c", "r", "5", path=crm_path)

# preload all the talkers and get a second sentence from memory
crm = CRMPreload(fs, path=crm_path)
x2 = crm.sentence("f", "0", "ringo", "green", "6")

x = add_pad([x1, x2], alignment="start")
Preparing sex 0.
    Preparing talker 0.

Preparing sex 1.
    Preparing talker 0.

Finished in 0.0 minutes.
Valid callsigns are ['charlie', 'ringo', 'laker', 'hopper', 'arrow', 'tiger', 'eagle', 'baron']

Now we actually run the experiment.

max_wait = 0.01 if building_doc else 3
with ExperimentController(
    exp_name="CRM corpus example",
    window_size=(720, 480),
    full_screen=False,
    participant="foo",
    session="foo",
    version="dev",
    output_dir=None,
    stim_fs=40000,
) as ec:
    ec.screen_text(
        "Report the color and number spoken by the female " "talker.", wrap=True
    )
    screenshot = ec.screenshot()
    ec.flip()
    ec.wait_secs(max_wait)

    ec.load_buffer(x)
    ec.identify_trial(ec_id="", ttl_id=[])
    ec.start_stimulus()
    ec.wait_secs(x.shape[-1] / float(fs))

    resp = crm_response_menu(ec, max_wait=0.01 if building_doc else np.inf)
    if resp == ("g", "6"):
        ec.screen_prompt("Correct!", max_wait=max_wait)
    else:
        ec.screen_prompt("Incorrect.", max_wait=max_wait)
    ec.trial_ok()

analyze.plot_screen(screenshot)
crm stimuli
exp_name: CRM corpus example
date: 2024-10-08 13_43_11.159775
file: /home/circleci/project/examples/stimuli/crm_stimuli.py
participant: foo
session: foo
2024-10-08 13:43:11,160 - INFO    - Expyfun: Using version 2.0.0.dev0 (requested dev)
2024-10-08 13:43:11,160 - INFO    - Expyfun: Setting up sound card using pyglet backend with 2 playback channels
2024-10-08 13:43:11,343 - WARNING - Expyfun: Mismatch between reported stim sample rate (40000) and device sample rate (44100.0). Experiment Controller will resample for you, but this takes a non-trivial amount of processing time and may compromise your experimental timing and/or cause artifacts.
2024-10-08 13:43:11,343 - INFO    - Expyfun: Setting up screen
2024-10-08 13:43:11,376 - EXP     - Expyfun: Set screen visibility True
2024-10-08 13:43:11,385 - INFO    - Initialized [720 480] window on screen XlibScreen(display=<pyglet.canvas.xlib.XlibDisplay object at 0x7f392c363f10>, x=0, y=0, width=1400, height=900, xinerama=0) with DPI 69.73
2024-10-08 13:43:11,385 - INFO    - Expyfun: Initializing dummy triggering mode
2024-10-08 13:43:11,387 - INFO    - Expyfun: Initialization complete
2024-10-08 13:43:11,387 - EXP     - Expyfun: Participant: foo
2024-10-08 13:43:11,387 - EXP     - Expyfun: Session: foo
2024-10-08 13:43:11,437 - WARNING - Expyfun: Resampling 2.01 seconds of audio
2024-10-08 13:43:11,478 - WARNING - Expyfun: Stimulus max RMS (0.025984056293964386) exceeds stated RMS (0.01) by more than 6 dB.
/home/circleci/project/expyfun/_experiment_controller.py:2041: UserWarning: Expyfun: Stimulus max RMS (0.025984056293964386) exceeds stated RMS (0.01) by more than 6 dB.
  warnings.warn(warn_string)
2024-10-08 13:43:11,479 - EXP     - Expyfun: Loading 177652 samples to buffer
2024-10-08 13:43:11,480 - EXP     - Expyfun: Stamp trial ID to ec_id  :
2024-10-08 13:43:11,480 - EXP     - Expyfun: Stamp trial ID to ttl_id : []
2024-10-08 13:43:11,480 - EXP     - Stamping TTL triggers: []
2024-10-08 13:43:11,481 - EXP     - Expyfun: Starting stimuli: flipping screen and playing audio
2024-10-08 13:43:11,482 - EXP     - Stamping TTL triggers: [1]
2024-10-08 13:43:13,623 - WARNING - ec.trial_ok called before stimulus had stopped
2024-10-08 13:43:13,623 - EXP     - Expyfun: Trial OK
2024-10-08 13:43:13,623 - INFO    - Expyfun: Exiting
2024-10-08 13:43:13,629 - EXP     - Expyfun: Audio stopped and reset.

Total running time of the script: (0 minutes 6.078 seconds)

Gallery generated by Sphinx-Gallery