Wavelength
Privacy-focused, cross-platform, and open-source communication application
Loading...
Searching...
No Matches
audio_player.h
Go to the documentation of this file.
1#ifndef INLINE_AUDIO_PLAYER_H
2#define INLINE_AUDIO_PLAYER_H
3
4#include <QLabel>
5#include <QPropertyAnimation>
6
7class AudioDecoder;
11
22class AudioPlayer final : public QFrame {
23 Q_OBJECT
28
29public:
39 explicit AudioPlayer(const QByteArray &audio_data, const QString &mime_type, QWidget *parent = nullptr);
40
45 double GetScanlineOpacity() const { return scanline_opacity_; }
46
52 void SetScanlineOpacity(const double opacity) {
53 scanline_opacity_ = opacity;
54 update();
55 }
56
61 double GetSpectrumIntensity() const { return spectrum_intensity_; }
62
68 void SetSpectrumIntensity(const double intensity) {
69 spectrum_intensity_ = intensity;
70 update();
71 }
72
76 ~AudioPlayer() override {
78 }
79
84 void ReleaseResources();
85
91 void Activate();
92
98 void Deactivate();
99
105 void AdjustVolume(int volume) const;
106
111 void ToggleMute();
112
118 void UpdateVolumeIcon(float volume) const;
119
120protected:
125 void paintEvent(QPaintEvent *event) override;
126
134 bool eventFilter(QObject *watched, QEvent *event) override;
135
136private slots:
141 void OnSliderPressed();
142
148 void OnSliderReleased();
149
155 void UpdateTimeLabel(int position);
156
163 void UpdateSliderPosition(double position) const;
164
170 void SeekAudio(int position) const;
171
177 void TogglePlayback();
178
184 void HandleError(const QString &message) const;
185
194 void HandleAudioInfo(int sample_rate, int channels, double duration);
195
201 void UpdateUI();
202
207 const auto spectrum_animation = new QPropertyAnimation(this, "spectrumIntensity");
208 spectrum_animation->setDuration(600);
209 spectrum_animation->setStartValue(spectrum_intensity_);
210 spectrum_animation->setEndValue(0.6);
211 spectrum_animation->setEasingCurve(QEasingCurve::OutCubic);
212 spectrum_animation->start(QPropertyAnimation::DeleteWhenStopped);
213 }
214
219 const auto spectrum_animation = new QPropertyAnimation(this, "spectrumIntensity");
220 spectrum_animation->setDuration(800);
221 spectrum_animation->setStartValue(spectrum_intensity_);
222 spectrum_animation->setEndValue(0.2);
223 spectrum_animation->setEasingCurve(QEasingCurve::OutCubic);
224 spectrum_animation->start(QPropertyAnimation::DeleteWhenStopped);
225 }
226
227private:
234 void PaintSpectrum(QWidget *target);
235
247 QLabel *time_label_;
252
254 std::shared_ptr<AudioDecoder> decoder_;
256 QTimer *ui_timer_;
259
261 QByteArray m_audioData;
263 QString mime_type_;
264
266 double audio_duration_ = 0;
270 bool slider_dragging_ = false;
272 int last_volume_ = 100;
274 bool playback_finished_ = false;
276 bool was_playing_ = false;
278 bool is_active_ = false;
279
285 QVector<double> spectrum_data_;
286
288 static inline AudioPlayer *active_player_ = nullptr;
289
292};
293
294#endif //INLINE_AUDIO_PLAYER_H
Decodes and plays audio data using FFmpeg libraries in a separate thread.
Definition audio_decoder.h:33
bool was_playing_
Stores whether playback was active before the user started dragging the slider.
Definition audio_player.h:276
void OnSliderPressed()
Slot called when the user presses the progress slider. Remembers the playback state and pauses playba...
Definition audio_player.cpp:296
void ToggleMute()
Toggles the mute state. If currently unmuted, mutes and remembers the last volume....
Definition audio_player.cpp:225
CyberAudioSlider * progress_slider_
Custom slider for displaying and seeking playback progress.
Definition audio_player.h:245
void PaintSpectrum(QWidget *target)
Custom painting method for the spectrum visualization widget. Draws a background grid and vertical ba...
Definition audio_player.cpp:452
double spectrum_intensity_
Intensity multiplier for the spectrum visualization (property).
Definition audio_player.h:283
void IncreaseSpectrumIntensity()
Starts an animation to increase the spectrum intensity (e.g., when playback starts).
Definition audio_player.h:206
static AudioPlayer * active_player_
Static pointer to the currently active InlineAudioPlayer instance. Ensures only one player uses audio...
Definition audio_player.h:288
QTimer * spectrum_timer_
Timer for triggering repaints of the spectrum view.
Definition audio_player.h:258
QLabel * audio_info_label_
Label displaying audio type, sample rate, channels.
Definition audio_player.h:237
void Deactivate()
Deactivates this player instance. Pauses playback if active, clears the global active player referenc...
Definition audio_player.cpp:195
QString mime_type_
Stores the MIME type of the audio data.
Definition audio_player.h:263
bool playback_finished_
Flag indicating if playback has reached the end.
Definition audio_player.h:274
void DecreaseSpectrumIntensity()
Starts an animation to decrease the spectrum intensity (e.g., when playback pauses or finishes).
Definition audio_player.h:218
std::shared_ptr< AudioDecoder > decoder_
Shared pointer to the AudioDecoder instance responsible for decoding and playback.
Definition audio_player.h:254
double spectrumIntensity
Property controlling the intensity/height of the spectrum visualization bars. Animatable.
Definition audio_player.h:27
double audio_duration_
Total duration of the audio in seconds. Set by HandleAudioInfo.
Definition audio_player.h:266
AudioPlayer(const QByteArray &audio_data, const QString &mime_type, QWidget *parent=nullptr)
Constructs an InlineAudioPlayer widget. Initializes UI elements (labels, buttons, sliders,...
Definition audio_player.cpp:16
void OnSliderReleased()
Slot called when the user releases the progress slider. Performs the actual seek operation based on t...
Definition audio_player.cpp:307
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 p...
Definition audio_player.cpp:408
void Activate()
Activates this player instance. If another player is active, it deactivates it first....
Definition audio_player.cpp:168
TranslationManager * translator_
Pointer to the translation manager for handling UI text translations.
Definition audio_player.h:291
void paintEvent(QPaintEvent *event) override
Overridden paint event handler. Draws the cyberpunk-style frame border.
Definition audio_player.cpp:247
CyberAudioButton * volume_button_
Custom button for toggling mute. Icon changes based on volume.
Definition audio_player.h:249
bool slider_dragging_
Flag indicating if the user is currently dragging the progress slider.
Definition audio_player.h:270
void UpdateTimeLabel(int position)
Slot called when the progress slider's value changes (e.g., user dragging). Updates the time label di...
Definition audio_player.cpp:323
void SetSpectrumIntensity(const double intensity)
Sets the intensity multiplier for the spectrum visualization. Triggers an update() to repaint the spe...
Definition audio_player.h:68
void UpdateVolumeIcon(float volume) const
Updates the volume button icon based on the current volume level. Shows different icons for muted,...
Definition audio_player.cpp:237
void AdjustVolume(int volume) const
Adjusts the playback volume via the AudioDecoder. Also updates the volume icon based on the new volum...
Definition audio_player.cpp:216
double scanlineOpacity
Property controlling the opacity of a potential scanline effect.
Definition audio_player.h:25
void HandleError(const QString &message) const
Slot connected to the decoder's error signal. Prints the error message to debug output and updates th...
Definition audio_player.cpp:402
QByteArray m_audioData
Stores the raw audio data passed in the constructor.
Definition audio_player.h:261
CyberAudioButton * play_button_
Custom button for play/pause/replay control.
Definition audio_player.h:243
int last_volume_
Stores the volume level before muting.
Definition audio_player.h:272
double GetScanlineOpacity() const
Gets the current opacity value for the scanline effect.
Definition audio_player.h:45
void ReleaseResources()
Stops timers, stops and waits for the decoder thread, and releases decoder resources....
Definition audio_player.cpp:148
bool eventFilter(QObject *watched, QEvent *event) override
Overridden event filter. Intercepts paint events for the spectrum_view_ widget to call the custom Pai...
Definition audio_player.cpp:288
void UpdateUI()
Slot called periodically by ui_timer_. Updates UI elements, primarily the spectrum data for visualiza...
Definition audio_player.cpp:428
double GetSpectrumIntensity() const
Gets the current intensity multiplier for the spectrum visualization.
Definition audio_player.h:61
~AudioPlayer() override
Destructor. Ensures resources are released.
Definition audio_player.h:76
QLabel * status_label_
Label displaying playback status (Initializing, Ready, Playing, Paused, Error, Finished).
Definition audio_player.h:239
bool is_active_
Flag indicating if this player instance is the currently active one.
Definition audio_player.h:278
void SeekAudio(int position) const
Initiates a seek operation in the AudioDecoder. Converts the slider position (milliseconds) to second...
Definition audio_player.cpp:364
double current_position_
Current playback position in seconds. Updated by UpdateTimeLabel.
Definition audio_player.h:268
void SetScanlineOpacity(const double opacity)
Sets the opacity for the scanline effect. Triggers an update() to repaint the widget.
Definition audio_player.h:52
QWidget * spectrum_view_
Widget container where the spectrum visualization is drawn.
Definition audio_player.h:241
QTimer * ui_timer_
Timer for triggering periodic UI updates (e.g., spectrum data).
Definition audio_player.h:256
void UpdateSliderPosition(double position) const
Slot connected to the decoder's positionChanged signal. Updates the progress slider's position and th...
Definition audio_player.cpp:338
double scanline_opacity_
Opacity value for the scanline effect (property).
Definition audio_player.h:281
QLabel * time_label_
Label displaying current time / total duration (MM:SS / MM:SS).
Definition audio_player.h:247
void TogglePlayback()
Toggles the playback state (play/pause). Activates the player if necessary. Handles restarting playba...
Definition audio_player.cpp:372
CyberAudioSlider * volume_slider_
Custom slider for controlling playback volume.
Definition audio_player.h:251
QVector< double > spectrum_data_
Vector storing the current height values for the spectrum bars. Updated by UpdateUI.
Definition audio_player.h:285
A custom QPushButton styled with a cyberpunk aesthetic, specifically themed for audio controls.
Definition cyber_audio_button.h:14
A custom QSlider styled with a cyberpunk aesthetic, specifically themed for audio controls.
Definition cyber_audio_slider.h:13
Manages the loading and delivery of translations for applications.
Definition translation_manager.h:15