| 28 | ); |
| 29 | |
| 30 | export class Waveform implements Mountable { |
| 31 | readonly root: HTMLElement; |
| 32 | private cursor: HTMLElement; |
| 33 | private canvas: HTMLCanvasElement | null = null; |
| 34 | private currentScore: alphaTab.model.Score | null = null; |
| 35 | private audioElement: HTMLAudioElement | null = null; |
| 36 | private updateCursor = () => this.refreshCursor(); |
| 37 | private unsubScoreLoaded: () => void; |
| 38 | private unsubPlayerReady: () => void; |
| 39 | private unsubStateChanged: () => void; |
| 40 | |
| 41 | constructor(private api: alphaTab.AlphaTabApi) { |
| 42 | this.root = parseHtml(html` |
| 43 | <div class="at-waveform hidden"> |
| 44 | <div class="at-waveform-cursor"></div> |
| 45 | </div> |
| 46 | `); |
| 47 | this.cursor = this.root.querySelector('.at-waveform-cursor')!; |
| 48 | this.root.addEventListener('click', e => { |
| 49 | if (this.audioElement) { |
| 50 | const rect = this.root.getBoundingClientRect(); |
| 51 | const percent = (e.clientX - rect.left) / rect.width; |
| 52 | this.audioElement.currentTime = this.audioElement.duration * percent; |
| 53 | } |
| 54 | }); |
| 55 | |
| 56 | this.unsubScoreLoaded = api.scoreLoaded.on(() => this.refresh()); |
| 57 | this.unsubPlayerReady = api.playerReady.on(() => this.refresh()); |
| 58 | this.unsubStateChanged = api.playerStateChanged.on(() => this.refresh()); |
| 59 | } |
| 60 | |
| 61 | private async refresh(): Promise<void> { |
| 62 | const score = this.api.score; |
| 63 | const player = this.api.player; |
| 64 | if (!score || !player || !score.backingTrack || this.api.actualPlayerMode !== alphaTab.PlayerMode.EnabledBackingTrack) { |
| 65 | this.hide(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | const output = player.output as alphaTab.synth.IAudioElementBackingTrackSynthOutput; |
| 70 | if (typeof output.audioElement === 'undefined') { |
| 71 | this.hide(); |
| 72 | return; |
| 73 | } |
| 74 | this.bindAudio(output.audioElement); |
| 75 | |
| 76 | if (score === this.currentScore) { |
| 77 | return; |
| 78 | } |
| 79 | this.currentScore = score; |
| 80 | this.root.classList.remove('hidden'); |
| 81 | await this.draw(score.backingTrack); |
| 82 | } |
| 83 | |
| 84 | private bindAudio(audio: HTMLAudioElement): void { |
| 85 | if (this.audioElement === audio) { |
| 86 | return; |
| 87 | } |
nothing calls this directly
no test coverage detected