コード例 #1
0
ファイル: ipusout.py プロジェクト: lym0302/sppas
 def __write_trs_track(track_filename, track_content, duration):
     begin = TimePoint(0.)
     end = TimePoint(duration)
     ann = Annotation(TimeInterval(begin, end), Label(track_content))
     trs = Transcription()
     tier = trs.NewTier("Transcription")
     tier.Append(ann)
     sppas.src.annotationdata.aio.write(track_filename, trs)
コード例 #2
0
ファイル: ipusout.py プロジェクト: lym0302/sppas
    def tracks2transcription(self, ipustrs, ipusaudio, add_ipu_idx=False):
        """ Create a Transcription object from tracks.

        :param ipustrs: (IPUsTrs)
        :param ipusaudio: (IPUsAudio)
        :param add_ipu_idx: (bool)

        """
        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('Inconsistent number of tracks and units. '
                                'Got %d audio 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 = ipusaudio.get_win_length() / 8.
        # vagueness is win_length divided by 4 (see "refine" method of sppasChannelSilence class)
        # radius is vagueness divided by 2

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

        for (from_time, to_time) in tracks_times:

            # 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 add_ipu_idx 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().add_link('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