text to speech export to wav files?
November 5, 2015 1:09 PM Subscribe
Suppose I have a text file, is there something that can do English text-to-speech and save a series of wav files, one for each line in the text file? Suppose I'm using Windows.
Best answer: If you have PowerShell installed, it should let you do this very simply. There's an included module (System.speech) that can handle writing text-to-speech audio directly to file. Disclaimer, I've only tested this on my local Windows 7 machine.
1) Copy the following script into a text file, name it something like "textToSpeech.ps1" and save it somewhere appropriate. You'll need to make some modifications to the paths in the $textFilePath and $outputDirectoryPath variables to suit where your input file and output directories should go. This script will create the audio files with sequential numeric filenames, 1.wav, 2.wav, etc.
3) In PowerShell, navigate to the directory where you saved the script file using DOS commands. Type ".\textToSpeech.ps1" (or whatever you named the script file) and hit Enter to run it.
If everything goes well (fingers crossed), you should get a directory somewhere with a bunch of .wav files. If you start seeing errors, either the speech module isn't available or there might be a problem with the paths to the input file or output directory. It's also possible that PowerShell might get confused if it doesn't like the line-break format of your input file, so if you just get one long audio file as output you may need to re-save the file in a different text editor.
posted by figurant at 2:24 PM on November 5, 2015
1) Copy the following script into a text file, name it something like "textToSpeech.ps1" and save it somewhere appropriate. You'll need to make some modifications to the paths in the $textFilePath and $outputDirectoryPath variables to suit where your input file and output directories should go. This script will create the audio files with sequential numeric filenames, 1.wav, 2.wav, etc.
2) Open up a PowerShell terminal (start typing "PowerShell" in the start menu search and it should show up)$textFilePath = "C:\PATH\TO\INPUT\TEXT\FILE.txt" #important - make sure this directory path ends with a slash $outputDirectoryPath = "C:\PATH\TO\DIRECTORY\FOR\AUDIO\FILES\" Add-Type -AssemblyName System.speech $speak = New-Object System.Speech.Synthesis.SpeechSynthesizer $lineCounter = 0 foreach($line in get-content $textFilePath) { $speak.SetOutputToWaveFile($outputDirectoryPath + ($lineCounter -as [string]) + ".wav") echo ("Writing file " + $outputDirectoryPath + ($lineCounter -as [string]) + ".wav") $speak.Speak($line) $lineCounter++ } $speak.Dispose()
3) In PowerShell, navigate to the directory where you saved the script file using DOS commands. Type ".\textToSpeech.ps1" (or whatever you named the script file) and hit Enter to run it.
If everything goes well (fingers crossed), you should get a directory somewhere with a bunch of .wav files. If you start seeing errors, either the speech module isn't available or there might be a problem with the paths to the input file or output directory. It's also possible that PowerShell might get confused if it doesn't like the line-break format of your input file, so if you just get one long audio file as output you may need to re-save the file in a different text editor.
posted by figurant at 2:24 PM on November 5, 2015
fyi, the python script that mattamatic linked to returns empty files and lots of 503s.
posted by scruss at 3:01 PM on November 5, 2015
posted by scruss at 3:01 PM on November 5, 2015
Sorry about that, I used that exact script it to setup a phone system a while back and it did work then. The second link is still sound and the direct google URLs are pretty easy to build for a techie, but I'm not personally familiar with any of the other scripts to pull it down. The powershell solution is probably simplest.
posted by mattamatic at 3:12 PM on November 5, 2015
posted by mattamatic at 3:12 PM on November 5, 2015
Response by poster: For now, I've used figurant's PowerShell script.
This is a temporary solution which will be replaced eventually with a voice actor once the copy is finalized, so it's probably not worth any extra work to get the Google voice even if it sounds nicer.
posted by RobotHero at 3:28 PM on November 5, 2015 [1 favorite]
This is a temporary solution which will be replaced eventually with a voice actor once the copy is finalized, so it's probably not worth any extra work to get the Google voice even if it sounds nicer.
posted by RobotHero at 3:28 PM on November 5, 2015 [1 favorite]
The GoogleTextToSpeech.py one works with the following minor patch (2 lines changed):
posted by neckro23 at 3:38 PM on November 5, 2015 [3 favorites]
diff --git a/GoogleTextToSpeech.py b/GoogleTextToSpeech.py index 212a4ef..aa57348 100755 --- a/GoogleTextToSpeech.py +++ b/GoogleTextToSpeech.py @@ -76,9 +76,9 @@ def downloadAudioFile(text_lines, language, audio_file): ''' for idx, line in enumerate(text_lines): query_params = {"tl": language, "q": line, "total": len(text_lines), "idx": idx} - url = "http://translate.google.com/translate_tts?ie=UTF-8" + "&" + unicode_urlencode(query_params) + url = "http://translate.google.com/translate_tts?ie=UTF-8&client=t&" + unicode_urlencode(query_params) headers = {"Host":"translate.google.com", "User-Agent":"Mozilla 5.10"} - req = urllib2.Request(url, '', headers) + req = urllib2.Request(url, None, headers) sys.stdout.write('.')
posted by neckro23 at 3:38 PM on November 5, 2015 [3 favorites]
This thread is closed to new comments.
posted by mattamatic at 1:42 PM on November 5, 2015 [2 favorites]