|
1 | 1 | import * as React from 'react'; |
| 2 | +import {Component} from 'react'; |
| 3 | +import chunk = require('lodash.chunk'); |
| 4 | +import sum = require('lodash.sum'); |
| 5 | + |
| 6 | +import {Analyser} from '../analyser'; |
| 7 | +import {Dimensions} from '../utils/dimensions'; |
| 8 | +import {BarsCanvas} from './styled'; |
| 9 | + |
| 10 | +export interface AudioBarsProps { |
| 11 | + analyser: Analyser; |
| 12 | + dimensions?: Dimensions; |
| 13 | +} |
| 14 | + |
| 15 | +export class AudioBars extends Component<AudioBarsProps, {}> { |
| 16 | + private canvasEl: HTMLCanvasElement; |
| 17 | + private canvasContext: CanvasRenderingContext2D; |
| 18 | + private animationId: number; |
| 19 | + private dataArray: Uint8Array; |
| 20 | + |
| 21 | + componentDidMount() { |
| 22 | + const {audioEl} = this.props.analyser; |
| 23 | + |
| 24 | + audioEl.addEventListener('playing', this.onPlaying); |
| 25 | + audioEl.addEventListener('pause', this.onPause); |
| 26 | + audioEl.addEventListener('ended', this.onEnded); |
| 27 | + } |
| 28 | + |
| 29 | + componentWillUnmount() { |
| 30 | + this.stopAnimation(); |
| 31 | + } |
2 | 32 |
|
3 | | -export class AudioBars extends React.Component<{}, null> { |
4 | 33 | render() { |
5 | 34 | return ( |
6 | | - <div> |
7 | | - AudioBars |
8 | | - </div> |
| 35 | + <BarsCanvas |
| 36 | + width={this.width} |
| 37 | + height={this.height} |
| 38 | + innerRef={this.onCanvasElMountOrUnmount} |
| 39 | + /> |
9 | 40 | ); |
10 | 41 | } |
| 42 | + |
| 43 | + private onCanvasElMountOrUnmount = (ref: HTMLCanvasElement): void => { |
| 44 | + if (!ref) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + this.canvasEl = ref; |
| 49 | + } |
| 50 | + |
| 51 | + private onPlaying = () => { |
| 52 | + this.draw(); |
| 53 | + } |
| 54 | + |
| 55 | + private draw = (): void => { |
| 56 | + const context = this.canvasEl.getContext('2d'); |
| 57 | + |
| 58 | + if (!context) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + this.canvasContext = context; |
| 63 | + |
| 64 | + const {analyserNode} = this.props.analyser; |
| 65 | + const bufferLength = analyserNode.frequencyBinCount; |
| 66 | + this.dataArray = new Uint8Array(bufferLength); |
| 67 | + |
| 68 | + this.drawBars(); |
| 69 | + } |
| 70 | + |
| 71 | + private drawBars = (): void => { |
| 72 | + const {canvasContext, width: canvasWidth, height: canvasHeight, dataArray} = this; |
| 73 | + |
| 74 | + // clear the canvas |
| 75 | + this.canvasContext.clearRect(0, 0, canvasWidth, canvasHeight); |
| 76 | + |
| 77 | + const {analyserNode} = this.props.analyser; |
| 78 | + const maxByteValue = 256; |
| 79 | + analyserNode.getByteFrequencyData(dataArray); |
| 80 | + |
| 81 | + const numBarsToDraw = Math.min(dataArray.length, 64); |
| 82 | + const bufferLength = dataArray.length; |
| 83 | + |
| 84 | + // chunk values if too many to display |
| 85 | + const numValuesPerChunk = Math.ceil(bufferLength / numBarsToDraw); |
| 86 | + const chunkedData = chunk(dataArray, numValuesPerChunk); |
| 87 | + const barValues = chunkedData.map((arr: Array<number>) => sum(arr) / arr.length); |
| 88 | + |
| 89 | + const barWidth = canvasWidth / numBarsToDraw; |
| 90 | + |
| 91 | + // draw the bars |
| 92 | + for (let i = 0; i < barValues.length; i++) { |
| 93 | + const x = i * barWidth + i; |
| 94 | + |
| 95 | + const percentBarHeight = barValues[i] / maxByteValue; |
| 96 | + const barHeight = canvasHeight * percentBarHeight; |
| 97 | + |
| 98 | + const red = Math.round(87 + (169 * percentBarHeight)); |
| 99 | + canvasContext.fillStyle = `rgba(${red}, 175, 229, 1)`; // TODO: Play with alpha channel based on height? |
| 100 | + |
| 101 | + canvasContext.fillRect(x, canvasHeight - barHeight, barWidth, barHeight); |
| 102 | + } |
| 103 | + |
| 104 | + this.animationId = requestAnimationFrame(this.drawBars); |
| 105 | + } |
| 106 | + |
| 107 | + private onPause = () => { |
| 108 | + this.stopAnimation(); |
| 109 | + } |
| 110 | + |
| 111 | + private onEnded = () => { |
| 112 | + this.stopAnimation(); |
| 113 | + } |
| 114 | + |
| 115 | + private stopAnimation = () => { |
| 116 | + cancelAnimationFrame(this.animationId); |
| 117 | + } |
| 118 | + |
| 119 | + private get width(): number { |
| 120 | + const defaultWidth = 400; |
| 121 | + const {dimensions} = this.props; |
| 122 | + |
| 123 | + if (!dimensions || !dimensions.width) { |
| 124 | + return defaultWidth; |
| 125 | + } |
| 126 | + |
| 127 | + return dimensions.width; |
| 128 | + } |
| 129 | + |
| 130 | + private get height(): number { |
| 131 | + const defaultHeight = 400; |
| 132 | + const {dimensions} = this.props; |
| 133 | + |
| 134 | + |
| 135 | + if (!dimensions || !dimensions.height) { |
| 136 | + return defaultHeight; |
| 137 | + } |
| 138 | + |
| 139 | + return dimensions.height; |
| 140 | + } |
11 | 141 | } |
0 commit comments