I am trying to change the name of the output file for the microphone.
Looking through the forums, I saw suggestions for using microphone.setFile(’ filepath’ ) but I get an error stating that ’ Microphone’ object does not have this attribute. I assume the new Microphone Component works differently, but I can’t find in the documentation what is the correct way of choosing an output name now.
OS (e.g. Win10): Windows 11
PsychoPy version (e.g. 1.84.x): Builder v2023.2.3
**Standard Standalone? Yes
I’ve have some success with code like this:
Begin Experiment
renameRecordings = []
End 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 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])
2 Likes
First, thanks for this marvelous tip! It works. I now have intelligible filenames.
In case anyone, like me, is not the most dextrous with code and stumbles upon this post for help, I will add a few tips that got this working for me.
If you rename your mic component in Builder, you have to reference that new name in the ‘End Routine’ code chunk. Mine, for example, is named ‘trial_mic’, so my code chunk there is this:
new_file = os.path.join("data", trial_micRecFolder, '%s_%s_%s_%s.wav' % (expInfo['participant'],voice,set,item))
renameRecordings.append([old_file,new_file])
‘voice’, ‘set’, and ‘item’ are from the conditions file in the trial routine. This yields file names like the following: 397618_f_fam_a11_r1.wav, 397618_m_fam_a01_r2.wav, 397618_m_fam_u10_r1.wav
This code chunk should go at the end of the routine containing that mic component and that code chunk should be physically below the mic component in the Builder pane. Finally, the ‘End Experiment’ code chunk can be pasted in exactly as it appears above, but it didn’t work for me until I put it in a code chunk under “End Experiment” in the final routine of the experiment (rather than under ‘End Experiment’ in the same routine as I had the prior chunk under ‘End Routine’).
Happy renaming!
Hello and thanks for the tips!
But I still have some problems after doing this. After I edited the code chunks as instructed, I ended up with just one wav. file with the intended filename. But since my microphone component is in a loop, it should generate multiple files. I wonder if there are some other adjustments I should make? Thanks!!!
Thanks for this! I’m a bit confused about the tag
variable. Where exactly is that coming from?