<s> '''<STR_LIT>''' <EOL> import librosa <EOL> import pyrubberband as pyrb <EOL> import re <EOL> import numpy as np <EOL> import six <EOL> from . . base import BaseTransformer <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def transpose ( label , n_semitones ) : <EOL> '''<STR_LIT>''' <EOL> match = re . match ( six . text_type ( '<STR_LIT>' ) , <EOL> six . text_type ( label ) ) <EOL> if not match : <EOL> return label <EOL> note = match . group ( '<STR_LIT>' ) <EOL> new_note = librosa . midi_to_note ( librosa . note_to_midi ( note ) + n_semitones , <EOL> octave = False ) <EOL> return new_note + match . group ( '<STR_LIT>' ) <EOL> class AbstractPitchShift ( BaseTransformer ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> '''<STR_LIT>''' <EOL> BaseTransformer . __init__ ( self ) <EOL> self . _register ( '<STR_LIT>' , self . deform_note ) <EOL> self . _register ( '<STR_LIT>' , self . deform_frequency ) <EOL> self . _register ( '<STR_LIT>' , self . deform_midi ) <EOL> self . _register ( '<STR_LIT>' , self . deform_tonic ) <EOL> def states ( self , jam ) : <EOL> mudabox = jam . sandbox . muda <EOL> state = dict ( tuning = librosa . estimate_tuning ( y = mudabox . _audio [ '<STR_LIT:y>' ] , <EOL> sr = mudabox . _audio [ '<STR_LIT>' ] ) ) <EOL> yield state <EOL> @ staticmethod <EOL> def audio ( mudabox , state ) : <EOL> mudabox . _audio [ '<STR_LIT:y>' ] = pyrb . pitch_shift ( mudabox . _audio [ '<STR_LIT:y>' ] , <EOL> mudabox . _audio [ '<STR_LIT>' ] , <EOL> state [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def deform_frequency ( annotation , state ) : <EOL> annotation . data . value *= <NUM_LIT> ** ( state [ '<STR_LIT>' ] / <NUM_LIT> ) <EOL> @ staticmethod <EOL> def deform_midi ( annotation , state ) : <EOL> annotation . data . value += state [ '<STR_LIT>' ] <EOL> @ staticmethod <EOL> def deform_tonic ( annotation , state ) : <EOL> if - <NUM_LIT:0.5> < ( state [ '<STR_LIT>' ] + state [ '<STR_LIT>' ] ) <= <NUM_LIT:0.5> : <EOL> return <EOL> for obs in annotation . data . value : <EOL> obs [ '<STR_LIT>' ] = transpose ( obs [ '<STR_LIT>' ] , state [ '<STR_LIT>' ] ) <EOL> @ staticmethod <EOL> def deform_note ( annotation , state ) : <EOL> if - <NUM_LIT:0.5> < ( state [ '<STR_LIT>' ] + state [ '<STR_LIT>' ] ) <= <NUM_LIT:0.5> : <EOL> return <EOL> annotation . data . value = [ transpose ( obs , state [ '<STR_LIT>' ] ) <EOL> for obs in annotation . data . value ] <EOL> class PitchShift ( AbstractPitchShift ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , n_semitones = <NUM_LIT:1> ) : <EOL> AbstractPitchShift . __init__ ( self ) <EOL> self . n_semitones = float ( n_semitones ) <EOL> def states ( self , jam ) : <EOL> for state in AbstractPitchShift . states ( self , jam ) : <EOL> state [ '<STR_LIT>' ] = self . n_semitones <EOL> yield state <EOL> class RandomPitchShift ( AbstractPitchShift ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , n_samples = <NUM_LIT:3> , mean = <NUM_LIT:0.0> , sigma = <NUM_LIT:1.0> ) : <EOL> AbstractPitchShift . __init__ ( self ) <EOL> if sigma <= <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if n_samples <= <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . n_samples = n_samples <EOL> self . mean = float ( mean ) <EOL> self . sigma = float ( sigma ) <EOL> def states ( self , jam ) : <EOL> for state in AbstractPitchShift . states ( self , jam ) : <EOL> for _ in range ( self . n_samples ) : <EOL> state [ '<STR_LIT>' ] = np . random . normal ( loc = self . mean , <EOL> scale = self . sigma , <EOL> size = None ) <EOL> yield state <EOL> class LinearPitchShift ( AbstractPitchShift ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , n_samples = <NUM_LIT:3> , lower = - <NUM_LIT:1> , upper = <NUM_LIT:1> ) : <EOL> AbstractPitchShift . __init__ ( self ) <EOL> if upper <= lower : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if n_samples <= <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . n_samples = n_samples <EOL> self . lower = float ( lower ) <EOL> self . upper = float ( upper ) <EOL> def states ( self , jam ) : <EOL> shifts = np . linspace ( self . lower , <EOL> self . upper , <EOL> num = self . n_samples , <EOL> endpoint = True ) <EOL> for state in AbstractPitchShift . states ( self , jam ) : <EOL> for n_semitones in shifts : <EOL> state [ '<STR_LIT>' ] = n_semitones <EOL> yield state </s>
