asigalov61 commited on
Commit
866d7df
1 Parent(s): fa306c1

Upload TMIDIX.py

Browse files
Files changed (1) hide show
  1. TMIDIX.py +317 -0
TMIDIX.py CHANGED
@@ -9748,6 +9748,323 @@ def escore_notes_to_text_description(escore_notes, song_name='', artist_name='')
9748
 
9749
  return description
9750
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9751
  ###################################################################################
9752
  #
9753
  # This is the end of the TMIDI X Python module
 
9748
 
9749
  return description
9750
 
9751
+ ###################################################################################
9752
+
9753
+ #==================================================================================
9754
+ #
9755
+ # Below constants code is a courtesy of MidiTok
9756
+ #
9757
+ # Retrieved on 12/29/2024
9758
+ #
9759
+ # https://github.com/Natooz/MidiTok/blob/main/src/miditok/constants.py
9760
+ #
9761
+ #==================================================================================
9762
+
9763
+ MIDI_FILES_EXTENSIONS = [".mid", ".midi", ".kar", ".MID", ".MIDI", ".KAR"]
9764
+
9765
+ # The recommended pitches for piano in the GM2 specs are from 21 to 108
9766
+ PIANO_PITCH_RANGE = range(21, 109)
9767
+
9768
+ # Chord params
9769
+ # "chord_unknown" specifies the range of number of notes that can form "unknown" chords
9770
+ # (that do not fit in "chord_maps") to add in tokens.
9771
+ # Known chord maps, with 0 as root note
9772
+ BASIC_CHORDS_MAP = {
9773
+ "min": (0, 3, 7),
9774
+ "maj": (0, 4, 7),
9775
+ "dim": (0, 3, 6),
9776
+ "aug": (0, 4, 8),
9777
+ "sus2": (0, 2, 7),
9778
+ "sus4": (0, 5, 7),
9779
+ "7dom": (0, 4, 7, 10),
9780
+ "7min": (0, 3, 7, 10),
9781
+ "7maj": (0, 4, 7, 11),
9782
+ "7halfdim": (0, 3, 6, 10),
9783
+ "7dim": (0, 3, 6, 9),
9784
+ "7aug": (0, 4, 8, 11),
9785
+ "9maj": (0, 4, 7, 10, 14),
9786
+ "9min": (0, 4, 7, 10, 13),
9787
+ }
9788
+
9789
+ # Drums
9790
+ # Recommended range from the GM2 specs
9791
+ DRUMS_PITCH_RANGE = range(27, 90)
9792
+
9793
+ # Used with chords
9794
+ PITCH_CLASSES = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]
9795
+
9796
+ # http://newt.phys.unsw.edu.au/jw/notes.html
9797
+ # https://www.midi.org/specifications
9798
+
9799
+ # index i = program i+1 in the GM2 specs (7. Appendix A)
9800
+ # index i = program i as retrieved by packages
9801
+ MIDI_INSTRUMENTS = [
9802
+ # Piano
9803
+ {"name": "Acoustic Grand Piano", "pitch_range": range(21, 109)},
9804
+ {"name": "Bright Acoustic Piano", "pitch_range": range(21, 109)},
9805
+ {"name": "Electric Grand Piano", "pitch_range": range(21, 109)},
9806
+ {"name": "Honky-tonk Piano", "pitch_range": range(21, 109)},
9807
+ {"name": "Electric Piano 1", "pitch_range": range(28, 104)},
9808
+ {"name": "Electric Piano 2", "pitch_range": range(28, 104)},
9809
+ {"name": "Harpsichord", "pitch_range": range(41, 90)},
9810
+ {"name": "Clavi", "pitch_range": range(36, 97)},
9811
+ # Chromatic Percussion
9812
+ {"name": "Celesta", "pitch_range": range(60, 109)},
9813
+ {"name": "Glockenspiel", "pitch_range": range(72, 109)},
9814
+ {"name": "Music Box", "pitch_range": range(60, 85)},
9815
+ {"name": "Vibraphone", "pitch_range": range(53, 90)},
9816
+ {"name": "Marimba", "pitch_range": range(48, 85)},
9817
+ {"name": "Xylophone", "pitch_range": range(65, 97)},
9818
+ {"name": "Tubular Bells", "pitch_range": range(60, 78)},
9819
+ {"name": "Dulcimer", "pitch_range": range(60, 85)},
9820
+ # Organs
9821
+ {"name": "Drawbar Organ", "pitch_range": range(36, 97)},
9822
+ {"name": "Percussive Organ", "pitch_range": range(36, 97)},
9823
+ {"name": "Rock Organ", "pitch_range": range(36, 97)},
9824
+ {"name": "Church Organ", "pitch_range": range(21, 109)},
9825
+ {"name": "Reed Organ", "pitch_range": range(36, 97)},
9826
+ {"name": "Accordion", "pitch_range": range(53, 90)},
9827
+ {"name": "Harmonica", "pitch_range": range(60, 85)},
9828
+ {"name": "Tango Accordion", "pitch_range": range(53, 90)},
9829
+ # Guitars
9830
+ {"name": "Acoustic Guitar (nylon)", "pitch_range": range(40, 85)},
9831
+ {"name": "Acoustic Guitar (steel)", "pitch_range": range(40, 85)},
9832
+ {"name": "Electric Guitar (jazz)", "pitch_range": range(40, 87)},
9833
+ {"name": "Electric Guitar (clean)", "pitch_range": range(40, 87)},
9834
+ {"name": "Electric Guitar (muted)", "pitch_range": range(40, 87)},
9835
+ {"name": "Overdriven Guitar", "pitch_range": range(40, 87)},
9836
+ {"name": "Distortion Guitar", "pitch_range": range(40, 87)},
9837
+ {"name": "Guitar Harmonics", "pitch_range": range(40, 87)},
9838
+ # Bass
9839
+ {"name": "Acoustic Bass", "pitch_range": range(28, 56)},
9840
+ {"name": "Electric Bass (finger)", "pitch_range": range(28, 56)},
9841
+ {"name": "Electric Bass (pick)", "pitch_range": range(28, 56)},
9842
+ {"name": "Fretless Bass", "pitch_range": range(28, 56)},
9843
+ {"name": "Slap Bass 1", "pitch_range": range(28, 56)},
9844
+ {"name": "Slap Bass 2", "pitch_range": range(28, 56)},
9845
+ {"name": "Synth Bass 1", "pitch_range": range(28, 56)},
9846
+ {"name": "Synth Bass 2", "pitch_range": range(28, 56)},
9847
+ # Strings & Orchestral instruments
9848
+ {"name": "Violin", "pitch_range": range(55, 94)},
9849
+ {"name": "Viola", "pitch_range": range(48, 85)},
9850
+ {"name": "Cello", "pitch_range": range(36, 73)},
9851
+ {"name": "Contrabass", "pitch_range": range(28, 56)},
9852
+ {"name": "Tremolo Strings", "pitch_range": range(28, 94)},
9853
+ {"name": "Pizzicato Strings", "pitch_range": range(28, 94)},
9854
+ {"name": "Orchestral Harp", "pitch_range": range(23, 104)},
9855
+ {"name": "Timpani", "pitch_range": range(36, 58)},
9856
+ # Ensembles
9857
+ {"name": "String Ensembles 1", "pitch_range": range(28, 97)},
9858
+ {"name": "String Ensembles 2", "pitch_range": range(28, 97)},
9859
+ {"name": "SynthStrings 1", "pitch_range": range(36, 97)},
9860
+ {"name": "SynthStrings 2", "pitch_range": range(36, 97)},
9861
+ {"name": "Choir Aahs", "pitch_range": range(48, 80)},
9862
+ {"name": "Voice Oohs", "pitch_range": range(48, 80)},
9863
+ {"name": "Synth Voice", "pitch_range": range(48, 85)},
9864
+ {"name": "Orchestra Hit", "pitch_range": range(48, 73)},
9865
+ # Brass
9866
+ {"name": "Trumpet", "pitch_range": range(58, 95)},
9867
+ {"name": "Trombone", "pitch_range": range(34, 76)},
9868
+ {"name": "Tuba", "pitch_range": range(29, 56)},
9869
+ {"name": "Muted Trumpet", "pitch_range": range(58, 83)},
9870
+ {"name": "French Horn", "pitch_range": range(41, 78)},
9871
+ {"name": "Brass Section", "pitch_range": range(36, 97)},
9872
+ {"name": "Synth Brass 1", "pitch_range": range(36, 97)},
9873
+ {"name": "Synth Brass 2", "pitch_range": range(36, 97)},
9874
+ # Reed
9875
+ {"name": "Soprano Sax", "pitch_range": range(54, 88)},
9876
+ {"name": "Alto Sax", "pitch_range": range(49, 81)},
9877
+ {"name": "Tenor Sax", "pitch_range": range(42, 76)},
9878
+ {"name": "Baritone Sax", "pitch_range": range(37, 69)},
9879
+ {"name": "Oboe", "pitch_range": range(58, 92)},
9880
+ {"name": "English Horn", "pitch_range": range(52, 82)},
9881
+ {"name": "Bassoon", "pitch_range": range(34, 73)},
9882
+ {"name": "Clarinet", "pitch_range": range(50, 92)},
9883
+ # Pipe
9884
+ {"name": "Piccolo", "pitch_range": range(74, 109)},
9885
+ {"name": "Flute", "pitch_range": range(60, 97)},
9886
+ {"name": "Recorder", "pitch_range": range(60, 97)},
9887
+ {"name": "Pan Flute", "pitch_range": range(60, 97)},
9888
+ {"name": "Blown Bottle", "pitch_range": range(60, 97)},
9889
+ {"name": "Shakuhachi", "pitch_range": range(55, 85)},
9890
+ {"name": "Whistle", "pitch_range": range(60, 97)},
9891
+ {"name": "Ocarina", "pitch_range": range(60, 85)},
9892
+ # Synth Lead
9893
+ {"name": "Lead 1 (square)", "pitch_range": range(21, 109)},
9894
+ {"name": "Lead 2 (sawtooth)", "pitch_range": range(21, 109)},
9895
+ {"name": "Lead 3 (calliope)", "pitch_range": range(36, 97)},
9896
+ {"name": "Lead 4 (chiff)", "pitch_range": range(36, 97)},
9897
+ {"name": "Lead 5 (charang)", "pitch_range": range(36, 97)},
9898
+ {"name": "Lead 6 (voice)", "pitch_range": range(36, 97)},
9899
+ {"name": "Lead 7 (fifths)", "pitch_range": range(36, 97)},
9900
+ {"name": "Lead 8 (bass + lead)", "pitch_range": range(21, 109)},
9901
+ # Synth Pad
9902
+ {"name": "Pad 1 (new age)", "pitch_range": range(36, 97)},
9903
+ {"name": "Pad 2 (warm)", "pitch_range": range(36, 97)},
9904
+ {"name": "Pad 3 (polysynth)", "pitch_range": range(36, 97)},
9905
+ {"name": "Pad 4 (choir)", "pitch_range": range(36, 97)},
9906
+ {"name": "Pad 5 (bowed)", "pitch_range": range(36, 97)},
9907
+ {"name": "Pad 6 (metallic)", "pitch_range": range(36, 97)},
9908
+ {"name": "Pad 7 (halo)", "pitch_range": range(36, 97)},
9909
+ {"name": "Pad 8 (sweep)", "pitch_range": range(36, 97)},
9910
+ # Synth SFX
9911
+ {"name": "FX 1 (rain)", "pitch_range": range(36, 97)},
9912
+ {"name": "FX 2 (soundtrack)", "pitch_range": range(36, 97)},
9913
+ {"name": "FX 3 (crystal)", "pitch_range": range(36, 97)},
9914
+ {"name": "FX 4 (atmosphere)", "pitch_range": range(36, 97)},
9915
+ {"name": "FX 5 (brightness)", "pitch_range": range(36, 97)},
9916
+ {"name": "FX 6 (goblins)", "pitch_range": range(36, 97)},
9917
+ {"name": "FX 7 (echoes)", "pitch_range": range(36, 97)},
9918
+ {"name": "FX 8 (sci-fi)", "pitch_range": range(36, 97)},
9919
+ # Ethnic Misc.
9920
+ {"name": "Sitar", "pitch_range": range(48, 78)},
9921
+ {"name": "Banjo", "pitch_range": range(48, 85)},
9922
+ {"name": "Shamisen", "pitch_range": range(50, 80)},
9923
+ {"name": "Koto", "pitch_range": range(55, 85)},
9924
+ {"name": "Kalimba", "pitch_range": range(48, 80)},
9925
+ {"name": "Bag pipe", "pitch_range": range(36, 78)},
9926
+ {"name": "Fiddle", "pitch_range": range(55, 97)},
9927
+ {"name": "Shanai", "pitch_range": range(48, 73)},
9928
+ # Percussive
9929
+ {"name": "Tinkle Bell", "pitch_range": range(72, 85)},
9930
+ {"name": "Agogo", "pitch_range": range(60, 73)},
9931
+ {"name": "Steel Drums", "pitch_range": range(52, 77)},
9932
+ {"name": "Woodblock", "pitch_range": range(128)},
9933
+ {"name": "Taiko Drum", "pitch_range": range(128)},
9934
+ {"name": "Melodic Tom", "pitch_range": range(128)},
9935
+ {"name": "Synth Drum", "pitch_range": range(128)},
9936
+ {"name": "Reverse Cymbal", "pitch_range": range(128)},
9937
+ # SFX
9938
+ {"name": "Guitar Fret Noise, Guitar Cutting Noise", "pitch_range": range(128)},
9939
+ {"name": "Breath Noise, Flute Key Click", "pitch_range": range(128)},
9940
+ {
9941
+ "name": "Seashore, Rain, Thunder, Wind, Stream, Bubbles",
9942
+ "pitch_range": range(128),
9943
+ },
9944
+ {"name": "Bird Tweet, Dog, Horse Gallop", "pitch_range": range(128)},
9945
+ {
9946
+ "name": "Telephone Ring, Door Creaking, Door, Scratch, Wind Chime",
9947
+ "pitch_range": range(128),
9948
+ },
9949
+ {"name": "Helicopter, Car Sounds", "pitch_range": range(128)},
9950
+ {
9951
+ "name": "Applause, Laughing, Screaming, Punch, Heart Beat, Footstep",
9952
+ "pitch_range": range(128),
9953
+ },
9954
+ {"name": "Gunshot, Machine Gun, Lasergun, Explosion", "pitch_range": range(128)},
9955
+ ]
9956
+
9957
+ INSTRUMENTS_CLASSES = [
9958
+ {"name": "Piano", "program_range": range(8)}, # 0
9959
+ {"name": "Chromatic Percussion", "program_range": range(8, 16)},
9960
+ {"name": "Organ", "program_range": range(16, 24)},
9961
+ {"name": "Guitar", "program_range": range(24, 32)},
9962
+ {"name": "Bass", "program_range": range(32, 40)},
9963
+ {"name": "Strings", "program_range": range(40, 48)}, # 5
9964
+ {"name": "Ensemble", "program_range": range(48, 56)},
9965
+ {"name": "Brass", "program_range": range(56, 64)},
9966
+ {"name": "Reed", "program_range": range(64, 72)},
9967
+ {"name": "Pipe", "program_range": range(72, 80)},
9968
+ {"name": "Synth Lead", "program_range": range(80, 88)}, # 10
9969
+ {"name": "Synth Pad", "program_range": range(88, 96)},
9970
+ {"name": "Synth Effects", "program_range": range(96, 104)},
9971
+ {"name": "Ethnic", "program_range": range(104, 112)},
9972
+ {"name": "Percussive", "program_range": range(112, 120)},
9973
+ {"name": "Sound Effects", "program_range": range(120, 128)}, # 15
9974
+ {"name": "Drums", "program_range": range(-1, 0)},
9975
+ ]
9976
+
9977
+ # To easily get the class index of any instrument program
9978
+ CLASS_OF_INST = [
9979
+ i
9980
+ for i, inst_class in enumerate(INSTRUMENT_CLASSES)
9981
+ for _ in inst_class["program_range"]
9982
+ ]
9983
+
9984
+ # index i = program i+1 in the GM2 specs (8. Appendix B)
9985
+ # index i = program i retrieved by packages
9986
+ DRUMS_SETS = {
9987
+ 0: "Standard",
9988
+ 8: "Room",
9989
+ 16: "Power",
9990
+ 24: "Electronic",
9991
+ 25: "Analog",
9992
+ 32: "Jazz",
9993
+ 40: "Brush",
9994
+ 48: "Orchestra",
9995
+ 56: "SFX",
9996
+ }
9997
+
9998
+ # Control changes list (without specifications):
9999
+ # https://www.midi.org/specifications-old/item/table-3-control-change-messages-data-bytes-2
10000
+ # Undefined and general control changes are not considered here
10001
+ # All these attributes can take values from 0 to 127, with some of them being on/off
10002
+ CONTROL_CHANGES = {
10003
+ # MSB
10004
+ 0: "Bank Select",
10005
+ 1: "Modulation Depth",
10006
+ 2: "Breath Controller",
10007
+ 4: "Foot Controller",
10008
+ 5: "Portamento Time",
10009
+ 6: "Data Entry",
10010
+ 7: "Channel Volume",
10011
+ 8: "Balance",
10012
+ 10: "Pan",
10013
+ 11: "Expression Controller",
10014
+ # LSB
10015
+ 32: "Bank Select",
10016
+ 33: "Modulation Depth",
10017
+ 34: "Breath Controller",
10018
+ 36: "Foot Controller",
10019
+ 37: "Portamento Time",
10020
+ 38: "Data Entry",
10021
+ 39: "Channel Volume",
10022
+ 40: "Balance",
10023
+ 42: "Pan",
10024
+ 43: "Expression Controller",
10025
+ # On / Off control changes, ≤63 off, ≥64 on
10026
+ 64: "Damper Pedal",
10027
+ 65: "Portamento",
10028
+ 66: "Sostenuto",
10029
+ 67: "Soft Pedal",
10030
+ 68: "Legato Footswitch",
10031
+ 69: "Hold 2",
10032
+ # Continuous controls
10033
+ 70: "Sound Variation",
10034
+ 71: "Timbre/Harmonic Intensity",
10035
+ 72: "Release Time",
10036
+ 73: "Attack Time",
10037
+ 74: "Brightness",
10038
+ 75: "Decay Time",
10039
+ 76: "Vibrato Rate",
10040
+ 77: "Vibrato Depth",
10041
+ 78: "Vibrato Delay",
10042
+ 84: "Portamento Control",
10043
+ 88: "High Resolution Velocity Prefix",
10044
+ # Effects depths
10045
+ 91: "Reverb Depth",
10046
+ 92: "Tremolo Depth",
10047
+ 93: "Chorus Depth",
10048
+ 94: "Celeste Depth",
10049
+ 95: "Phaser Depth",
10050
+ # Registered parameters numbers
10051
+ 96: "Data Increment",
10052
+ 97: "Data Decrement",
10053
+ # 98: 'Non-Registered Parameter Number (NRPN) - LSB',
10054
+ # 99: 'Non-Registered Parameter Number (NRPN) - MSB',
10055
+ 100: "Registered Parameter Number (RPN) - LSB",
10056
+ 101: "Registered Parameter Number (RPN) - MSB",
10057
+ # Channel mode controls
10058
+ 120: "All Sound Off",
10059
+ 121: "Reset All Controllers",
10060
+ 122: "Local Control On/Off",
10061
+ 123: "All Notes Off",
10062
+ 124: "Omni Mode Off", # + all notes off
10063
+ 125: "Omni Mode On", # + all notes off
10064
+ 126: "Mono Mode On", # + poly off, + all notes off
10065
+ 127: "Poly Mode On", # + mono off, +all notes off
10066
+ }
10067
+
10068
  ###################################################################################
10069
  #
10070
  # This is the end of the TMIDI X Python module