Created
October 15, 2012 11:52
-
-
Save bofh/3892086 to your computer and use it in GitHub Desktop.
learning words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Name files like: L12-14 foreign translation.wav | |
import re, os, sys, tempfile, shutil | |
from subprocess import call | |
class Word: | |
filename = "" | |
def __init__(self, filename): | |
self.filename = filename | |
self.parse() | |
self.tmp = tempfile.mkdtemp() | |
def parse(self): | |
self.parsed = re.search('L(\d*)-(\d*) ([^\s]+) (.*)\.wav', self.filename) | |
def part(self, no): | |
return self.parsed.group(no) | |
def lesson(self): | |
return self.part(1) | |
def track(self): | |
return self.part(2) | |
def foreign(self): | |
return self.part(3) | |
def translation(self): | |
return self.part(4) | |
def __str__(self): | |
return self.foreign() + " " + self.translation() | |
def cleanup(self): | |
shutil.rmtree(self.tmp) | |
return | |
def say_translation(self): | |
t = self.translation() | |
# say it | |
n = self.tmp + '/' + t + '.aiff' | |
call(['say', '--channels', '2', '-o', n, t]) | |
# convert to WAV | |
self.translation_wav = self.tmp + '/' + t + '.wav' | |
call(['sox', n, '-r', '44.1k', self.translation_wav]) | |
# delete aiff | |
os.remove(n) | |
def merge_original(self): | |
call(['sox', self.translation_wav, os.path.join(IN_PATH, self.filename), os.path.join(OUT_PATH, self.filename), 'pad', '1', '1']) | |
def make(self): | |
self.say_translation() | |
self.merge_original() | |
if len(sys.argv) > 1: | |
IN_PATH = sys.argv[1] | |
else: | |
IN_PATH = "." | |
OUT_PATH=os.path.join(IN_PATH, 'out') | |
if not os.path.exists(OUT_PATH): | |
os.mkdir(OUT_PATH) | |
for filename in os.listdir(IN_PATH): | |
_, extension = filename.split('.') | |
if not extension == 'wav': | |
continue | |
w = Word(filename) | |
print '* ' + w.__str__() | |
w.make() | |
w.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment