| Reference | Downloads | Github

Self-paced reading task w/accompanying musical chords

OS (e.g. Win10): MacOS High Sierra 10.13.6
PsychoPy version (e.g. 1.84.x): V3.0.0b11
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?: Trying to assemble a self-paced reading task with musical chords playing behind each segment (e.g., “the chef/who wore/the poofy hat/remembered/the recipe/would/require/using/fresh basil” would have 9 separate wav files).

After each sentence, a comprehension will appear (“did the chef wear a hat? y/n”, if incorrect “Incorrect!” will appear to encourage mindful reading, no wav file necessary for comprehension question).

There will be 48 sentences in total arranged in a pseudorandom order.

What did you try to make it work?: I have successfully created the self-paced reading task. However, I cannot figure out how to place individual wav files to correspond with each segment. I have scoured the forum, and it appears that most accompanying music inquiries pertain to continuous loops or repeating segments.

I’m assuming each sentence will be under its own routine, however, I am not sure if the wavs should be inserted as a loop or coded into the routine as well. What I’m trying to avoid is having each word under a new routine…

I should mention I have absolutely zero experience with coding so any layman advice would be most helpful!


Hi, we would need to see the code you are using to display the sentence segments, so we know how to suggest to integrate them with sounds.

Hi Michael,
not sure how to present the appropriate code, but I can attach pictures if it helps.

This is essentially my task (except there would be 48 sentences in total for each participant):

Do I nest audio within each routine like such? The problem with this is that I cannot get different wavs to play with each segment of text, as the same audio plays for all.

Or, do I somehow code it into Excel? This seems like it would make more sense, but I am so not Excel savvy and not sure even how to do this. Disregard the audio names, each wav will be different for each word.

Once I can get this figured out then I’ll tackle the y/n comprehension Q in between routines.


OK, I assumed your condition file would be based on a row per sentence rather than a row per segment. In this case, yes, you simply specify the accompanying sound file as you have done in the the final screenshot. You just have to set the sound component to update the file it uses on every iteration of the loop, just as you do with the text component accessing its new segment of text.

Do you mean like this? And if so, which cells do wavs get entered in?

Or is it something like this route:

OR, this works but it’s not ideal:

No, I was suggesting a way that worked with your arrangement of the conditions file as you provided above, with a row per text segment.

You need to control your sound component in the same way you control your text component. i.e. put $aud1 in your sound component. There is no point in putting it in a text component: text components display text, not play sounds.

Got it! Thank you so much. Such an easy fix. Now, if I wanted to insert comprehension questions in between sentences (e.g., Did the chef use basil? y/n [incorrect responses would prompt an Incorrect! on the screen before next trial]), would this be a separate routine? And would this data be plugged into the Excel sheet as well?

I guess you need to conceptualise “what is a trial” in terms of your design. PsychoPy Builder is basically structured around each row in a conditions file holding all the information related to a given trial. So if one trial equals presenting a sentence fragment and a sound, then the arrangement you have above works. But if one trial is actually presenting a sentence (which is split into fragments) followed by a question relating to that sentence, then really, all this information should be in one row of the file, grouped together. e.g. I’d suggest putting the whole sentence in one cell, with fragments delimited by some character (e.g. The chef*who wore*the hat* etc). Then you would have a small loop nested inside your trial loop that would present each sentence fragment in turn, along with its associated sound file. This would require a small amount of custom code to implement the non-standard nature of the presentation.

The question sentence would then just be another column. i.e. each row would contain two sentences.

Yes, having both sentences in one row per routine seems to make sense. How would I go about coding the associated sounds files to match the segments of text? And how do I code for correct/incorrect answers? Ideally, i’d like an “Incorrect!” to display when a participant is incorrect before moving to the next trial. Correct answers would automatically advance.

Add a column giving the correct keyboard response on each trial, and use that variable in the keyboard component so that it can record whether the response is correct or not.

If you could be so kind, could you please snapshot what this would look like in builder? Also Along with the custom code to implement the small loop nested inside the trial loop that would present each sentence fragment in turn along with its associated sound file? I would be eternally grateful…

It would probably be worth working through this tutorial by Jon Peirce to get to grips with the basic concepts:

Re code, insert a loop around your text presentation routine that is not linked to a conditions file (i.e. it will be nested inside your main, outer loop). Insert a code component from the “custom” section of the component panel. In its “begin routine” tab, put something like this:

# on the first iteration only:
# (use your actual inner loop name)
if your_inner_loop_name.thisN == 0:
    # break up your sentence into fragments, as delimited by the *s:
    word_list = your_sentence_variable_name.split('*')

Make sure the code component is above your text component so the segments are created before the text component needs to refer to them (you can move components by right-clicking their icons).

In your text component, put something like this, set to update every routine:


Give the inner loop some large number of repeats that will exceed the maximum possible number of sentence fragments (e.g. 25). So now you need to end that loop on each trial depending on how many segments were in this trial’s sentence. So insert something like this in the “End routine” tab:

# check if all the segments have been popped from the list:
if len(word_list) == 0:
    your_inner_loop_name.finished == True

Hello @Michael - thank you for the advice, I have been able to arrange the experiment in the original fashion I suggested (only because I was not able to get segments of text to sync with specific audio files). However, my problem now is that Psychopy wants to “fill in” additional audio to match the longest column of text by repeating the last audio file. i.e., S1 contains 6 rows of text with 6 corresponding audio files, but when I get to the last text segment/audio file, the audio file #6 repeats and additional 3 times (because the next column contains 9 rows of text) before initiating the next trial. Any advice?

Screen Shot 2019-01-31 at 1.03.03 PM

As above:

It seems like you are trying to hammer a square peg into a round hole.