Пример #1
0
def sayAndCache_internal( strTextToSay, bJustPrepare, bStoreToNonVolatilePath, bDirectPlay, nUseLang, bWaitEnd, bCalledFromSayAndCacheFromLight, strUseVoice ):
  "generate a text in a file, then read it, next time it will be directly played from that file"
  "bJustPrepare: render the text to a file, but don't play it now"
  "bStoreToNonVolatilePath: copy the generated file to a non volatile path (/usr/generatedvoices)"
  "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!"
  "strUseVoice: if different of None or default: use specific voice"
  "return the length of the text in seconds, or None if impossible"
  print( "sayAndCache_internal( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, bDirectPlay: %s, nUseLang: %d, bWaitEnd: %s, bCalledFromSayAndCacheFromLight: %s, strUseVoice: '%s' )" % ( strTextToSay, str( bJustPrepare ), str( bStoreToNonVolatilePath ), str( bDirectPlay ), nUseLang, str( bWaitEnd ), str( bCalledFromSayAndCacheFromLight ), str( strUseVoice ) ) );
  if( not config.bPrecomputeText ):
      print( "sayAndCache: disabled by configuration: bPrecomputeText is false" );
      if( bJustPrepare ):
          return None; # do nothing
      tts = naoqitools.myGetProxy( "ALTextToSpeech" );
      tts.say( strTextToSay );
      return None;
  print( "sayAndCache: FORCING DIRECT_PLAY CAR SINON C'EST BUGGE DANS LA VERSION COURANTE!");  
  bDirectPlay = True;
  
  if( strUseVoice == "default" ):
      strUseVoice = None;
  
  if( config.bRemoveDirectPlay ):
    print( "WRN: DISABLING DIRECT_PLAY SETTINGS for testing/temporary purpose" );
    bDirectPlay = False;  
  
  strTextToSay = assumeTextHasDefaultSettings( strTextToSay, nUseLang );
  szFilename = sayAndCache_getFilename( strTextToSay, nUseLang, strUseVoice );
  
  szPathVolatile = pathtools.getVolatilePath() + "generatedvoices" + pathtools.getDirectorySeparator();
  try:
    os.mkdir( szPathVolatile );
  except BaseException, err:
    pass
Пример #2
0
def playSound(strFilename, bWait=True, bDirectPlay=False, nSoundVolume=100):
    "Play a sound, return True if ok"
    "bDirectPlay: play it Now! (could fragilise system and video drivers"
    "nSoundVolume: if bDirectPlay is on, will play the sound with a specific volume (ndev)"

    print(
        "playSound( '%s', bWait = %s, bDirectPlay = %s, nSoundVolume = %d )" %
        (strFilename, str(bWait), str(bDirectPlay), nSoundVolume))

    if (config.bRemoveDirectPlay):
        print(
            "WRN: DISABLING_DIRECTPLAY SETTINGS for testing/temporary purpose")
        bDirectPlay = False

    try:
        # If strFilename has an absolute path, go ahead with this path !
        if strFilename.startswith(pathtools.getDirectorySeparator()):
            strSoundFile = strFilename
        else:
            strSoundFile = pathtools.getApplicationSharedPath(
            ) + "wav/0_work_free/" + strFilename
            if (not filetools.isFileExists(strSoundFile)):
                # then try another path
                strSoundFile = pathtools.getApplicationSharedPath(
                ) + "wav/1_validated/" + strFilename
            if (not filetools.isFileExists(strSoundFile)):
                # and another path
                strSoundFile = pathtools.getApplicationSharedPath(
                ) + "wav/0_work_copyright/" + strFilename
            if (not filetools.isFileExists(strSoundFile)):
                # and another path
                strSoundFile = pathtools.getApplicationSharedPath(
                ) + "wav/" + strFilename
            if (not filetools.isFileExists(strSoundFile)):
                # and another path
                strSoundFile = pathtools.getNaoqiPath(
                ) + "/share/naoqi/wav/" + strFilename
            if (not filetools.isFileExists(strSoundFile)):
                print("ERR: appu.playSound: can't find file '%s'" %
                      strFilename)
                return False

        analyseSound_pause(bWait)
        if (bDirectPlay):
            system.mySystemCall("aplay -q " + strSoundFile, bWait)
        else:
            global_proxyAudioPlayer = naoqitools.myGetProxy("ALAudioPlayer")
            if (global_proxyAudioPlayer == None):
                print(
                    "ERR: sound.playSound: can't find module 'ALAudioPlayer'")
            else:
                if (bWait):
                    global_proxyAudioPlayer.playFile(strSoundFile)
                else:
                    global_proxyAudioPlayer.post.playFile(strSoundFile)
        analyseSound_resume(bWait)
    except BaseException, err:
        debug.debug("playSound: ERR: " + str(err))
        print("errr: " + str(err))
Пример #3
0
def sayAndCacheAndLight( strTextToSay, bJustPrepare = False, bStoreToNonVolatilePath = False, nEyesColor = 0, nUseLang = -1 ):
    "say a cached text with light animation"
    "nEyesColor: 0: white, 1: blue, 2: green; 3: red"
    "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!"
    "return the length of the text in seconds, or None if impossible"
    print( "sayAndCacheAndLight( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, nEyesColor: %s, nUseLang: %s )" % ( strTextToSay, str( bJustPrepare ), str( bStoreToNonVolatilePath ), str( nEyesColor ), str( nUseLang ) ) );
    if( not config.bPrecomputeText ):
        print( "sayAndCacheAndLight: disabled by configuration: bPrecomputeText is false" );
        if( bJustPrepare ):
            return None; # do nothing
        tts = naoqitools.myGetProxy( "ALTextToSpeech" );
        tts.say( strTextToSay );
        return None;

    rLength = sayAndCache( strTextToSay, bJustPrepare = True, bStoreToNonVolatilePath = bStoreToNonVolatilePath, nUseLang = nUseLang, bCalledFromSayAndCacheFromLight = True ); # we store it to disk, only if we must do it

    # this two lines are done too in sayAndCache...
    strTextToSay = assumeTextHasDefaultSettings( strTextToSay, nUseLang );
    szFilename = sayAndCache_getFilename( strTextToSay, nUseLang );

    szPathVolatile = pathtools.getVolatilePath();
    rSampleLenSec = 0.05;
#    szPathFilenamePeak = szPathVolatile + szFilename + ("_%5.3f.egy" % rSampleLenSec);
    szPathFilenamePeak = szFilename + ("_%5.3f.egy" % rSampleLenSec);
    szPathFilenamePeakCache = pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szPathFilenamePeak;
    szPathFilenamePeak = szPathVolatile + szPathFilenamePeak;
    anLedsColorSequency = [];
    aBufFile = "";
    bFileGenerated = False;
    if( not filetools.isFileExists( szPathFilenamePeak ) ):
        if( filetools.isFileExists( szPathFilenamePeakCache ) ):
            copyFile( szPathFilenamePeakCache, szPathFilenamePeak );
    if( not filetools.isFileExists( szPathFilenamePeak ) ):
        # generate peak file
        timeBegin = time.time();
        print( "sayAndCacheAndLight: generating peak light - begin\n" );
        szPathFilename = szPathVolatile + szFilename + ".raw";
        anLedsColorSequency = [];
        try:
            une = naoqitools.myGetProxy( 'UsageNoiseExtractor' );
            anLedsColorSequency = une.analyseSpeakSound( szPathFilename, int( rSampleLenSec * 1000 ), False );
        except BaseException, err:
            print( "ERR: sayAndCacheAndLight( '%s' ): err: %s" % ( strTextToSay, str( err ) ) );
            print( "ERR: sayAndCacheAndLight => trying old cpp version" );
            anLedsColorSequency = analyseSpeakSound( szPathFilename, rSampleLenSec * 1000 );
        print( "sayAndCacheAndLight: analyseSpeakSound - end - time: %fs\n" % float( time.time() - timeBegin ) );        
#        print( "anLedsColorSequency: %d samples: %s\n" % ( len( anLedsColorSequency ), str( anLedsColorSequency ) ) );
        
        print( "Writing file with %d peak samples (time: %d)\n" % ( len( anLedsColorSequency ), int( time.time() ) ) );
        #         struct.pack_into( "f"*len( anLedsColorSequency ), aBufFile, anLedsColorSequency[:] );
        for peakValue in anLedsColorSequency:
            aBufFile += struct.pack( "f", peakValue );
        try:
            file = open( szPathFilenamePeak, "wb" );
            file.write( aBufFile );
        except RuntimeError, err:
            print( "ERR: sayAndCacheAndLight( '%s' ): err: %s" % ( strTextToSay, str( err ) ) );
Пример #4
0
def sayMumbled( strText ):
    sayAndCache( strText, bJustPrepare = True );
    strText = assumeTextHasDefaultSettings( strText );
    szFilename = sayAndCache_getFilename( strText );
    szPathFilename = pathtools.getVolatilePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szFilename + ".raw";
    szProcessed = szPathFilename + "_mumbled.raw";
    if( not processSoundMumbled( szPathFilename, szProcessed ) ):
        return False;
    nFreq = 22050;
    system.mySystemCall( "aplay -c1 -r%d -fS16_LE -q %s" % ( nFreq, szProcessed ) );    
    return True;
Пример #5
0
def sayMumbled(strText):
    sayAndCache(strText, bJustPrepare=True)
    strText = assumeTextHasDefaultSettings(strText)
    szFilename = sayAndCache_getFilename(strText)
    szPathFilename = pathtools.getVolatilePath(
    ) + "generatedvoices" + pathtools.getDirectorySeparator(
    ) + szFilename + ".raw"
    szProcessed = szPathFilename + "_mumbled.raw"
    if (not processSoundMumbled(szPathFilename, szProcessed)):
        return False
    nFreq = 22050
    system.mySystemCall("aplay -c1 -r%d -fS16_LE -q %s" % (nFreq, szProcessed))
    return True
Пример #6
0
def playSound( strFilename, bWait = True, bDirectPlay = False, nSoundVolume = 100 ):
    "Play a sound, return True if ok"
    "bDirectPlay: play it Now! (could fragilise system and video drivers"
    "nSoundVolume: if bDirectPlay is on, will play the sound with a specific volume (ndev)"
    
    print( "playSound( '%s', bWait = %s, bDirectPlay = %s, nSoundVolume = %d )" % ( strFilename, str( bWait ), str( bDirectPlay ), nSoundVolume ) );
    
    if( config.bRemoveDirectPlay ):
        print( "WRN: DISABLING_DIRECTPLAY SETTINGS for testing/temporary purpose" );
        bDirectPlay = False;
    
    try:
        # If strFilename has an absolute path, go ahead with this path !
        if strFilename.startswith( pathtools.getDirectorySeparator() ):
            strSoundFile = strFilename
        else:
            strSoundFile = pathtools.getApplicationSharedPath() + "wav/0_work_free/" + strFilename;
            if( not filetools.isFileExists( strSoundFile ) ):
                # then try another path
                strSoundFile = pathtools.getApplicationSharedPath() + "wav/1_validated/" + strFilename;
            if( not filetools.isFileExists( strSoundFile ) ):
                # and another path
                strSoundFile = pathtools.getApplicationSharedPath() + "wav/0_work_copyright/" + strFilename;
            if( not filetools.isFileExists( strSoundFile ) ):
                # and another path
                strSoundFile = pathtools.getApplicationSharedPath() + "wav/" + strFilename;
            if( not filetools.isFileExists( strSoundFile ) ):
                # and another path
                strSoundFile = pathtools.getNaoqiPath() + "/share/naoqi/wav/" + strFilename;
            if( not filetools.isFileExists( strSoundFile ) ):
                print( "ERR: appu.playSound: can't find file '%s'" % strFilename );
                return False;

        analyseSound_pause( bWait );
        if( bDirectPlay ):
            system.mySystemCall( "aplay -q " + strSoundFile, bWait );
        else:
            global_proxyAudioPlayer = naoqitools.myGetProxy( "ALAudioPlayer" );            
            if( global_proxyAudioPlayer == None ):
                print( "ERR: sound.playSound: can't find module 'ALAudioPlayer'" );
            else:
                if( bWait ):
                    global_proxyAudioPlayer.playFile( strSoundFile );
                else:
                    global_proxyAudioPlayer.post.playFile( strSoundFile );
        analyseSound_resume( bWait );
    except BaseException, err:
        debug.debug( "playSound: ERR: " + str( err ) );
        print( "errr: " + str( err ) );
Пример #7
0
def sayAndCache_internal(strTextToSay, bJustPrepare, bStoreToNonVolatilePath,
                         bDirectPlay, nUseLang, bWaitEnd,
                         bCalledFromSayAndCacheFromLight, strUseVoice):
    "generate a text in a file, then read it, next time it will be directly played from that file"
    "bJustPrepare: render the text to a file, but don't play it now"
    "bStoreToNonVolatilePath: copy the generated file to a non volatile path (/usr/generatedvoices)"
    "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!"
    "strUseVoice: if different of None or default: use specific voice"
    "return the length of the text in seconds, or None if impossible"
    print(
        "sayAndCache_internal( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, bDirectPlay: %s, nUseLang: %d, bWaitEnd: %s, bCalledFromSayAndCacheFromLight: %s, strUseVoice: '%s' )"
        % (strTextToSay, str(bJustPrepare), str(bStoreToNonVolatilePath),
           str(bDirectPlay), nUseLang, str(bWaitEnd),
           str(bCalledFromSayAndCacheFromLight), str(strUseVoice)))
    if (not config.bPrecomputeText):
        print(
            "sayAndCache: disabled by configuration: bPrecomputeText is false")
        if (bJustPrepare):
            return None
            # do nothing
        tts = naoqitools.myGetProxy("ALTextToSpeech")
        tts.say(strTextToSay)
        return None
    print(
        "sayAndCache: FORCING DIRECT_PLAY CAR SINON C'EST BUGGE DANS LA VERSION COURANTE!"
    )
    bDirectPlay = True

    if (strUseVoice == "default"):
        strUseVoice = None

    if (config.bRemoveDirectPlay):
        print(
            "WRN: DISABLING DIRECT_PLAY SETTINGS for testing/temporary purpose"
        )
        bDirectPlay = False

    strTextToSay = assumeTextHasDefaultSettings(strTextToSay, nUseLang)
    szFilename = sayAndCache_getFilename(strTextToSay, nUseLang, strUseVoice)

    szPathVolatile = pathtools.getVolatilePath(
    ) + "generatedvoices" + pathtools.getDirectorySeparator()
    try:
        os.mkdir(szPathVolatile)
    except BaseException, err:
        pass
Пример #8
0
 def __getVarPath_Inner__(self):
     import pathtools  # module already unloaded when called from __del__
     return pathtools.getCachePath(
     ) + "mem" + pathtools.getDirectorySeparator()
Пример #9
0
 def getVarPath():
     return pathtools.getCachePath(
     ) + 'mem' + pathtools.getDirectorySeparator()
Пример #10
0
  szFilename = sayAndCache_getFilename( strTextToSay, nUseLang, strUseVoice );
  
  szPathVolatile = pathtools.getVolatilePath() + "generatedvoices" + pathtools.getDirectorySeparator();
  try:
    os.mkdir( szPathVolatile );
  except BaseException, err:
    pass
  
  
#  if( not szFilename.isalnum() ): # the underscore is not an alphanumeric, but is valid there
#    debug.debug( "WRN: sayAndCache: some chars are not alphanumeric in filename '%s'" % szFilename );
  szPathFilename = szPathVolatile + szFilename + ".raw";
  bGenerate = not filetools.isFileExists( szPathFilename );
  
  if( bGenerate ):
    szAlternatePathFilename = pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szFilename + ".raw"; # look in a non volatile path
    
    if( filetools.isFileExists( szAlternatePathFilename ) ):
      debug.debug( "sayAndCache: get static precomputed text for '%s'" % ( strTextToSay ) );
      filetools.copyFile( szAlternatePathFilename, szPathFilename );
      bGenerate = not filetools.isFileExists( szPathFilename );  #update this variable
    # if alternate
  # if bGenerate
  
  if( bGenerate ):
    # generate it!
    debug.debug( "sayAndCache: generating '%s' to file '%s'" % ( strTextToSay, szPathFilename ) );
    sayAndCache_InformProcess();
    timeBegin = time.time();
    tts = naoqitools.myGetProxy( "ALTextToSpeech" );
    
Пример #11
0
def sayAndCacheAndLight(strTextToSay,
                        bJustPrepare=False,
                        bStoreToNonVolatilePath=False,
                        nEyesColor=0,
                        nUseLang=-1):
    "say a cached text with light animation"
    "nEyesColor: 0: white, 1: blue, 2: green; 3: red"
    "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!"
    "return the length of the text in seconds, or None if impossible"
    print(
        "sayAndCacheAndLight( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, nEyesColor: %s, nUseLang: %s )"
        % (strTextToSay, str(bJustPrepare), str(bStoreToNonVolatilePath),
           str(nEyesColor), str(nUseLang)))
    if (not config.bPrecomputeText):
        print(
            "sayAndCacheAndLight: disabled by configuration: bPrecomputeText is false"
        )
        if (bJustPrepare):
            return None
            # do nothing
        tts = naoqitools.myGetProxy("ALTextToSpeech")
        tts.say(strTextToSay)
        return None

    rLength = sayAndCache(strTextToSay,
                          bJustPrepare=True,
                          bStoreToNonVolatilePath=bStoreToNonVolatilePath,
                          nUseLang=nUseLang,
                          bCalledFromSayAndCacheFromLight=True)
    # we store it to disk, only if we must do it

    # this two lines are done too in sayAndCache...
    strTextToSay = assumeTextHasDefaultSettings(strTextToSay, nUseLang)
    szFilename = sayAndCache_getFilename(strTextToSay, nUseLang)

    szPathVolatile = pathtools.getVolatilePath()
    rSampleLenSec = 0.05
    #    szPathFilenamePeak = szPathVolatile + szFilename + ("_%5.3f.egy" % rSampleLenSec);
    szPathFilenamePeak = szFilename + ("_%5.3f.egy" % rSampleLenSec)
    szPathFilenamePeakCache = pathtools.getCachePath(
    ) + "generatedvoices" + pathtools.getDirectorySeparator(
    ) + szPathFilenamePeak
    szPathFilenamePeak = szPathVolatile + szPathFilenamePeak
    anLedsColorSequency = []
    aBufFile = ""
    bFileGenerated = False
    if (not filetools.isFileExists(szPathFilenamePeak)):
        if (filetools.isFileExists(szPathFilenamePeakCache)):
            copyFile(szPathFilenamePeakCache, szPathFilenamePeak)
    if (not filetools.isFileExists(szPathFilenamePeak)):
        # generate peak file
        timeBegin = time.time()
        print("sayAndCacheAndLight: generating peak light - begin\n")
        szPathFilename = szPathVolatile + szFilename + ".raw"
        anLedsColorSequency = []
        try:
            une = naoqitools.myGetProxy('UsageNoiseExtractor')
            anLedsColorSequency = une.analyseSpeakSound(
                szPathFilename, int(rSampleLenSec * 1000), False)
        except BaseException, err:
            print("ERR: sayAndCacheAndLight( '%s' ): err: %s" %
                  (strTextToSay, str(err)))
            print("ERR: sayAndCacheAndLight => trying old cpp version")
            anLedsColorSequency = analyseSpeakSound(szPathFilename,
                                                    rSampleLenSec * 1000)
        print("sayAndCacheAndLight: analyseSpeakSound - end - time: %fs\n" %
              float(time.time() - timeBegin))
        #        print( "anLedsColorSequency: %d samples: %s\n" % ( len( anLedsColorSequency ), str( anLedsColorSequency ) ) );

        print("Writing file with %d peak samples (time: %d)\n" %
              (len(anLedsColorSequency), int(time.time())))
        #         struct.pack_into( "f"*len( anLedsColorSequency ), aBufFile, anLedsColorSequency[:] );
        for peakValue in anLedsColorSequency:
            aBufFile += struct.pack("f", peakValue)
        try:
            file = open(szPathFilenamePeak, "wb")
            file.write(aBufFile)
        except RuntimeError, err:
            print("ERR: sayAndCacheAndLight( '%s' ): err: %s" %
                  (strTextToSay, str(err)))
Пример #12
0
def sayAndCache(strTextToSay,
                bJustPrepare=False,
                bStoreToNonVolatilePath=False,
                bDirectPlay=False,
                nUseLang=-1,
                bWaitEnd=True,
                bCalledFromSayAndCacheFromLight=False):
    "generate a text in a file, then read it, next time it will be directly played from that file"
    "bJustPrepare: render the text to a file, but don't play it now"
    "bStoreToNonVolatilePath: copy the generated file to a non volatile path (/usr/generatedvoices)"
    "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!"
    "return the length of the text in seconds, or None if impossible"
    print(
        "sayAndCache( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, bDirectPlay: %s, nUseLang: %d, bWaitEnd: %s, bCalledFromSayAndCacheFromLight: %s )"
        % (strTextToSay, str(bJustPrepare), str(bStoreToNonVolatilePath),
           str(bDirectPlay), nUseLang, str(bWaitEnd),
           str(bCalledFromSayAndCacheFromLight)))
    if (not config.bPrecomputeText):
        print(
            "sayAndCache: disabled by configuration: bPrecomputeText is false")
        if (bJustPrepare):
            return None
            # do nothing
        tts = naoqitools.myGetProxy("ALTextToSpeech")
        tts.say(strTextToSay)
        return None
    print(
        "sayAndCache: FORCING DIRECT_PLAY CAR SINON C'EST BUGGE DANS LA VERSION COURANTE!"
    )
    bDirectPlay = True

    if (config.bRemoveDirectPlay):
        print(
            "WRN: DISABLING DIRECT_PLAY SETTINGS for testing/temporary purpose"
        )
        bDirectPlay = False

    strTextToSay = assumeTextHasDefaultSettings(strTextToSay, nUseLang)
    szFilename = sayAndCache_getFilename(strTextToSay, nUseLang)

    szPathVolatile = pathtools.getVolatilePath()

    #  if( not szFilename.isalnum() ): # the underscore is not an alphanumeric, but is valid there
    #    debug( "WRN: sayAndCache: some chars are not alphanumeric in filename '%s'" % szFilename );
    szPathFilename = szPathVolatile + szFilename + ".raw"
    bGenerate = not filetools.isFileExists(szPathFilename)

    if (bGenerate):
        szAlternatePathFilename = pathtools.getCachePath(
        ) + "generatedvoices" + pathtools.getDirectorySeparator(
        ) + szFilename + ".raw"
        # look in a non volatile path

        if (filetools.isFileExists(szAlternatePathFilename)):
            debug("sayAndCache: get static precomputed text for '%s'" %
                  (strTextToSay))
            copyFile(szAlternatePathFilename, szPathFilename)
            bGenerate = not filetools.isFileExists(szPathFilename)
            #update this variable
        # if alternate
    # if bGenerate

    if (bGenerate):
        # generate it!
        debug("sayAndCache: generating '%s' to file '%s'" %
              (strTextToSay, szPathFilename))
        sayAndCache_InformProcess()
        timeBegin = time.time()
        tts = naoqitools.myGetProxy("ALTextToSpeech")

        if (nUseLang != -1):
            # change the language to the wanted one
            setSpeakLanguage(nUseLang)
        if (len(strTextToSay) > 150
                and (not bJustPrepare or bCalledFromSayAndCacheFromLight)):
            # if it's a long text, we had a blabla to tell the user we will wait (if it's a just prepare from inner, we don't use it)
            sayAndCache_InformPrepare()

        print("TTS TO FILE 1 - BEGIN")
        tts.sayToFile(strTextToSay, szPathFilename)
        print("TTS TO FILE 1 - END")
        sayAndCache_InformProcess_end()

        debug("sayAndCache: generating text to file - end (tts) - time: %fs" %
              (time.time() - timeBegin))
        timeBegin = time.time()

        removeBlankFromFile(szPathFilename)
        debug(
            "sayAndCache: generating text to file - end (post-process1) - time: %fs"
            % (time.time() - timeBegin))
        timeBegin = time.time()

        if (bStoreToNonVolatilePath):
            try:
                os.makedirs(pathtools.getCachePath() + "generatedvoices" +
                            pathtools.getDirectorySeparator())
            except:
                pass
            szAlternatePathFilename = pathtools.getCachePath(
            ) + "generatedvoices" + pathtools.getDirectorySeparator(
            ) + szFilename + ".raw"
            # a non volatile path
            copyFile(szPathFilename, szAlternatePathFilename)

        time.sleep(0.1)
        # pour laisser la synthese souffler un peu (dans les scripts je mettais 300ms)
        debug(
            "sayAndCache: generating text to file - end (post-process2) - time: %fs"
            % (time.time() - timeBegin))

    statinfo = os.stat(szPathFilename)
    rLength = statinfo.st_size / float(22050 * 1 * 2)
    # sizefile => secondes

    if (not bJustPrepare):
        #    debug( "speech::sayAndCache: launching sound now!" );
        if (bWaitEnd):
            analyseSound_pause(True)
        if (bDirectPlay):
            nLang = nUseLang
            if (nLang == -1):
                nLang = getSpeakLanguage()
            nFreq = 22050
            if (nLang == constants.LANG_CH or nLang == constants.LANG_KO):
                nFreq = 17000
                # parce que c'est beau, (ca fait a peu pres du speed a 72%) # todo: ca désynchronise les yeux qui se lisent trop vite ! argh !
            mySystemCall("aplay -c1 -r%d -fS16_LE -q %s" %
                         (nFreq, szPathFilename),
                         bWaitEnd=bWaitEnd)
        else:
            leds = naoqitools.myGetProxy("ALLeds", True)
            leds.post.fadeRGB("RightFootLeds", 0xFF0000, 0.7)
            # right in red (skip)
            audioProxy = naoqitools.myGetProxy("ALAudioPlayer", True)
            # read it in background and check if someone press the right feet a long times => skip text playing

            id = audioProxy.post.playFile(szPathFilename)
            if (not bWaitEnd):
                # attention: no unpause of analyse dans ce cas la!
                return rLength
            nbrFramesBumpersPushed = 0
            nbrFramesBumpersPushedMinToSkip = 2
            strTemplateKeyName = "Device/SubDeviceList/%sFoot/Bumper/%s/Sensor/Value"
            stm = naoqitools.myGetProxy("ALMemory")
            while (audioProxy.isRunning(id)):
                time.sleep(0.1)
                # time for user to release precedent push
                listRightFeetBumpers = stm.getListData([
                    strTemplateKeyName % ("R", "Left"),
                    strTemplateKeyName % ("R", "Right")
                ])
                if (listRightFeetBumpers[0] > 0.0
                        or listRightFeetBumpers[1] > 0.0):
                    nbrFramesBumpersPushed += 1
                    if (nbrFramesBumpersPushed >=
                            nbrFramesBumpersPushedMinToSkip):
                        print(
                            "sayAndCache: skipping current text reading because users press on right bumpers"
                        )
                        audioProxy.stop(id)
            leds.post.fadeRGB("RightFootLeds", 0x000000, 0.2)
            # turn off it
            # while - end
        #if( bDirectPlay ) - end
        analyseSound_resume(True)
    # if( not bJustPrepare ) - end
    print("sayAndCache: End !!!")
    return rLength
Пример #13
0
    szPathVolatile = pathtools.getVolatilePath(
    ) + "generatedvoices" + pathtools.getDirectorySeparator()
    try:
        os.mkdir(szPathVolatile)
    except BaseException, err:
        pass

#  if( not szFilename.isalnum() ): # the underscore is not an alphanumeric, but is valid there
#    debug.debug( "WRN: sayAndCache: some chars are not alphanumeric in filename '%s'" % szFilename );
    szPathFilename = szPathVolatile + szFilename + ".raw"
    bGenerate = not filetools.isFileExists(szPathFilename)

    if (bGenerate):
        szAlternatePathFilename = pathtools.getCachePath(
        ) + "generatedvoices" + pathtools.getDirectorySeparator(
        ) + szFilename + ".raw"
        # look in a non volatile path

        if (filetools.isFileExists(szAlternatePathFilename)):
            debug.debug("sayAndCache: get static precomputed text for '%s'" %
                        (strTextToSay))
            filetools.copyFile(szAlternatePathFilename, szPathFilename)
            bGenerate = not filetools.isFileExists(szPathFilename)
            #update this variable
        # if alternate
    # if bGenerate

    if (bGenerate):
        # generate it!
        debug.debug("sayAndCache: generating '%s' to file '%s'" %
                    (strTextToSay, szPathFilename))
 def __getVarPath_Inner__( self ):
     import pathtools # module already unloaded when called from __del__
     return pathtools.getCachePath() + "mem" + pathtools.getDirectorySeparator();
 def getVarPath():
     return pathtools.getCachePath() + 'mem' + pathtools.getDirectorySeparator();
Пример #16
0
def sayAndCache( strTextToSay, bJustPrepare = False, bStoreToNonVolatilePath = False, bDirectPlay = False, nUseLang = -1, bWaitEnd = True, bCalledFromSayAndCacheFromLight = False ):
  "generate a text in a file, then read it, next time it will be directly played from that file"
  "bJustPrepare: render the text to a file, but don't play it now"
  "bStoreToNonVolatilePath: copy the generated file to a non volatile path (/usr/generatedvoices)"
  "nUseLang: if different of -1: speak with a specific languages (useful, when text are already generated: doesn't need to swap languages for nothing!"
  "return the length of the text in seconds, or None if impossible"
  print( "sayAndCache( '%s', bJustPrepare: %s, bStoreToNonVolatilePath: %s, bDirectPlay: %s, nUseLang: %d, bWaitEnd: %s, bCalledFromSayAndCacheFromLight: %s )" % ( strTextToSay, str( bJustPrepare ), str( bStoreToNonVolatilePath ), str( bDirectPlay ), nUseLang, str( bWaitEnd ), str( bCalledFromSayAndCacheFromLight ) ) );
  if( not config.bPrecomputeText ):
      print( "sayAndCache: disabled by configuration: bPrecomputeText is false" );
      if( bJustPrepare ):
          return None; # do nothing
      tts = naoqitools.myGetProxy( "ALTextToSpeech" );
      tts.say( strTextToSay );
      return None;
  print( "sayAndCache: FORCING DIRECT_PLAY CAR SINON C'EST BUGGE DANS LA VERSION COURANTE!");  
  bDirectPlay = True;
  
  if( config.bRemoveDirectPlay ):
    print( "WRN: DISABLING DIRECT_PLAY SETTINGS for testing/temporary purpose" );
    bDirectPlay = False;  
  
  strTextToSay = assumeTextHasDefaultSettings( strTextToSay, nUseLang );
  szFilename = sayAndCache_getFilename( strTextToSay, nUseLang );
  
  szPathVolatile = pathtools.getVolatilePath();
  
#  if( not szFilename.isalnum() ): # the underscore is not an alphanumeric, but is valid there
#    debug( "WRN: sayAndCache: some chars are not alphanumeric in filename '%s'" % szFilename );
  szPathFilename = szPathVolatile + szFilename + ".raw";
  bGenerate = not filetools.isFileExists( szPathFilename );
  
  if( bGenerate ):
    szAlternatePathFilename = pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szFilename + ".raw"; # look in a non volatile path
    
    if( filetools.isFileExists( szAlternatePathFilename ) ):
      debug( "sayAndCache: get static precomputed text for '%s'" % ( strTextToSay ) );
      copyFile( szAlternatePathFilename, szPathFilename );
      bGenerate = not filetools.isFileExists( szPathFilename );  #update this variable
    # if alternate
  # if bGenerate
  
  if( bGenerate ):
    # generate it!
    debug( "sayAndCache: generating '%s' to file '%s'" % ( strTextToSay, szPathFilename ) );
    sayAndCache_InformProcess();
    timeBegin = time.time();
    tts = naoqitools.myGetProxy( "ALTextToSpeech" );
    
    if( nUseLang != -1 ):
        # change the language to the wanted one
        setSpeakLanguage( nUseLang );
    if( len( strTextToSay ) > 150 and ( not bJustPrepare or bCalledFromSayAndCacheFromLight ) ):
        # if it's a long text, we had a blabla to tell the user we will wait (if it's a just prepare from inner, we don't use it)
        sayAndCache_InformPrepare();
    
    print( "TTS TO FILE 1 - BEGIN" );
    tts.sayToFile( strTextToSay, szPathFilename );
    print( "TTS TO FILE 1 - END" );
    sayAndCache_InformProcess_end();
    
    debug( "sayAndCache: generating text to file - end (tts) - time: %fs" % ( time.time() - timeBegin ) );    
    timeBegin = time.time();
    
    removeBlankFromFile( szPathFilename );
    debug( "sayAndCache: generating text to file - end (post-process1) - time: %fs" % ( time.time() - timeBegin ) );
    timeBegin = time.time();

    if( bStoreToNonVolatilePath ):
      try:
        os.makedirs( pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator());
      except:
        pass
      szAlternatePathFilename = pathtools.getCachePath() + "generatedvoices" + pathtools.getDirectorySeparator() + szFilename + ".raw"; # a non volatile path
      copyFile( szPathFilename, szAlternatePathFilename );

    time.sleep( 0.1 ); # pour laisser la synthese souffler un peu (dans les scripts je mettais 300ms)
    debug( "sayAndCache: generating text to file - end (post-process2) - time: %fs" % ( time.time() - timeBegin ) );
    
  statinfo = os.stat( szPathFilename );
  rLength = statinfo.st_size / float(22050*1*2); # sizefile => secondes
    
  if( not bJustPrepare ):
#    debug( "speech::sayAndCache: launching sound now!" );
    if( bWaitEnd ):
        analyseSound_pause( True );
    if( bDirectPlay ):
        nLang = nUseLang;
        if( nLang == -1 ):
            nLang = getSpeakLanguage();
        nFreq = 22050;
        if( nLang == constants.LANG_CH or nLang == constants.LANG_KO ):
            nFreq = 17000; # parce que c'est beau, (ca fait a peu pres du speed a 72%) # todo: ca désynchronise les yeux qui se lisent trop vite ! argh !
        mySystemCall( "aplay -c1 -r%d -fS16_LE -q %s" % ( nFreq, szPathFilename ), bWaitEnd = bWaitEnd );
    else:
        leds = naoqitools.myGetProxy( "ALLeds", True );
        leds.post.fadeRGB( "RightFootLeds", 0xFF0000, 0.7 ); # right in red (skip)
        audioProxy = naoqitools.myGetProxy( "ALAudioPlayer", True );
        # read it in background and check if someone press the right feet a long times => skip text playing
        
        id = audioProxy.post.playFile(szPathFilename);
        if( not bWaitEnd ):
            # attention: no unpause of analyse dans ce cas la!
            return rLength;
        nbrFramesBumpersPushed = 0;
        nbrFramesBumpersPushedMinToSkip = 2;
        strTemplateKeyName = "Device/SubDeviceList/%sFoot/Bumper/%s/Sensor/Value";
        stm = naoqitools.myGetProxy( "ALMemory" );
        while( audioProxy.isRunning( id ) ):
            time.sleep( 0.1 ); # time for user to release precedent push
            listRightFeetBumpers = stm.getListData( [strTemplateKeyName % ( "R", "Left" ), strTemplateKeyName % ( "R", "Right" )] );
            if( listRightFeetBumpers[0] > 0.0 or listRightFeetBumpers[1] > 0.0 ):
                nbrFramesBumpersPushed += 1;
                if( nbrFramesBumpersPushed >= nbrFramesBumpersPushedMinToSkip ):
                    print( "sayAndCache: skipping current text reading because users press on right bumpers" );
                    audioProxy.stop( id );
        leds.post.fadeRGB( "RightFootLeds", 0x000000, 0.2 ); # turn off it
        # while - end
    #if( bDirectPlay ) - end
    analyseSound_resume( True );
  # if( not bJustPrepare ) - end
  print( "sayAndCache: End !!!");
  return rLength;