hlrc / client / python / hlrc_client / hlrc_speak_utterance.py @ 62d50515
History | View | Annotate | Download (4.192 KB)
| 1 | 0c286af0 | Simon Schulz | #!/usr/bin/python
|
|---|---|---|---|
| 2 | #PYTHONPATH="/opt/ros/groovy/lib/python2.7/dist-packages:/vol/csra/releases/nightly/lib/python2.7/:/vol/csra/releases/nightly/lib/python2.7/site-packages/
|
||
| 3 | import sys |
||
| 4 | import wave |
||
| 5 | |||
| 6 | from textgrid_hlc import * |
||
| 7 | import logging |
||
| 8 | import errno |
||
| 9 | import os.path |
||
| 10 | |||
| 11 | try:
|
||
| 12 | import rsb |
||
| 13 | import rsb.converter |
||
| 14 | import rst |
||
| 15 | import rstsandbox |
||
| 16 | from rst.audition.Utterance_pb2 import Utterance |
||
| 17 | from rst.audition.SoundChunk_pb2 import SoundChunk |
||
| 18 | except ImportError as exception: |
||
| 19 | sys.stderr.write("ImportError: {}\n> HINT: try to export PYTHONPATH=$PYTHONPATH:$YOUR_PREFIX/lib/python2.7/site-packages/\n\n".format(exception))
|
||
| 20 | sys.exit(errno.ENOPKG) |
||
| 21 | |||
| 22 | class hlrc_utterance(): |
||
| 23 | def __init__(self, _base_scope): |
||
| 24 | #print "> registering rst converter"
|
||
| 25 | converter = rsb.converter.ProtocolBufferConverter(messageClass = Utterance) |
||
| 26 | rsb.converter.registerGlobalConverter(converter) |
||
| 27 | self.set_scope(_base_scope)
|
||
| 28 | |||
| 29 | def set_scope(self, scope): |
||
| 30 | self.base_scope = str(scope) #NOTE: str() is important here, scope is a qstring (?) and gets deleted during call |
||
| 31 | print "> setting scope to '%s'" % self.base_scope |
||
| 32 | try:
|
||
| 33 | self.server = rsb.createRemoteServer(self.base_scope + '/set') |
||
| 34 | except ValueError: |
||
| 35 | print "> invalid scope given. server deactivated" |
||
| 36 | self.server.deactivate()
|
||
| 37 | |||
| 38 | def trigger_utterance(self, filename_praat, filename_wav, blocking): |
||
| 39 | if (self.server is None): |
||
| 40 | print("> invalid server")
|
||
| 41 | return
|
||
| 42 | |||
| 43 | if (not os.path.isfile(filename_praat)): |
||
| 44 | print "can not open file '%s'" % (filename_praat) |
||
| 45 | return 0 |
||
| 46 | |||
| 47 | if (not os.path.isfile(filename_wav)): |
||
| 48 | print "can not open file '%s'" % (filename_wav) |
||
| 49 | return 0 |
||
| 50 | |||
| 51 | print "> reading wave file '%s'" % (filename_wav) |
||
| 52 | wav = wave.open(filename_wav, "r")
|
||
| 53 | |||
| 54 | |||
| 55 | print "> parsing praat file '%s'" % (filename_praat) |
||
| 56 | tgrid = TextGrid.load(filename_praat) |
||
| 57 | |||
| 58 | |||
| 59 | #create utterance & fill it with values:
|
||
| 60 | ut = Utterance() |
||
| 61 | |||
| 62 | #textual description of audio file
|
||
| 63 | ut.text = filename_praat |
||
| 64 | |||
| 65 | ut.audio.data = wav.readframes(-1)
|
||
| 66 | ut.audio.sample_count = wav.getnframes() |
||
| 67 | ut.audio.channels = wav.getnchannels() |
||
| 68 | ut.audio.rate = wav.getframerate() |
||
| 69 | |||
| 70 | if (wav.getsampwidth() == 1): |
||
| 71 | ut.audio.sample_type = SoundChunk.SAMPLE_U8 |
||
| 72 | elif (wav.getsampwidth() == 2): |
||
| 73 | ut.audio.sample_type = SoundChunk.SAMPLE_S16 |
||
| 74 | else:
|
||
| 75 | print "> invalid sample type. py doc says wave files are either u8 or s16" |
||
| 76 | exit(0) |
||
| 77 | |||
| 78 | #wave spec says always little endian
|
||
| 79 | ut.audio.endianness = SoundChunk.ENDIAN_LITTLE |
||
| 80 | |||
| 81 | print "> filling phones with data from praat" |
||
| 82 | for tier in tgrid.tiers: |
||
| 83 | idx = (tgrid.tiers.index(tier)) + 1
|
||
| 84 | transcript = tier.simple_transcript |
||
| 85 | for (xmin, xmax, utt) in transcript: |
||
| 86 | phoneme = ut.phonemes.add() |
||
| 87 | phoneme.symbol = utt |
||
| 88 | phoneme.duration = int(1000.0*(float(xmax)-float(xmin))) |
||
| 89 | |||
| 90 | with rsb.createRemoteServer(self.base_scope + '/set') as server: |
||
| 91 | |||
| 92 | if (blocking):
|
||
| 93 | #blocking:
|
||
| 94 | print "> calling the utterance rpc (blocking until we finished talking)..." |
||
| 95 | print '> server reply: "%s"' % server.utterance(ut) |
||
| 96 | else:
|
||
| 97 | print "> calling the utterance rpc (NON-BLOCKING)..." |
||
| 98 | future = server.utterance.async(ut) |
||
| 99 | #we can block here for a incoming result with a timeout in seconds
|
||
| 100 | #print '> server reply: "%s"' % future.get(timeout = 10);
|
||
| 101 | |||
| 102 | print "> done" |
||
| 103 | return 1 |
||
| 104 | |||
| 105 | def main(): |
||
| 106 | # Pacify logger.
|
||
| 107 | #logging.basicConfig()
|
||
| 108 | if (len(sys.argv) != 4): |
||
| 109 | print "> usage: %s <base_scope> file.praat file.wav\n> example: %s /flobi1 hello.praat hello.wav" % (sys.argv[0] , sys.argv[0]) |
||
| 110 | sys.exit(0)
|
||
| 111 | |||
| 112 | filename_praat = sys.argv[2]
|
||
| 113 | filename_wav = sys.argv[3]
|
||
| 114 | base = sys.argv[1]
|
||
| 115 | |||
| 116 | hlc = hlrc_utterance(base) |
||
| 117 | hlc.trigger_utterance(filename_praat, filename_wav, 1)
|
||
| 118 | |||
| 119 | if __name__ == '__main__': |
||
| 120 | main() |