Player12
An open source football management simulator
Loading...
Searching...
No Matches
match_audio.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <array>
12#include <cstdint>
13#include <memory>
14#include <span>
15#include <vector>
16
17#include "audio/audio_mixer.h"
18#include "model/match_events.h"
19
20class MatchEngine;
21struct Settings;
22
23namespace Audio
24{
25class AudioDevice;
26}
27
33{
34 float ballX = 0.5f;
36 float ballSpeed = 0.0f;
37 bool ballIsShot = false;
39 bool ballLoose = false;
42 bool homeInPossession = false;
43 bool awayInPossession = false;
44 float matchMinute = 0.0f;
45 int period = 1;
46 int homeScore = 0;
47 int awayScore = 0;
49 bool stopped = false;
51 float playbackSpeed = 1.0f;
52 bool paused = false;
57 bool skipped = false;
58};
59
62 float playbackSpeed, bool paused,
63 bool skipped);
64
73{
74 public:
76 void collect(std::span<const MatchEvent> events,
77 std::vector<const MatchEvent*>& fresh);
79 void skipAll(std::span<const MatchEvent> events);
80
81 private:
82 static constexpr std::size_t SAME_TIME_CAPACITY = 16;
83
85 bool isBefore(const MatchEvent& event) const;
86 bool isNew(const MatchEvent& event, std::uint64_t fingerprint) const;
87 void mark(const MatchEvent& event, std::uint64_t fingerprint);
88
89 int period = 0;
90 float minute = -1.0f;
92 std::array<std::uint64_t, SAME_TIME_CAPACITY> sameTime{};
93 std::size_t sameTimeCount = 0;
94};
95
106{
107 public:
109 explicit MatchAudio(bool openDevice = true);
110 ~MatchAudio();
111 MatchAudio(const MatchAudio&) = delete;
112 MatchAudio& operator=(const MatchAudio&) = delete;
113
115 static Audio::BusLevels levelsFrom(const Settings& settings);
116 void setLevels(const Audio::BusLevels& levels);
117
118 void update(float wallSeconds, const MatchAudioFrame& frame,
119 std::span<const MatchEvent> events);
120
123 bool deviceOpen();
124
125 private:
127 enum class Priority : std::uint8_t
128 {
129 MINOR,
130 MAJOR,
131 KEY
132 };
133
135 {
136 bool active = false;
137 bool home = false;
138 float xg = 0.0f;
139 int period = 1;
140 float minute = 0.0f;
141 };
142
143 void react(const MatchEvent& event);
144 void reactToGoal(const MatchEvent& event);
145 void reactToMiss(float x);
147 void detectKicks(const MatchAudioFrame& frame, bool replaying);
148 void updateCrowd(float wallSeconds, const MatchAudioFrame& frame);
149 bool allowed(Priority priority) const;
150 void play(Priority priority, Audio::SoundCommand command);
151 void whistle(Priority priority, Audio::WhistlePattern pattern, float x);
152
153 std::unique_ptr<Audio::Mixer> mixerInstance =
154 std::make_unique<Audio::Mixer>();
156 std::unique_ptr<Audio::AudioDevice> device;
158 std::vector<const MatchEvent*> freshEvents;
159
160 float playbackSpeed = 1.0f;
161 float previousBallSpeed = 0.0f;
162 bool previousShot = false;
163 bool previousLoose = false;
165 bool haveBallHistory = false;
166 float kickCooldown = 0.0f;
168 float sinceReaction = 10.0f;
171 float tension = 0.0f;
175 float latestMinute = -1.0f;
176};
Definition audio_mixer.h:40
Definition match_audio.h:106
float previousBallSpeed
Definition match_audio.h:161
std::unique_ptr< Audio::Mixer > mixerInstance
Definition match_audio.h:153
PlayerID previousKicker
Definition match_audio.h:164
bool haveBallHistory
Definition match_audio.h:165
MatchAudio(const MatchAudio &)=delete
void whistle(Priority priority, Audio::WhistlePattern pattern, float x)
Definition match_audio.cpp:196
void react(const MatchEvent &event)
Definition match_audio.cpp:363
bool deviceOpen()
Definition match_audio.cpp:160
~MatchAudio()
Definition match_audio.cpp:154
void setLevels(const Audio::BusLevels &levels)
Definition match_audio.cpp:168
void detectKicks(const MatchAudioFrame &frame, bool replaying)
Definition match_audio.cpp:246
Priority lastReaction
Definition match_audio.h:169
bool previousLoose
Definition match_audio.h:163
static Audio::BusLevels levelsFrom(const Settings &settings)
Definition match_audio.cpp:162
void play(Priority priority, Audio::SoundCommand command)
Definition match_audio.cpp:181
void reactToMiss(float x)
Definition match_audio.cpp:353
MatchEventCursor cursor
Definition match_audio.h:157
void update(float wallSeconds, const MatchAudioFrame &frame, std::span< const MatchEvent > events)
Definition match_audio.cpp:205
Audio::Mixer & mixer()
Definition match_audio.h:121
MatchAudio & operator=(const MatchAudio &)=delete
void reactToGoal(const MatchEvent &event)
Definition match_audio.cpp:304
int latestPeriod
Definition match_audio.h:174
std::unique_ptr< Audio::AudioDevice > device
Definition match_audio.h:156
float sinceReaction
Definition match_audio.h:168
void updateCrowd(float wallSeconds, const MatchAudioFrame &frame)
Definition match_audio.cpp:282
float playbackSpeed
Definition match_audio.h:160
bool allowed(Priority priority) const
Definition match_audio.cpp:173
std::vector< const MatchEvent * > freshEvents
Definition match_audio.h:158
float tension
Definition match_audio.h:171
Priority
Definition match_audio.h:128
PendingShot pendingShot
Definition match_audio.h:172
bool previousShot
Definition match_audio.h:162
float kickCooldown
Definition match_audio.h:166
float latestMinute
Definition match_audio.h:175
Definition match_engine.h:517
Definition match_audio.h:73
float minute
Definition match_audio.h:90
int period
Definition match_audio.h:89
void collect(std::span< const MatchEvent > events, std::vector< const MatchEvent * > &fresh)
Definition match_audio.cpp:90
std::array< std::uint64_t, SAME_TIME_CAPACITY > sameTime
Definition match_audio.h:92
void mark(const MatchEvent &event, std::uint64_t fingerprint)
Definition match_audio.cpp:78
void skipAll(std::span< const MatchEvent > events)
Definition match_audio.cpp:107
bool isNew(const MatchEvent &event, std::uint64_t fingerprint) const
Definition match_audio.cpp:69
std::size_t sameTimeCount
Definition match_audio.h:93
static constexpr std::size_t SAME_TIME_CAPACITY
Definition match_audio.h:82
bool isBefore(const MatchEvent &event) const
Definition match_audio.cpp:63
MatchAudioFrame captureMatchAudioFrame(const MatchEngine &engine, float playbackSpeed, bool paused, bool skipped)
Definition match_audio.cpp:120
Definition audio_device.cpp:17
WhistlePattern
Definition synth.h:163
Definition audio_mixer.h:22
Definition synth.h:175
Definition match_audio.h:33
float playbackSpeed
Definition match_audio.h:51
int period
Definition match_audio.h:45
bool awayInPossession
Definition match_audio.h:43
float matchMinute
Definition match_audio.h:44
float ballX
Definition match_audio.h:34
int awayScore
Definition match_audio.h:47
int homeScore
Definition match_audio.h:46
bool paused
Definition match_audio.h:52
bool ballIsShot
Definition match_audio.h:37
bool stopped
Definition match_audio.h:49
bool homeInPossession
Definition match_audio.h:42
bool skipped
Definition match_audio.h:57
bool ballLoose
Definition match_audio.h:39
float ballSpeed
Definition match_audio.h:36
PlayerID kickerId
Definition match_audio.h:41
Definition match_audio.h:135
int period
Definition match_audio.h:139
float minute
Definition match_audio.h:140
bool home
Definition match_audio.h:137
float xg
Definition match_audio.h:138
bool active
Definition match_audio.h:136
Definition match_events.h:93
Stores the game's configuration settings.
Definition settings_manager.h:26
std::string pattern
Definition transfer_terms_editor.cpp:75
uint32_t PlayerID
Type alias for Player identifiers.
Definition types.h:21