-
Notifications
You must be signed in to change notification settings - Fork 536
Introducing LavaBeats #428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8bd569
Adding the LavaBeatsHapticHelper
4540461
Adding a LavaBeatsViewModel
ee14fae
Adding the LavaBeatsShader
0ae44f8
Adding the LavaBeatsGraphics composable
affbbe4
Adding the settings composables for haptic beat parameters
ed8d4ab
Adding the LavaBeatsRoute
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
...face/haptics/src/main/java/com/example/platform/ui/haptics/lavabeats/LavaBeatsGraphics.kt
This file contains hidden or 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,91 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.platform.ui.haptics.lavabeats | ||
|
|
||
| import android.os.Build | ||
| import androidx.compose.animation.core.withInfiniteAnimationFrameMillis | ||
| import androidx.compose.foundation.isSystemInDarkTheme | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.BoxWithConstraints | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableFloatStateOf | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.draw.drawWithCache | ||
| import androidx.compose.ui.graphics.ShaderBrush | ||
| import androidx.compose.ui.platform.LocalDensity | ||
|
|
||
| @Composable | ||
| fun LavaBeatsGraphics( | ||
| pulseTime: Float, | ||
| beatEffectTimingParams: BeatEffectTimingParams, | ||
| pulse: Boolean = false, | ||
| ) { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return | ||
|
|
||
| BoxWithConstraints { | ||
| val constraints = this | ||
| val density = LocalDensity.current | ||
| val width = with(density) { constraints.maxWidth.toPx() } | ||
| val height = with(density) { constraints.maxHeight.toPx() } | ||
| var firstTime by remember { mutableFloatStateOf(-1f) } | ||
| var time by remember { mutableFloatStateOf(0f) } | ||
| val isInDarkMode = isSystemInDarkTheme() | ||
| val surfaceColor = MaterialTheme.colorScheme.background | ||
|
|
||
| LaunchedEffect(Unit) { | ||
| // Use withInfiniteAnimationFrameMillis to update the time uniform per frame. | ||
| // This is a more efficient approach than passing a new shader instance | ||
| // or re-creating the RenderEffect on every frame. | ||
| while (true) { | ||
| withInfiniteAnimationFrameMillis { frameTime -> | ||
| if (firstTime == -1f) { | ||
| firstTime = frameTime / 1000f | ||
| } else { | ||
| time = frameTime / 1000f - firstTime | ||
| } | ||
| } | ||
| } | ||
| } | ||
| val shader = remember { LavaBeatsShader() } | ||
|
|
||
| Box( | ||
| modifier = | ||
| Modifier.drawWithCache { | ||
| if (isInDarkMode) { | ||
| shader.enableDarkMode() | ||
| } else { | ||
| shader.enableLightMode() | ||
| } | ||
| shader.setBackground(surfaceColor) | ||
| shader.enablePulsing(pulse) | ||
| shader.setResolution(width, height) | ||
| shader.setTime(time) | ||
| shader.setPulseTime(pulseTime) | ||
| shader.setBeatEffectTimingParameters(beatEffectTimingParams) | ||
| val shaderBrush = ShaderBrush(shader) | ||
| onDrawBehind { drawRect(shaderBrush) } | ||
| } | ||
| .fillMaxSize() | ||
| ) | ||
| } | ||
| } |
183 changes: 183 additions & 0 deletions
183
.../haptics/src/main/java/com/example/platform/ui/haptics/lavabeats/LavaBeatsHapticHelper.kt
This file contains hidden or 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,183 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.platform.ui.haptics.lavabeats | ||
|
|
||
| import android.os.Build | ||
| import android.os.VibrationEffect | ||
| import android.util.Log | ||
| import androidx.annotation.RequiresApi | ||
| import androidx.compose.runtime.Stable | ||
|
|
||
| /** | ||
| * A Helper that creates the Lava Beats haptic effect as well as beat parameters | ||
| */ | ||
| object LavaBeatsHapticHelper { | ||
| private const val TAG = "LavaBeatsEffectPlayer" | ||
| private const val MIN_BEAT_DELAY_MILLIS = 5f | ||
| private const val ENVELOPE_RAMP_DURATION_MILLIS = 5L | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.BAKLAVA) | ||
| fun createEnvelopeBeatEffect(beatParameters: List<BeatParameter>): HapticBeatEffect { | ||
| val effect = createEnvelopeEffect(beatParameters) | ||
| val rampsDuration = 4L * ENVELOPE_RAMP_DURATION_MILLIS * beatParameters.getNumBeats() | ||
| val duration = rampsDuration + beatParameters.getIntrinsicDurationMillis() | ||
| val timingParams = | ||
| BeatEffectTimingParams( | ||
| beatDurationMillis = | ||
| beatParameters.getBaseBeatDurationMillis() + 4 * ENVELOPE_RAMP_DURATION_MILLIS, | ||
| timeToFirstPulseMillis = ENVELOPE_RAMP_DURATION_MILLIS.toFloat(), | ||
| timeToSecondPulseMillis = | ||
| beatParameters.getFirstPulseDurationMillis() + | ||
| beatParameters.getFirstToSecondPulseDelayMillis() + | ||
| 3 * ENVELOPE_RAMP_DURATION_MILLIS, | ||
| beatDelayMillis = beatParameters.getBeatDelayMillis(), | ||
| ) | ||
| return HapticBeatEffect( | ||
| vibrationEffect = effect, | ||
| totalDurationEstimateMillis = duration, | ||
| beats = beatParameters.getNumBeats(), | ||
| timingParams = timingParams, | ||
| ) | ||
| } | ||
|
|
||
| @RequiresApi(Build.VERSION_CODES.BAKLAVA) | ||
| private fun createEnvelopeEffect(beatParameters: List<BeatParameter>): VibrationEffect = | ||
| VibrationEffect.WaveformEnvelopeBuilder() | ||
| .apply { | ||
| repeat(beatParameters.getNumBeats()) { | ||
| // First pulse chirp | ||
| addControlPoint( | ||
| beatParameters.getFirstPulseAmplitude(), | ||
| beatParameters.getFirstPulseStartFreq(), | ||
| ENVELOPE_RAMP_DURATION_MILLIS, | ||
| ) | ||
| addControlPoint( | ||
| beatParameters.getFirstPulseAmplitude(), | ||
| beatParameters.getFirstPulseEndFreq(), | ||
| beatParameters.getFirstPulseDurationMillis().toLong(), | ||
| ) | ||
| addControlPoint( | ||
| 0f, | ||
| beatParameters.getFirstPulseEndFreq(), | ||
| ENVELOPE_RAMP_DURATION_MILLIS, | ||
| ) | ||
| // Delay between first and second pulse | ||
| addControlPoint( | ||
| 0f, | ||
| beatParameters.getFirstPulseEndFreq(), | ||
| beatParameters.getFirstToSecondPulseDelayMillis().toLong(), | ||
| ) | ||
| // Second pulse | ||
| addControlPoint( | ||
| beatParameters.getSecondPulseAmplitude(), | ||
| beatParameters.getSecondPulseFreq(), | ||
| ENVELOPE_RAMP_DURATION_MILLIS, | ||
| ) | ||
| addControlPoint( | ||
| beatParameters.getSecondPulseAmplitude(), | ||
| beatParameters.getSecondPulseFreq(), | ||
| (1_000 / (2f * beatParameters.getSecondPulseFreq())).toLong(), | ||
| ) | ||
| addControlPoint( | ||
| 0f, | ||
| beatParameters.getSecondPulseFreq(), | ||
| ENVELOPE_RAMP_DURATION_MILLIS, | ||
| ) | ||
| addControlPoint( | ||
| 0f, | ||
| beatParameters.getSecondPulseFreq(), | ||
| beatParameters.getBeatDelayMillis().toLong(), | ||
| ) | ||
| } | ||
| } | ||
| .build() | ||
|
|
||
| // Helper functions to idiomatically index the list of parameters | ||
|
|
||
| private fun List<BeatParameter>.getFirstPulseStartFreq(): Float = this[0].value | ||
|
|
||
| private fun List<BeatParameter>.getFirstPulseEndFreq(): Float = this[1].value | ||
|
|
||
| private fun List<BeatParameter>.getFirstPulseDurationMillis(): Float = this[2].value | ||
|
|
||
| private fun List<BeatParameter>.getFirstPulseAmplitude(): Float = this[3].value | ||
|
|
||
| private fun List<BeatParameter>.getSecondPulseFreq(): Float = this[4].value | ||
|
|
||
| private fun List<BeatParameter>.getSecondPulseAmplitude(): Float = this[5].value | ||
|
|
||
| private fun List<BeatParameter>.getFirstToSecondPulseDelayMillis(): Float = this[6].value | ||
|
|
||
| private fun List<BeatParameter>.getBpm(): Float = this[7].value | ||
|
|
||
| private fun List<BeatParameter>.getNumBeats(): Int = this[8].value.toInt() | ||
|
|
||
| private fun List<BeatParameter>.getBeatDelayMillis(): Float { | ||
| val targetValue = | ||
| 60_000f / getBpm() - getFirstPulseDurationMillis() - getFirstToSecondPulseDelayMillis() | ||
| if (targetValue <= 0) { | ||
| Log.e( | ||
| TAG, | ||
| "Invalid beat delay from selected parameters, returning a minimum of " + | ||
| "$MIN_BEAT_DELAY_MILLIS", | ||
| ) | ||
| return MIN_BEAT_DELAY_MILLIS | ||
| } | ||
| return targetValue | ||
| } | ||
|
|
||
| private fun List<BeatParameter>.getBaseBeatDurationMillis(): Float = | ||
| getBeatDelayMillis() + | ||
| getFirstPulseDurationMillis() + | ||
| getFirstToSecondPulseDelayMillis() + | ||
| (1_000 * 2f / getSecondPulseFreq()) | ||
|
|
||
| private fun List<BeatParameter>.getIntrinsicDurationMillis(): Float = | ||
| getNumBeats() * getBaseBeatDurationMillis() | ||
| } | ||
|
|
||
| /** A parameter of a haptic beat effect that represents an EKG signal parameter */ | ||
| @Stable | ||
| data class BeatParameter( | ||
| val description: String = "", | ||
| val value: Float = 0f, | ||
| val range: ClosedFloatingPointRange<Float> = 0f..1f, | ||
| val steps: Int = 0, | ||
| val isFrequencyType: Boolean = false, | ||
| ) | ||
|
|
||
| /** Encapsulates the vibration effect of a haptic beat effect with its timing parameters */ | ||
| @Stable | ||
| data class HapticBeatEffect( | ||
| val vibrationEffect: VibrationEffect, | ||
| val totalDurationEstimateMillis: Float, | ||
| val beats: Int, | ||
| val timingParams: BeatEffectTimingParams, | ||
| ) | ||
|
|
||
| /** Timing parameters of the overall haptic beat effect */ | ||
| @Stable | ||
| data class BeatEffectTimingParams( | ||
| val beatDurationMillis: Float = 0f, | ||
| val timeToFirstPulseMillis: Float = 0f, | ||
| val timeToSecondPulseMillis: Float = 0f, | ||
| val beatDelayMillis: Float = 0f, | ||
| ) { | ||
| companion object { | ||
| val Empty = BeatEffectTimingParams() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is my understanding correct that for devices < API Level 33, this sample app would just show an non interactive screen with the "Not supported .." notification. I know you are following the model for the previous haptic samples but am curious if we should just enclose the sample with the MinSdkBox,
e.g.
content = {
MinSdkBox(minSdk = Build.VERSION_CODES.BAKLAVA) {
LavaBeats()
}
Which would have the same effect and save the user a click from learning that it's not enabled for their device.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense! However, the device not only has to have an API level >= 33, but its vibrator must also support envelope effects, so the MinSdkBox wouldn't be enough to perform this check unfortunately. Do you think its better to extend/add a function like MinSdkBox that lets us extend the support conditions so that we can apply them earlier like in this location?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yea that would be ideal but out of scope for this change. We can add as a feature request for the future. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. Thanks Aaron!