Audio File Name

OS (e.g. Win10): macOS Tahoe 26.4.1
PsychoPy version (e.g. 2024.2.4 Py 3.8): v2026.1.3
Standard Standalone Installation? (y/n) y
Do you want it to also run online? (y/n) n

Hi! I am currently designing a study that will record participants’ voice using the Microphone component. However, I would like to make PsychoPy name each audio file in a specific way. Does anyone know how to customize PsychoPy audio file name? Thank you for helping.

Best,

Tommy

Dear Tommy,

I am not sure what specific way you need it named, that would be helpful if you could provide it.

However, the way the recordings are named are based on unix time of onset.

You could just implement the solution found here.

Issac

Hi Issac! I just tried the solution in the post you shared. However, for the “End Routine” portion of the code, PsychoPy says “/* Syntax Error: Fix Python code */”

Since I don’t have prior background in coding, I am not sure what the syntax error is. The code in the solution was:

old_file = os.path.join(“data”, micRecFolder, ‘recording_mic_%s.wav’ % tag)
new_file = os.path.join(“data”, micRecFolder, ‘%d%s_%s_%d.wav’ % (score,Correct,expInfo[‘participant’],trials.thisN))
renameRecordings.append([old_file,new_file])

Do you perhaps know what the error might be?

Thank you!

Without seeing how it is formatted in the coding box not really. It should look something like this.

#beginning of exp

renameRecordings = []

#end of routine 
old_file = os.path.join("data", micRecFolder, 'recording_mic_%s.wav' % tag)

new_file = os.path.join("data", micRecFolder, '%d%s_%s_%d.wav' % (score,Correct,expInfo['participant'],trials.thisN))

renameRecordings.append([old_file,new_file])

#end of experiment

for Idx in range(len(renameRecordings)):
    # Check file size
    if os.stat(renameRecordings[Idx][0]).st_size > 0:
        # Rename files
        os.rename(renameRecordings[Idx][0],renameRecordings[Idx][1])
    else:
        # Delete empty files
        os.remove(renameRecordings[Idx][0])

For the end of routine code, you will need to change “micRecFolder” to “your_microphone_component_nameRecFolder”. Same with expInfo[‘participant’] is whatever that field is called (might be participant_id or something similar).

Similarly this section → (score, Correct, expInfo[‘participant’], trials.thisN) is what each of the “%s_” will be in “%s_%s_%s_%s.wav”.
So if you want just participant name and the trial number you would have:

(expInfo['participant'], trials.thisN)
#and
%s_%s.wav

Which would make that section of the file name “participantA1_12.wav”.

Then the “renameRecordings.append” adds the new name to the old name (which was just unix timestamp numbers).

Edit: you will also have to look at the % tag code provided by wakecarter in that thread for its usage. They may be able to provide some context on its purpose.

Issac

Was the error message in the Runner when you tried to run it, or in the Auto JS translation? The code won’t run online.

Hi Issac! I pasted the code into End Routine, but the code Properties window shows “Syntax Error: Fix Python Code.” I tried to see where this problem stems from, and it looks like the problem is the “.join” part of the function “os.path.join()”

Once I delete the “.join” part from line 1 and line 2, there is no longer a Syntax Error. Do you know what problem this might be–I am new to Python…

Hi!

I looked at the code you provided for the tag variable in the earlier post. I wonder if I also need to add this to the code variable in my PsychoPy builder to make audio file naming work. If so, which part should I add this code to (e.g., End Routine, End of Experiment)?

Hello @cz_8

The message /* Syntax error: Fix Python code */ tells you that your PsychoPy code can not be properly translated into PsychoJS.

Change your code to

old_file = "data/" + micRecFolder + "/recording_mic_" + tag + ".wav"

new_file = "data/" + micRecFolder + "/" + str(score) + str(Correct) + "_" + expInfo['participant'] + "_" + str(trials.thisN) + ".wav"

if you want to run the experiment online. .join is the problem here.

Best wishes Jens

I don’t think the JS code is relevant here… renaming microphone recordings is unlikely to work.

They are running offline. They just need to ignore that warning.

@cz_8 that warning only matters if you plan on running on a browser/online. Have you tried running the code/experiment yet at all?

Issac

Hi Issac! I switched to “Py” for the code component, and I am happy to find that it works now! The audio files are renamed as I expected.

Also a huge thank-you to wakecarter and Jens for helping!