Skip to content

Instantly share code, notes, and snippets.

@sivers
Created June 25, 2019 20:32
Show Gist options
  • Save sivers/b900bf4004e148b32a5ddf3fffbe3a1c to your computer and use it in GitHub Desktop.
Save sivers/b900bf4004e148b32a5ddf3fffbe3a1c to your computer and use it in GitHub Desktop.
Create my audiobook, 88 chapters with 9 ingredients to each chapter, using Ruby + sox
#!/usr/bin/env ruby
# if limiting to one chapter, ./audiobook.rb 05
LIMIT = (ARGV[0] =~ /\A[0-9][0-9]\Z/) ? ARGV[0] : false
BASE = Dir.pwd + '/'
NUMS = BASE + 'ChapterNums/'
DRUM = BASE + 'DrumFills/'
GUIT = BASE + 'GuitarChords/'
URLS = BASE + 'URLs/'
TALK = BASE + 'YourMusicAndPeople/clean/'
TEMP = BASE + 'tmp/'
OUT = BASE + 'out/'
def seconds(filename)
%x(soxi -D #{filename}).to_f
end
def pad(filename, seconds, outfile)
fullout = OUT + outfile + '.wav'
%x(sox #{filename} #{fullout} pad #{seconds} 0)
fullout
end
drum_in = 'drumch-37.wav'
drum_x = 'drumch-48.wav'
drum_out = 'drumch-80.wav'
File.readlines('ymap.txt').each do |line|
title, body, guitar, chapter_n, url = line.strip.split("\t")
next if (LIMIT && LIMIT != title[0,2])
puts body
# start with the drum
pad(DRUM + drum_in, 0, 'drum_in')
# guitar comes in at final drum hit
pad(GUIT + guitar, 0.8, 'guitar_in')
# chapter number at set time
nu = pad(NUMS + chapter_n, 2.3, 'chapter_n')
# title after chapter
nu = pad(TALK + title, seconds(nu) + 0.7, 'title')
# drum_x after title
nu = pad(DRUM + drum_x, seconds(nu) + 0.5, 'drum_x')
# body after drum_x
nu = pad(TALK + body, seconds(nu) - 1, 'body')
# url after body
nu = pad(URLS + url, seconds(nu) + 1, 'url')
# reverse guitar : ends after url
rguit = TEMP + guitar
%x(sox #{GUIT + guitar} #{rguit} reverse)
nu = pad(rguit, (seconds(nu) - seconds(rguit)) + 2, 'guitar_out')
%x(rm #{rguit})
# final drum ends at same time as reverse guitar
pad(DRUM + drum_out, (seconds(nu) - seconds(DRUM + drum_out)) + 2.2, 'drum_out')
# move it all to a subdir
subdir = OUT + title[0,2]
%x(mkdir -p #{subdir})
%x(mv #{OUT + '*.wav'} #{subdir + '/'})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment