Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Silenoid/38f00c52a3f349f5e64c0fd9f3b7e2e5 to your computer and use it in GitHub Desktop.
Save Silenoid/38f00c52a3f349f5e64c0fd9f3b7e2e5 to your computer and use it in GitHub Desktop.
A Powershell script that uses SoX and fixes the sample rate of the audio files in input to a different one so that the mod won't crash during the compilation od the sound cache
# Loop through each .wav file in the current directory
Get-ChildItem -Path . -Filter *.wav | ForEach-Object {
# Call sox and trim only the "Sample rate" line. From that line, then, trim the word "Sample rate"
$sampleRate = [Int32](
sox --i $_
| Select-Object -Last 7
| Select-String -Pattern "Sample rate.*" -AllMatches
| ForEach-Object { $_.Matches }
| ForEach-Object { $_.Value }
| ForEach-Object { $_.Replace("Sample Rate : ", "") }
| ForEach-Object { $_.trim()}
)
Write-Output "Sample rate of $_ is $sampleRate"
# If the sample rate is 22050, convert the file to 22000 and append a timestamp to the filename
if ($sampleRate -eq 22050) {
# If the "converted" directory doesn't exists, create it
if (!(Test-Path "converted")) {
New-Item -ItemType Directory -Path "converted"
}
Write-Output "Converting $_ to 44100"
$newFilename = $_.BaseName + ".wav"
$outputFile = Join-Path $_.DirectoryName "converted" $newFilename
& sox $_.FullName -r 44100 $outputFile
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment