Skip to content

CATROID-1412 Fix loudness sensor being always 0#5167

Merged
urkat merged 16 commits intoCatrobat:developfrom
wslany:codex/loudness-sensor-investigation
Apr 13, 2026
Merged

CATROID-1412 Fix loudness sensor being always 0#5167
urkat merged 16 commits intoCatrobat:developfrom
wslany:codex/loudness-sensor-investigation

Conversation

@wslany
Copy link
Copy Markdown
Member

@wslany wslany commented Mar 26, 2026

Summary

This PR fixes the loudness sensor returning 0 by replacing its /dev/null recorder target with a real cache-backed recording file.

https://catrobat.atlassian.net/browse/CATROID-1412

There are several red -> green commits which are only meant to make reviewing easier. The PR should be squashed when merged.

Red phase

The first commit adds a regression test for the loudness recorder setup and a small test seam in SensorLoudness so the chosen recorder path can be inspected in a unit test.

In the red state, the test fails because SensorLoudness still creates its recorder with /dev/null.

Green phase

The second commit changes the default loudness recorder target to a file inside Constants.SOUND_RECORDER_CACHE_DIRECTORY.

This keeps the test green and avoids the /dev/null setup that appears to break MediaRecorder.getMaxAmplitude() on current Android/device combinations.

Verification

  • Red phase: SensorLoudnessTest fails before the fix
  • Green phase: SensorLoudnessTest passes after the fix
  • Manual verification on a real phone (Samsung Galaxy S24 Ultra with Android 16):
    • loudness updates correctly in the formula editor preview
    • loudness updates correctly at runtime in Catrobat programs

Your checklist for this pull request

Please review the contributing guidelines and wiki pages of this repository.

  • Include the name of the Jira ticket in the PR’s title
  • Include a summary of the changes plus the relevant context
  • Choose the proper base branch (develop)
  • Confirm that the changes follow the project’s coding guidelines
  • Verify that the changes generate no compiler or linter warnings
  • Perform a self-review of the changes
  • Verify to commit no other files than the intentionally changed ones
  • Include reasonable and readable tests verifying the added or changed behavior
  • Confirm that new and existing unit tests pass locally
  • Check that the commits’ message style matches the project’s guideline
  • Stick to the project’s gitflow workflow
  • Verify that your changes do not have any conflicts with the base branch
  • After the PR, verify that all CI checks have passed (that have passed until this PR)
  • Post a message in the catroid-stage or catroid-ide Slack channel and ask for a code reviewer

@wslany
Copy link
Copy Markdown
Member Author

wslany commented Mar 26, 2026

Red phase.

Add a regression test that captures the loudness recorder target path and proves that the current /dev/null setup is wrong for the loudness sensor use case.

A small test seam is introduced in SensorLoudness so the recorder path can be observed without relying on real microphone input.

@wslany
Copy link
Copy Markdown
Member Author

wslany commented Mar 26, 2026

Green phase.

Switch the loudness sensor from /dev/null to a cache-backed .m4a file in Constants.SOUND_RECORDER_CACHE_DIRECTORY.

This keeps the regression test green and restores working loudness readings on a real device.

@wslany wslany marked this pull request as ready for review March 26, 2026 09:08
@ratschillerp ratschillerp requested a review from Copilot March 26, 2026 09:17
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes the loudness sensor returning 0 by replacing the recorder output from /dev/null with a real file under the app’s cache directory, and adds a regression test to ensure the recorder path is cache-backed.

Changes:

  • Add SensorLoudnessTest to assert the loudness recorder no longer targets /dev/null and uses the sound recorder cache directory.
  • Update SensorLoudness to create its SoundRecorder with a cache-file output path (via a small test seam/factory).
  • Centralize the default loudness recording file path (Constants.SOUND_RECORDER_CACHE_DIRECTORY/loudness_sensor.m4a).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
catroid/src/test/java/org/catrobat/catroid/formulaeditor/SensorLoudnessTest.java Adds a Robolectric regression test verifying the recorder path selection.
catroid/src/main/java/org/catrobat/catroid/formulaeditor/SensorLoudness.java Switches loudness recording target from /dev/null to a cache-backed file and introduces a factory seam for testing.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wslany wslany added the Active Member Tickets that are assigned to members that are still currently active label Mar 27, 2026
@wslany wslany changed the title CATROID-1412 Fix loudness sensor by recording to a cache file instead of /dev/null CATROID-1412 Fix loudness sensor being always 0 Mar 27, 2026
@harshsomankar123-tech
Copy link
Copy Markdown
Member

@wslany The SensorLoudness fix itself looks good to me, and the added tests are helpful.
but it also exposes an older lifecycle issue in FormulaEditorComputeDialog. The dialog sets up and starts loudness capture in setFormula(), while onStop() only unregisters the dialog itself as a SensorEventListener and never goes through the SensorHandler cleanup that stops SensorLoudness.

This likely stayed unnoticed before because SensorLoudness was using /dev/null, so on the affected devices the recorder path was effectively not working anyway. Now that this PR switches to a real cache-backed recording file, the missing teardown becomes visible: closing the compute dialog can leave the microphone recorder and loudness polling running until some later lifecycle cleanup happens.

Could You Please add explicit loudness/sensor cleanup when the dialog is dismissed or stopped, then this looks good to me.
Thanks!

@harshsomankar123-tech
Copy link
Copy Markdown
Member

@wslany LGTM!

@wslany
Copy link
Copy Markdown
Member Author

wslany commented Apr 2, 2026

Thanks @harshsomankar123-tech , it was a good catch. I added a regression test for FormulaEditorComputeDialog.onStop() and fixed the teardown by calling SensorHandler.stopSensorListeners() there, so loudness capture is now explicitly cleaned up when the dialog closes. Red-green commits to prove it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wslany
Copy link
Copy Markdown
Member Author

wslany commented Apr 3, 2026

Here's a screen recording with sound of an on-device test of the build: https://photos.app.goo.gl/z1ywpYWsDRW78VLa9 --- I tested:

  • working sensor in two objects in parallel
  • resume from stage pause-menu
  • resume when scene is continued
  • restart when scene is restarted
  • and that the sensor is working in the formula editor preview

Copy link
Copy Markdown
Contributor

@dorianpercic dorianpercic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! :)
I think we should implement source code files in Kotlin, since otherwise we need a refactoring ticket again.

@wslany wslany marked this pull request as draft April 5, 2026 09:13
@wslany
Copy link
Copy Markdown
Member Author

wslany commented Apr 5, 2026

Thanks for the PR! :) I think we should implement source code files in Kotlin, since otherwise we need a refactoring ticket again.

I rewrote the touched loudness-related Java files and their dedicated tests to Kotlin in a red/green sequence:

  • 2c69f34c test: rewrite loudness tests to Kotlin with red stubs
  • 8f4eea0a refactor: rewrite loudness files to Kotlin

Verification:

  • red: the rewritten targeted tests failed before the production rewrite
  • green: the same targeted tests passed after the Kotlin production rewrite
  • manual testing on a real phone:
    • loudness sensor works in the formula editor preview
    • loudness sensor works at runtime
    • compute dialog still works correctly
    • other compute-dialog sensor/reporter cases also still work, including time-based values like seconds and device sensors like inclination

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@wslany wslany marked this pull request as ready for review April 5, 2026 12:44
@wslany wslany requested a review from dorianpercic April 5, 2026 12:44
@wslany
Copy link
Copy Markdown
Member Author

wslany commented Apr 5, 2026

I've also tested the latest head on my phone once more: loudness sensor works everywhere, Other sensors also work in the formula editor compute dialog.

@sonarqubecloud
Copy link
Copy Markdown

@urkat urkat merged commit fed9697 into Catrobat:develop Apr 13, 2026
10 of 15 checks passed
@wslany wslany deleted the codex/loudness-sensor-investigation branch April 13, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Active Member Tickets that are assigned to members that are still currently active

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants