Fix merge conflicts and logical errors in engine sound modules#14
Fix merge conflicts and logical errors in engine sound modules#14barkleesanders wants to merge 1 commit into
Conversation
This commit resolves issues arising from a previous merge in the engine sound system.
Key changes:
- Corrected `EngineSoundGenerator.py` to use sound file paths from `GlobalConstants.VEHICLE_ASSETS` (i.e., under `static/sounds/`) instead of a local `./Sounds` directory. It now maps its internal sound names to `engineSoundID`s from `GlobalConstants` to fetch the correct sound path.
- Fixed an incorrect constant usage in `Main.py` for `EngineSoundPitchShifter` initialization. It now correctly uses `GC.MC_LAREN_F1` (from `GlobalConstants`) instead of `ESPS.MC_LAREN_F1`.
- Verified that `GlobalConstants.validate_assets()` functions correctly, confirming consistent asset path expectations.
- Reintegrated `EngineSoundGenerator` into `Main.py`'s `integration_test()`:
- Uncommented the import.
- Added a basic instantiation and functionality test.
- Ensured both `EngineSoundGenerator` and `EngineSoundPitchShifter` can be instantiated and attempt to load sounds without critical errors in `Main.py`'s `integration_test()`. Playback errors due to missing audio hardware in test environments are handled gracefully.
- Commented out `esps.unit_test()` in `Main.py` as it's unsuitable for automated testing due to its interactive nature and `pynput` dependencies.
There was a problem hiding this comment.
Hello @barkleesanders, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
Summary of Changes
Hello team,
Gemini here, providing a summary of this pull request authored by barkleesanders. The primary goal of this PR, as indicated by the title and description, is to resolve merge conflicts and logical errors that arose in the engine sound modules (EngineSoundGenerator.py and EngineSoundPitchShifter.py) following a previous merge. The changes focus on standardizing the use of GlobalConstants for sound asset paths and IDs, improving error handling around audio device availability and file loading, and updating the integration tests in Main.py to reflect these changes and better handle test environments without audio hardware.
Highlights
- Standardized Asset Paths and IDs: The
EngineSoundGeneratorhas been updated to use sound file paths defined inGlobalConstants.VEHICLE_ASSETSand map its internal sound names to the correspondingengineSoundIDs fromGlobalConstants, moving away from hardcoded local paths. - Improved Audio Error Handling: Both
EngineSoundGeneratorandEngineSoundPitchShifternow include more robust error handling during initialization and sound loading/playback, specifically addressing scenarios where sound files are missing or audio devices are unavailable (common in CI/test environments) by logging warnings and attempting graceful fallback. - Updated Integration Tests: The
integration_testfunction inMain.pyhas been updated to correctly initializeEngineSoundPitchShifterusingGlobalConstantsand now includes a basic test forEngineSoundGenerator. The interactiveEngineSoundPitchShifter.unit_test()has been commented out as it's not suitable for automated testing. - Enhanced Unit Tests for EngineSoundGenerator: The
EngineSoundGenerator.unit_test()has been significantly expanded to cover initialization with valid/invalid keys, attempting to set sounds with missing files, and attempting to set invalid keys after initialization, including assertions and logging for each step.
Changelog
Click here to see the changelog
- EngineSoundGenerator.py
- Imported
GlobalConstants(line 10). - Updated
engine_sounds_dictto useGlobalConstantsIDs instead of integers (lines 32-39). - Initialized
base_audio_filename,engine_sound_wave_object, andengine_sound_idtoNonein__init__(lines 41-44). - Added logic in
__init__to attempt loading a default sound (mc_laren_f1) if the initial sound fails to load, logging warnings (lines 48-59). - Added a check in
start_audio_loopto ensureengine_sound_wave_objectis loaded before attempting to play (lines 74-76). - Refactored
set_engine_soundto find sound paths inGC.VEHICLE_ASSETSbased on the sound ID (lines 97-102). - Added checks in
set_engine_soundfor file existence before loading (lines 108-110). - Added
try...exceptblock inset_engine_soundto catch exceptions during wave file loading, logging warnings and keeping the current sound (lines 106-119). - Improved logging in
set_engine_soundfor successful changes, file not found, path not found in assets, and invalid sound keys (lines 117, 109, 123, 127). - Expanded
unit_testwith multiple test cases covering valid/invalid initialization and sound setting attempts, including assertions and error handling for audio playback (lines 140-222).
- Imported
- EngineSoundPitchShifter.py
- Initialized
self.streamtoNoneandself.runningtoTruein__init__(lines 81-82). - Refactored stream setup in
__init__to movestream.start()into thetryblock (lines 85-87). - Added specific
except sd.PortAudioErrorfor handling environments without audio devices, logging a message (lines 89-93). - Added a general
except Exceptionfor unexpected errors during stream setup (lines 97-99). - Updated
cleanupto check ifself.streamis notNonebefore attempting to stop/close it (line 105). - Added print statements in
cleanupfor clarity (lines 108, 110). - Added a check for
statusinaudio_callbackand prints it (lines 115-116). - Added a check for
self.stream is not Nonein the main logic ofaudio_callback(line 118).
- Initialized
- Main.py
- Imported
GlobalConstants as GC(line 22). - Uncommented and imported
EngineSoundGenerator(line 36). - In
integration_test, changedESPSinitialization to useGC.MC_LAREN_F1instead ofESPS.MC_LAREN_F1(line 47). - Commented out the call to
esps.unit_test()(line 48). - Added a section to test
EngineSoundGenerator, including instantiation, assertion, playback attempt with a short delay, stopping, and error handling for playback issues (lines 50-71). - Removed the
devDBparameter from theintegration_testfunction definition (line 43). - Removed the
devDBargument from the call tointegration_testin themainfunction (line 105). - Removed a commented-out line
GC.validate_assets()(line 94).
- Imported
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Audio files fixed,
Constants now guide the way,
Sounds play, or not.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Code Review
This pull request effectively addresses several issues, including merge conflicts, incorrect constant usage, and file path problems in the engine sound modules. The changes significantly improve the robustness of EngineSoundGenerator by adding better error handling for sound file loading and more comprehensive unit tests. The integration of EngineSoundGenerator into Main.py's integration test is also a good step forward. The code is clearer and more maintainable with the use of GlobalConstants.
Overall, great work on these fixes and enhancements!
Summary of Findings
- Clarity of fallback string in log messages: In
EngineSoundGenerator.py(lines 122, 126), the fallback string forcurrent_sound_display,"current (or None if not initialized)", is a bit verbose. This is a minor point and the current implementation is explicit, but it could potentially be simplified for conciseness in log messages. (Not commented due to review settings: low severity) - Exception type checking in unit tests: In
EngineSoundGenerator.pyunit tests (e.g., line 154), checking exception types usinge.__class__.__name__ == "SimpleaudioError"is functional but can be fragile if the exception class name changes in future versions of thesimpleaudiolibrary. Ifsimpleaudio's specific error types are importable, usingisinstance(e, SimpleaudioErrorType)or a directexcept SimpleaudioErrorType:would be more robust. This is a minor concern, especially within test code. (Not commented due to review settings: low severity) - Logging consistency in EngineSoundPitchShifter.py:
EngineSoundPitchShifter.pyusesprint()for logging, whileEngineSoundGenerator.pyuses thelogging_utilsmodule. While the changes in this PR are consistent with the existing style ofEngineSoundPitchShifter.py, standardizing logging across modules usinglogging_utilscould be considered for future enhancements. (Not commented due to review settings: low severity, and potentially out of scope for this PR)
Merge Readiness
The pull request introduces valuable fixes and improvements. The code is generally in good shape. I recommend addressing the medium-severity issue raised regarding the logging level for critical messages in EngineSoundGenerator.py to ensure consistency and appropriate error signaling. After that, the PR should be in a strong position for merging. As a reviewer, I am not authorized to approve the pull request; please ensure further review and approval as per your team's policy.
|
|
||
| # If default sound also failed, log a critical warning. | ||
| if not self.engine_sound_wave_object: | ||
| log_warning(f"CRITICAL: Default sound '{self.mc_laren_f1}' also failed to load. EngineSoundGenerator may not function correctly.") |
There was a problem hiding this comment.
The log message here indicates a "CRITICAL" failure, but it's being logged with log_warning. While this will make the message visible, using log_error would more accurately reflect the severity of the situation within the logging system, especially since log_error is available in logging_utils.py. This helps in filtering and prioritizing logs based on severity levels.
Could log_error be more appropriate here to align the log function with the message's stated severity?
| log_warning(f"CRITICAL: Default sound '{self.mc_laren_f1}' also failed to load. EngineSoundGenerator may not function correctly.") | |
| log_error(f"CRITICAL: Default sound '{self.mc_laren_f1}' also failed to load. EngineSoundGenerator may not function correctly.") |
This commit resolves issues arising from a previous merge in the engine sound system.
Key changes:
EngineSoundGenerator.pyto use sound file paths fromGlobalConstants.VEHICLE_ASSETS(i.e., understatic/sounds/) instead of a local./Soundsdirectory. It now maps its internal sound names toengineSoundIDs fromGlobalConstantsto fetch the correct sound path.Main.pyforEngineSoundPitchShifterinitialization. It now correctly usesGC.MC_LAREN_F1(fromGlobalConstants) instead ofESPS.MC_LAREN_F1.GlobalConstants.validate_assets()functions correctly, confirming consistent asset path expectations.EngineSoundGeneratorintoMain.py'sintegration_test():EngineSoundGeneratorandEngineSoundPitchShiftercan be instantiated and attempt to load sounds without critical errors inMain.py'sintegration_test(). Playback errors due to missing audio hardware in test environments are handled gracefully.esps.unit_test()inMain.pyas it's unsuitable for automated testing due to its interactive nature andpynputdependencies.