Looking for batch solution for text to speech conversion.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
peejaygee
Posts: 6
Joined: 15 Jun 2017 13:26

Looking for batch solution for text to speech conversion.

#1 Post by peejaygee » 15 Jun 2017 13:50

Hey All,

I don't know where to start and a google search turned up these forums, so I figured I'd start here.

I'm looking to see if it's possible to create a batch (.bat) file, that will parse a text file, and replace a fixed string with a couple of variables in the batch script, then pass the line of text to a 3rd party open source app (that has command line options) which will then convert said text into a .mp3 file, the filename of the said file would be an increasing number.

So, lets say, I have

[name] = Bob
[direction1] = north
[direction2] = south

textfile to parse would/could contain

hello, my name is [name], and I am from the [direction1], but we will go [direction2] today
so, [name] said we should go [direction2], but I felt we needed to go [direction1]

each line could output, something like

audio_output_direction_0001.mp3
audio_output_direction_0002.mp3

the 3rd party software i"m looking to use is https://github.com/brookhong/tts which is free, and has command line output

looking at it's instructions, I could then pass it at the beginning of the script (I'd have to first run an output of -F and -V to get my voices and output qualities)

[voice] = 5 (which would be Microsoft Zira Desktop, in my case)
[quality] = 10 (which would be SPSF_48kHz16BitStero, in my case)
the other variables I specified above

and I know a simple script output from it worked, as I tried it.

tts.exe -f 10 -v 5 "hello, my name is bob, and I am from the north, but we will go south today" -o audio_output_direction_0001 -t

and it outputs to a mp3 (puts an extra 0 on the end for some reason, but I could file rename that)

If you guys are unable to help, even a another forum I could be pointed too would also help me immensely.

I know this could be done by hand, but it would be nice if I only had to change a few variables and the rest was auto'magic'

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Looking for batch solution for text to speech conversion.

#2 Post by elzooilogico » 16 Jun 2017 06:03

If I understand, you want to get some user input, read a text file and record as sound,
changing some keywords of the text file to the actual value given by user.
Is that right?

Well, here we go

Code: Select all

@echo off

SetLocal
cls

:: %~dp0 is the directory this script is run from, so change
:: set "mp3Convert=%~dp0\tts.exe" to whatever the actual location is (i.e)
:: set "mp3Convert=C:\Program Files (x86)\conversor\tts.exe"
set "textFile=%~dp0\sample.txt"
set "mp3Convert=%~dp0\tts.exe"

::create a sample text file if it doesn't exist
if not exist "%textFile%" (
  >"%textFile%" (
    echo/hello, my name is !name!, and I am from the !dir1!, but we will go !dir2! today
    echo/so, !name! said we should go !dir2!, but I felt we needed to go !dir1!
  )
)

::get user input
echo/&echo/
set /p name="Enter name: "
set /p dir1="Enter direction 1: "
set /p dir2="Enter direction 2: "

:: create a directory based on name if it doesn't exist
:: again, create under directory this script is run from
:: change %~dp0\ to whatever destination folder
set "destDir=%~dp0\%name%"
if not exist "%destDir%" mkdir "%destDir%"
:: change to destination directory
pushd "%destDir%"

::convert to wav (no external tools needed)
call :convertWAV

::convert to mp3 (using tts) (NOT TESTED!!)
call :convertMP3

:: return to source directory
popd

EndLocal
exit/B

:convertMP3
SetLocal EnableDelayedExpansion
set "voice=5"
set "quality=10"
set/a counter=0
for /f "tokens=*" %%i in (%textFile%) do (
  set/a counter+=1 & set "suffix=000!counter!" & set "suffix=!suffix:~-4!"
  del audio_output_direction_!suffix! >nul 2>nul
  "%mp3Convert%" -f %quality% -v %voice% "%%~i" -o "audio_output_direction_!suffix!" -t >nul && echo/Conversion successful. || echo/Export to .MP3 Failed. 
)
EndLocal 
exit/B

:convertWAV
SetLocal EnableDelayedExpansion
set/a counter=0
set "vbsFile=%temp%\speech.vbs"
> "%vbsFile%" (
  echo/const SAFT48kHz16BitStereo = 39
  echo/const SAFT16kHz16BitStereo = 19
  echo/const SSFMCreateForWrite = 3 ' overwrite existing file
  echo/set oFileStream = CreateObject("SAPI.SpFileStream"^)
  echo/oFileStream.Format.Type = SAFT16kHz16BitStereo
  echo/set oVoice = CreateObject("SAPI.SpVoice"^)
  for /f "tokens=*" %%i in (%textFile%) do (
    set/a counter+=1 & set "suffix=000!counter!" & set "suffix=!suffix:~-4!"
    echo/oFileStream.Open "audio_output_direction_!suffix!.wav", SSFMCreateForWrite
    echo/set oVoice.AudioOutputStream = oFileStream
    echo/oVoice.speak "%%~i"
    echo/oFileStream.Close
  )
)
Cscript.exe /B /E:vbs "%vbsFile%" >NUL
del "%vbsFile%" >nul 2>nul
EndLocal
exit/B
Last edited by elzooilogico on 18 Jun 2017 05:43, edited 3 times in total.

peejaygee
Posts: 6
Joined: 15 Jun 2017 13:26

Re: Looking for batch solution for text to speech conversion.

#3 Post by peejaygee » 16 Jun 2017 07:34

elzooilogico wrote:
Well, here we go



Wow, thanks.. I'll try this right now and see what I get.

I'm kinda guessing I just put in the text file

this is my name and i'm going dir1 today, tomorrow i'll go dir2

and it will change it? well, we'll have to see.

Thanks again for this.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Looking for batch solution for text to speech conversion.

#4 Post by elzooilogico » 16 Jun 2017 13:51

peejaygee wrote:I'm kinda guessing I just put in the text file

this is my name and i'm going dir1 today, tomorrow i'll go dir2

You must write the input file's strings you want to replace between exclamation marks (that's the magic delayed expansion do).

Code: Select all

this is my !name! and i'm going !dir1! today, tomorrow i'll go !dir2!

When file is being read, the content between exclamation marks will be expanded (due to delayed expansion) to whatever the variable content is.

In this case, if variables name, dir1, dir2 don't exist or are undefined or empty, they will be expanded to nothing!

Simply, set content to whatever you want,

Code: Select all

set "name=Phil"
set "dir1=Washington"
set "dir2=New York"

or ask the user (as in the example)

Code: Select all

set /p name="Enter name: "
set /p dir1="Enter direction 1: "
set /p dir2="Enter direction 2: "

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Looking for batch solution for text to speech conversion.

#5 Post by PaperTronics » 16 Jun 2017 22:17

Text-to -speech conversion huh? Well you're in the right place. You can use my program Speecher, modify it by your needs and you can do the thing!
But be sure to give credits.

I suggest you read the full article on my website to understand its usage.

Link to Speecher : http://www.thebateam.org/2017/03/speecher-pro-text-to-speech-engine-in.html

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Looking for batch solution for text to speech conversion.

#6 Post by ShadowThief » 16 Jun 2017 22:49

PaperTronics wrote:Text-to -speech conversion huh? Well you're in the right place. You can use my program Speecher, modify it by your needs and you can do the thing!
But be sure to give credits.

I suggest you read the full article on my website to understand its usage.

Link to Speecher : http://www.thebateam.org/2017/03/speecher-pro-text-to-speech-engine-in.html

Meh, solving the problem instead of using somebody else's solution can result in code that can be used for different things in the future; the question isn't really about text-to-speech, it's about dynamically filling in a template.

peejaygee
Posts: 6
Joined: 15 Jun 2017 13:26

Re: Looking for batch solution for text to speech conversion.

#7 Post by peejaygee » 17 Jun 2017 10:15

ShadowThief wrote:Meh, solving the problem instead of using somebody else's solution can result in code that can be used for different things in the future; the question isn't really about text-to-speech, it's about dynamically filling in a template.


Yeah, I have a couple of free text to speech programs, but thanks for the url to add to my arsenal, but as ShadowThief says, I'm looking for a means of processing a text file and having an output completed before passing it to the speech program.

peejaygee
Posts: 6
Joined: 15 Jun 2017 13:26

Re: Looking for batch solution for text to speech conversion.

#8 Post by peejaygee » 17 Jun 2017 10:59

@elzooilogico

Question, is it possible to tweak the script, so it's able to create a folder based on the 'name' that is provided, and then the samples will go into that?

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Looking for batch solution for text to speech conversion.

#9 Post by PaperTronics » 17 Jun 2017 11:35

Sorry guys, for my foolish behavior. I just thought that I could be of help to someone.

peejaygee
Posts: 6
Joined: 15 Jun 2017 13:26

Re: Looking for batch solution for text to speech conversion.

#10 Post by peejaygee » 17 Jun 2017 11:43

PaperTronics wrote:Sorry guys, for my foolish behavior. I just thought that I could be of help to someone.


it's not 'foolish behavior' I understood your reasoning for posting, maybe go about it differently next time and offer to post the link if people want it, rather than post it right off the bat (pun intended) :) Then if it's needed it can be posted rather than it looking like a 'plug' to your site. I probably would do the same, but ask if anybody wanted it first. :) it's nice to still see people out there offering software that is open source and does what someone wants it too. If I hadn't found another opensource text to speech application I probably would have downloaded yours. Keep up the good work.

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Looking for batch solution for text to speech conversion.

#11 Post by elzooilogico » 18 Jun 2017 03:24

peejaygee wrote:@elzooilogico
Question, is it possible to tweak the script, so it's able to create a folder based on the 'name' that is provided, and then the samples will go into that?


Code edited. This is not a free coding site, please do some research on your own.

peejaygee
Posts: 6
Joined: 15 Jun 2017 13:26

Re: Looking for batch solution for text to speech conversion.

#12 Post by peejaygee » 18 Jun 2017 10:26

elzooilogico wrote:
peejaygee wrote:@elzooilogico
Question, is it possible to tweak the script, so it's able to create a folder based on the 'name' that is provided, and then the samples will go into that?


Code edited. This is not a free coding site, please do some research on your own.


I did, and came to the same conclusion as you, but mine was not as 'proficient' as yours, as I didn't think of 'checking for existence' along with it.

I was not expecting it to be a 'free coding site', I turned to you (and this forum) as I'm not as proficient in scripting in windows, as I am in 6502, GML, HTML and Java.

Have a nice day!

Post Reply