|
Wavelength
Privacy-focused, cross-platform, and open-source communication application
|
An audio player widget with a cyberpunk aesthetic. More...
#include <audio_player.h>
Public Member Functions | |
| void | Activate () |
| Activates this player instance. If another player is active, it deactivates it first. Ensures the decoder is initialized and running. Sets this instance as the globally active player and animates the spectrum intensity up. | |
| void | AdjustVolume (int volume) const |
| Adjusts the playback volume via the AudioDecoder. Also updates the volume icon based on the new volume level. | |
| AudioPlayer (const QByteArray &audio_data, const QString &mime_type, QWidget *parent=nullptr) | |
| Constructs an InlineAudioPlayer widget. Initializes UI elements (labels, buttons, sliders, spectrum view), sets up layouts, creates the AudioDecoder instance, connects signals and slots for controls and decoder events, and starts timers for UI updates. | |
| void | Deactivate () |
| Deactivates this player instance. Pauses playback if active, clears the global active player reference if it points to this instance, and animates the spectrum intensity down. | |
| double | GetScanlineOpacity () const |
| Gets the current opacity value for the scanline effect. | |
| double | GetSpectrumIntensity () const |
| Gets the current intensity multiplier for the spectrum visualization. | |
| void | ReleaseResources () |
| Stops timers, stops and waits for the decoder thread, and releases decoder resources. Also ensures the global active_player_ pointer is cleared if it points to this instance. | |
| void | SetScanlineOpacity (const double opacity) |
| Sets the opacity for the scanline effect. Triggers an update() to repaint the widget. | |
| void | SetSpectrumIntensity (const double intensity) |
| Sets the intensity multiplier for the spectrum visualization. Triggers an update() to repaint the spectrum view. | |
| void | ToggleMute () |
| Toggles the mute state. If currently unmuted, mutes and remembers the last volume. If muted, restore the last volume (or 100% if the last volume was 0). | |
| void | UpdateVolumeIcon (float volume) const |
| Updates the volume button icon based on the current volume level. Shows different icons for muted, low volume, and high volume. | |
| ~AudioPlayer () override | |
| Destructor. Ensures resources are released. | |
Protected Member Functions | |
| bool | eventFilter (QObject *watched, QEvent *event) override |
| Overridden event filter. Intercepts paint events for the spectrum_view_ widget to call the custom PaintSpectrum method. | |
| void | paintEvent (QPaintEvent *event) override |
| Overridden paint event handler. Draws the cyberpunk-style frame border. | |
Properties | |
| double | scanlineOpacity |
| Property controlling the opacity of a potential scanline effect. | |
| double | spectrumIntensity |
| Property controlling the intensity/height of the spectrum visualization bars. Animatable. | |
Private Slots | |
| void | DecreaseSpectrumIntensity () |
| Starts an animation to decrease the spectrum intensity (e.g., when playback pauses or finishes). | |
| void | HandleAudioInfo (int sample_rate, int channels, double duration) |
| Slot connected to the decoder's audioInfo signal. Stores the audio duration, sets the range for the progress slider, updates the info label with audio details (type, sample rate, channels), sets status to "READY", and initializes spectrum data. | |
| void | HandleError (const QString &message) const |
| Slot connected to the decoder's error signal. Prints the error message to debug output and updates the status/info labels. | |
| void | IncreaseSpectrumIntensity () |
| Starts an animation to increase the spectrum intensity (e.g., when playback starts). | |
| void | OnSliderPressed () |
| Slot called when the user presses the progress slider. Remembers the playback state and pauses playback temporarily during seeking. Updates status label. | |
| void | OnSliderReleased () |
| Slot called when the user releases the progress slider. Performs the actual seek operation based on the slider's final position. Resumes playback if it was active before seeking. Updates status label. | |
| void | SeekAudio (int position) const |
| Initiates a seek operation in the AudioDecoder. Converts the slider position (milliseconds) to seconds before calling decoder_->Seek(). | |
| void | TogglePlayback () |
| Toggles the playback state (play/pause). Activates the player if necessary. Handles restarting playback after finishing. Updates the play button text and status label. Manages spectrum intensity animation. | |
| void | UpdateSliderPosition (double position) const |
| Slot connected to the decoder's positionChanged signal. Updates the progress slider's position and the time label display based on the decoder's current playback position. Blocks slider signals during update to prevent feedback loops. | |
| void | UpdateTimeLabel (int position) |
| Slot called when the progress slider's value changes (e.g., user dragging). Updates the time label display based on the slider's current position. | |
| void | UpdateUI () |
| Slot called periodically by ui_timer_. Updates UI elements, primarily the spectrum data for visualization (random fluctuations). It also includes placeholder logic for updating status label text occasionally. | |
Private Member Functions | |
| void | PaintSpectrum (QWidget *target) |
| Custom painting method for the spectrum visualization widget. Draws a background grid and vertical bars representing audio frequency levels (using spectrum_data_). Called via the event filter on the spectrum_view_ widget. | |
Private Attributes | |
| double | audio_duration_ = 0 |
| Total duration of the audio in seconds. Set by HandleAudioInfo. | |
| QLabel * | audio_info_label_ |
| Label displaying audio type, sample rate, channels. | |
| double | current_position_ = 0 |
| Current playback position in seconds. Updated by UpdateTimeLabel. | |
| std::shared_ptr< AudioDecoder > | decoder_ |
| Shared pointer to the AudioDecoder instance responsible for decoding and playback. | |
| bool | is_active_ = false |
| Flag indicating if this player instance is the currently active one. | |
| int | last_volume_ = 100 |
| Stores the volume level before muting. | |
| QByteArray | m_audioData |
| Stores the raw audio data passed in the constructor. | |
| QString | mime_type_ |
| Stores the MIME type of the audio data. | |
| CyberAudioButton * | play_button_ |
| Custom button for play/pause/replay control. | |
| bool | playback_finished_ = false |
| Flag indicating if playback has reached the end. | |
| CyberAudioSlider * | progress_slider_ |
| Custom slider for displaying and seeking playback progress. | |
| double | scanline_opacity_ |
| Opacity value for the scanline effect (property). | |
| bool | slider_dragging_ = false |
| Flag indicating if the user is currently dragging the progress slider. | |
| QVector< double > | spectrum_data_ |
| Vector storing the current height values for the spectrum bars. Updated by UpdateUI. | |
| double | spectrum_intensity_ |
| Intensity multiplier for the spectrum visualization (property). | |
| QTimer * | spectrum_timer_ |
| Timer for triggering repaints of the spectrum view. | |
| QWidget * | spectrum_view_ |
| Widget container where the spectrum visualization is drawn. | |
| QLabel * | status_label_ |
| Label displaying playback status (Initializing, Ready, Playing, Paused, Error, Finished). | |
| QLabel * | time_label_ |
| Label displaying current time / total duration (MM:SS / MM:SS). | |
| TranslationManager * | translator_ |
| Pointer to the translation manager for handling UI text translations. | |
| QTimer * | ui_timer_ |
| Timer for triggering periodic UI updates (e.g., spectrum data). | |
| CyberAudioButton * | volume_button_ |
| Custom button for toggling mute. Icon changes based on volume. | |
| CyberAudioSlider * | volume_slider_ |
| Custom slider for controlling playback volume. | |
| bool | was_playing_ = false |
| Stores whether playback was active before the user started dragging the slider. | |
Static Private Attributes | |
| static AudioPlayer * | active_player_ = nullptr |
| Static pointer to the currently active InlineAudioPlayer instance. Ensures only one player uses audio output. | |
An audio player widget with a cyberpunk aesthetic.
This widget provides playback controls (play/pause, seek, volume) for audio data, displays audio information (type, sample rate, channels), shows the current time and duration, and includes a simple spectrum visualization. It uses AudioDecoder for decoding and playback. It manages an "active player" concept, ensuring only one player is actively using resources (like the audio output device) at a time. Includes visual effects like animated spectrum intensity.
|
explicit |
Constructs an InlineAudioPlayer widget. Initializes UI elements (labels, buttons, sliders, spectrum view), sets up layouts, creates the AudioDecoder instance, connects signals and slots for controls and decoder events, and starts timers for UI updates.
| audio_data | The raw audio data to be played. |
| mime_type | The MIME type of the audio data (e.g., "audio/mpeg"). |
| parent | Optional parent widget. |
|
inlineoverride |
Destructor. Ensures resources are released.
| void AudioPlayer::Activate | ( | ) |
Activates this player instance. If another player is active, it deactivates it first. Ensures the decoder is initialized and running. Sets this instance as the globally active player and animates the spectrum intensity up.
| void AudioPlayer::AdjustVolume | ( | int | volume | ) | const |
Adjusts the playback volume via the AudioDecoder. Also updates the volume icon based on the new volume level.
| volume | The desired volume level (0-100, corresponding to 0.0-1.0 float). |
| void AudioPlayer::Deactivate | ( | ) |
Deactivates this player instance. Pauses playback if active, clears the global active player reference if it points to this instance, and animates the spectrum intensity down.
|
inlineprivateslot |
Starts an animation to decrease the spectrum intensity (e.g., when playback pauses or finishes).
|
overrideprotected |
Overridden event filter. Intercepts paint events for the spectrum_view_ widget to call the custom PaintSpectrum method.
| watched | The object being watched. |
| event | The event being processed. |
|
inline |
Gets the current opacity value for the scanline effect.
|
inline |
Gets the current intensity multiplier for the spectrum visualization.
|
privateslot |
Slot connected to the decoder's audioInfo signal. Stores the audio duration, sets the range for the progress slider, updates the info label with audio details (type, sample rate, channels), sets status to "READY", and initializes spectrum data.
| sample_rate | Audio sample rate in Hz. |
| channels | Number of audio channels. |
| duration | Total audio duration in seconds. |
|
privateslot |
Slot connected to the decoder's error signal. Prints the error message to debug output and updates the status/info labels.
| message | The error message from the decoder. |
|
inlineprivateslot |
Starts an animation to increase the spectrum intensity (e.g., when playback starts).
|
privateslot |
Slot called when the user presses the progress slider. Remembers the playback state and pauses playback temporarily during seeking. Updates status label.
|
privateslot |
Slot called when the user releases the progress slider. Performs the actual seek operation based on the slider's final position. Resumes playback if it was active before seeking. Updates status label.
|
overrideprotected |
Overridden paint event handler. Draws the cyberpunk-style frame border.
| event | The paint event. |
|
private |
Custom painting method for the spectrum visualization widget. Draws a background grid and vertical bars representing audio frequency levels (using spectrum_data_). Called via the event filter on the spectrum_view_ widget.
| target | The QWidget (spectrum_view_) to paint onto. |
| void AudioPlayer::ReleaseResources | ( | ) |
Stops timers, stops and waits for the decoder thread, and releases decoder resources. Also ensures the global active_player_ pointer is cleared if it points to this instance.
|
privateslot |
Initiates a seek operation in the AudioDecoder. Converts the slider position (milliseconds) to seconds before calling decoder_->Seek().
| position | The target position in milliseconds. |
|
inline |
Sets the opacity for the scanline effect. Triggers an update() to repaint the widget.
| opacity | The desired scanline opacity (0.0 to 1.0). |
|
inline |
Sets the intensity multiplier for the spectrum visualization. Triggers an update() to repaint the spectrum view.
| intensity | The desired spectrum intensity. |
| void AudioPlayer::ToggleMute | ( | ) |
Toggles the mute state. If currently unmuted, mutes and remembers the last volume. If muted, restore the last volume (or 100% if the last volume was 0).
|
privateslot |
Toggles the playback state (play/pause). Activates the player if necessary. Handles restarting playback after finishing. Updates the play button text and status label. Manages spectrum intensity animation.
|
privateslot |
Slot connected to the decoder's positionChanged signal. Updates the progress slider's position and the time label display based on the decoder's current playback position. Blocks slider signals during update to prevent feedback loops.
| position | The current playback position in seconds. |
|
privateslot |
Slot called when the progress slider's value changes (e.g., user dragging). Updates the time label display based on the slider's current position.
| position | The slider's current value (milliseconds). |
|
privateslot |
Slot called periodically by ui_timer_. Updates UI elements, primarily the spectrum data for visualization (random fluctuations). It also includes placeholder logic for updating status label text occasionally.
| void AudioPlayer::UpdateVolumeIcon | ( | float | volume | ) | const |
Updates the volume button icon based on the current volume level. Shows different icons for muted, low volume, and high volume.
| volume | The current volume level (0.0 to 1.0). |
|
inlinestaticprivate |
Static pointer to the currently active InlineAudioPlayer instance. Ensures only one player uses audio output.
|
private |
Total duration of the audio in seconds. Set by HandleAudioInfo.
|
private |
Label displaying audio type, sample rate, channels.
|
private |
Current playback position in seconds. Updated by UpdateTimeLabel.
|
private |
Shared pointer to the AudioDecoder instance responsible for decoding and playback.
|
private |
Flag indicating if this player instance is the currently active one.
|
private |
Stores the volume level before muting.
|
private |
Stores the raw audio data passed in the constructor.
|
private |
Stores the MIME type of the audio data.
|
private |
Custom button for play/pause/replay control.
|
private |
Flag indicating if playback has reached the end.
|
private |
Custom slider for displaying and seeking playback progress.
|
private |
Opacity value for the scanline effect (property).
|
private |
Flag indicating if the user is currently dragging the progress slider.
|
private |
Vector storing the current height values for the spectrum bars. Updated by UpdateUI.
|
private |
Intensity multiplier for the spectrum visualization (property).
|
private |
Timer for triggering repaints of the spectrum view.
|
private |
Widget container where the spectrum visualization is drawn.
|
private |
Label displaying playback status (Initializing, Ready, Playing, Paused, Error, Finished).
|
private |
Label displaying current time / total duration (MM:SS / MM:SS).
|
private |
Pointer to the translation manager for handling UI text translations.
|
private |
Timer for triggering periodic UI updates (e.g., spectrum data).
|
private |
Custom button for toggling mute. Icon changes based on volume.
|
private |
Custom slider for controlling playback volume.
|
private |
Stores whether playback was active before the user started dragging the slider.
|
readwrite |
Property controlling the opacity of a potential scanline effect.
|
readwrite |
Property controlling the intensity/height of the spectrum visualization bars. Animatable.