Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
AudioPlayer Class Referencefinal

An audio player widget with a cyberpunk aesthetic. More...

#include <audio_player.h>

Inheritance diagram for AudioPlayer:
Collaboration diagram for AudioPlayer:

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< AudioDecoderdecoder_
 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.
 
CyberAudioButtonplay_button_
 Custom button for play/pause/replay control.
 
bool playback_finished_ = false
 Flag indicating if playback has reached the end.
 
CyberAudioSliderprogress_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).
 
TranslationManagertranslator_
 Pointer to the translation manager for handling UI text translations.
 
QTimer * ui_timer_
 Timer for triggering periodic UI updates (e.g., spectrum data).
 
CyberAudioButtonvolume_button_
 Custom button for toggling mute. Icon changes based on volume.
 
CyberAudioSlidervolume_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 AudioPlayeractive_player_ = nullptr
 Static pointer to the currently active InlineAudioPlayer instance. Ensures only one player uses audio output.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ AudioPlayer()

AudioPlayer::AudioPlayer ( const QByteArray & audio_data,
const QString & mime_type,
QWidget * parent = nullptr )
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.

Parameters
audio_dataThe raw audio data to be played.
mime_typeThe MIME type of the audio data (e.g., "audio/mpeg").
parentOptional parent widget.
Here is the call graph for this function:

◆ ~AudioPlayer()

AudioPlayer::~AudioPlayer ( )
inlineoverride

Destructor. Ensures resources are released.

Here is the call graph for this function:

Member Function Documentation

◆ Activate()

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.

Here is the caller graph for this function:

◆ AdjustVolume()

void AudioPlayer::AdjustVolume ( int volume) const

Adjusts the playback volume via the AudioDecoder. Also updates the volume icon based on the new volume level.

Parameters
volumeThe desired volume level (0-100, corresponding to 0.0-1.0 float).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ Deactivate()

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.

◆ DecreaseSpectrumIntensity

void AudioPlayer::DecreaseSpectrumIntensity ( )
inlineprivateslot

Starts an animation to decrease the spectrum intensity (e.g., when playback pauses or finishes).

Here is the caller graph for this function:

◆ eventFilter()

bool AudioPlayer::eventFilter ( QObject * watched,
QEvent * event )
overrideprotected

Overridden event filter. Intercepts paint events for the spectrum_view_ widget to call the custom PaintSpectrum method.

Parameters
watchedThe object being watched.
eventThe event being processed.
Returns
True if the event was handled (spectrum painted), false otherwise.
Here is the call graph for this function:

◆ GetScanlineOpacity()

double AudioPlayer::GetScanlineOpacity ( ) const
inline

Gets the current opacity value for the scanline effect.

Returns
The scanline opacity (0.0 to 1.0).
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetSpectrumIntensity()

double AudioPlayer::GetSpectrumIntensity ( ) const
inline

Gets the current intensity multiplier for the spectrum visualization.

Returns
The spectrum intensity value.

◆ HandleAudioInfo

void AudioPlayer::HandleAudioInfo ( int sample_rate,
int channels,
double duration )
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.

Parameters
sample_rateAudio sample rate in Hz.
channelsNumber of audio channels.
durationTotal audio duration in seconds.
Here is the caller graph for this function:

◆ HandleError

void AudioPlayer::HandleError ( const QString & message) const
privateslot

Slot connected to the decoder's error signal. Prints the error message to debug output and updates the status/info labels.

Parameters
messageThe error message from the decoder.
Here is the caller graph for this function:

◆ IncreaseSpectrumIntensity

void AudioPlayer::IncreaseSpectrumIntensity ( )
inlineprivateslot

Starts an animation to increase the spectrum intensity (e.g., when playback starts).

Here is the caller graph for this function:

◆ OnSliderPressed

void AudioPlayer::OnSliderPressed ( )
privateslot

Slot called when the user presses the progress slider. Remembers the playback state and pauses playback temporarily during seeking. Updates status label.

Here is the caller graph for this function:

◆ OnSliderReleased

void AudioPlayer::OnSliderReleased ( )
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ paintEvent()

void AudioPlayer::paintEvent ( QPaintEvent * event)
overrideprotected

Overridden paint event handler. Draws the cyberpunk-style frame border.

Parameters
eventThe paint event.

◆ PaintSpectrum()

void AudioPlayer::PaintSpectrum ( QWidget * target)
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.

Parameters
targetThe QWidget (spectrum_view_) to paint onto.
Here is the caller graph for this function:

◆ ReleaseResources()

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.

Here is the caller graph for this function:

◆ SeekAudio

void AudioPlayer::SeekAudio ( int position) const
privateslot

Initiates a seek operation in the AudioDecoder. Converts the slider position (milliseconds) to seconds before calling decoder_->Seek().

Parameters
positionThe target position in milliseconds.
Here is the caller graph for this function:

◆ SetScanlineOpacity()

void AudioPlayer::SetScanlineOpacity ( const double opacity)
inline

Sets the opacity for the scanline effect. Triggers an update() to repaint the widget.

Parameters
opacityThe desired scanline opacity (0.0 to 1.0).

◆ SetSpectrumIntensity()

void AudioPlayer::SetSpectrumIntensity ( const double intensity)
inline

Sets the intensity multiplier for the spectrum visualization. Triggers an update() to repaint the spectrum view.

Parameters
intensityThe desired spectrum intensity.

◆ ToggleMute()

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).

Here is the caller graph for this function:

◆ TogglePlayback

void AudioPlayer::TogglePlayback ( )
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.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateSliderPosition

void AudioPlayer::UpdateSliderPosition ( double position) const
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.

Parameters
positionThe current playback position in seconds.
Here is the caller graph for this function:

◆ UpdateTimeLabel

void AudioPlayer::UpdateTimeLabel ( int position)
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.

Parameters
positionThe slider's current value (milliseconds).
Here is the caller graph for this function:

◆ UpdateUI

void AudioPlayer::UpdateUI ( )
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.

Here is the caller graph for this function:

◆ UpdateVolumeIcon()

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.

Parameters
volumeThe current volume level (0.0 to 1.0).
Here is the caller graph for this function:

Member Data Documentation

◆ active_player_

AudioPlayer* AudioPlayer::active_player_ = nullptr
inlinestaticprivate

Static pointer to the currently active InlineAudioPlayer instance. Ensures only one player uses audio output.

◆ audio_duration_

double AudioPlayer::audio_duration_ = 0
private

Total duration of the audio in seconds. Set by HandleAudioInfo.

◆ audio_info_label_

QLabel* AudioPlayer::audio_info_label_
private

Label displaying audio type, sample rate, channels.

◆ current_position_

double AudioPlayer::current_position_ = 0
private

Current playback position in seconds. Updated by UpdateTimeLabel.

◆ decoder_

std::shared_ptr<AudioDecoder> AudioPlayer::decoder_
private

Shared pointer to the AudioDecoder instance responsible for decoding and playback.

◆ is_active_

bool AudioPlayer::is_active_ = false
private

Flag indicating if this player instance is the currently active one.

◆ last_volume_

int AudioPlayer::last_volume_ = 100
private

Stores the volume level before muting.

◆ m_audioData

QByteArray AudioPlayer::m_audioData
private

Stores the raw audio data passed in the constructor.

◆ mime_type_

QString AudioPlayer::mime_type_
private

Stores the MIME type of the audio data.

◆ play_button_

CyberAudioButton* AudioPlayer::play_button_
private

Custom button for play/pause/replay control.

◆ playback_finished_

bool AudioPlayer::playback_finished_ = false
private

Flag indicating if playback has reached the end.

◆ progress_slider_

CyberAudioSlider* AudioPlayer::progress_slider_
private

Custom slider for displaying and seeking playback progress.

◆ scanline_opacity_

double AudioPlayer::scanline_opacity_
private

Opacity value for the scanline effect (property).

◆ slider_dragging_

bool AudioPlayer::slider_dragging_ = false
private

Flag indicating if the user is currently dragging the progress slider.

◆ spectrum_data_

QVector<double> AudioPlayer::spectrum_data_
private

Vector storing the current height values for the spectrum bars. Updated by UpdateUI.

◆ spectrum_intensity_

double AudioPlayer::spectrum_intensity_
private

Intensity multiplier for the spectrum visualization (property).

◆ spectrum_timer_

QTimer* AudioPlayer::spectrum_timer_
private

Timer for triggering repaints of the spectrum view.

◆ spectrum_view_

QWidget* AudioPlayer::spectrum_view_
private

Widget container where the spectrum visualization is drawn.

◆ status_label_

QLabel* AudioPlayer::status_label_
private

Label displaying playback status (Initializing, Ready, Playing, Paused, Error, Finished).

◆ time_label_

QLabel* AudioPlayer::time_label_
private

Label displaying current time / total duration (MM:SS / MM:SS).

◆ translator_

TranslationManager* AudioPlayer::translator_
private

Pointer to the translation manager for handling UI text translations.

◆ ui_timer_

QTimer* AudioPlayer::ui_timer_
private

Timer for triggering periodic UI updates (e.g., spectrum data).

◆ volume_button_

CyberAudioButton* AudioPlayer::volume_button_
private

Custom button for toggling mute. Icon changes based on volume.

◆ volume_slider_

CyberAudioSlider* AudioPlayer::volume_slider_
private

Custom slider for controlling playback volume.

◆ was_playing_

bool AudioPlayer::was_playing_ = false
private

Stores whether playback was active before the user started dragging the slider.

Property Documentation

◆ scanlineOpacity

double AudioPlayer::scanlineOpacity
readwrite

Property controlling the opacity of a potential scanline effect.

◆ spectrumIntensity

double AudioPlayer::spectrumIntensity
readwrite

Property controlling the intensity/height of the spectrum visualization bars. Animatable.


The documentation for this class was generated from the following files: