Instrument.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. export class Instrument extends InstrumentalGroup implements ISettableInstrument, IInstrument {
  2. constructor(id: number, idString: string, phonicScoreInterface: IPhonicScoreInterface, musicSheet: MusicSheet, parent: InstrumentalGroup) {
  3. super(musicSheet, parent);
  4. this.phonicScoreInterface = phonicScoreInterface;
  5. this.id = id;
  6. this.idString = idString;
  7. this.nameLabel = new Label(idString);
  8. }
  9. public Transpose: number = 0;
  10. public Highlight: boolean;
  11. public InstrumentParameterChanged: InstrumentParameterChangedDelegate;
  12. private phonicScoreInterface: IPhonicScoreInterface;
  13. private voices: List<Voice> = new List<Voice>();
  14. private staves: List<Staff> = new List<Staff>();
  15. private nameLabel: Label;
  16. // private range: ToneRange;
  17. private idString: string;
  18. private id: number;
  19. private hasLyrics: boolean = false;
  20. private hasChordSymbols: boolean = false;
  21. // private playback
  22. private lyricVersesNumbers: List<number> = new List<number>();
  23. private subInstruments: List<SubInstrument> = new List<SubInstrument>();
  24. public get Voices(): List<Voice> {
  25. return this.voices;
  26. }
  27. public get Staves(): List<Staff> {
  28. return this.staves;
  29. }
  30. public get NameLabel(): Label {
  31. return this.nameLabel;
  32. }
  33. public get HasLyrics(): boolean {
  34. return this.hasLyrics;
  35. }
  36. public set HasLyrics(value: boolean) {
  37. this.hasLyrics = value;
  38. }
  39. public get HasChordSymbols(): boolean {
  40. return this.hasChordSymbols;
  41. }
  42. public set HasChordSymbols(value: boolean) {
  43. this.hasChordSymbols = value;
  44. }
  45. public get LyricVersesNumbers(): List<number> {
  46. return this.lyricVersesNumbers;
  47. }
  48. public set LyricVersesNumbers(value: List<number>) {
  49. this.lyricVersesNumbers = value;
  50. }
  51. public get Name(): string {
  52. return this.nameLabel.Text;
  53. }
  54. public set Name(value: string) {
  55. this.nameLabel.Text = value;
  56. }
  57. public set PhonicScoreInterface(value: IPhonicScoreInterface) {
  58. this.phonicScoreInterface = value;
  59. }
  60. public get IdString(): string {
  61. return this.idString;
  62. }
  63. public get Id(): number {
  64. return this.id;
  65. }
  66. public get MidiInstrumentId(): MidiInstrument {
  67. return this.subInstruments[0].MidiInstrumentId;
  68. }
  69. public set MidiInstrumentId(value: MidiInstrument) {
  70. this.subInstruments[0].MidiInstrumentId = value;
  71. }
  72. public get Volume(): number {
  73. return this.subInstruments[0].Volume;
  74. }
  75. public set Volume(value: number) {
  76. for (let idx: number = 0, len: number = this.subInstruments.Count; idx < len; ++idx) {
  77. let subInstrument: SubInstrument = this.subInstruments[idx];
  78. subInstrument.Volume = value;
  79. }
  80. }
  81. public get PlaybackTranspose(): number {
  82. return this.playbackTranspose;
  83. }
  84. public set PlaybackTranspose(value: number) {
  85. this.playbackTranspose = value;
  86. }
  87. public get SubInstruments(): List<SubInstrument> {
  88. return this.subInstruments;
  89. }
  90. public getSubInstrument(subInstrumentIdString: string): SubInstrument {
  91. for (let idx: number = 0, len: number = this.subInstruments.Count; idx < len; ++idx) {
  92. let subInstrument: SubInstrument = this.subInstruments[idx];
  93. if (subInstrument.IdString === subInstrumentIdString) {
  94. return subInstrument;
  95. }
  96. }
  97. return undefined;
  98. }
  99. public get Visible(): boolean {
  100. if (this.voices.Count > 0) {
  101. return this.Voices[0].Visible;
  102. } else {
  103. return false;
  104. }
  105. }
  106. public set Visible(value: boolean) {
  107. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  108. let v: Voice = this.Voices[idx];
  109. v.Visible = value;
  110. }
  111. }
  112. public get Audible(): boolean {
  113. let result: boolean = false;
  114. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  115. let v: Voice = this.Voices[idx];
  116. result = result || v.Audible;
  117. }
  118. return result;
  119. }
  120. public set Audible(value: boolean) {
  121. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  122. let v: Voice = this.Voices[idx];
  123. v.Audible = value;
  124. }
  125. for (let idx: number = 0, len: number = this.staves.Count; idx < len; ++idx) {
  126. let staff: Staff = this.staves[idx];
  127. staff.Audible = value;
  128. }
  129. }
  130. public get Following(): boolean {
  131. let result: boolean = false;
  132. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  133. let v: Voice = this.Voices[idx];
  134. result = result || v.Following;
  135. }
  136. return result;
  137. }
  138. public set Following(value: boolean) {
  139. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  140. let v: Voice = this.Voices[idx];
  141. v.Following = value;
  142. }
  143. for (let idx: number = 0, len: number = this.staves.Count; idx < len; ++idx) {
  144. let staff: Staff = this.staves[idx];
  145. staff.Following = value;
  146. }
  147. }
  148. public SetVoiceAudible(voiceId: number, audible: boolean): void {
  149. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  150. let v: Voice = this.Voices[idx];
  151. if (v.VoiceId === voiceId) {
  152. v.Audible = audible;
  153. break;
  154. }
  155. }
  156. }
  157. public SetVoiceFollowing(voiceId: number, following: boolean): void {
  158. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  159. let v: Voice = this.Voices[idx];
  160. if (v.VoiceId === voiceId) {
  161. v.Following = following;
  162. break;
  163. }
  164. }
  165. }
  166. public SetStaffAudible(staffId: number, audible: boolean): void {
  167. let staff: Staff = this.staves[staffId - 1];
  168. staff.Audible = audible;
  169. if (audible) {
  170. for (let idx: number = 0, len: number = staff.Voices.Count; idx < len; ++idx) {
  171. let v: Voice = staff.Voices[idx];
  172. v.Audible = true;
  173. }
  174. } else {
  175. for (let idx: number = 0, len: number = staff.Voices.Count; idx < len; ++idx) {
  176. let voice: Voice = staff.Voices[idx];
  177. let isAudibleInOtherStaves: boolean = false;
  178. for (let idx2: number = 0, len2: number = this.Staves.Count; idx2 < len2; ++idx2) {
  179. let st: Staff = this.Staves[idx2];
  180. if (st.Id === staffId || !st.Audible) { continue; }
  181. for (let idx3: number = 0, len3: number = st.Voices.Count; idx3 < len3; ++idx3) {
  182. let v: Voice = st.Voices[idx3];
  183. if (v === voice) {
  184. isAudibleInOtherStaves = true;
  185. }
  186. }
  187. }
  188. if (!isAudibleInOtherStaves) {
  189. voice.Audible = false;
  190. }
  191. }
  192. }
  193. }
  194. public SetStaffFollow(staffId: number, follow: boolean): void {
  195. let staff: Staff = this.staves[staffId - 1];
  196. staff.Following = follow;
  197. if (follow) {
  198. for (let idx: number = 0, len: number = staff.Voices.Count; idx < len; ++idx) {
  199. let v: Voice = staff.Voices[idx];
  200. v.Following = true;
  201. }
  202. } else {
  203. for (let idx: number = 0, len: number = staff.Voices.Count; idx < len; ++idx) {
  204. let voice: Voice = staff.Voices[idx];
  205. let isFollowingInOtherStaves: boolean = false;
  206. for (let idx2: number = 0, len2: number = this.Staves.Count; idx2 < len2; ++idx2) {
  207. let st: Staff = this.Staves[idx2];
  208. if (st.Id === staffId || !st.Following) { continue; }
  209. for (let idx3: number = 0, len3: number = st.Voices.Count; idx3 < len3; ++idx3) {
  210. let v: Voice = st.Voices[idx3];
  211. if (v === voice) {
  212. isFollowingInOtherStaves = true;
  213. }
  214. }
  215. }
  216. if (!isFollowingInOtherStaves) {
  217. voice.Following = false;
  218. }
  219. }
  220. }
  221. }
  222. public areAllVoiceVisible(): boolean {
  223. let counter: number = 0;
  224. for (let idx: number = 0, len: number = this.Voices.Count; idx < len; ++idx) {
  225. let voice: Voice = this.Voices[idx];
  226. if (voice.Visible) {
  227. counter++;
  228. }
  229. }
  230. if (counter === this.voices.Count) {
  231. return true;
  232. }
  233. return false;
  234. }
  235. public createStaves(numberOfStaves: number): void {
  236. for (let i: number = 0; i < numberOfStaves; i++) {
  237. let staff: Staff = new Staff(this, i + 1);
  238. this.staves.Add(staff);
  239. }
  240. }
  241. public SetInstrumentParameter(parameter: InstrumentParameters, value: Object): void {
  242. this.phonicScoreInterface.RequestInstrumentParameter(this.Id, parameter, value);
  243. }
  244. public Dispose(): void {
  245. this.InstrumentParameterChanged = undefined;
  246. }
  247. }