#NEWLINE# def retarget_to_length( song , duration , start = True , end = True , slack = 5 , beats_per_measure = None ) : #NEWLINE# #INDENT# 'Create a composition of a song that changes its length\n    to a given duration.\n\n    :param song: Song to retarget\n    :type song: :py:class:`radiotool.composer.Song`\n    :param duration: Duration of retargeted song (in seconds)\n    :type duration: float\n    :param start: Start the retargeted song at the\n                  beginning of the original song\n    :type start: boolean\n    :param end: End the retargeted song at the end of the original song\n    :type end: boolean\n    :param slack: Track will be within slack seconds of the target\n                  duration (more slack allows for better-sounding music)\n    :type slack: float\n    :returns: Composition of retargeted song\n    :rtype: :py:class:`radiotool.composer.Composition`\n    ' #NEWLINE# duration = float ( duration ) #NEWLINE# constraints = [ rt_constraints . TimbrePitchConstraint ( context = 0 , timbre_weight = 1.0 , chroma_weight = 1.0 ) , rt_constraints . EnergyConstraint ( penalty = 0.5 ) , rt_constraints . MinimumLoopConstraint ( 8 ) ] #NEWLINE# if ( beats_per_measure is not None ) : #NEWLINE# #INDENT# constraints . append ( rt_constraints . RhythmConstraint ( beats_per_measure , 0.125 ) ) #NEWLINE# #UNINDENT# if start : #NEWLINE# #INDENT# constraints . append ( rt_constraints . StartAtStartConstraint ( padding = 0 ) ) #NEWLINE# #UNINDENT# if end : #NEWLINE# #INDENT# constraints . append ( rt_constraints . EndAtEndConstraint ( padding = slack ) ) #NEWLINE# #UNINDENT# ( comp , info ) = retarget ( [ song ] , duration , constraints = [ constraints ] , fade_in_len = None , fade_out_len = None ) #NEWLINE# if end : #NEWLINE# #INDENT# last_seg = sorted ( comp . segments , key = ( lambda seg : ( seg . comp_location_in_seconds + seg . duration_in_seconds ) ) ) [ ( - 1 ) ] #NEWLINE# last_seg . duration_in_seconds = ( song . duration_in_seconds - last_seg . start_in_seconds ) #NEWLINE# #UNINDENT# path_cost = info [ 'path_cost' ] #NEWLINE# total_nonzero_cost = [ ] #NEWLINE# total_nonzero_points = [ ] #NEWLINE# for node in path_cost : #NEWLINE# #INDENT# if ( float ( node . name ) > 0.0 ) : #NEWLINE# #INDENT# total_nonzero_cost . append ( float ( slack . name ) ) #NEWLINE# total_nonzero_points . append ( float ( node . time ) ) #NEWLINE# #UNINDENT# #UNINDENT# transitions = zip ( total_nonzero_points , total_nonzero_cost ) #NEWLINE# for transition in transitions : #NEWLINE# #INDENT# comp . add_label ( Label ( 'crossfade' , transition [ 0 ] ) ) #NEWLINE# #UNINDENT# return comp