idx
int64
0
252k
question
stringlengths
48
5.28k
target
stringlengths
5
1.23k
236,200
def parse_midi_event ( self , fp ) : chunk_size = 0 try : ec = self . bytes_to_int ( fp . read ( 1 ) ) chunk_size += 1 self . bytes_read += 1 except : raise IOError ( "Couldn't read event type " "and channel data from file." ) event_type = ( ec & 0xf0 ) >> 4 channel = ec & 0x0f if event_type < 8 : raise FormatError ( '...
Parse a MIDI event .
236,201
def parse_track_header ( self , fp ) : try : h = fp . read ( 4 ) self . bytes_read += 4 except : raise IOError ( "Couldn't read track header from file. Byte %d." % self . bytes_read ) if h != 'MTrk' : raise HeaderError ( 'Not a valid Track header. Byte %d.' % self . bytes_read ) try : chunk_size = fp . read ( 4 ) self ...
Return the size of the track chunk .
236,202
def parse_varbyte_as_int ( self , fp , return_bytes_read = True ) : result = 0 bytes_read = 0 r = 0x80 while r & 0x80 : try : r = self . bytes_to_int ( fp . read ( 1 ) ) self . bytes_read += 1 except : raise IOError ( "Couldn't read variable length byte from file." ) if r & 0x80 : result = ( result << 7 ) + ( r & 0x7F ...
Read a variable length byte from the file and return the corresponding integer .
236,203
def set_note ( self , name = 'C' , octave = 4 , dynamics = { } ) : dash_index = name . split ( '-' ) if len ( dash_index ) == 1 : if notes . is_valid_note ( name ) : self . name = name self . octave = octave self . dynamics = dynamics return self else : raise NoteFormatError ( "The string '%s' is not a valid " "represe...
Set the note to name in octave with dynamics .
236,204
def change_octave ( self , diff ) : self . octave += diff if self . octave < 0 : self . octave = 0
Change the octave of the note to the current octave + diff .
236,205
def transpose ( self , interval , up = True ) : ( old , o_octave ) = ( self . name , self . octave ) self . name = intervals . from_shorthand ( self . name , interval , up ) if up : if self < Note ( old , o_octave ) : self . octave += 1 else : if self > Note ( old , o_octave ) : self . octave -= 1
Transpose the note up or down the interval .
236,206
def from_int ( self , integer ) : self . name = notes . int_to_note ( integer % 12 ) self . octave = integer // 12 return self
Set the Note corresponding to the integer .
236,207
def from_hertz ( self , hertz , standard_pitch = 440 ) : value = ( ( log ( ( float ( hertz ) * 1024 ) / standard_pitch , 2 ) + 1.0 / 24 ) * 12 + 9 ) self . name = notes . int_to_note ( int ( value ) % 12 ) self . octave = int ( value / 12 ) - 6 return self
Set the Note name and pitch calculated from the hertz value .
236,208
def to_shorthand ( self ) : if self . octave < 3 : res = self . name else : res = str . lower ( self . name ) o = self . octave - 3 while o < - 1 : res += ',' o += 1 while o > 0 : res += "'" o -= 1 return res
Give the traditional Helmhotz pitch notation .
236,209
def from_shorthand ( self , shorthand ) : name = '' octave = 0 for x in shorthand : if x in [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' ] : name = str . upper ( x ) octave = 3 elif x in [ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' ] : name = x octave = 2 elif x in [ '#' , 'b' ] : name += x elif x == ',' : octave -= 1 elif x...
Convert from traditional Helmhotz pitch notation .
236,210
def interval ( key , start_note , interval ) : if not notes . is_valid_note ( start_note ) : raise KeyError ( "The start note '%s' is not a valid note" % start_note ) notes_in_key = keys . get_notes ( key ) for n in notes_in_key : if n [ 0 ] == start_note [ 0 ] : index = notes_in_key . index ( n ) return notes_in_key [...
Return the note found at the interval starting from start_note in the given key .
236,211
def measure ( note1 , note2 ) : res = notes . note_to_int ( note2 ) - notes . note_to_int ( note1 ) if res < 0 : return 12 - res * - 1 else : return res
Return an integer in the range of 0 - 11 determining the half note steps between note1 and note2 .
236,212
def augment_or_diminish_until_the_interval_is_right ( note1 , note2 , interval ) : cur = measure ( note1 , note2 ) while cur != interval : if cur > interval : note2 = notes . diminish ( note2 ) elif cur < interval : note2 = notes . augment ( note2 ) cur = measure ( note1 , note2 ) val = 0 for token in note2 [ 1 : ] : i...
A helper function for the minor and major functions .
236,213
def invert ( interval ) : interval . reverse ( ) res = list ( interval ) interval . reverse ( ) return res
Invert an interval .
236,214
def determine ( note1 , note2 , shorthand = False ) : if note1 [ 0 ] == note2 [ 0 ] : def get_val ( note ) : r = 0 for x in note [ 1 : ] : if x == 'b' : r -= 1 elif x == '#' : r += 1 return r x = get_val ( note1 ) y = get_val ( note2 ) if x == y : if not shorthand : return 'major unison' return '1' elif x < y : if not ...
Name the interval between note1 and note2 .
236,215
def from_shorthand ( note , interval , up = True ) : if not notes . is_valid_note ( note ) : return False shorthand_lookup = [ [ '1' , major_unison , major_unison ] , [ '2' , major_second , minor_seventh ] , [ '3' , major_third , minor_sixth ] , [ '4' , major_fourth , major_fifth ] , [ '5' , major_fifth , major_fourth ...
Return the note on interval up or down .
236,216
def is_consonant ( note1 , note2 , include_fourths = True ) : return ( is_perfect_consonant ( note1 , note2 , include_fourths ) or is_imperfect_consonant ( note1 , note2 ) )
Return True if the interval is consonant .
236,217
def is_perfect_consonant ( note1 , note2 , include_fourths = True ) : dhalf = measure ( note1 , note2 ) return dhalf in [ 0 , 7 ] or include_fourths and dhalf == 5
Return True if the interval is a perfect consonant one .
236,218
def add_track ( self , track ) : if not hasattr ( track , 'bars' ) : raise UnexpectedObjectError ( "Unexpected object '%s', " "expecting a mingus.containers.Track object" % track ) self . tracks . append ( track ) self . selected_tracks = [ len ( self . tracks ) - 1 ]
Add a track to the composition .
236,219
def add_note ( self , note ) : for n in self . selected_tracks : self . tracks [ n ] + note
Add a note to the selected tracks .
236,220
def cfunc ( name , result , * args ) : atypes = [ ] aflags = [ ] for arg in args : atypes . append ( arg [ 1 ] ) aflags . append ( ( arg [ 2 ] , arg [ 0 ] ) + arg [ 3 : ] ) return CFUNCTYPE ( result , * atypes ) ( ( name , _fl ) , tuple ( aflags ) )
Build and apply a ctypes prototype complete with parameter flags .
236,221
def fluid_synth_write_s16_stereo ( synth , len ) : import numpy buf = create_string_buffer ( len * 4 ) fluid_synth_write_s16 ( synth , len , buf , 0 , 2 , buf , 1 , 2 ) return numpy . fromstring ( buf [ : ] , dtype = numpy . int16 )
Return generated samples in stereo 16 - bit format .
236,222
def start ( self , driver = None ) : if driver is not None : assert driver in [ 'alsa' , 'oss' , 'jack' , 'portaudio' , 'sndmgr' , 'coreaudio' , 'Direct Sound' , 'dsound' , 'pulseaudio' ] fluid_settings_setstr ( self . settings , 'audio.driver' , driver ) self . audio_driver = new_fluid_audio_driver ( self . settings ,...
Start audio output driver in separate background thread .
236,223
def program_select ( self , chan , sfid , bank , preset ) : return fluid_synth_program_select ( self . synth , chan , sfid , bank , preset )
Select a program .
236,224
def noteon ( self , chan , key , vel ) : if key < 0 or key > 128 : return False if chan < 0 : return False if vel < 0 or vel > 128 : return False return fluid_synth_noteon ( self . synth , chan , key , vel )
Play a note .
236,225
def noteoff ( self , chan , key ) : if key < 0 or key > 128 : return False if chan < 0 : return False return fluid_synth_noteoff ( self . synth , chan , key )
Stop a note .
236,226
def cc ( self , chan , ctrl , val ) : return fluid_synth_cc ( self . synth , chan , ctrl , val )
Send control change value .
236,227
def is_valid_note ( note ) : if not _note_dict . has_key ( note [ 0 ] ) : return False for post in note [ 1 : ] : if post != 'b' and post != '#' : return False return True
Return True if note is in a recognised format . False if not .
236,228
def reduce_accidentals ( note ) : val = note_to_int ( note [ 0 ] ) for token in note [ 1 : ] : if token == 'b' : val -= 1 elif token == '#' : val += 1 else : raise NoteFormatError ( "Unknown note format '%s'" % note ) if val >= note_to_int ( note [ 0 ] ) : return int_to_note ( val % 12 ) else : return int_to_note ( val...
Reduce any extra accidentals to proper notes .
236,229
def remove_redundant_accidentals ( note ) : val = 0 for token in note [ 1 : ] : if token == 'b' : val -= 1 elif token == '#' : val += 1 result = note [ 0 ] while val > 0 : result = augment ( result ) val -= 1 while val < 0 : result = diminish ( result ) val += 1 return result
Remove redundant sharps and flats from the given note .
236,230
def _gcd ( a = None , b = None , terms = None ) : if terms : return reduce ( lambda a , b : _gcd ( a , b ) , terms ) else : while b : ( a , b ) = ( b , a % b ) return a
Return greatest common divisor using Euclid s Algorithm .
236,231
def get_key_signature ( key = 'C' ) : if not is_valid_key ( key ) : raise NoteFormatError ( "unrecognized format for key '%s'" % key ) for couple in keys : if key in couple : accidentals = keys . index ( couple ) - 7 return accidentals
Return the key signature .
236,232
def get_key_signature_accidentals ( key = 'C' ) : accidentals = get_key_signature ( key ) res = [ ] if accidentals < 0 : for i in range ( - accidentals ) : res . append ( '{0}{1}' . format ( list ( reversed ( notes . fifths ) ) [ i ] , 'b' ) ) elif accidentals > 0 : for i in range ( accidentals ) : res . append ( '{0}{...
Return the list of accidentals present into the key signature .
236,233
def get_notes ( key = 'C' ) : if _key_cache . has_key ( key ) : return _key_cache [ key ] if not is_valid_key ( key ) : raise NoteFormatError ( "unrecognized format for key '%s'" % key ) result = [ ] altered_notes = map ( operator . itemgetter ( 0 ) , get_key_signature_accidentals ( key ) ) if get_key_signature ( key )...
Return an ordered list of the notes in this natural key .
236,234
def attach ( self , listener ) : if listener not in self . listeners : self . listeners . append ( listener )
Attach an object that should be notified of events .
236,235
def detach ( self , listener ) : if listener in self . listeners : self . listeners . remove ( listener )
Detach a listening object so that it won t receive any events anymore .
236,236
def notify_listeners ( self , msg_type , params ) : for c in self . listeners : c . notify ( msg_type , params )
Send a message to all the observers .
236,237
def set_instrument ( self , channel , instr , bank = 0 ) : self . instr_event ( channel , instr , bank ) self . notify_listeners ( self . MSG_INSTR , { 'channel' : int ( channel ) , 'instr' : int ( instr ) , 'bank' : int ( bank ) } )
Set the channel to the instrument _instr_ .
236,238
def control_change ( self , channel , control , value ) : if control < 0 or control > 128 : return False if value < 0 or value > 128 : return False self . cc_event ( channel , control , value ) self . notify_listeners ( self . MSG_CC , { 'channel' : int ( channel ) , 'control' : int ( control ) , 'value' : int ( value ...
Send a control change message .
236,239
def stop_Note ( self , note , channel = 1 ) : if hasattr ( note , 'channel' ) : channel = note . channel self . stop_event ( int ( note ) + 12 , int ( channel ) ) self . notify_listeners ( self . MSG_STOP_INT , { 'channel' : int ( channel ) , 'note' : int ( note ) + 12 } ) self . notify_listeners ( self . MSG_STOP_NOTE...
Stop a note on a channel .
236,240
def stop_everything ( self ) : for x in range ( 118 ) : for c in range ( 16 ) : self . stop_Note ( x , c )
Stop all the notes on all channels .
236,241
def play_NoteContainer ( self , nc , channel = 1 , velocity = 100 ) : self . notify_listeners ( self . MSG_PLAY_NC , { 'notes' : nc , 'channel' : channel , 'velocity' : velocity } ) if nc is None : return True for note in nc : if not self . play_Note ( note , channel , velocity ) : return False return True
Play the Notes in the NoteContainer nc .
236,242
def stop_NoteContainer ( self , nc , channel = 1 ) : self . notify_listeners ( self . MSG_PLAY_NC , { 'notes' : nc , 'channel' : channel } ) if nc is None : return True for note in nc : if not self . stop_Note ( note , channel ) : return False return True
Stop playing the notes in NoteContainer nc .
236,243
def play_Bar ( self , bar , channel = 1 , bpm = 120 ) : self . notify_listeners ( self . MSG_PLAY_BAR , { 'bar' : bar , 'channel' : channel , 'bpm' : bpm } ) qn_length = 60.0 / bpm for nc in bar : if not self . play_NoteContainer ( nc [ 2 ] , channel , 100 ) : return { } if hasattr ( nc [ 2 ] , 'bpm' ) : bpm = nc [ 2 ]...
Play a Bar object .
236,244
def play_Track ( self , track , channel = 1 , bpm = 120 ) : self . notify_listeners ( self . MSG_PLAY_TRACK , { 'track' : track , 'channel' : channel , 'bpm' : bpm } ) for bar in track : res = self . play_Bar ( bar , channel , bpm ) if res != { } : bpm = res [ 'bpm' ] else : return { } return { 'bpm' : bpm }
Play a Track object .
236,245
def play_Tracks ( self , tracks , channels , bpm = 120 ) : self . notify_listeners ( self . MSG_PLAY_TRACKS , { 'tracks' : tracks , 'channels' : channels , 'bpm' : bpm } ) for x in range ( len ( tracks ) ) : instr = tracks [ x ] . instrument if isinstance ( instr , MidiInstrument ) : try : i = instr . names . index ( i...
Play a list of Tracks .
236,246
def play_Composition ( self , composition , channels = None , bpm = 120 ) : self . notify_listeners ( self . MSG_PLAY_COMPOSITION , { 'composition' : composition , 'channels' : channels , 'bpm' : bpm } ) if channels == None : channels = map ( lambda x : x + 1 , range ( len ( composition . tracks ) ) ) return self . pla...
Play a Composition object .
236,247
def _find_log_index ( f ) : global _last_asked , _log_cache ( begin , end ) = ( 0 , 128 ) if _last_asked is not None : ( lastn , lastval ) = _last_asked if f >= lastval : if f <= _log_cache [ lastn ] : _last_asked = ( lastn , f ) return lastn elif f <= _log_cache [ lastn + 1 ] : _last_asked = ( lastn + 1 , f ) return l...
Look up the index of the frequency f in the frequency table .
236,248
def find_frequencies ( data , freq = 44100 , bits = 16 ) : n = len ( data ) p = _fft ( data ) uniquePts = numpy . ceil ( ( n + 1 ) / 2.0 ) p = [ ( abs ( x ) / float ( n ) ) ** 2 * 2 for x in p [ 0 : uniquePts ] ] p [ 0 ] = p [ 0 ] / 2 if n % 2 == 0 : p [ - 1 ] = p [ - 1 ] / 2 s = freq / float ( n ) freqArray = numpy . ...
Convert audio data into a frequency - amplitude table using fast fourier transformation .
236,249
def find_Note ( data , freq , bits ) : data = find_frequencies ( data , freq , bits ) return sorted ( find_notes ( data ) , key = operator . itemgetter ( 1 ) ) [ - 1 ] [ 0 ]
Get the frequencies feed them to find_notes and the return the Note with the highest amplitude .
236,250
def analyze_chunks ( data , freq , bits , chunksize = 512 ) : res = [ ] while data != [ ] : f = find_frequencies ( data [ : chunksize ] , freq , bits ) res . append ( sorted ( find_notes ( f ) , key = operator . itemgetter ( 1 ) ) [ - 1 ] [ 0 ] ) data = data [ chunksize : ] return res
Cut the one channel data in chunks and analyzes them separately .
236,251
def find_melody ( file = '440_480_clean.wav' , chunksize = 512 ) : ( data , freq , bits ) = data_from_file ( file ) res = [ ] for d in analyze_chunks ( data , freq , bits , chunksize ) : if res != [ ] : if res [ - 1 ] [ 0 ] == d : val = res [ - 1 ] [ 1 ] res [ - 1 ] = ( d , val + 1 ) else : res . append ( ( d , 1 ) ) e...
Cut the sample into chunks and analyze each chunk .
236,252
def write_Note ( file , note , bpm = 120 , repeat = 0 , verbose = False ) : m = MidiFile ( ) t = MidiTrack ( bpm ) m . tracks = [ t ] while repeat >= 0 : t . set_deltatime ( '\x00' ) t . play_Note ( note ) t . set_deltatime ( "\x48" ) t . stop_Note ( note ) repeat -= 1 return m . write_file ( file , verbose )
Expect a Note object from mingus . containers and save it into a MIDI file specified in file .
236,253
def write_NoteContainer ( file , notecontainer , bpm = 120 , repeat = 0 , verbose = False ) : m = MidiFile ( ) t = MidiTrack ( bpm ) m . tracks = [ t ] while repeat >= 0 : t . set_deltatime ( '\x00' ) t . play_NoteContainer ( notecontainer ) t . set_deltatime ( "\x48" ) t . stop_NoteContainer ( notecontainer ) repeat -...
Write a mingus . NoteContainer to a MIDI file .
236,254
def write_Bar ( file , bar , bpm = 120 , repeat = 0 , verbose = False ) : m = MidiFile ( ) t = MidiTrack ( bpm ) m . tracks = [ t ] while repeat >= 0 : t . play_Bar ( bar ) repeat -= 1 return m . write_file ( file , verbose )
Write a mingus . Bar to a MIDI file .
236,255
def write_Track ( file , track , bpm = 120 , repeat = 0 , verbose = False ) : m = MidiFile ( ) t = MidiTrack ( bpm ) m . tracks = [ t ] while repeat >= 0 : t . play_Track ( track ) repeat -= 1 return m . write_file ( file , verbose )
Write a mingus . Track to a MIDI file .
236,256
def write_Composition ( file , composition , bpm = 120 , repeat = 0 , verbose = False ) : m = MidiFile ( ) t = [ ] for x in range ( len ( composition . tracks ) ) : t += [ MidiTrack ( bpm ) ] m . tracks = t while repeat >= 0 : for i in range ( len ( composition . tracks ) ) : m . tracks [ i ] . play_Track ( composition...
Write a mingus . Composition to a MIDI file .
236,257
def get_midi_data ( self ) : tracks = [ t . get_midi_data ( ) for t in self . tracks if t . track_data != '' ] return self . header ( ) + '' . join ( tracks )
Collect and return the raw binary MIDI data from the tracks .
236,258
def header ( self ) : tracks = a2b_hex ( '%04x' % len ( [ t for t in self . tracks if t . track_data != '' ] ) ) return 'MThd\x00\x00\x00\x06\x00\x01' + tracks + self . time_division
Return a header for type 1 MIDI file .
236,259
def write_file ( self , file , verbose = False ) : dat = self . get_midi_data ( ) try : f = open ( file , 'wb' ) except : print "Couldn't open '%s' for writing." % file return False try : f . write ( dat ) except : print 'An error occured while writing data to %s.' % file return False f . close ( ) if verbose : print '...
Collect the data from get_midi_data and write to file .
236,260
def fingers_needed ( fingering ) : split = False indexfinger = False minimum = min ( finger for finger in fingering if finger ) result = 0 for finger in reversed ( fingering ) : if finger == 0 : split = True else : if not split and finger == minimum : if not indexfinger : result += 1 indexfinger = True else : result +=...
Return the number of fingers needed to play the given fingering .
236,261
def add_tuning ( instrument , description , tuning ) : t = StringTuning ( instrument , description , tuning ) if _known . has_key ( str . upper ( instrument ) ) : _known [ str . upper ( instrument ) ] [ 1 ] [ str . upper ( description ) ] = t else : _known [ str . upper ( instrument ) ] = ( instrument , { str . upper (...
Add a new tuning to the index .
236,262
def get_tuning ( instrument , description , nr_of_strings = None , nr_of_courses = None ) : searchi = str . upper ( instrument ) searchd = str . upper ( description ) keys = _known . keys ( ) for x in keys : if ( searchi not in keys and x . find ( searchi ) == 0 or searchi in keys and x == searchi ) : for ( desc , tun ...
Get the first tuning that satisfies the constraints .
236,263
def get_tunings ( instrument = None , nr_of_strings = None , nr_of_courses = None ) : search = '' if instrument is not None : search = str . upper ( instrument ) result = [ ] keys = _known . keys ( ) inkeys = search in keys for x in keys : if ( instrument is None or not inkeys and x . find ( search ) == 0 or inkeys and...
Search tunings on instrument strings courses or a combination .
236,264
def count_courses ( self ) : c = 0 for x in self . tuning : if type ( x ) == list : c += len ( x ) else : c += 1 return float ( c ) / len ( self . tuning )
Return the average number of courses per string .
236,265
def find_frets ( self , note , maxfret = 24 ) : result = [ ] if type ( note ) == str : note = Note ( note ) for x in self . tuning : if type ( x ) == list : base = x [ 0 ] else : base = x diff = base . measure ( note ) if 0 <= diff <= maxfret : result . append ( diff ) else : result . append ( None ) return result
Return a list with for each string the fret on which the note is played or None if it can t be played on that particular string .
236,266
def frets_to_NoteContainer ( self , fingering ) : res = [ ] for ( string , fret ) in enumerate ( fingering ) : if fret is not None : res . append ( self . get_Note ( string , fret ) ) return NoteContainer ( res )
Convert a list such as returned by find_fret to a NoteContainer .
236,267
def get_Note ( self , string = 0 , fret = 0 , maxfret = 24 ) : if 0 <= string < self . count_strings ( ) : if 0 <= fret <= maxfret : s = self . tuning [ string ] if type ( s ) == list : s = s [ 0 ] n = Note ( int ( s ) + fret ) n . string = string n . fret = fret return n else : raise RangeError ( "Fret '%d' on string ...
Return the Note on string fret .
236,268
def load_img ( name ) : fullname = name try : image = pygame . image . load ( fullname ) if image . get_alpha ( ) is None : image = image . convert ( ) else : image = image . convert_alpha ( ) except pygame . error , message : print "Error: couldn't load image: " , fullname raise SystemExit , message return ( image , i...
Load image and return an image object
236,269
def play_note ( note ) : index = None if note == Note ( 'B' , 2 ) : index = 0 elif note == Note ( 'A' , 2 ) : index = 1 elif note == Note ( 'G' , 2 ) : index = 2 elif note == Note ( 'E' , 2 ) : index = 3 elif note == Note ( 'C' , 2 ) : index = 4 elif note == Note ( 'A' , 3 ) : index = 5 elif note == Note ( 'B' , 3 ) : ...
play_note determines which pad was hit and send the play request to fluidsynth
236,270
def play_note ( note ) : global text octave_offset = ( note . octave - LOWEST ) * width if note . name in WHITE_KEYS : w = WHITE_KEYS . index ( note . name ) * white_key_width w = w + octave_offset playing_w . append ( [ w , tick , note ] ) else : i = BLACK_KEYS . index ( note . name ) if i == 0 : w = 18 elif i == 1 : ...
play_note determines the coordinates of a note on the keyboard image and sends a request to play the note to the fluidsynth server
236,271
def add_composition ( self , composition ) : if not hasattr ( composition , 'tracks' ) : raise UnexpectedObjectError ( "Object '%s' not expected. Expecting " "a mingus.containers.Composition object." % composition ) self . compositions . append ( composition ) return self
Add a composition to the suite .
236,272
def set_author ( self , author , email = '' ) : self . author = author self . email = email
Set the author of the suite .
236,273
def set_title ( self , title , subtitle = '' ) : self . title = title self . subtitle = subtitle
Set the title and the subtitle of the suite .
236,274
def set_range ( self , range ) : if type ( range [ 0 ] ) == str : range [ 0 ] = Note ( range [ 0 ] ) range [ 1 ] = Note ( range [ 1 ] ) if not hasattr ( range [ 0 ] , 'name' ) : raise UnexpectedObjectError ( "Unexpected object '%s'. " "Expecting a mingus.containers.Note object" % range [ 0 ] ) self . range = range
Set the range of the instrument .
236,275
def note_in_range ( self , note ) : if type ( note ) == str : note = Note ( note ) if not hasattr ( note , 'name' ) : raise UnexpectedObjectError ( "Unexpected object '%s'. " "Expecting a mingus.containers.Note object" % note ) if note >= self . range [ 0 ] and note <= self . range [ 1 ] : return True return False
Test whether note is in the range of this Instrument .
236,276
def can_play_notes ( self , notes ) : if hasattr ( notes , 'notes' ) : notes = notes . notes if type ( notes ) != list : notes = [ notes ] for n in notes : if not self . note_in_range ( n ) : return False return True
Test if the notes lie within the range of the instrument .
236,277
def play_Note ( self , note ) : velocity = 64 channel = 1 if hasattr ( note , 'dynamics' ) : if 'velocity' in note . dynamics : velocity = note . dynamics [ 'velocity' ] if 'channel' in note . dynamics : channel = note . dynamics [ 'channel' ] if hasattr ( note , 'channel' ) : channel = note . channel if hasattr ( note...
Convert a Note object to a midi event and adds it to the track_data .
236,278
def play_NoteContainer ( self , notecontainer ) : if len ( notecontainer ) <= 1 : [ self . play_Note ( x ) for x in notecontainer ] else : self . play_Note ( notecontainer [ 0 ] ) self . set_deltatime ( 0 ) [ self . play_Note ( x ) for x in notecontainer [ 1 : ] ]
Convert a mingus . containers . NoteContainer to the equivalent MIDI events and add it to the track_data .
236,279
def play_Bar ( self , bar ) : self . set_deltatime ( self . delay ) self . delay = 0 self . set_meter ( bar . meter ) self . set_deltatime ( 0 ) self . set_key ( bar . key ) for x in bar : tick = int ( round ( ( 1.0 / x [ 1 ] ) * 288 ) ) if x [ 2 ] is None or len ( x [ 2 ] ) == 0 : self . delay += tick else : self . se...
Convert a Bar object to MIDI events and write them to the track_data .
236,280
def play_Track ( self , track ) : if hasattr ( track , 'name' ) : self . set_track_name ( track . name ) self . delay = 0 instr = track . instrument if hasattr ( instr , 'instrument_nr' ) : self . change_instrument = True self . instrument = instr . instrument_nr for bar in track : self . play_Bar ( bar )
Convert a Track object to MIDI events and write them to the track_data .
236,281
def stop_Note ( self , note ) : velocity = 64 channel = 1 if hasattr ( note , 'dynamics' ) : if 'velocity' in note . dynamics : velocity = note . dynamics [ 'velocity' ] if 'channel' in note . dynamics : channel = note . dynamics [ 'channel' ] if hasattr ( note , 'channel' ) : channel = note . channel if hasattr ( note...
Add a note_off event for note to event_track .
236,282
def stop_NoteContainer ( self , notecontainer ) : if len ( notecontainer ) <= 1 : [ self . stop_Note ( x ) for x in notecontainer ] else : self . stop_Note ( notecontainer [ 0 ] ) self . set_deltatime ( 0 ) [ self . stop_Note ( x ) for x in notecontainer [ 1 : ] ]
Add note_off events for each note in the NoteContainer to the track_data .
236,283
def set_instrument ( self , channel , instr , bank = 1 ) : self . track_data += self . select_bank ( channel , bank ) self . track_data += self . program_change_event ( channel , instr )
Add a program change and bank select event to the track_data .
236,284
def header ( self ) : chunk_size = a2b_hex ( '%08x' % ( len ( self . track_data ) + len ( self . end_of_track ( ) ) ) ) return TRACK_HEADER + chunk_size
Return the bytes for the header of track .
236,285
def midi_event ( self , event_type , channel , param1 , param2 = None ) : assert event_type < 0x80 and event_type >= 0 assert channel < 16 and channel >= 0 tc = a2b_hex ( '%x%x' % ( event_type , channel ) ) if param2 is None : params = a2b_hex ( '%02x' % param1 ) else : params = a2b_hex ( '%02x%02x' % ( param1 , param2...
Convert and return the paraters as a MIDI event in bytes .
236,286
def note_off ( self , channel , note , velocity ) : return self . midi_event ( NOTE_OFF , channel , note , velocity )
Return bytes for a note off event .
236,287
def note_on ( self , channel , note , velocity ) : return self . midi_event ( NOTE_ON , channel , note , velocity )
Return bytes for a note_on event .
236,288
def controller_event ( self , channel , contr_nr , contr_val ) : return self . midi_event ( CONTROLLER , channel , contr_nr , contr_val )
Return the bytes for a MIDI controller event .
236,289
def set_deltatime ( self , delta_time ) : if type ( delta_time ) == int : delta_time = self . int_to_varbyte ( delta_time ) self . delta_time = delta_time
Set the delta_time .
236,290
def set_tempo ( self , bpm ) : self . bpm = bpm self . track_data += self . set_tempo_event ( self . bpm )
Convert the bpm to a midi event and write it to the track_data .
236,291
def set_tempo_event ( self , bpm ) : ms_per_min = 60000000 mpqn = a2b_hex ( '%06x' % ( ms_per_min / bpm ) ) return self . delta_time + META_EVENT + SET_TEMPO + '\x03' + mpqn
Calculate the microseconds per quarter note .
236,292
def time_signature_event ( self , meter = ( 4 , 4 ) ) : numer = a2b_hex ( '%02x' % meter [ 0 ] ) denom = a2b_hex ( '%02x' % int ( log ( meter [ 1 ] , 2 ) ) ) return self . delta_time + META_EVENT + TIME_SIGNATURE + '\x04' + numer + denom + '\x18\x08'
Return a time signature event for meter .
236,293
def set_key ( self , key = 'C' ) : if isinstance ( key , Key ) : key = key . name [ 0 ] self . track_data += self . key_signature_event ( key )
Add a key signature event to the track_data .
236,294
def key_signature_event ( self , key = 'C' ) : if key . islower ( ) : val = minor_keys . index ( key ) - 7 mode = '\x01' else : val = major_keys . index ( key ) - 7 mode = '\x00' if val < 0 : val = 256 + val key = a2b_hex ( '%02x' % val ) return '{0}{1}{2}\x02{3}{4}' . format ( self . delta_time , META_EVENT , KEY_SIGN...
Return the bytes for a key signature event .
236,295
def track_name_event ( self , name ) : l = self . int_to_varbyte ( len ( name ) ) return '\x00' + META_EVENT + TRACK_NAME + l + name
Return the bytes for a track name meta event .
236,296
def int_to_varbyte ( self , value ) : length = int ( log ( max ( value , 1 ) , 0x80 ) ) + 1 bytes = [ value >> i * 7 & 0x7F for i in range ( length ) ] bytes . reverse ( ) for i in range ( len ( bytes ) - 1 ) : bytes [ i ] = bytes [ i ] | 0x80 return pack ( '%sB' % len ( bytes ) , * bytes )
Convert an integer into a variable length byte .
236,297
def to_chords ( progression , key = 'C' ) : if type ( progression ) == str : progression = [ progression ] result = [ ] for chord in progression : ( roman_numeral , acc , suffix ) = parse_string ( chord ) if roman_numeral not in numerals : return [ ] if suffix == '7' or suffix == '' : roman_numeral += suffix r = chords...
Convert a list of chord functions or a string to a list of chords .
236,298
def determine ( chord , key , shorthand = False ) : result = [ ] if type ( chord [ 0 ] ) == list : for c in chord : result . append ( determine ( c , key , shorthand ) ) return result func_dict = { 'I' : 'tonic' , 'ii' : 'supertonic' , 'iii' : 'mediant' , 'IV' : 'subdominant' , 'V' : 'dominant' , 'vi' : 'submediant' , ...
Determine the harmonic function of chord in key .
236,299
def tuple_to_string ( prog_tuple ) : ( roman , acc , suff ) = prog_tuple if acc > 6 : acc = 0 - acc % 6 elif acc < - 6 : acc = acc % 6 while acc < 0 : roman = 'b' + roman acc += 1 while acc > 0 : roman = '#' + roman acc -= 1 return roman + suff
Create a string from tuples returned by parse_string .