mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-09-21 09:05:10 -07:00
Some checks are pending
CodeQL / Analyze (cpp) (push) Waiting to run
CodeQL / Analyze (actions) (push) Waiting to run
Build Desktop / Configure (push) Waiting to run
Build Desktop / Debian 13 (push) Blocked by required conditions
Build Desktop / Debian 12 (push) Blocked by required conditions
Build Desktop / Fedora 44 (push) Blocked by required conditions
Build Desktop / Fedora 43 (push) Blocked by required conditions
Build Desktop / Servatrice_Debian 12 (push) Blocked by required conditions
Build Desktop / Ubuntu 26.04 (push) Blocked by required conditions
Build Desktop / Ubuntu 24.04 (push) Blocked by required conditions
Build Desktop / Arch (push) Blocked by required conditions
Build Desktop / macOS 13 Intel (push) Blocked by required conditions
Build Desktop / macOS 14 (push) Blocked by required conditions
Build Desktop / macOS 15 (push) Blocked by required conditions
Build Desktop / macOS 26 Debug (push) Blocked by required conditions
Build Desktop / Windows 10 (push) Blocked by required conditions
Build Docker / Servatrice (arm) (push) Waiting to run
Build Docker / Servatrice (x86) (push) Waiting to run
Build Docker / Publish multi-platform Servatrice image (push) Blocked by required conditions
* use capitalized app name * update urls * app description * Update main.cpp
162 lines
4.8 KiB
C++
162 lines
4.8 KiB
C++
#include "sound_engine.h"
|
|
|
|
#include "settings/cache_settings.h"
|
|
|
|
#include <QApplication>
|
|
#include <QAudioOutput>
|
|
#include <QDir>
|
|
#include <QMediaPlayer>
|
|
#include <libcockatrice/settings/sound_settings.h>
|
|
|
|
#define DEFAULT_THEME_NAME "Default"
|
|
#define TEST_SOUND_FILENAME "player_join"
|
|
|
|
SoundEngine::SoundEngine(QObject *parent) : QObject(parent), audioOutput(nullptr), player(nullptr)
|
|
{
|
|
ensureThemeDirectoryExists();
|
|
connect(&SettingsCache::instance().sound(), &SoundSettings::soundThemeChanged, this,
|
|
&SoundEngine::themeChangedSlot);
|
|
connect(&SettingsCache::instance().sound(), &SoundSettings::soundEnabledChanged, this,
|
|
&SoundEngine::soundEnabledChanged);
|
|
|
|
soundEnabledChanged();
|
|
themeChangedSlot();
|
|
}
|
|
|
|
SoundEngine::~SoundEngine()
|
|
{
|
|
if (player) {
|
|
player->deleteLater();
|
|
player = nullptr;
|
|
}
|
|
if (audioOutput) {
|
|
audioOutput->deleteLater();
|
|
audioOutput = nullptr;
|
|
}
|
|
}
|
|
|
|
void SoundEngine::soundEnabledChanged()
|
|
{
|
|
if (SettingsCache::instance().sound().getSoundEnabled()) {
|
|
qCInfo(SoundEngineLog) << "SoundEngine: enabling sound with" << audioData.size() << "sounds";
|
|
if (!player) {
|
|
player = new QMediaPlayer;
|
|
audioOutput = new QAudioOutput(player);
|
|
player->setAudioOutput(audioOutput);
|
|
}
|
|
} else {
|
|
qCInfo(SoundEngineLog) << "SoundEngine: disabling sound";
|
|
if (player) {
|
|
player->stop();
|
|
player->deleteLater();
|
|
player = nullptr;
|
|
}
|
|
if (audioOutput) {
|
|
audioOutput->deleteLater();
|
|
audioOutput = nullptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
void SoundEngine::playSound(const QString &fileName)
|
|
{
|
|
if (!player) {
|
|
return;
|
|
}
|
|
|
|
if (!audioData.contains(fileName)) {
|
|
return;
|
|
}
|
|
|
|
player->stop();
|
|
int volumeSliderValue = SettingsCache::instance().sound().getMasterVolume();
|
|
player->audioOutput()->setVolume(qreal(volumeSliderValue) / 100);
|
|
player->setSource(QUrl::fromLocalFile(audioData[fileName]));
|
|
player->play();
|
|
}
|
|
|
|
void SoundEngine::testSound()
|
|
{
|
|
playSound(TEST_SOUND_FILENAME);
|
|
}
|
|
|
|
void SoundEngine::ensureThemeDirectoryExists()
|
|
{
|
|
if (SettingsCache::instance().sound().getSoundThemeName().isEmpty() ||
|
|
!getAvailableThemes().contains(SettingsCache::instance().sound().getSoundThemeName())) {
|
|
qCInfo(SoundEngineLog) << "Sounds theme name not set, setting default value";
|
|
SettingsCache::instance().sound().setSoundThemeName(DEFAULT_THEME_NAME);
|
|
}
|
|
}
|
|
|
|
QStringMap &SoundEngine::getAvailableThemes()
|
|
{
|
|
QDir dir;
|
|
availableThemes.clear();
|
|
|
|
// Load themes from user profile dir
|
|
|
|
dir.setPath(SettingsCache::instance().getDataPath() + "/sounds");
|
|
|
|
for (const QString &themeName : dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
|
|
if (!availableThemes.contains(themeName)) {
|
|
availableThemes.insert(themeName, dir.absoluteFilePath(themeName));
|
|
}
|
|
}
|
|
|
|
// Load themes from Cockatrice system dir
|
|
dir.setPath(qApp->applicationDirPath() +
|
|
#ifdef Q_OS_MAC
|
|
"/../Resources/sounds"
|
|
#elif defined(Q_OS_WIN)
|
|
"/sounds"
|
|
#else // linux
|
|
"/../share/cockatrice/sounds"
|
|
#endif
|
|
);
|
|
|
|
for (const QString &themeName : dir.entryList(QDir::AllDirs | QDir::NoDotAndDotDot, QDir::Name)) {
|
|
if (!availableThemes.contains(themeName)) {
|
|
availableThemes.insert(themeName, dir.absoluteFilePath(themeName));
|
|
}
|
|
}
|
|
|
|
return availableThemes;
|
|
}
|
|
|
|
void SoundEngine::themeChangedSlot()
|
|
{
|
|
QString themeName = SettingsCache::instance().sound().getSoundThemeName();
|
|
qCInfo(SoundEngineLog) << "Sound theme changed:" << themeName;
|
|
|
|
QDir dir = getAvailableThemes().value(themeName);
|
|
|
|
audioData.clear();
|
|
|
|
static const QStringList extensions = {".wav", ".mp3", ".ogg"};
|
|
static const QStringList fileNames = {
|
|
// Phases
|
|
"untap_step", "upkeep_step", "draw_step", "main_1", "start_combat", "attack_step", "block_step", "damage_step",
|
|
"end_combat", "main_2", "end_step",
|
|
// Game Actions
|
|
"draw_card", "play_card", "tap_card", "untap_card", "shuffle", "roll_dice", "life_change",
|
|
// Player
|
|
"player_join", "player_leave", "player_disconnect", "player_reconnect", "player_concede",
|
|
// Spectator
|
|
"spectator_join", "spectator_leave",
|
|
// Buddy
|
|
"buddy_join", "buddy_leave",
|
|
// Chat & UI
|
|
"chat_mention", "all_mention", "private_message"};
|
|
|
|
for (const QString &extension : extensions) {
|
|
for (const QString &name : fileNames) {
|
|
QFile file(dir.filePath(name + extension));
|
|
if (file.exists()) {
|
|
audioData.insert(name, file.fileName());
|
|
}
|
|
}
|
|
}
|
|
|
|
soundEnabledChanged();
|
|
}
|