pympi/Praat.py
def from_file(self, ifile, codec='ascii'):
        """Read textgrid from stream.

        :param file ifile: Stream to read from.
        :param str codec: Text encoding for the input. Note that this will be
            ignored for binary TextGrids.
        """
        if ifile.read(12) == b'ooBinaryFile':
            def bin2str(ifile):
                textlen = struct.unpack('>h', ifile.read(2))[0]
                # Single byte characters
                if textlen >= 0:
                    return ifile.read(textlen).decode('ascii')
                # Multi byte characters have initial len -1 and then \xff bytes
                elif textlen == -1:
                    textlen = struct.unpack('>h', ifile.read(2))[0]
                    data = ifile.read(textlen*2)
                    # Hack to go from number to unicode in python3 and python2
                    fun = unichr if 'unichr' in __builtins__ else chr
                    charlist = (data[i:i+2] for i in range(0, len(data), 2))
                    return u''.join(
                        fun(struct.unpack('>h', i)[0]) for i in charlist)

            ifile.read(ord(ifile.read(1)))  # skip oo type
            self.xmin = struct.unpack('>d', ifile.read(8))[0]
            self.xmax = struct.unpack('>d', ifile.read(8))[0]
            ifile.read(1)  # skip <exists>
            self.tier_num = struct.unpack('>i', ifile.read(4))[0]
            for i in range(self.tier_num):
                tier_type = ifile.read(ord(ifile.read(1))).decode('ascii')
                name = bin2str(ifile)
                tier = Tier(0, 0, name=name, tier_type=tier_type)
                self.tiers.append(tier)
                tier.xmin = struct.unpack('>d', ifile.read(8))[0]
                tier.xmax = struct.unpack('>d', ifile.read(8))[0]
                nint = struct.unpack('>i', ifile.read(4))[0]
                for i in range(nint):
                    x1 = struct.unpack('>d', ifile.read(8))[0]
                    if tier.tier_type == 'IntervalTier':
                        x2 = struct.unpack('>d', ifile.read(8))[0]
                    text = bin2str(ifile)
                    if tier.tier_type == 'IntervalTier':
                        tier.intervals.append((x1, x2, text))
                    elif tier.tier_type == 'TextTier':
                        tier.intervals.append((x1, text))
                    else:
                        raise Exception('Tiertype does not exist.')
        else:
            def nn(ifile, pat):
                line = next(ifile).decode(codec)
                return pat.search(line).group(1)

            regfloat = re.compile('([\d.]+)\s*$', flags=re.UNICODE)
            regint = re.compile('([\d]+)\s*$', flags=re.UNICODE)
            regstr = re.compile('"(.*)"\s*$', flags=re.UNICODE)
            # Skip the Headers and empty line
            next(ifile), next(ifile), next(ifile)
            self.xmin = float(nn(ifile, regfloat))
            self.xmax = float(nn(ifile, regfloat))
            # Skip <exists>
            line = next(ifile)
            short = line.strip() == b'<exists>'
            self.tier_num = int(nn(ifile, regint))
            not short and next(ifile)
            for i in range(self.tier_num):
                not short and next(ifile)  # skip item[]: and item[\d]:
                tier_type = nn(ifile, regstr)
                name = nn(ifile, regstr)
                tier = Tier(0, 0, name=name, tier_type=tier_type)
                self.tiers.append(tier)
                tier.xmin = float(nn(ifile, regfloat))
                tier.xmax = float(nn(ifile, regfloat))
                for i in range(int(nn(ifile, regint))):
                    not short and next(ifile)  # skip intervals [\d]
                    x1 = float(nn(ifile, regfloat))
                    if tier.tier_type == 'IntervalTier':
                        x2 = float(nn(ifile, regfloat))
                        t = nn(ifile, regstr)
                        tier.intervals.append((x1, x2, t))
                    elif tier.tier_type == 'TextTier':
                        t = nn(ifile, regstr)
                        tier.intervals.append((x1, t))
def from_file(self, ifile, codec='ascii'):
        """Read textgrid from stream.

        :param file ifile: Stream to read from.
        :param str codec: Text encoding for the input. Note that this will be
            ignored for binary TextGrids.
        """
        if ifile.read(12) == b'ooBinaryFile':
            def bin2str(ifile):
                textlen = struct.unpack('>h', ifile.read(2))[0]
                # Single byte characters
                if textlen >= 0:
                    return ifile.read(textlen).decode('ascii')
                # Multi byte characters have initial len -1 and then \xff bytes
                elif textlen == -1:
                    textlen = struct.unpack('>h', ifile.read(2))[0]
                    data = ifile.read(textlen*2)
                    # Hack to go from number to unicode in python3 and python2
                    fun = unichr if 'unichr' in __builtins__ else chr
                    charlist = (data[i:i+2] for i in range(0, len(data), 2))
                    return u''.join(
                        fun(struct.unpack('>h', i)[0]) for i in charlist)

            ifile.read(ord(ifile.read(1)))  # skip oo type
            self.xmin = struct.unpack('>d', ifile.read(8))[0]
            self.xmax = struct.unpack('>d', ifile.read(8))[0]
            ifile.read(1)  # skip <exists>
            self.tier_num = struct.unpack('>i', ifile.read(4))[0]
            for i in range(self.tier_num):
                tier_type = ifile.read(ord(ifile.read(1))).decode('ascii')
                name = bin2str(ifile)
                tier = Tier(0, 0, name=name, tier_type=tier_type)
                self.tiers.append(tier)
                tier.xmin = struct.unpack('>d', ifile.read(8))[0]
                tier.xmax = struct.unpack('>d', ifile.read(8))[0]
                nint = struct.unpack('>i', ifile.read(4))[0]
                for i in range(nint):
                    x1 = struct.unpack('>d', ifile.read(8))[0]
                    if tier.tier_type == 'IntervalTier':
                        x2 = struct.unpack('>d', ifile.read(8))[0]
                    text = bin2str(ifile)
                    if tier.tier_type == 'IntervalTier':
                        tier.intervals.append((x1, x2, text))
                    elif tier.tier_type == 'TextTier':
                        tier.intervals.append((x1, text))
                    else:
                        raise Exception('Tiertype does not exist.')
        else:
            def nn(ifile, pat):
                line = next(ifile).decode(codec)
                return pat.search(line).group(1)

            regfloat = re.compile('([\d.]+)\s*$', flags=re.UNICODE)
            regint = re.compile('([\d]+)\s*$', flags=re.UNICODE)
            regstr = re.compile('"(.*)"\s*$', flags=re.UNICODE)
            # Skip the Headers and empty line
            next(ifile), next(ifile), next(ifile)
            self.xmin = float(nn(ifile, regfloat))
            self.xmax = float(nn(ifile, regfloat))
            # Skip <exists>
            line = next(ifile)
            short = line.strip() == b'<exists>'
            self.tier_num = int(nn(ifile, regint))
            not short and next(ifile)
            for i in range(self.tier_num):
                not short and next(ifile)  # skip item[]: and item[\d]:
                tier_type = nn(ifile, regstr)
                name = nn(ifile, regstr)
                tier = Tier(0, 0, name=name, tier_type=tier_type)
                self.tiers.append(tier)
                tier.xmin = float(nn(ifile, regfloat))
                tier.xmax = float(nn(ifile, regfloat))
                for i in range(int(nn(ifile, regint))):
                    not short and next(ifile)  # skip intervals [\d]
                    x1 = float(nn(ifile, regfloat))
                    if tier.tier_type == 'IntervalTier':
                        x2 = float(nn(ifile, regfloat))
                        t = nn(ifile, regstr)
                        tier.intervals.append((x1, x2, t))
                    elif tier.tier_type == 'TextTier':
                        t = nn(ifile, regstr)
                        tier.intervals.append((x1, t))
def from_file ( self , ifile , codec = 'ascii' ) : if ifile . read ( 12 ) == b'ooBinaryFile' : def bin2str ( ifile ) : textlen = struct . unpack ( '>h' , ifile . read ( 2 ) ) [ 0 ] # Single byte characters if textlen >= 0 : return ifile . read ( textlen ) . decode ( 'ascii' ) # Multi byte characters have initial len -1 and then \xff bytes elif textlen == - 1 : textlen = struct . unpack ( '>h' , ifile . read ( 2 ) ) [ 0 ] data = ifile . read ( textlen * 2 ) # Hack to go from number to unicode in python3 and python2 fun = unichr if 'unichr' in __builtins__ else chr charlist = ( data [ i : i + 2 ] for i in range ( 0 , len ( data ) , 2 ) ) return u'' . join ( fun ( struct . unpack ( '>h' , i ) [ 0 ] ) for i in charlist ) ifile . read ( ord ( ifile . read ( 1 ) ) ) # skip oo type self . xmin = struct . unpack ( '>d' , ifile . read ( 8 ) ) [ 0 ] self . xmax = struct . unpack ( '>d' , ifile . read ( 8 ) ) [ 0 ] ifile . read ( 1 ) # skip <exists> self . tier_num = struct . unpack ( '>i' , ifile . read ( 4 ) ) [ 0 ] for i in range ( self . tier_num ) : tier_type = ifile . read ( ord ( ifile . read ( 1 ) ) ) . decode ( 'ascii' ) name = bin2str ( ifile ) tier = Tier ( 0 , 0 , name = name , tier_type = tier_type ) self . tiers . append ( tier ) tier . xmin = struct . unpack ( '>d' , ifile . read ( 8 ) ) [ 0 ] tier . xmax = struct . unpack ( '>d' , ifile . read ( 8 ) ) [ 0 ] nint = struct . unpack ( '>i' , ifile . read ( 4 ) ) [ 0 ] for i in range ( nint ) : x1 = struct . unpack ( '>d' , ifile . read ( 8 ) ) [ 0 ] if tier . tier_type == 'IntervalTier' : x2 = struct . unpack ( '>d' , ifile . read ( 8 ) ) [ 0 ] text = bin2str ( ifile ) if tier . tier_type == 'IntervalTier' : tier . intervals . append ( ( x1 , x2 , text ) ) elif tier . tier_type == 'TextTier' : tier . intervals . append ( ( x1 , text ) ) else : raise Exception ( 'Tiertype does not exist.' ) else : def nn ( ifile , pat ) : line = next ( ifile ) . decode ( codec ) return pat . search ( line ) . group ( 1 ) regfloat = re . compile ( '([\d.]+)\s*$' , flags = re . UNICODE ) regint = re . compile ( '([\d]+)\s*$' , flags = re . UNICODE ) regstr = re . compile ( '"(.*)"\s*$' , flags = re . UNICODE ) # Skip the Headers and empty line next ( ifile ) , next ( ifile ) , next ( ifile ) self . xmin = float ( nn ( ifile , regfloat ) ) self . xmax = float ( nn ( ifile , regfloat ) ) # Skip <exists> line = next ( ifile ) short = line . strip ( ) == b'<exists>' self . tier_num = int ( nn ( ifile , regint ) ) not short and next ( ifile ) for i in range ( self . tier_num ) : not short and next ( ifile ) # skip item[]: and item[\d]: tier_type = nn ( ifile , regstr ) name = nn ( ifile , regstr ) tier = Tier ( 0 , 0 , name = name , tier_type = tier_type ) self . tiers . append ( tier ) tier . xmin = float ( nn ( ifile , regfloat ) ) tier . xmax = float ( nn ( ifile , regfloat ) ) for i in range ( int ( nn ( ifile , regint ) ) ) : not short and next ( ifile ) # skip intervals [\d] x1 = float ( nn ( ifile , regfloat ) ) if tier . tier_type == 'IntervalTier' : x2 = float ( nn ( ifile , regfloat ) ) t = nn ( ifile , regstr ) tier . intervals . append ( ( x1 , x2 , t ) ) elif tier . tier_type == 'TextTier' : t = nn ( ifile , regstr ) tier . intervals . append ( ( x1 , t ) )
Read textgrid from stream.

        :param file ifile: Stream to read from.
        :param str codec: Text encoding for the input. Note that this will be
            ignored for binary TextGrids.
Read textgrid from stream .