Example #1
0
    def write_list(self, filename, ipustrs, ipusaudio):
        """
        Write the list of tracks: from_time to_time (in seconds).
        Last line is the audio file duration.

        @param filename (str) The list file name
        @param ipustrs (IPUsTrs)
        @param ipusaudio (IPUsAudio)

        """
        # Convert the tracks: from frames to times
        trackstimes = frames2times(self.tracks, ipusaudio.get_channel().get_framerate())

        with codecs.open(filename ,'w', encoding) as fp:
            idx = 0

            for (from_time,to_time) in trackstimes:
                fp.write( "%.4f %.4f " %(from_time,to_time))

                # if we assigned a filename to this tracks...
                if len(ipustrs.get_names()) > 0 and idx < len(ipustrs.get_names()):
                    ustr = ipustrs.get_names()[idx].encode('utf8')
                    fp.write( ustr.decode(encoding)+"\n" )
                else:
                    fp.write( "\n" )

                idx = idx+1

            # Finally, print audio duration
            fp.write( "%.4f\n" %ipusaudio.get_channel().get_duration() )
Example #2
0
    def write_text_tracks(self, ipustrs, ipusaudio, output, extension):
        """
        Write the units in track files.

        """
        if not os.path.exists( output ):
            os.mkdir( output )

        if extension is None:
            raise IOError('An extension is required to write units.')

        # Get units and names
        units = ipustrs.get_units()
        names = ipustrs.get_names()

        # Convert the tracks: from frames to times
        trackstimes = frames2times(self.tracks, ipusaudio.get_channel().get_framerate())

        # Write text tracks
        for i,track in enumerate(trackstimes):
            trackbasename = ""
            if len(names) > 0 and len(names[i])>0:
                # Specific names are given
                trackbasename = os.path.join(output, names[i])
            else:
                trackbasename = os.path.join(output, "track_%.06d" % (i+1))

            tracktxtname = trackbasename+"."+extension
            if extension.lower() == "txt":
                self.__write_txttrack(tracktxtname, units[i])
            else:
                d = track[1] - track[0]
                self.__write_trstrack(tracktxtname, units[i], d)
Example #3
0
    def tracks2transcription(self, ipustrs, ipusaudio, addipuidx=False):
        """
        Create a Transcription object from tracks.

        """
        if len(self.tracks) == 0:
            raise IOError('No IPUs to write.\n')

        # Extract the info we need from ipusaudio
        framerate = ipusaudio.get_channel().get_framerate()
        end_time  = ipusaudio.get_channel().get_duration()

        # Extract the info we need from ipustrs
        try:
            medialist = ipustrs.trsinput.GetMedia()
            if len(medialist) > 0:
                media = medialist[0]
            else:
                media = None
        except Exception:
            media = None
        units = ipustrs.get_units()
        if len(units) != 0:
            if len(self.tracks) != len(units):
                raise Exception('Bad number of tracks and units. Got %d tracks, and %d units.\n'%(len(self.tracks),len(units)))

        # Create the transcription and tiers
        trs = Transcription("IPU-Segmentation")
        tieripu = trs.NewTier("IPUs")
        tier    = trs.NewTier("Transcription")
        radius  = 1.0 / framerate

        # Convert the tracks: from frames to times
        trackstimes = frames2times(self.tracks, framerate)
        i = 0
        to_prec = 0.

        for (from_time,to_time) in trackstimes:

            # From the previous track to the current track: silence
            if to_prec < from_time:
                begin = to_prec
                end   = from_time
                a     = Annotation(TimeInterval(TimePoint(begin,radius), TimePoint(end,radius)), Label("#"))
                tieripu.Append(a)
                tier.Append(a.Copy())

            # New track with speech
            begin = from_time
            end   = to_time

            # ... IPU tier
            label = "ipu_%d"%(i+1)
            a  = Annotation(TimeInterval(TimePoint(begin,radius), TimePoint(end,radius)), Label(label))
            tieripu.Append(a)

            # ... Transcription tier
            if addipuidx is False:
                label = ""
            if len(units) > 0:
                label = label + " " + units[i]
            a  = Annotation(TimeInterval(TimePoint(begin,radius), TimePoint(end,radius)), Label(label))
            tier.Append(a)

            # Go to the next
            i += 1
            to_prec = to_time

        # The end is a silence?
        if to_prec < end_time:
            begin = TimePoint(to_prec,radius)
            end   = TimePoint(end_time,radius)
            if begin < end:
                a  = Annotation(TimeInterval(begin, end), Label("#"))
                tieripu.Append(a)
                tier.Append(a.Copy())

        # Link both tiers: IPU and Transcription
        try:
            trs.GetHierarchy().addLink('TimeAssociation', tieripu, tier)
        except Exception:
            pass

        # Set media
        if media is not None:
            trs.AddMedia( media )
            for tier in trs:
                tier.SetMedia( media )

        return trs