forked from soundio/soundstage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assign settings accepts string unit values
- Loading branch information
Showing
4 changed files
with
54 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import toGain from '../../../fn/modules/to-gain.js'; | ||
|
||
// Be generous in what we accept, space-wise, but exclude spaces between the | ||
// number and the unit | ||
const runit = /\d([^\s\d]*)\s*$/; | ||
|
||
const units = { | ||
'': Math.fround, | ||
'db': (value) => Math.fround(toGain(value)), | ||
'khz': (value) => Math.fround(value * 1000), | ||
'hz': Math.fround, | ||
'ms': (value) => Math.fround(value / 1000), | ||
's': Math.fround | ||
}; | ||
|
||
export default function parseValue32(value) { | ||
// Return 32-bit representation of number | ||
if (typeof value === 'number') { | ||
return Math.fround(value); | ||
} | ||
|
||
var entry = runit.exec(value); | ||
if (!entry) { | ||
throw new Error('Cannot parse value "' + value + '" (accepted units ' + Object.keys(units).join(', ') + ')'); | ||
} | ||
|
||
const unit = entry[1].toLowerCase(); | ||
if (!units[unit]) { | ||
throw new Error('Cannot parse value "' + string + '" (accepted units ' + Object.keys(units).join(', ') + ')'); | ||
} | ||
|
||
return units[unit](parseFloat(value)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters