Skip to content

Instantly share code, notes, and snippets.

@henrikj242
Last active August 3, 2018 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henrikj242/0bec751b5343829d9322e0c2a4075865 to your computer and use it in GitHub Desktop.
Save henrikj242/0bec751b5343829d9322e0c2a4075865 to your computer and use it in GitHub Desktop.
Bash examples
#!/usr/bin/ruby
# We use this script to rename samples. The samples come from Logic, and have then been batch-processed (but not renamed)
# This script renames samples in a specified directory like so:
# - Give them all a three digit numeric suffix.
# - If it's a BD or SD +OSC2 sample, assume it's "colored" and name it accordingly.
# - In all other cases, assume not "colored", and just name it with A (round-robin, not accent) or B (round-robin, accent).
#
# Note that while the dry run does not make any changes, it may not reveal potential issues when running the script for real.
require 'fileutils'
if ARGV[0] == 'help' || ARGV == []
puts "Copies and renames files from /(.+) ([0-9]{1,3})\.wav$/ to 3-digit zero-padded sequence."
puts " if new filename matches / [BS]D.+ OSC2.+\.wav/ a 'color' number sequence is alo added"
puts "Usage:"
puts " 1: cd to the same folder as the folder with your samples."
puts " 2: Run the script `<PATH>/copy-rename.rb <SAMPLES_FOLDER>`"
puts " You may add an argument `dry` at the end to see the renaming without making any changes"
puts " For example: `cd ~/samples; ~/scripts/copy-rename.rb XT-808 dry`"
exit 0
end
# Source dir
source = ARGV[0]
dry_run = ARGV[1] == 'dry'
if ! File.directory?(source)
puts "The source directory #{source} does not exist. Exiting"
exit 1
end
# Destination dir
dest = "#{source}-Renamed-for-Kontakt"
if File.directory?(dest) && !dry_run
puts "The destination directory #{dest} already exists. Exiting"
exit 1
end
Dir.mkdir dest unless dry_run
def sequence_format(filename)
pattern = /(.+) ([0-9]{1,3})\.wav$/
return filename unless filename =~ pattern
matches = filename.match(/(.+) ([0-9]{1,3})\.wav/)
replacement = sprintf("%03d", matches[2].to_i)
"#{matches[1]} #{replacement}.wav"
end
def round_robin(file_number)
((file_number - 1) % 5) + 1
end
def color(file_number)
sprintf('%03d', file_number - 1)[0..1].to_i + 1
end
def accent(file_number)
(((file_number) - 1) % 10) + 1 <= 5 ? 'A' : 'B'
end
def rename_with_color(filename)
if filename =~ / [0-9]{3}\.wav/
matches = filename.match(/(.+) ([0-9]{3})\.wav/)
file_number = matches[2].to_i
replacement = sprintf("R%d_%s_%02d",
round_robin(file_number),
accent(file_number),
color(file_number)
)
"#{matches[1]} #{replacement}.wav"
end
end
begin
FileUtils.copy_entry "#{source}", "#{dest}"
rescue StandardError => e
puts "Something went wrong when copying"
puts e.message
exit
end
Dir.glob("#{dest}/*").sort.each do |entry|
filename = File.basename(entry)
new_filename = sequence_format(filename)
puts "SEQ: #{filename} -> #{new_filename}"
if filename != new_filename
FileUtils.mv "#{dest}/#{filename}", "#{dest}/#{new_filename}" unless dry_run
end
filename = new_filename
if filename =~ / [BS]D.+ OSC2.+\.wav/
new_filename = rename_with_color(filename)
puts "OSC2: #{filename} -> #{new_filename}"
end
if filename != new_filename
FileUtils.mv "#{dest}/#{filename}", "#{dest}/#{new_filename}" unless dry_run
end
end
# We had a folder with a bunch of wave files named by cateory and some number.
# This script creates folders for the categories and moves the files into the folder
# Funny note: If the script is executed from a file, the script also creates a folder for the script itself - and moves the script file.
# Loop over the files in working dir
for filename in *; do
# extract the foldername from the filename
foldername="$(echo $filename | perl -pe 's/^([a-zA-Z]+).*$/$1/')"
# create the folder - unless it already exists
if [[ ! -d $foldername ]]; then
mkdir -p $foldername
fi
# Move the file into the folder
mv $filename $foldername
done
# Run through folders in working dir, and then loop over all the containing wav files.
# For each of the wav files, run a ruby script that generates some preset based on the wav file name.
# The preset filename is named 00x where x is incremented for each file.
for foldername in *; do
cd "$foldername"
idx=1
for filename in *.wav; do
preset_number="$(printf %03d $idx)"
ruby ~/Projects/wavinfo/make-assimilator-preset.rb "$filename" "prst$preset_number.yml"
let idx++
done
cd ..
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment