wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
AudioOutputElement.cpp
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
5
6#include <QAudioDevice>
7#include <QMediaDevices>
8
9#include "App/Wiring/Port.h"
10
11AudioOutputElement::AudioOutputElement(ElementType type, QGraphicsItem *parent, float initialVolume)
12 : GraphicElement(type, parent)
13 , m_volume(initialVolume)
14{
15 // Position label to the right of the 64×64 body, vertically centred
16 setLabelAnchor(QPointF(64, 34));
17
18 // Detect audio hardware once per process; subsequent calls are guarded by
19 // m_hasOutputDevice so the element works silently in headless/CI environments.
20 static const bool hasOutputDevice = !QMediaDevices::defaultAudioOutput().description().isEmpty();
21 m_hasOutputDevice = hasOutputDevice;
22}
23
25{
26 if (!isValid()) {
27 stop();
28 return;
29 }
30
31 const Status inputValue = inputs().constFirst()->status();
32
33 (inputValue == Status::Active) ? play() : stop();
34}
35
37{
38 return m_isPlaying;
39}
40
42{
43 return m_muted;
44}
45
47{
48 return m_volume;
49}
50
52{
53 m_muted = mute;
54 if (!m_hasOutputDevice) {
55 return;
56 }
57 applyMute();
58}
59
61{
62 m_volume = vol;
65 }
66}
67
69{
70 if (m_isPlaying) {
71 return;
72 }
73
74 setPixmap(1);
75
77 startAudio();
78 }
79
80 m_isPlaying = true;
81}
82
84{
85 if (!m_isPlaying) {
86 return;
87 }
88
89 setPixmap(0);
90
92 stopAudio();
93 }
94
95 m_isPlaying = false;
96}
Shared base class for audio output elements (AudioBox and Buzzer).
Enums::ElementType ElementType
Definition Enums.h:107
Enums::Status Status
Definition Enums.h:106
Port classes: Port (base), InputPort, and OutputPort.
bool isMuted() const
Returns true if audio output is muted.
virtual void startAudio()=0
Starts hardware playback (called from play() when m_hasOutputDevice is true).
float volume() const override
Returns the audio playback volume (0.0–1.0).
void mute(const bool mute=true)
Mutes or unmutes audio according to mute.
void refresh() override
Refreshes the visual appearance based on the current input state.
bool isPlaying() const
Returns true if audio is currently playing.
void setVolume(float vol) override
Sets the audio playback volume to vol (0.0–1.0).
virtual void applyVolume()=0
Applies the current m_volume to the hardware backend.
AudioOutputElement(ElementType type, QGraphicsItem *parent=nullptr, float initialVolume=kDefaultVolume)
virtual void stopAudio()=0
Stops hardware playback (called from stop() when m_hasOutputDevice is true).
virtual void applyMute()=0
Applies the current m_muted state to the hardware backend.
GraphicElement(ElementType type, QGraphicsItem *parent=nullptr)
Constructs a graphic element of the given type, fetching all properties from the metadata registry.
void setLabelAnchor(const QPointF &pos)
void setPixmap(const QString &pixmapPath)
Loads and applies the pixmap located at pixmapPath.
bool isValid()
Returns true if the element is fully initialised and connected correctly.
int type() const override
Returns the custom type identifier for this item.
const QVector< InputPort * > & inputs() const
Returns a const reference to the vector of all input ports.