Skip to content

Instantly share code, notes, and snippets.

@knorrli
Created January 7, 2025 01:08
Show Gist options
  • Save knorrli/ed42d29bd5190655c682efc7368e92fa to your computer and use it in GitHub Desktop.
Save knorrli/ed42d29bd5190655c682efc7368e92fa to your computer and use it in GitHub Desktop.
csv_to_audio.rb
# frozen_string_literal: true
require 'csv'
##### CSV To Audio Speech Converter ####
#== README
# This script takes a CSV file and converts its contents to audio files by
# using the OSX builtin speech synthesizer. The CSV input file must not contain
# a header line and all words or lines must be stored in the first column. If
# your words or lines contain commas, the whole line must be quoted with double
# quotes ("), otherwise the CSV parser will interpret the comma as a column
# separator.
#
# If you need an example for the input, the following command will write an
# example CSV to your Desktop if you run it in the terminal:
# echo "Hello\nTest\nThis is a sentence\n\"Sentences with commas, must be quoted\"" > ~/Desktop/input.csv
#
# NOTE: Running random scripts from the Internet is never a good idea, please
# make sure that you understand what the code does before you run this!
# This script will do the following:
# - Create a new Folder called "output"
# - Read a File "input.csv" from your computer
# - Use the OSX builtin speech synthesizer "say" to convert the CSV contents to audio files
# - Store the audio files in the "output" folder
#
# Please read the code below and verify that no malicious code is executed on your computer.
#
# To run the script, perform the following steps:
# 1. Move the CSV with your input to the Desktop, and name the file "input.csv"
# 2. Download this ruby script and save it to your Desktop with the filename "csv_to_audio.rb"
# 3. Open the Terminal app (found in Applications > Utilities)
# 4. Switch to the Desktop directory in the terminal with the following command: cd ~/Desktop
# 3. Make this ruby script executable with the following command: chmod +x ~/Desktop/csv_to_audio.rb
# 4. Execute this ruby script with the following command: ruby ./csv_to_audio.rb
#==
#== SETTINGS: edit the following settings to control input/output
### INPUT FILE
# The location of the input file. By default, it assumes that the input file is
# named 'input.csv' and is stored in the same directory where you run this script.
# You can change this by changing the path like this:
# input_file = '/path/to/my/input_file.csv'
input_file = './input.csv'
### OUTPUT FOLDER
# The output files will be stored in the folder with the name 'output' in the
# same directory as this script.
# You can change the folder name by changing the string 'output' below like this:
# output_folder = '/path/to/output_folder'
output_folder = './output'
### OUTPUT FILENAMES
# By default, the files will be numbered starting from 1.
# You can change this to name the files matching with the content, or both a number and words.
# To do this, remove the leading '#' from one of the lines below:
output_format = :number
# output_format = :words
# output_format = :number_and_words
### OUTPUT VOICE
# You can choose different voices for the speech synthesizer. Some of them are
# optimized for different languages. By default, it will use the voice that is
# configured in your system settings, see:
# System Settings > Accessibility > Spoken Content > System voice
#
# To obtain a list of available, run the following command in your terminal:
# say -v "?"
#
# If you want to change the voice, remove the leading '#' from one of the lines below:
# voice = 'Daniel' # this is a normal US male voice
# voice = 'Samantha' # this is a normal US female voice
# voice = 'Zarvox' # this is a roboter voice
##############################################################
##############################################################
#### SCRIPT: only edit this if you know what you're doing ####
# create the output folder if it does not exist yet
system("mkdir -p #{output_folder}")
# Set starting file number
filenumber = 0
# Process the CSV data
CSV.table(input_file, headers: false).each do |line|
# ensure multiple columns are joined to a single output
words = line.join(', ')
# skip empty lines
next if words.empty?
# increment the filenumber for the current line
filenumber += 1
# Show you in the terminal what is being processed
puts "Processing: [#{filenumber}] #{words}"
### Choose how you want the output to be named based on the setting above
filename = if output_format == :words
words.split(' ').join('_')
elsif output_format == :number_and_words
"#{filenumber}_#{words.split(' ').join('_')}"
else
filenumber
end
# Execute a system command to run the "say" program of OSX with the necessary parameters
system("say #{defined?(voice) ? "--voice=#{voice}" : ''} --output-file='#{output_folder}/#{filename}.wav' --data-format=LEF32@32000 #{words}")
end
# Report that the script has terminated
puts "DONE! converted #{filenumber} lines to audio files. Your audio files are stored here: #{File.absolute_path(output_folder)}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment