Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Tim van der Lippe | 305e948 | 2021-04-20 08:22:05 | [diff] [blame] | 5 | import * as i18n from '../../core/i18n/i18n.js'; |
| 6 | import * as UI from '../../ui/legacy/legacy.js'; |
Tim van der Lippe | 229a54f | 2021-05-14 16:59:05 | [diff] [blame] | 7 | import type * as Protocol from '../../generated/protocol.js'; |
Tim van der Lippe | 9081c90 | 2020-01-24 14:32:42 | [diff] [blame] | 8 | |
Simon Zünd | f6afbca | 2021-03-02 06:11:14 | [diff] [blame] | 9 | const UIStrings = { |
Christy Chen | 598b98f | 2020-11-13 23:47:39 | [diff] [blame] | 10 | /** |
Christy Chen | 598b98f | 2020-11-13 23:47:39 | [diff] [blame] | 11 | *@description The current state of an item |
| 12 | */ |
| 13 | state: 'State', |
| 14 | /** |
| 15 | *@description Text in Audio Context Content Builder |
| 16 | */ |
| 17 | sampleRate: 'Sample Rate', |
| 18 | /** |
| 19 | *@description Text in Audio Context Content Builder |
| 20 | */ |
| 21 | callbackBufferSize: 'Callback Buffer Size', |
| 22 | /** |
Peter Marshall | b8bd00f | 2021-02-24 08:25:18 | [diff] [blame] | 23 | * @description Label in the Audio Context Content Builder for the maximum number of output channels |
| 24 | * that this Audio Context has. |
Christy Chen | 598b98f | 2020-11-13 23:47:39 | [diff] [blame] | 25 | */ |
| 26 | maxOutputChannels: 'Max Output Channels', |
| 27 | /** |
| 28 | *@description Text in Audio Context Content Builder |
| 29 | */ |
| 30 | currentTime: 'Current Time', |
| 31 | /** |
| 32 | *@description Text in Audio Context Content Builder |
| 33 | */ |
| 34 | callbackInterval: 'Callback Interval', |
| 35 | /** |
| 36 | *@description Text in Audio Context Content Builder |
| 37 | */ |
| 38 | renderCapacity: 'Render Capacity', |
| 39 | }; |
Tim van der Lippe | 305e948 | 2021-04-20 08:22:05 | [diff] [blame] | 40 | const str_ = i18n.i18n.registerUIStrings('panels/web_audio/AudioContextContentBuilder.ts', UIStrings); |
Christy Chen | 598b98f | 2020-11-13 23:47:39 | [diff] [blame] | 41 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
| 42 | |
Paul Lewis | 2766f08 | 2019-11-28 13:42:57 | [diff] [blame] | 43 | export class ContextDetailBuilder { |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 44 | private readonly fragment: DocumentFragment; |
| 45 | private readonly container: HTMLDivElement; |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 46 | constructor(context: Protocol.WebAudio.BaseAudioContext) { |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 47 | this.fragment = document.createDocumentFragment(); |
| 48 | this.container = document.createElement('div'); |
| 49 | this.container.classList.add('context-detail-container'); |
| 50 | this.fragment.appendChild(this.container); |
| 51 | this.build(context); |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 52 | } |
| 53 | |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 54 | private build(context: Protocol.WebAudio.BaseAudioContext): void { |
Simon Zünd | 67789af | 2021-03-03 13:54:13 | [diff] [blame] | 55 | const title = context.contextType === 'realtime' ? i18n.i18n.lockedString('AudioContext') : |
Simon Zünd | 2bd0829 | 2021-03-03 11:25:58 | [diff] [blame] | 56 | i18n.i18n.lockedString('OfflineAudioContext'); |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 57 | this.addTitle(title, context.contextId); |
| 58 | this.addEntry(i18nString(UIStrings.state), context.contextState); |
| 59 | this.addEntry(i18nString(UIStrings.sampleRate), context.sampleRate, 'Hz'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 60 | if (context.contextType === 'realtime') { |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 61 | this.addEntry(i18nString(UIStrings.callbackBufferSize), context.callbackBufferSize, 'frames'); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 62 | } |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 63 | this.addEntry(i18nString(UIStrings.maxOutputChannels), context.maxOutputChannelCount, 'ch'); |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 64 | } |
| 65 | |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 66 | private addTitle(title: string, subtitle: string): void { |
| 67 | this.container.appendChild(UI.Fragment.html` |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 68 | <div class="context-detail-header"> |
| 69 | <div class="context-detail-title">${title}</div> |
| 70 | <div class="context-detail-subtitle">${subtitle}</div> |
| 71 | </div> |
| 72 | `); |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 73 | } |
| 74 | |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 75 | private addEntry(entry: string, value: string|number, unit?: string|undefined): void { |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 76 | const valueWithUnit = value + (unit ? ` ${unit}` : ''); |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 77 | this.container.appendChild(UI.Fragment.html` |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 78 | <div class="context-detail-row"> |
| 79 | <div class="context-detail-row-entry">${entry}</div> |
| 80 | <div class="context-detail-row-value">${valueWithUnit}</div> |
| 81 | </div> |
| 82 | `); |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 83 | } |
| 84 | |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 85 | getFragment(): DocumentFragment { |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 86 | return this.fragment; |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 87 | } |
Paul Lewis | 2766f08 | 2019-11-28 13:42:57 | [diff] [blame] | 88 | } |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 89 | |
Hongchan Choi | 0927e70 | 2020-01-15 20:13:37 | [diff] [blame] | 90 | export class ContextSummaryBuilder { |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 91 | private readonly fragment: DocumentFragment; |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 92 | constructor(contextId: string, contextRealtimeData: Protocol.WebAudio.ContextRealtimeData) { |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 93 | const time = contextRealtimeData.currentTime.toFixed(3); |
Hongchan Choi | 17a7425 | 2019-06-05 18:27:58 | [diff] [blame] | 94 | const mean = (contextRealtimeData.callbackIntervalMean * 1000).toFixed(3); |
| 95 | const stddev = (Math.sqrt(contextRealtimeData.callbackIntervalVariance) * 1000).toFixed(3); |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 96 | const capacity = (contextRealtimeData.renderCapacity * 100).toFixed(3); |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 97 | this.fragment = document.createDocumentFragment(); |
| 98 | this.fragment.appendChild(UI.Fragment.html` |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 99 | <div class="context-summary-container"> |
| 100 | <span>${i18nString(UIStrings.currentTime)}: ${time} s</span> |
| 101 | <span>\u2758</span> |
| 102 | <span>${i18nString(UIStrings.callbackInterval)}: μ = ${mean} ms, σ = ${stddev} ms</span> |
| 103 | <span>\u2758</span> |
| 104 | <span>${i18nString(UIStrings.renderCapacity)}: ${capacity} %</span> |
| 105 | </div> |
| 106 | `); |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 107 | } |
| 108 | |
Jan Scheffler | d862f14 | 2021-01-12 10:25:03 | [diff] [blame] | 109 | getFragment(): DocumentFragment { |
Jan Scheffler | 755efdd | 2021-08-12 09:32:18 | [diff] [blame] | 110 | return this.fragment; |
Hongchan Choi | 7dd0b3e | 2019-05-13 21:19:03 | [diff] [blame] | 111 | } |
Paul Lewis | 2766f08 | 2019-11-28 13:42:57 | [diff] [blame] | 112 | } |