FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
synth.py 69.5 KiB
Newer Older
Silas S. Brown's avatar
Silas S. Brown committed
            r.append(",")
        elif not e.__class__==Event: r.append(fname(str(e)))
    return " ".join(r)

Silas S. Brown's avatar
Silas S. Brown committed
def abspath_from_start(p): # for just_synthesize to check for paths relative to the original starting directory if this is different from the gradint directory (see system.py)
    d=os.getcwd()
    os.chdir(starting_directory)
    try: r=os.path.abspath(p)
    except: r="" # library problems on Windows?
    os.chdir(d)
    return r

Silas S. Brown's avatar
Silas S. Brown committed
def just_synthesize(callSanityCheck=0,lastLang_override=None):
Silas S. Brown's avatar
Silas S. Brown committed
    # Handle the justSynthesize setting (see advanced.txt)
    global startAnnouncement,endAnnouncement,logFile,synth_partials_cache
    synth_partials_cache = {} # to stop 'memory leak' when running from the GUI
    oldStart,oldEnd,oldLogfile = startAnnouncement,endAnnouncement,logFile
    startAnnouncement=endAnnouncement=logFile=None
    if app: app.setLabel("") # not "wait a moment"
    called_synth = 0
    # we re-generate the lesson on each repeat, so sporadic-synthcache stuff works
    global repeatMode ; repeatMode = 1
    while repeatMode:
      repeatMode = 0
      less = Lesson()
      lastStartTime = lastEndTime = lastWasDelay = 0
Silas S. Brown's avatar
Silas S. Brown committed
      if lastLang_override: lastLanguage = lastLang_override
      else: lastLanguage = secondLanguage
      def checkCanSynth(fname):
          ret=can_be_synthesized(fname)
          if ret: return fileToEvent(fname)
Silas S. Brown's avatar
Silas S. Brown committed
          else: show_warning("Can't say "+repr(fname)) # previous warnings should have said why (e.g. partials-only language)
Silas S. Brown's avatar
Silas S. Brown committed
      for line in justSynthesize.split("#"):
        line = line.strip(wsp) ; l = line.split(None,1)
        if extsep in line and fileExists(line): event = fileToEvent(line,"")
Silas S. Brown's avatar
Silas S. Brown committed
        elif extsep in line and fileExists(abspath_from_start(line)): event = fileToEvent(abspath_from_start(line),"")
Silas S. Brown's avatar
Silas S. Brown committed
        elif line=='R':
            repeatMode=1 ; continue
        elif len(l)==1:
            try: delayVal = float(l[0])
            except ValueError: delayVal = None
            if delayVal==None:
                # no float value; assume it's a single word to synth in secondLanguage or whatever was the last language used
Silas S. Brown's avatar
Silas S. Brown committed
                show_warning("Assuming that %s is a word to synthesize in language '%s'" % (repr(l[0]),lastLanguage))
Silas S. Brown's avatar
Silas S. Brown committed
                if callSanityCheck and sanityCheck(l[0],lastLanguage,1): return
Silas S. Brown's avatar
Silas S. Brown committed
                event = checkCanSynth("!synth:"+l[0]+"_"+lastLanguage)
                if not event: continue # couldn't synth
Silas S. Brown's avatar
Silas S. Brown committed
                called_synth = 1
            else:
                lastWasDelay = 1
                if delayVal<0: lastEndTime = lastStartTime-delayVal
                else: lastEndTime += delayVal
                continue
        elif len(l)==2:
            lang, text = l
            if lang=="sh:": event = ShellEvent(text)
            else:
                fname = "!synth:"+text+"_"+lang
                if not can_be_synthesized(fname):
                    if lang in [firstLanguage,secondLanguage]+otherLanguages:
Silas S. Brown's avatar
Silas S. Brown committed
                        show_warning("Can't say %s in %s" % (repr(text),repr(lang)))
Silas S. Brown's avatar
Silas S. Brown committed
                        lastLanguage=lang ; continue
                    # otherwise, user might have omitted lang by mistake
Silas S. Brown's avatar
Silas S. Brown committed
                    show_warning("Assuming %s was meant to be synthesized in language '%s'" % (cond("#" in justSynthesize or len(repr(line))<10,"that '"+repr(line)+"'","this line"),lastLanguage))
Silas S. Brown's avatar
Silas S. Brown committed
                    if callSanityCheck and sanityCheck(line,lastLanguage,1): return
Silas S. Brown's avatar
Silas S. Brown committed
                    event = checkCanSynth("!synth:"+line+"_"+lastLanguage)
Silas S. Brown's avatar
Silas S. Brown committed
                else:
                    if callSanityCheck and sanityCheck(text,lang,1): return
Silas S. Brown's avatar
Silas S. Brown committed
                    event = checkCanSynth(fname)
Silas S. Brown's avatar
Silas S. Brown committed
                    lastLanguage = lang
Silas S. Brown's avatar
Silas S. Brown committed
                if not event: continue
Silas S. Brown's avatar
Silas S. Brown committed
                called_synth = 1
        else: continue # len(l)==0: ignore empty strings between #s
        event.addToEvents(less.events,lastEndTime)
        lastWasDelay = 0
        lastStartTime = lastEndTime
        lastEndTime += event.length
      if lastWasDelay: Event(0).addToEvents(less.events,lastEndTime)
      global dbase ; dbase = None # for handleInterrupt
      less.play()
    startAnnouncement,endAnnouncement,logFile = oldStart,oldEnd,oldLogfile
    if not called_synth: return None
    return lastLanguage