forked from chenxiaolong/BCR
-
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.
Create better abstraction for describing format parameters
With the new sealed class abstraction, WaveFormat no longer needs to provide dummy range and step size values. Signed-off-by: Andrew Gunnerson <chillermillerlong@hotmail.com>
- Loading branch information
1 parent
df1ea76
commit 88c612a
Showing
10 changed files
with
151 additions
and
87 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
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
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
77 changes: 77 additions & 0 deletions
77
app/src/main/java/com/chiller3/bcr/format/FormatParamInfo.kt
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,77 @@ | ||
package com.chiller3.bcr.format | ||
|
||
sealed class FormatParamInfo(val default: UInt) { | ||
/** | ||
* Ensure that [param] is valid. | ||
* | ||
* @throws IllegalArgumentException if [param] is invalid | ||
*/ | ||
abstract fun validate(param: UInt) | ||
|
||
/** | ||
* Convert a potentially-invalid [param] value to the nearest valid value. | ||
*/ | ||
abstract fun toNearest(param: UInt): UInt | ||
|
||
/** | ||
* Format [param] to present as a user-facing string. | ||
*/ | ||
abstract fun format(param: UInt): String | ||
} | ||
|
||
enum class RangedParamType { | ||
CompressionLevel, | ||
Bitrate, | ||
} | ||
|
||
class RangedParamInfo( | ||
val type: RangedParamType, | ||
val range: UIntRange, | ||
val stepSize: UInt, | ||
default: UInt, | ||
) : FormatParamInfo(default) { | ||
override fun validate(param: UInt) { | ||
if (param !in range) { | ||
throw IllegalArgumentException("Parameter ${format(param)} is not in the range: " + | ||
"[${format(range.first)}, ${format(range.last)}]") | ||
} | ||
} | ||
|
||
/** Clamp [param] to [range] and snap to nearest [stepSize]. */ | ||
override fun toNearest(param: UInt): UInt { | ||
val offset = param.coerceIn(range) - range.first | ||
val roundedDown = (offset / stepSize) * stepSize | ||
|
||
return range.first + if (roundedDown == offset) { | ||
// Already on step size boundary | ||
offset | ||
} else if (roundedDown >= UInt.MAX_VALUE - stepSize) { | ||
// Rounded up would overflow | ||
roundedDown | ||
} else { | ||
// Round to closer boundary, preferring the upper boundary if it's in the middle | ||
val roundedUp = roundedDown + stepSize | ||
if (roundedUp - offset <= offset - roundedDown) { | ||
roundedUp | ||
} else { | ||
roundedDown | ||
} | ||
} | ||
} | ||
|
||
override fun format(param: UInt): String = | ||
when (type) { | ||
RangedParamType.CompressionLevel -> param.toString() | ||
RangedParamType.Bitrate -> "${param / 1_000u} kbps" | ||
} | ||
} | ||
|
||
object NoParamInfo : FormatParamInfo(0u) { | ||
override fun validate(param: UInt) { | ||
// Always valid | ||
} | ||
|
||
override fun toNearest(param: UInt): UInt = param | ||
|
||
override fun format(param: UInt): String = "" | ||
} |
15 changes: 0 additions & 15 deletions
15
app/src/main/java/com/chiller3/bcr/format/FormatParamType.kt
This file was deleted.
Oops, something went wrong.
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
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