Skip to content
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

Remove audio player thread to fix crash on exit #681

Merged

Conversation

jonathanperret
Copy link
Contributor

@jonathanperret jonathanperret commented Jul 24, 2024

⚡ Summary

This PR fixes issue #680 by removing the audio player thread which is the cause of the crash on exit, due to not being exited properly before app termination.

ℹ️ Details

The current code spawns a dedicated thread (introduced in #382) to handle playing sound effects.

This is unnecessary as the simpleaudio API already is non-blocking, i.e. calling wave_obj.play() returns immediately while the sound plays in the background (see https://simpleaudio.readthedocs.io/en/latest/capabilities.html#asynchronous-interface). At the worst, loading the sound file, which is synchronous, could block the calling thread for a few milliseconds but that happens only once and is unlikely to be an issue.

The AudioWorker.play method does have a blocking argument that is supposed to support blocking the caller until the sound has finished playing, but this is never used in the current codebase.

But more conclusively, it turns out that due to an oversight, practically no code currently executes on the audio player thread. The AudioWorker object is assigned to a new thread, but because the AudioPlayer.play method calls __worker.play without going through a signal:

def play(self, sound: str) -> None:
self.__worker.play(sound, blocking=False)

…the AudioWorker.play method actually executes on the thread that called AudioPlayer.play. In practice this is the UI thread, because audio.play is called through a signal on SignalReceiver, which lives on that thread. Thus, the time-sensitive engine thread is already isolated from any slowness in reading or playing audio.

This commit reduces the AudioPlayer class to a plain Python object whose only responsibilities are to load and play the application's sound effects. It also simplifies the file loading logic by removing the direct call to the wave library in favor of the from_wave_file helper method offered by simpleaudio, which in practice uses the exact same call to the wave library.

A notable benefit of removing this thread is that the application no longer crashes with an abort call upon exit (issue #680), caused by improper termination of the audio thread.

🔬 How to test

  • Knit a pattern from start to finish; observe that the start, new line and end sound effects are still played correctly. Note that (at least for me) when knitting in "Simulation" mode, the per-line sound effect is not heard; this is not a regression introduced by this PR since it already occured before and needs to be investigated separately.
  • Follow the instructions in [BUG] macOS: a crash report pop-up appears on exiting the app #680 to ascertain that a crash no longer happens on exit.

Summary by CodeRabbit

  • New Features

    • Simplified audio playback functionality for a more efficient user experience.
  • Bug Fixes

    • Removed unnecessary complexity in audio handling, which may improve responsiveness.
  • Refactor

    • Streamlined the AudioPlayer class to enhance performance and maintainability by removing threading logic.
    • Updated signal connections to improve the handling of audio playback.
  • Chores

    • Removed unused dependencies from the project to minimize bloat.

The current code spawns a dedicated thread to handle playing sound effects.

This is unnecessary as the `simpleaudio` API already is non-blocking, i.e. calling `wave_obj.play()`
returns immediately while the sound plays in the background (see https://simpleaudio.readthedocs.io/en/latest/capabilities.html#asynchronous-interface). At the worst,
loading the sound file, which is synchronous, could block the calling thread for a few milliseconds
but that happens only once and is unlikely to be an issue.

The `AudioWorker.play` method does have a `blocking` argument that is supposed to support
blocking the caller until the sound has finished playing, but this is never used in the current codebase.

But more conclusively, it turns out that due to an oversight, practically no code currently executes
on the audio player thread. The `AudioWorker` object is assigned to a new thread, but because the
`AudioPlayer.play` method calls `__worker.play` without going through a signal, the `AudioWorker.play`
method actually executes on the thread that called `AudioPlayer.play`. In practice this is the UI thread,
because `audio.play` is called through a signal on `SignalReceiver`, which lives on that thread. Thus,
the time-sensitive engine thread is already isolated from any slowness in reading or playing audio.

This commit reduces the `AudioPlayer` class to a plain Python object whose only responsibilities
are to load and play the application's sound effects. It also simplifies the file loading logic by
removing the direct call to the `wave` library in favor of the `from_wave_file` helper method
offered by `simpleaudio`, which in practice uses the exact same call to the `wave` library.

A notable benefit of removing this thread is that the application no longer crashes with an `abort`
call upon exit (issue AllYarnsAreBeautiful#680), caused by improper termination of the audio thread.
Copy link
Contributor

coderabbitai bot commented Jul 24, 2024

Walkthrough

The recent changes focus on simplifying the audio playback functionality within the application. The AudioWorker class has been removed, and its responsibilities have been consolidated into a new AudioPlayer class, which directly handles playback without threading. The requirements.build.txt file has also been updated to remove the wave package, potentially indicating a shift in how audio processing is managed. These modifications aim to streamline the code and improve maintainability while preserving core features.

Changes

Files Change Summary
requirements.build.txt Removed wave==0.0.2, indicating a shift in audio processing dependencies.
src/main/python/main/ayab/audio.py Refactored audio playback: removed AudioWorker, simplified AudioPlayer, and revised methods for clarity.
src/main/python/main/ayab/signal_receiver.py Simplified signal-slot connections, removing unnecessary threading specifics for audio playback.

Sequence Diagram(s)

sequenceDiagram
    participant GUI as GUI Main
    participant Audio as AudioPlayer

    GUI->>Audio: play(sound)
    Audio-->>Audio: Load audio file
    Audio-->>Audio: Play audio
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bc96d5c and 99a24c1.

Files selected for processing (3)
  • requirements.build.txt (1 hunks)
  • src/main/python/main/ayab/audio.py (1 hunks)
  • src/main/python/main/ayab/signal_receiver.py (2 hunks)
Additional comments not posted (6)
requirements.build.txt (1)

Line range hint 1-12:
Approved: Removal of wave package.

The removal of the wave==0.0.2 package aligns with the refactoring of the audio playback functionality, as simpleaudio is now used exclusively.

src/main/python/main/ayab/audio.py (4)

33-36: Approved: Initialization of AudioPlayer.

The __init__ method correctly initializes the audio player with the GUI context, preferences, and a cache for wave objects.


39-48: Approved: Simplified play method.

The play method correctly handles quiet mode and plays the audio without threading and blocking logic, aligning with the PR objectives.


50-57: Approved: Efficient caching in __wave method.

The __wave method correctly retrieves and caches audio wave objects, ensuring efficient audio playback.


59-62: Approved: Simplified __load_wave method.

The __load_wave method correctly constructs the file path and loads the wave object, aligning with the simplified approach.

src/main/python/main/ayab/signal_receiver.py (1)

76-76: Approved: Simplified signal-slot connection for audio_player.

The connection for audio_player has been simplified, aligning with the removal of threading logic.

@dl1com dl1com merged commit f3361f3 into AllYarnsAreBeautiful:1.0.0-dev Jul 25, 2024
2 checks passed
@jonathanperret jonathanperret deleted the fix-audio-crash-on-exit branch July 25, 2024 08:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants