Add a linear crossfade to the composition between two
        segments.

        :param seg1: First segment (fading out)
        :type seg1: :py:class:`radiotool.composer.Segment`
        :param seg2: Second segment (fading in)
        :type seg2: :py:class:`radiotool.composer.Segment`
        :param duration: Duration of crossfade (in seconds)

def cross_fade(self, seg1, seg2, duration):
        """Add a linear crossfade to the composition between two
        segments.

        :param seg1: First segment (fading out)
        :type seg1: :py:class:`radiotool.composer.Segment`
        :param seg2: Second segment (fading in)
        :type seg2: :py:class:`radiotool.composer.Segment`
        :param duration: Duration of crossfade (in seconds)
        """

        if seg1.comp_location + seg1.duration - seg2.comp_location < 2:
            dur = int(duration * seg1.track.samplerate)

            if dur % 2 == 1:
                dur -= 1

            if dur / 2 > seg1.duration:
                dur = seg1.duration * 2

            if dur / 2 > seg2.duration:
                dur = seg2.duration * 2

            # we're going to compute the crossfade and then create a RawTrack
            # for the resulting frames

            if seg2.start - (dur / 2) < 0:
                diff = seg2.start
                seg2.start = 0
                seg2.duration -= diff
                seg2.comp_location -= diff
                dur = 2 * diff
            else:
                seg2.start -= (dur / 2)
                seg2.duration += (dur / 2)
                seg2.comp_location -= (dur / 2)

            seg1.duration += (dur / 2)
            out_frames = seg1.get_frames(channels=self.channels)[-dur:]
            seg1.duration -= dur

            in_frames = seg2.get_frames(channels=self.channels)[:dur]
            seg2.start += dur
            seg2.duration -= dur
            seg2.comp_location += dur

            # compute the crossfade
            in_frames = in_frames[:min(map(len, [in_frames, out_frames]))]
            out_frames = out_frames[:min(map(len, [in_frames, out_frames]))]

            cf_frames = radiotool.utils.linear(out_frames, in_frames)
            # cf_frames = equal_power(out_frames, in_frames)

            raw_track = RawTrack(cf_frames, name="crossfade",
                                 samplerate=seg1.track.samplerate)

            rs_comp_location = (seg1.comp_location + seg1.duration) /\
                float(seg1.track.samplerate)

            rs_duration = raw_track.duration / float(raw_track.samplerate)

            raw_seg = Segment(raw_track, rs_comp_location, 0.0, rs_duration)
            # will this fix a bug?
            raw_seg.duration = raw_track.duration
            raw_seg.comp_location = seg1.comp_location + seg1.duration

            self.add_track(raw_track)
            self.add_segment(raw_seg)

            return raw_seg

        else:
            print seg1.comp_location + seg1.duration, seg2.comp_location
            raise Exception("Segments must be adjacent"
                            "to add a crossfade ({}, {})".format(
                            seg1.comp_location + seg1.duration,
                            seg2.comp_location))