Skip to content

Commit db8f827

Browse files
committed
Band Count description adapts to current frequency scale
1 parent c40e22a commit db8f827

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

src/index.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -835,15 +835,17 @@ const getIndex = node => {
835835
return i;
836836
}
837837

838-
// returns the text of the selected option in a `select` or custom radio element
838+
// returns the text/label of a settings control
839839
const getText = el => {
840-
let text = '';
840+
let text = el.value ?? '';
841841
if ( isCustomRadio( el ) ) {
842842
const option = el.querySelector(':checked ~ label');
843843
if ( option )
844844
text = option.textContent;
845845
}
846-
else
846+
else if ( el.type == 'range' )
847+
text = translateRangeValue( el );
848+
else if ( el.tagName == 'SELECT' )
847849
text = el[ el.selectedIndex ].text;
848850
return text;
849851
}
@@ -1064,7 +1066,7 @@ const setControlValue = ( el, val ) => {
10641066
if ( el.selectedIndex == -1 ) // fix invalid values in select elements
10651067
el.selectedIndex = 0;
10661068
}
1067-
updateRangeValue( el );
1069+
updateRangeValue( el == elFreqScale ? elBandCount : el );
10681070
}
10691071

10701072
// update configuration options from an existing preset
@@ -1089,6 +1091,9 @@ const toggleDisplay = ( el, status ) => {
10891091
el.style.display = status ? '' : 'none';
10901092
}
10911093

1094+
// capitalize first letter of a string
1095+
const ucFirst = str => str.charAt(0).toUpperCase() + str.slice(1);
1096+
10921097
// promise-compatible `onloadeddata` event handler for media elements
10931098
const waitForLoadedData = async audioEl => new Promise( ( resolve, reject ) => {
10941099
audioEl.onerror = () => {
@@ -3796,6 +3801,7 @@ function setProperty( elems, save = true ) {
37963801

37973802
case elFreqScale:
37983803
audioMotion.frequencyScale = getControlValue( elFreqScale );
3804+
updateRangeValue( elBandCount );
37993805
break;
38003806

38013807
case elFsHeight:
@@ -4804,6 +4810,30 @@ function toggleMute( mute ) {
48044810
audioMotion.connectOutput();
48054811
}
48064812

4813+
/**
4814+
* Convert value of a range control to a friendly text label
4815+
*/
4816+
function translateRangeValue( el ) {
4817+
const val = el.value,
4818+
{ abs, sign } = Math;
4819+
4820+
if ( el == elBandCount ) {
4821+
const isOctaves = getControlValue( elFreqScale ) == SCALE_LOG,
4822+
bands = isOctaves
4823+
? [ '', '', 'half-', '1/3rd-', '1/4th-', '1/6th-', '1/8th-', '1/12th-', '1/24th-' ]
4824+
: [ '', '10', '20', '30', '40', '60', '80', '120', '240' ];
4825+
4826+
return ucFirst( bands[ +val || 0 ] + ( isOctaves ? 'octave' : '' ) + ' bands' );
4827+
}
4828+
else if ( el == elBarSpace )
4829+
return val == 0 ? 'None' : ( val == 1 ? 'Legacy' : `${ val * 100 | 0 }%` );
4830+
else if ( el == elFillAlpha )
4831+
return val == 0 ? 0 : `${ val * 100 | 0 }%`;
4832+
else if ( el == elSpin )
4833+
return val == 0 ? 'OFF' : abs( val ) + ' RPM' + ( sign( val ) == -1 ? ' (CCW)' : '' );
4834+
return val;
4835+
}
4836+
48074837
/**
48084838
* Update last used configuration
48094839
*/
@@ -4824,31 +4854,7 @@ function updateRangeValue( el ) {
48244854
if ( ! elVal || elVal.className != 'value' )
48254855
return;
48264856

4827-
const translation = val => {
4828-
const { abs, sign } = Math;
4829-
if ( el == elBandCount ) {
4830-
return [
4831-
'',
4832-
'10 bands (octaves)',
4833-
'20 bands (half octaves)',
4834-
'30 bands (1/3rd oct.)',
4835-
'40 bands (1/4th oct.)',
4836-
'60 bands (1/6th oct.)',
4837-
'80 bands (1/8th oct.)',
4838-
'120 bands (1/12th oct.)',
4839-
'240 bands (1/24th oct.)'
4840-
][ +val || 0 ];
4841-
}
4842-
else if ( el == elBarSpace )
4843-
return val == 0 ? 'None' : ( val == 1 ? 'Legacy' : `${ val * 100 | 0 }%` );
4844-
else if ( el == elFillAlpha )
4845-
return val == 0 ? 0 : `${ val * 100 | 0 }%`;
4846-
else if ( el == elSpin )
4847-
return val == 0 ? 'OFF' : abs( val ) + ' RPM' + ( sign( val ) == -1 ? ' (CCW)' : '' );
4848-
return val;
4849-
}
4850-
4851-
elVal.innerText = translation( el.value );
4857+
elVal.innerText = translateRangeValue( el );
48524858
}
48534859

48544860

@@ -4996,7 +5002,7 @@ function updateRangeValue( el ) {
49965002
drawText( getSelectedGradients(), centerPos, topLine1, maxWidthTop );
49975003

49985004
canvasCtx.textAlign = 'left';
4999-
drawText( getText( elMode ), baseSize, topLine1, maxWidthTop );
5005+
drawText( getText( getControlValue( elMode ) == MODE_BARS ? elBandCount : elMode ), baseSize, topLine1, maxWidthTop );
50005006
drawText( `Randomize: ${ getText( elRandomMode ) }`, baseSize, topLine2, maxWidthTop );
50015007

50025008
canvasCtx.textAlign = 'right';

0 commit comments

Comments
 (0)