-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update change_sample_rate_in_folder.praat
- Loading branch information
Showing
1 changed file
with
53 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,72 @@ | ||
# This Praat script will globally change the sample rate of all the AIFF files in the given folder. | ||
# See the Praat manual for details. | ||
# This Praat script will change the sample rate of all the audio files in the given folder. | ||
# The resampled files will be copied to a subfolder under the existing folder. | ||
# If some of the files already have the target sampling rate, they will be copied as such. | ||
# All resulting files (copied or resampled) will be saved in WAV format (Praat default). | ||
# | ||
# For details regarding the resampling algorithm, see the built-in manual in Praat (Sound: Resample...). | ||
# | ||
# This script is distributed under the GNU General Public License. | ||
# 10.3.2002 Mietta Lennes | ||
# | ||
# Changes: | ||
# 11.8.2023 Upgraded the script to support different audio file formats and the current Praat script syntax. | ||
# The results will now be saved/copied under a new subfolder, without deleting the source files. | ||
|
||
form Change sample rate in sound files | ||
comment Changes will be made to ALL aiff sound files in the directory. | ||
comment Files must be in AIFF format and filenames must include the string .aif or .AIF! | ||
sentence Directory /home/lennes/tmp/ | ||
sentence Sound_file_extension .wav | ||
sentence Directory /Users/lennes/tmp/ | ||
comment (Empty directory path = the directory where this Praat script is located) | ||
positive New_sample_rate_(Hz) 22050 | ||
positive Precision_(samples) 50 | ||
boolean Suppress_warnings yes | ||
comment (This prevents the script from pausing at warnings about clipped samples, for instance.) | ||
endform | ||
|
||
Create Strings as file list... list 'directory$'* | ||
Create Strings as file list: "list", "'directory$'*'sound_file_extension$'" | ||
numberOfFiles = Get number of strings | ||
if numberOfFiles = 0 | ||
exitScript: "Nothing to do, no files found in dir:'directory$'" | ||
else | ||
newfolder$ = "resampled_to_'new_sample_rate'" | ||
createFolder: "'directory$''newfolder$'" | ||
writeInfoLine: "Found 'numberOfFiles' files:" | ||
endif | ||
for ifile to numberOfFiles | ||
select Strings list | ||
sound$ = Get string... ifile | ||
if index (sound$, ".aif") > 0 or index (sound$, ".AIF") > 0 | ||
Read from file... 'directory$''sound$' | ||
Rename... temp | ||
oldrate = Get sample rate | ||
if oldrate <> new_sample_rate | ||
Resample... new_sample_rate precision | ||
filedelete 'directory$''sound$' | ||
Write to AIFF file... 'directory$''sound$' | ||
selectObject: "Strings list" | ||
sound$ = Get string: ifile | ||
Read from file: "'directory$''sound$'" | ||
name$ = selected$ ("Sound") | ||
oldrate = Get sample rate | ||
if oldrate > new_sample_rate | ||
appendInfoLine: "'ifile'/'numberOfFiles': Downsampling file 'name$' from 'oldrate' Hz (saving as WAV)" | ||
Resample: new_sample_rate, precision | ||
if suppress_warnings = 1 | ||
nowarn Save as WAV file: "'directory$''newfolder$'/'name$'.wav" | ||
else | ||
Save as WAV file: "'directory$''newfolder$'/'name$'.wav" | ||
endif | ||
select Sound temp | ||
Remove | ||
selectObject: "Sound 'name$'" | ||
elsif oldrate < new_sample_rate | ||
appendInfoLine: "'ifile'/'numberOfFiles': Upsampling file 'name$' from 'oldrate' Hz (saving as WAV)" | ||
Resample: new_sample_rate, precision | ||
if suppress_warnings = 1 | ||
nowarn Save as WAV file: "'directory$''newfolder$'/'name$'.wav" | ||
else | ||
Save as WAV file: "'directory$''newfolder$'/'name$'.wav" | ||
endif | ||
Remove | ||
selectObject: "Sound 'name$'" | ||
else | ||
appendInfoLine: "'ifile'/'numberOfFiles': No need to resample file 'name$' (just copying the original to WAV)" | ||
Save as WAV file: "'directory$''newfolder$'/'name$'.wav" | ||
endif | ||
Remove | ||
# pauseScript: "Continue?" | ||
endfor | ||
|
||
select Strings list | ||
Remove | ||
|
||
|
||
|
||
|
||
|
||
appendInfoLine: "" | ||
appendInfoLine: "Finished! 'numberOfFiles' files were saved to 'directory$''newfolder$'." |