forked from latex-access/latex-access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latex_record.py
executable file
·81 lines (68 loc) · 2.72 KB
/
latex_record.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# latex_record.py
# A part of the latex-access project at http://latex-access.sourceforge.net/
# Author: Daniel Dalton <[email protected]>
# Copyright (C) 2012 Daniel Dalton/latex-access Contributors
#
# This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation;
# either version 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with this program; if not, visit <http://www.gnu.org/licenses>
"""This module is designed to record a whole document of latex into an
audio file.
At the current time this is just experimental. The motivation for such a
tool is so that you can listen your notes for instance on some audio
player perhaps a smart phone or mp3 player. In conjunction with
fast-forward, rewind, pause and in some cases speed control, I'm hoping
this might be useful to quickly review notes while on the train or
similar."""
from latex_access import speech
import os
import sys
import wave
import subprocess
if len (sys.argv) < 2:
print "Usage: %s <path-to-latex-document.text>" % (sys.argv[0])
exit (-1)
if not os.path.exists (sys.argv[1]):
print "Path %s does not exist." % (sys.argv[1])
exit (-1)
if not os.path.isfile (sys.argv[1]):
print "%s must be a file." % (sys.argv[1])
exit (-1)
def compileFiles (number):
"""Compile all the separate wav files into one file.
Takes all lines of input which were saved into wav files and saves
them into one file in the right order of course."""
infiles=[]
for name in range (0,number):
infiles.append("temp"+str(name))
outfile = sys.argv[1]+".wav"
data= []
for infile in infiles:
w = wave.open(infile, 'rb')
data.append( [w.getparams(), w.readframes(w.getnframes())] )
w.close()
os.remove (infile)
output = wave.open(outfile, 'wb')
output.setparams(data[0][0])
for item in range(0, len(data)):
output.writeframes(data[item][1])
output.close()
s=speech.speech ()
latexFile=open(sys.argv[1], "r")
count=0
startRecording=False
for line in latexFile.readlines ():
if not startRecording and '\\begin{document}' not in line:
continue
startRecording = True
if line == '' or line.isspace ():
continue # ignore blank lines which produce no audio
subprocess.call(["espeak","-w", "temp%s" % str(count), "-s","225", s.translate(line)])
count+=1
compileFiles (count)