ClefInstruction.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import {Pitch} from "../../../Common/DataObjects/Pitch";
  2. import {AbstractNotationInstruction} from "./AbstractNotationInstruction";
  3. import {NoteEnum} from "../../../Common/DataObjects/Pitch";
  4. import {AccidentalEnum} from "../../../Common/DataObjects/Pitch";
  5. import {ArgumentOutOfRangeException} from "../../Exceptions";
  6. export class ClefInstruction extends AbstractNotationInstruction {
  7. constructor(clefType: ClefEnum = ClefEnum.G, octaveOffset: number = 0, line: number = 2) {
  8. super(undefined); // FIXME? Missing SourceStaffEntry!
  9. this.line = line;
  10. this.clefType = clefType;
  11. this.octaveOffset = octaveOffset;
  12. this.calcParameters();
  13. }
  14. private clefType: ClefEnum = ClefEnum.G;
  15. private line: number = 2;
  16. private octaveOffset: number = 0;
  17. private clefPitch: Pitch;
  18. private referenceCyPosition: number;
  19. public static getDefaultClefFromMidiInstrument(instrument: MidiInstrument): ClefInstruction {
  20. switch (instrument) {
  21. case MidiInstrument.Acoustic_Grand_Piano:
  22. return new ClefInstruction(ClefEnum.F, 0, 4);
  23. case MidiInstrument.Electric_Bass_finger:
  24. return new ClefInstruction(ClefEnum.F, 0, 4);
  25. case MidiInstrument.Electric_Bass_pick:
  26. return new ClefInstruction(ClefEnum.F, 0, 4);
  27. case MidiInstrument.Fretless_Bass:
  28. return new ClefInstruction(ClefEnum.F, 0, 4);
  29. case MidiInstrument.Slap_Bass_1:
  30. return new ClefInstruction(ClefEnum.F, 0, 4);
  31. case MidiInstrument.Slap_Bass_2:
  32. return new ClefInstruction(ClefEnum.F, 0, 4);
  33. case MidiInstrument.Synth_Bass_1:
  34. return new ClefInstruction(ClefEnum.F, 0, 4);
  35. case MidiInstrument.Synth_Bass_2:
  36. return new ClefInstruction(ClefEnum.F, 0, 4);
  37. case MidiInstrument.Contrabass:
  38. return new ClefInstruction(ClefEnum.F, 0, 4);
  39. default:
  40. return new ClefInstruction(ClefEnum.G, 0, 2);
  41. }
  42. }
  43. public static getAllPossibleClefs(): ClefInstruction[] {
  44. let clefList: ClefInstruction[] = [];
  45. for (let i: number = 0; i <= 2; i++) {
  46. let clefInstructionG: ClefInstruction = new ClefInstruction(ClefEnum.G, i, 2);
  47. clefList.push(clefInstructionG);
  48. }
  49. for (let j: number = -2; j <= 0; j++) {
  50. let clefInstructionF: ClefInstruction = new ClefInstruction(ClefEnum.F, j, 4);
  51. clefList.push(clefInstructionF);
  52. }
  53. return clefList;
  54. }
  55. public static isSupportedClef(clef: ClefEnum): boolean {
  56. switch (clef) {
  57. case ClefEnum.G:
  58. case ClefEnum.F:
  59. case ClefEnum.C:
  60. case ClefEnum.percussion:
  61. return true;
  62. default:
  63. return false;
  64. }
  65. }
  66. public get ClefType(): ClefEnum {
  67. return this.clefType;
  68. }
  69. public set ClefType(value: ClefEnum) {
  70. this.clefType = value;
  71. }
  72. public get Line(): number {
  73. return this.line;
  74. }
  75. public set Line(value: number) {
  76. this.line = value;
  77. }
  78. public get OctaveOffset(): number {
  79. return this.octaveOffset;
  80. }
  81. public set OctaveOffset(value: number) {
  82. this.octaveOffset = value;
  83. }
  84. public get ClefPitch(): Pitch {
  85. return this.clefPitch;
  86. }
  87. public set ClefPitch(value: Pitch) {
  88. this.clefPitch = value;
  89. }
  90. public get ReferenceCyPosition(): number {
  91. return this.referenceCyPosition;
  92. }
  93. public set ReferenceCyPosition(value: number) {
  94. this.referenceCyPosition = value;
  95. }
  96. public Equals(other: ClefInstruction): boolean {
  97. if (this === other) {
  98. return true;
  99. }
  100. if (this === undefined || other === undefined) {
  101. return false;
  102. }
  103. return (this.ClefPitch === other.ClefPitch && this.Line === other.Line);
  104. }
  105. public NotEqual(clef2: ClefInstruction): boolean {
  106. return !this.Equals(clef2);
  107. }
  108. public ToString(): string {
  109. return "ClefType: " + this.clefType;
  110. }
  111. private calcParameters(): void {
  112. switch (this.clefType) {
  113. case ClefEnum.G:
  114. this.clefPitch = new Pitch(NoteEnum.G, 1 + this.octaveOffset, AccidentalEnum.NONE);
  115. this.referenceCyPosition = (5 - this.line) + 2;
  116. break;
  117. case ClefEnum.F:
  118. this.clefPitch = new Pitch(NoteEnum.F, 0 + this.octaveOffset, AccidentalEnum.NONE);
  119. this.referenceCyPosition = (5 - this.line) + 1.5;
  120. break;
  121. case ClefEnum.C:
  122. this.clefPitch = new Pitch(NoteEnum.C, 1 + this.octaveOffset, AccidentalEnum.NONE);
  123. this.referenceCyPosition = (5 - this.line);
  124. break;
  125. case ClefEnum.percussion:
  126. this.clefPitch = new Pitch(NoteEnum.C, 2, AccidentalEnum.NONE);
  127. this.referenceCyPosition = 2;
  128. break;
  129. default:
  130. throw new ArgumentOutOfRangeException("clefType");
  131. }
  132. }
  133. }
  134. export enum ClefEnum {
  135. G = 0,
  136. F = 1,
  137. C = 2,
  138. percussion = 3,
  139. TAB = 4
  140. }
  141. export enum MidiInstrument {
  142. None = -1,
  143. Acoustic_Grand_Piano,
  144. Bright_Acoustic_Piano,
  145. Electric_Grand_Piano,
  146. Honky_tonk_Piano,
  147. Electric_Piano_1,
  148. Electric_Piano_2,
  149. Harpsichord,
  150. Clavinet,
  151. Celesta,
  152. Glockenspiel,
  153. Music_Box,
  154. Vibraphone,
  155. Marimba,
  156. Xylophone,
  157. Tubular_Bells,
  158. Dulcimer,
  159. Drawbar_Organ,
  160. Percussive_Organ,
  161. Rock_Organ,
  162. Church_Organ,
  163. Reed_Organ,
  164. Accordion,
  165. Harmonica,
  166. Tango_Accordion,
  167. Acoustic_Guitar_nylon,
  168. Acoustic_Guitar_steel,
  169. Electric_Guitar_jazz,
  170. Electric_Guitar_clean,
  171. Electric_Guitar_muted,
  172. Overdriven_Guitar,
  173. Distortion_Guitar,
  174. Guitar_harmonics,
  175. Acoustic_Bass,
  176. Electric_Bass_finger,
  177. Electric_Bass_pick,
  178. Fretless_Bass,
  179. Slap_Bass_1,
  180. Slap_Bass_2,
  181. Synth_Bass_1,
  182. Synth_Bass_2,
  183. Violin,
  184. Viola,
  185. Cello,
  186. Contrabass,
  187. Tremolo_Strings,
  188. Pizzicato_Strings,
  189. Orchestral_Harp,
  190. Timpani,
  191. String_Ensemble_1,
  192. String_Ensemble_2,
  193. Synth_Strings_1,
  194. Synth_Strings_2,
  195. Choir_Aahs,
  196. Voice_Oohs,
  197. Synth_Voice,
  198. Orchestra_Hit,
  199. Trumpet,
  200. Trombone,
  201. Tuba,
  202. Muted_Trumpet,
  203. French_Horn,
  204. Brass_Section,
  205. Synth_Brass_1,
  206. Synth_Brass_2,
  207. Soprano_Sax,
  208. Alto_Sax,
  209. Tenor_Sax,
  210. Baritone_Sax,
  211. Oboe,
  212. English_Horn,
  213. Bassoon,
  214. Clarinet,
  215. Piccolo,
  216. Flute,
  217. Recorder,
  218. Pan_Flute,
  219. Blown_Bottle,
  220. Shakuhachi,
  221. Whistle,
  222. Ocarina,
  223. Lead_1_square,
  224. Lead_2_sawtooth,
  225. Lead_3_calliope,
  226. Lead_4_chiff,
  227. Lead_5_charang,
  228. Lead_6_voice,
  229. Lead_7_fifths,
  230. Lead_8_bass_lead,
  231. Pad_1_new_age,
  232. Pad_2_warm,
  233. Pad_3_polysynth,
  234. Pad_4_choir,
  235. Pad_5_bowed,
  236. Pad_6_metallic,
  237. Pad_7_halo,
  238. Pad_8_sweep,
  239. FX_1_rain,
  240. FX_2_soundtrack,
  241. FX_3_crystal,
  242. FX_4_atmosphere,
  243. FX_5_brightness,
  244. FX_6_goblins,
  245. FX_7_echoes,
  246. FX_8_scifi,
  247. Sitar,
  248. Banjo,
  249. Shamisen,
  250. Koto,
  251. Kalimba,
  252. Bag_pipe,
  253. Fiddle,
  254. Shanai,
  255. Tinkle_Bell,
  256. Agogo,
  257. Steel_Drums,
  258. Woodblock,
  259. Taiko_Drum,
  260. Melodic_Tom,
  261. Synth_Drum,
  262. Reverse_Cymbal,
  263. Guitar_Fret_Noise,
  264. Breath_Noise,
  265. Seashore,
  266. Bird_Tweet,
  267. Telephone_Ring,
  268. Helicopter,
  269. Applause,
  270. Gunshot,
  271. Percussion = 128
  272. }