Re-naming WAV files in versions post-2024.2.0

If this template helps then use it. If not then just delete and start from scratch.

MacOS 14.6
PsychoPy 2024.2.0+
Standalone

I’m out to rename WAV files to make them useful and easily dealt with after the fact.

I’ve been able to make this work just running the experiment under an older version (2024.1.0 works), but it is my understanding that the way Mic recordings are handled has changed (I assume with 2024.2.0). It seems to now generate the WAV files and store then immediately which is a huge improvement, but it goofs the way I have typically renamed files.

Here is what works in prior versions:
Before experiment

# rename recordings set up
renameRecordings = []

End routine containing Mic component [It has seemed to matter that this go physically below the Mic component within the routine]

# Create new recording names
old_file = os.path.join("data", micRecFolder, 'recording_mic_%s.wav' % tag)
new_file = os.path.join("data", micRecFolder, '%s_%s_%s_%s_%s.wav' % (expInfo['participant'],vowel,block_no,trial_no,item))
renameRecordings.append([old_file,new_file])

End experiment

# actually do the re-naming
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])

That seems to work in versions <2024.1.0. In later versions, that arrangement spits out a filename error–Can’t find the file. I tried moving it all to End Experiment, which was promising, but it only changed the first file name. I stopped there and figured I’d ask.

This is more informational for myself and others-- what would be the best way to go about handling this task? Additionally, bonus points if you can tell me precisely what tag as I use it in working with the old file name references. I assume it’s the autogenerated file name, but I’m not 100% sure.

Counter-intuitively I’m going to start with this :stuck_out_tongue: tag is defined at the end of the Routine by the Microphone, and it’s data.utils.getDateStr(), i.e. the time and date as a string at time of end Routine.

This therefore makes sense - as tag is defined by the Microphone’s “end routine” code, and Component code is written in the order they appear, your Code Component needs to be after the Microphone so that tag has been defined by the time it’s run.

Rather than renaming the files after saving, why not change the name they’re saved as initially? The recordings are stored inside the Microphone object as “clips”, they’re arranged in a dict according to the tag assigned to them when the Routine ended (and Microphone.bank was called), and the keys of this dict determine what filename they’re saved as. So you could do something like this in your End Routine:

# construct new tag
new_tag = '%s_%s_%s_%s_%s' % (expInfo['participant'],vowel,block_no,trial_no,item)
# reassign the clips for this Routine to the new tag
myMicrophone.clips[new_tag] = myMicrophone.clips.pop(tag)
1 Like

Thank you! This was the best explanation I’ve seen on how it actually works. I’d gotten my prior method from an earlier post on here, and it’s served me well until the current version.

I will try what you propose here over the weekend.

Tried it and it works!

Only thing was it still leaves “recording_mic_…” lead the tag I specify. Still, that’s easily removed after and the essentials of what I assign populate correctly.

1 Like