quintodrome/ui/src/common/QualityInfo.jsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
1.7 KiB
React
Raw Normal View History

import React, { useMemo } from 'react'
import PropTypes from 'prop-types'
import Chip from '@material-ui/core/Chip'
2021-04-06 18:18:48 -08:00
import config from '../config'
import { makeStyles } from '@material-ui/core'
import clsx from 'clsx'
import { calculateGain } from '../utils/calculateReplayGain'
2021-04-06 18:18:48 -08:00
const llFormats = new Set(config.losslessFormats.split(','))
const placeholder = 'N/A'
const useStyle = makeStyles(
(theme) => ({
chip: {
transform: 'scale(0.8)',
},
}),
{
name: 'NDQualityInfo',
},
)
export const QualityInfo = ({ record, size, gainMode, preAmp, className }) => {
const classes = useStyle()
let { suffix, bitRate, rgAlbumGain, rgAlbumPeak, rgTrackGain, rgTrackPeak } =
record
2021-04-06 18:18:48 -08:00
let info = placeholder
if (suffix) {
suffix = suffix.toUpperCase()
info = suffix
2023-01-19 18:52:55 -09:00
if (!llFormats.has(suffix) && bitRate > 0) {
2021-04-06 18:18:48 -08:00
info += ' ' + bitRate
}
}
2021-04-06 18:18:48 -08:00
const extra = useMemo(() => {
if (gainMode !== 'none') {
const gainValue = calculateGain(
{ gainMode, preAmp },
{ rgAlbumGain, rgAlbumPeak, rgTrackGain, rgTrackPeak },
)
// convert normalized gain (after peak) back to dB
const toDb = (Math.log10(gainValue) * 20).toFixed(2)
return ` (${toDb} dB)`
}
return ''
}, [gainMode, preAmp, rgAlbumGain, rgAlbumPeak, rgTrackGain, rgTrackPeak])
return (
<Chip
className={clsx(classes.chip, className)}
variant="outlined"
size={size}
label={`${info}${extra}`}
/>
)
}
QualityInfo.propTypes = {
record: PropTypes.object.isRequired,
size: PropTypes.string,
className: PropTypes.string,
gainMode: PropTypes.string,
}
QualityInfo.defaultProps = {
record: {},
size: 'small',
gainMode: 'none',
}