20 .type = ElementType::Buzzer,
21 .group = ElementGroup::Output,
24 .canChangeAppearance =
true,
28 .rotatesGraphic =
false,
30 static_assert(
validate(constraints));
35 meta.pixmapPath = []{
return QStringLiteral(
":/Components/Output/Buzzer/BuzzerOff.svg"); };
36 meta.titleText = QT_TRANSLATE_NOOP(
"Buzzer",
"BUZZER");
37 meta.translatedName = QT_TRANSLATE_NOOP(
"Buzzer",
"Buzzer");
38 meta.trContext =
"Buzzer";
39 meta.defaultAppearances = QStringList({
40 ":/Components/Output/Buzzer/BuzzerOff.svg",
41 ":/Components/Output/Buzzer/BuzzerOn.svg",
58 m_generator->setFrequency(m_frequency);
60 m_sink->setVolume(
static_cast<qreal
>(
m_volume));
76 if (!std::isfinite(freq) || freq <= 0.0) {
86 m_generator->setFrequency(freq);
92 m_sink->start(m_generator);
107 m_generator->start();
108 m_sink->start(m_generator);
114 m_generator->close();
120 m_sink->setVolume(
static_cast<qreal
>(
m_volume));
131 static const QHash<QString, int> map = {
132 {
"C6", 1047}, {
"D6", 1175}, {
"E6", 1319}, {
"F6", 1397},
133 {
"G6", 1568}, {
"A6", 1760}, {
"B6", 1976}, {
"C7", 2093},
137 {
"A7", 3520}, {
"B7", 3951},
140 return map.value(note, 1047);
149 QMap<QString, QVariant> map;
153 map.insert(
"frequency", m_frequency);
156 map.insert(
"volume",
static_cast<double>(
m_volume));
180 if (map.contains(
"frequency")) {
182 }
else if (map.contains(
"note")) {
184 setAudio(map.value(
"note").toString());
187 if (map.contains(
"volume")) {
189 const float vol = map.value(
"volume").toFloat(&ok);
Graphic element for the buzzer tone output.
Singleton factory for all circuit element types.
Self-registering element trait template and compile-time constraint validation.
ElementMetadata metadataFromConstraints(const ElementConstraints &c)
Converts ElementConstraints to an ElementMetadata with all constraint-derived fields set.
constexpr bool validate(const ElementConstraints &c)
Validates element constraints at compile time.
Enums::ElementType ElementType
Deserialization/serialization context structs passed through load()/save() call chains.
Circuit and waveform file serialization/deserialization utilities.
QIODevice that generates a continuous sine-wave tone for audio playback.
Named version predicates for file-format compatibility checks.
void setVolume(float vol) override
Sets the audio playback volume to vol (0.0–1.0).
AudioOutputElement(ElementType type, QGraphicsItem *parent=nullptr, float initialVolume=kDefaultVolume)
double frequency() const override
Returns the tone frequency in Hz.
void applyMute() override
Applies the current m_muted state to the hardware backend.
void stopAudio() override
Stops hardware playback (called from stop() when m_hasOutputDevice is true).
Buzzer(QGraphicsItem *parent=nullptr)
Constructs a Buzzer element (default: 1047 Hz / C6).
void startAudio() override
Starts hardware playback (called from play() when m_hasOutputDevice is true).
void setFrequency(double freq) override
Sets the tone frequency in Hz.
void save(QDataStream &stream, SerializationOptions options) const override
void setAudio(const QString ¬e) override
Sets the tone by note name (e.g. "C6") for backward compatibility.
static constexpr float kDefaultVolume
void load(QDataStream &stream, SerializationContext &context) override
static int noteToFrequency(const QString ¬e)
Maps a note name (e.g. "C6") to its frequency in Hz. Returns 1047 for unknown notes.
static constexpr double kDefaultFrequency
void applyVolume() override
Applies the current m_volume to the hardware backend.
static void registerCreator(ElementType type, std::function< GraphicElement *()> creator)
Registers a creator lambda for type, used by buildElement().
virtual void load(QDataStream &stream, SerializationContext &context)
Loads the graphic element through a binary data stream.
virtual void save(QDataStream &stream, SerializationOptions options) const
static QString readBoundedString(QDataStream &stream)
Reads a QString from stream, refusing to allocate more bytes than remain in the stream (prevents OOM ...
static QMap< QString, QVariant > readBoundedMetadata(QDataStream &stream)
Reads the file-level metadata QMap<QString,QVariant> from stream without calling QList::reserve() wit...
static QAudioFormat format()
Returns the QAudioFormat matching this generator's output.
bool hasQMapFormat(const QVersionNumber &v)
V4.1: Format changed from flat binary to keyed QMap; port serial IDs; rotation fix.
bool hasAudio(const QVersionNumber &v)
V2.4: Audio element support (Buzzer, AudioBox).
Compile-time-validatable subset of ElementMetadata.
static const bool registered
static ElementMetadata metadata()
static constexpr ElementConstraints constraints
Self-registering element information trait.
Bundles all per-deserialization state so that load() overrides receive it through one explicit parame...
QVersionNumber version
File-format version read from the stream header.
Options passed to GraphicElement::save() (and friends); the save-side counterpart of SerializationCon...
SerializationPurpose purpose
What this serialization is for; see SerializationPurpose. Mandatory, no default.