# # ## ### ##### ######## ############# ##################### ## Scale intensity of all sounds in a Directory ##### Check for clipped extrema # Give an option to lower the output level # until none of the sounds are clipped # ## Matthew B. Winn ## 2025 # ## NOTE: It helps to have the Praat info window open as you run this script, ###### as it will update info for you as it proceeds. # ## Once the script is done and you choose to save the files, #### Look in the original sound directory - #### there you will find a new sub-folder of normalized sounds. ################################## ##################### ############# ######## ##### ### ## # # form Input Enter specifications for Intensity scaling comment Enter target intensity (dB): real target_intensity 70 comment Enter directory where the ORIGINAL sound files will be retrieved: sentence soundDir C:\Enter-Your-Directory-Here #sentence soundDir L:\PraatScripts\Intensity_scaling\audio comment Enter filename to which the info should be written sentence outFile intensity_info.txt endform clearinfo # run it once all the way through, # looking for clipping call change_intensities procedure change_intensities # initiate overall_max = 0.001 clippedSounds = 0 # Reads in a list of files Create Strings as file list... list 'soundDir$'/*.wav numberOfFiles = Get number of strings # make a list of each sound in the directory for thisFile to numberOfFiles select Strings list fileName$ = Get string... thisFile name$ = fileName$ - ".wav" # Reads in all sound (wav) files Read from file... 'soundDir$'/'name$'.wav # get intensity of the original sound old_Int = Get intensity (dB) print 'name$' was 'old_Int:1' # scale it to the user-defined intensity Scale intensity... 'target_intensity' int_diff = 'target_intensity' - 'old_Int' print & adjusted by 'int_diff:2' to get 'target_intensity:1' 'tab$' # Check to see if it clips extremum = Get absolute extremum: 0, 0, "none" # (this value is unsigned) if extremum > overall_max overall_max = extremum endif if extremum >= 1 clippedSounds = clippedSounds + 1 print CLIPPED endif # wrap to the next line print 'newline$' # Remove the sound object from the objects list Remove endfor # calculate how much attenuation is needed to avoid clipping print 'newline$' overall max is 'overall_max''newline$' # this is the maximum allowable peak before clipping target_peak = 0.999 multiplier_needed = target_peak / overall_max if multiplier_needed > 1 # do nothing, sounds are good to go else # need to attenuate the sounds dB_change_needed = 20 * log10(multiplier_needed) # round down to nearest whole number dB_change_needed = floor(dB_change_needed) endif # decide whether you want to save the files call user_select_save endproc procedure user_select_save if multiplier_needed < 1 choice = 3 else choice = 1 endif beginPause ("'clippedSounds' sounds have been clipped (see info window for details)") comment ("'clippedSounds' sounds have been clipped") if multiplier_needed < 1 comment ("you should probably reduce your target intensity by 'dB_change_needed'") else comment ("it is safe to save them.") endif comment ("how do you want to proceed?") optionMenu ("Choice", choice) if multiplier_needed < 1 option ("save the sounds and clip them") option ("Quit") option ("Lower target intensity by 'dB_change_needed' dB and run again") else option ("save the sounds") option ("Quit") endif comment ("Then click Continue.") endPause ("Continue", 1) if choice = 1 call saveSounds elsif choice = 2 select all Remove else select all Remove target_intensity = target_intensity + dB_change_needed call change_intensities endif endproc procedure saveSounds # makes a directory new_directory$ = "'soundDir$'" + "/" + "_Intensity_'target_intensity'" createDirectory: new_directory$ # first deletes any existing output file filedelete 'soundDir$'/'outFile$' # creates simple header fileappend Step 'tab$' Duration 'tab$' 'newline$' # make a list of each sound in the directory for thisFile to numberOfFiles select Strings list fileName$ = Get string... thisFile name$ = fileName$ - ".wav" # Reads in the sound (wav) file Read from file... 'soundDir$'/'name$'.wav # scale it to the user-defined intensity (updated to reflect clip-adjustment changes) Scale intensity... 'target_intensity' # save it Save as WAV file... 'new_directory$'/'name$'.wav Remove endfor call saveInfoWindow "'new_directory$'" _Intensity_Info select Strings list Remove endproc procedure saveInfoWindow outputDirectory$ outputFileName$ filedelete 'outputDirectory$'/'outputFileName$'.txt fappendinfo 'outputDirectory$'/'outputFileName$'.txt endproc # # ## ### ##### ######## ############# ##################### ################################## DONE