4 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 # Copyright © 2010 Daniel Dalton <daniel.dalton10@gmail.com>
12 # This program is free software; you can redistribute it and/or
13 # modify it under the terms of the GNU General Public License
14 # as published by the Free Software Foundation; either version 2
15 # of the License, or (at your option) any later version.
17 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 # KIND, either express or implied.
23 # The following lines provide variables which you can modify to adjust
24 # settings... See comments next to each line.
26 espeak
= '/usr/bin/espeak' # location of easpeak binary
27 rbspeexenc
= './rbspeexenc' # path to rbspeexenc binary (default currentdir)
28 VOPTS
=espeak
+" -s 320 -z" # Your espeak opts
29 ROPTS
=rbspeexenc
+" -q 4 -c 10" # rbspeex opts
30 logfile
="/tmp/talkclips.log" # a file where output should be logged
32 # Don't touch the below settings. Unless you know what your doing.
33 log
=open(logfile
, 'w') # logging leave this var alone.
34 USAGE
="Usage: %s <directory>" % (sys
.argv
[0]) # usage prompt don't touch
35 if not os
.path
.exists(rbspeexenc
):
36 print "%s not found, please change your rbspeexenc path appropriately,\n"\
37 "or place the binary in %s\n"\
38 % (rbspeexenc
, os
.path
.realpath(rbspeexenc
))
40 exit (-1) # Rbspeexenc not found
41 if not os
.path
.exists(espeak
):
42 print "Espeak not found, please install espeak, or adjust the path of\n"\
43 'the "espeak" variable appropriately.\n'
45 exit (-1) # espeak not found
47 if len(sys
.argv
) != 2:
49 exit (-1) # user failed to supply enough arguments
51 RBDIR
=sys
.argv
[1] # grab user input on the command line (don't touch)
52 if not os
.path
.exists(sys
.argv
[1]):
53 print "The path %s doesn't exist, please try again.\n\n%s"\
54 % (sys
.argv
[1], USAGE
)
55 exit(-1) # path doesn't exist
56 else: # check if it's a dir
57 if not os
.path
.isdir(sys
.argv
[1]): # a file
58 print "This script only currently works for directories.\n\n%s" % (USAGE
)
61 def gentalkclip(clipname
, fullpath
, isdir
):
62 """Generate an individual talk clip.
64 Based on the file name structure of talk clips, run them through the
65 synth, and encoder, and save accordingly."""
68 output
=os
.path
.join(fullpath
, "_dirname.talk") # dir clip name
69 if os
.path
.exists(output
):
70 return True # no need to create again
71 try: # Don't let the script stop if bash raises filename errors
72 os
.system('%s "%s" -w "%s"' % (VOPTS
, clipname
, output
+".tmp"))
73 os
.system('%s "%s" "%s"' % (ROPTS
, output
+".tmp", output
))
74 os
.remove(output
+".tmp") # delete the old wav file
76 log
.write('Failed to create clip for directory: "%s"\n' % (clipname
))
78 log
.write( 'Created clip for directory: "%s"\n' % (clipname
)) # log
81 output
=fullpath
+".talk"
82 if os
.path
.exists(output
):
83 return True # no need to create again
85 os
.system('%s "%s" -w "%s"' % (VOPTS
, clipname
, output
+".tmp"))
86 os
.system('%s "%s" "%s"' % (ROPTS
, output
+".tmp", output
))
87 os
.remove (output
+".tmp")
88 except OSError: # don't let bash errors stop us
89 log
.write('Failed to create clip for file: "%s"\n' % (clipname
))
90 return False # something failed, so continue with next file
91 log
.write('Created clip for file: "%s"\n' % (clipname
)) # logging
92 return True # clips created
95 """Is file a talkclip?
97 Returns True if file is a .talk clip for rockbox, otherwise returns
100 if '_dirname.talk' in file or '.talk' in file:
101 return True # is talk clip
102 else: # Not a talk clip
105 def walker(directory
):
106 """Walk through a directory.
108 Walk through a directory and subdirs, and operate on it, passing files
109 through to the correct functions to generate talk clips."""
110 for item
in os
.listdir(directory
): # get subdirs and files
111 if os
.path
.isdir(os
.path
.join(directory
, item
)):
112 gentalkclip (item
, os
.path
.join(directory
, item
), True) # its a dir
113 walker(os
.path
.join(directory
, item
)) # go down into this sub dir
116 if istalkclip (item
):
117 continue # is a talk clip
119 gentalkclip(item
, os
.path
.join(directory
, item
), False) # a file
122 walker(RBDIR
) # start the program:)
123 log
.close() # close the log and finish