Rename snprintf when building with MSVC to link with CRT.
[maemo-rb.git] / tools / talkclips.py
blob425c1b12bf808119d83d5e9e20d2872b5d6b731f
1 #!/usr/bin/python
2 # -*- coding: utf8 -*-
3 # __________ __ ___.
4 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
5 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
6 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
7 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
8 # \/ \/ \/ \/ \/
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.
21 import os
22 import sys
23 # The following lines provide variables which you can modify to adjust
24 # settings... See comments next to each line.
25 # Start opts:
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
31 # End opts
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))
39 print USAGE
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'
44 print USAGE
45 exit (-1) # espeak not found
47 if len(sys.argv) != 2:
48 print USAGE
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)
59 exit (-1) # not a dir
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."""
67 if isdir: # directory
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
75 except OSError:
76 log.write('Failed to create clip for directory: "%s"\n' % (clipname))
77 return False
78 log.write( 'Created clip for directory: "%s"\n' % (clipname)) # log
79 return True
80 else: # file
81 output=fullpath+".talk"
82 if os.path.exists(output):
83 return True # no need to create again
84 try:
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
94 def istalkclip(file):
95 """Is file a talkclip?
97 Returns True if file is a .talk clip for rockbox, otherwise returns
98 False."""
100 if '_dirname.talk' in file or '.talk' in file:
101 return True # is talk clip
102 else: # Not a talk clip
103 return False
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
114 continue
115 else: # a file
116 if istalkclip (item):
117 continue # is a talk clip
118 else: # create clip
119 gentalkclip(item, os.path.join(directory, item), False) # a file
120 continue
122 walker(RBDIR) # start the program:)
123 log.close() # close the log and finish