Initial commit.

This commit is contained in:
Jonas Kvinge
2018-02-27 18:06:05 +01:00
parent 85d9664df7
commit b2b1ba7abe
1393 changed files with 177311 additions and 1 deletions

349
src/equalizer/equalizer.cpp Normal file
View File

@@ -0,0 +1,349 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "equalizer.h"
#include "ui_equalizer.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QSettings>
#include <QShortcut>
#include <QtDebug>
#include "core/iconloader.h"
#include "core/logging.h"
#include "equalizerslider.h"
const char *Equalizer::kGainText[] = { "60", "170", "310", "600", "1k", "3k", "6k", "12k", "14k", "16k" };
const char *Equalizer::kSettingsGroup = "Equalizer";
Equalizer::Equalizer(QWidget *parent)
: QDialog(parent), ui_(new Ui_Equalizer), loading_(false) {
//qLog(Debug) << __PRETTY_FUNCTION__;
ui_->setupUi(this);
// Icons
ui_->preset_del->setIcon(IconLoader::Load("list-remove"));
ui_->preset_save->setIcon(IconLoader::Load("document-save"));
preamp_ = AddSlider(tr("Pre-amp"));
QFrame* line = new QFrame(ui_->slider_container);
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
ui_->slider_container->layout()->addWidget(line);
for (int i = 0; i < kBands; ++i) gain_[i] = AddSlider(kGainText[i]);
// Must be done before the signals are connected
ReloadSettings();
connect(ui_->enable, SIGNAL(toggled(bool)), SIGNAL(EnabledChanged(bool)));
connect(ui_->enable, SIGNAL(toggled(bool)), ui_->slider_container, SLOT(setEnabled(bool)));
connect(ui_->enable, SIGNAL(toggled(bool)), SLOT(Save()));
connect(ui_->preset, SIGNAL(currentIndexChanged(int)), SLOT(PresetChanged(int)));
connect(ui_->preset_save, SIGNAL(clicked()), SLOT(SavePreset()));
connect(ui_->preset_del, SIGNAL(clicked()), SLOT(DelPreset()));
connect(ui_->balance_slider, SIGNAL(valueChanged(int)), SLOT(StereoSliderChanged(int)));
QShortcut* close = new QShortcut(QKeySequence::Close, this);
connect(close, SIGNAL(activated()), SLOT(close()));
}
Equalizer::~Equalizer() {
delete ui_;
}
void Equalizer::ReloadSettings() {
//qLog(Debug) << __PRETTY_FUNCTION__;
QSettings s;
s.beginGroup(kSettingsGroup);
presets_.clear();
ui_->preset->clear();
// Load presets
int count = s.beginReadArray("presets");
for (int i = 0; i < count; ++i) {
s.setArrayIndex(i);
AddPreset(s.value("name").toString(), s.value("params").value<Equalizer::Params>());
}
s.endArray();
if (count == 0) LoadDefaultPresets();
// Selected preset
QString selected_preset = s.value("selected_preset", "Custom").toString();
QString selected_preset_display_name = QString(tr(qPrintable(selected_preset)));
int selected_index = ui_->preset->findText(selected_preset_display_name);
if (selected_index != -1) ui_->preset->setCurrentIndex(selected_index);
// Enabled?
ui_->enable->setChecked(s.value("enabled", false).toBool());
ui_->slider_container->setEnabled(ui_->enable->isChecked());
int stereo_balance = s.value("stereo_balance", 0).toInt();
ui_->balance_slider->setValue(stereo_balance);
StereoSliderChanged(stereo_balance);
PresetChanged(selected_preset);
}
void Equalizer::LoadDefaultPresets() {
//qLog(Debug) << __PRETTY_FUNCTION__;
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Custom"), Params(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Classical"), Params(0, 0, 0, 0, 0, 0, -40, -40, -40, -50));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Club"), Params(0, 0, 20, 30, 30, 30, 20, 0, 0, 0));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Dance"), Params(50, 35, 10, 0, 0, -30, -40, -40, 0, 0));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Full Bass"), Params(70, 70, 70, 40, 20, -45, -50, -55, -55, -55));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Full Treble"), Params(-50, -50, -50, -25, 15, 55, 80, 80, 80, 85));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Full Bass + Treble"), Params(35, 30, 0, -40, -25, 10, 45, 55, 60, 60));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Laptop/Headphones"), Params(25, 50, 25, -20, 0, -30, -40, -40, 0, 0));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Large Hall"), Params(50, 50, 30, 30, 0, -25, -25, -25, 0, 0));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Live"), Params(-25, 0, 20, 25, 30, 30, 20, 15, 15, 10));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Party"), Params(35, 35, 0, 0, 0, 0, 0, 0, 35, 35));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Pop"), Params(-10, 25, 35, 40, 25, -5, -15, -15, -10, -10));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Reggae"), Params(0, 0, -5, -30, 0, -35, -35, 0, 0, 0));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Rock"), Params(40, 25, -30, -40, -20, 20, 45, 55, 55, 55));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Soft"), Params(25, 10, -5, -15, -5, 20, 45, 50, 55, 60));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Ska"), Params(-15, -25, -25, -5, 20, 30, 45, 50, 55, 50));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Soft Rock"), Params(20, 20, 10, -5, -25, -30, -20, -5, 15, 45));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Techno"), Params(40, 30, 0, -30, -25, 0, 40, 50, 50, 45));
AddPreset(QT_TRANSLATE_NOOP("Equalizer", "Zero"), Params(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
}
void Equalizer::AddPreset(const QString& name, const Params& params) {
QString name_displayed = tr(qPrintable(name));
presets_[name] = params;
if (ui_->preset->findText(name_displayed) == -1) {
ui_->preset->addItem(name_displayed, // name to display (translated)
QVariant(name) // original name
);
}
}
void Equalizer::PresetChanged(int index) {
PresetChanged(ui_->preset->itemData(index).toString());
}
void Equalizer::PresetChanged(const QString& name) {
if (presets_.contains(last_preset_)) {
if (presets_[last_preset_] != current_params()) {
SaveCurrentPreset();
}
}
last_preset_ = name;
Params& p = presets_[name];
loading_ = true;
preamp_->set_value(p.preamp);
for (int i = 0; i < kBands; ++i) gain_[i]->set_value(p.gain[i]);
loading_ = false;
ParametersChanged();
Save();
}
void Equalizer::SavePreset() {
QString name = SaveCurrentPreset();
if (!name.isEmpty()) {
last_preset_ = name;
ui_->preset->setCurrentIndex(ui_->preset->findText(tr(qPrintable(name))));
}
}
QString Equalizer::SaveCurrentPreset() {
QString name = QInputDialog::getText(this, tr("Save preset"), tr("Name"), QLineEdit::Normal, tr(qPrintable(last_preset_)));;
if (name.isEmpty())
return QString();
AddPreset(name, current_params());
Save();
return name;
}
void Equalizer::DelPreset() {
QString name = ui_->preset->itemData(ui_->preset->currentIndex()).toString();
QString name_displayed = ui_->preset->currentText();
if (!presets_.contains(name) || name.isEmpty()) return;
int ret = QMessageBox::question(
this, tr("Delete preset"),
tr("Are you sure you want to delete the \"%1\" preset?").arg(name_displayed),
QMessageBox::Yes, QMessageBox::No);
if (ret == QMessageBox::No) return;
presets_.remove(name);
ui_->preset->removeItem(ui_->preset->currentIndex());
Save();
}
EqualizerSlider* Equalizer::AddSlider(const QString &label) {
EqualizerSlider* ret = new EqualizerSlider(label, ui_->slider_container);
ui_->slider_container->layout()->addWidget(ret);
connect(ret, SIGNAL(ValueChanged(int)), SLOT(ParametersChanged()));
return ret;
}
bool Equalizer::is_enabled() const {
return ui_->enable->isChecked();
}
int Equalizer::preamp_value() const {
return preamp_->value();
}
QList<int> Equalizer::gain_values() const {
QList<int> ret;
for (int i = 0; i < kBands; ++i) {
ret << gain_[i]->value();
}
return ret;
}
Equalizer::Params Equalizer::current_params() const {
QList<int> gains = gain_values();
Params ret;
ret.preamp = preamp_value();
std::copy(gains.begin(), gains.end(), ret.gain);
return ret;
}
float Equalizer::stereo_balance() const {
return qBound(-1.0f, ui_->balance_slider->value() / 100.0f, 1.0f);
}
void Equalizer::ParametersChanged() {
if (loading_) return;
emit ParametersChanged(preamp_value(), gain_values());
}
void Equalizer::Save() {
QSettings s;
s.beginGroup(kSettingsGroup);
// Presets
s.beginWriteArray("presets", presets_.count());
int i = 0;
for (const QString& name : presets_.keys()) {
s.setArrayIndex(i++);
s.setValue("name", name);
s.setValue("params", QVariant::fromValue(presets_[name]));
}
s.endArray();
// Selected preset
s.setValue("selected_preset", ui_->preset->itemData(ui_->preset->currentIndex()).toString());
// Enabled?
s.setValue("enabled", ui_->enable->isChecked());
s.setValue("stereo_balance", ui_->balance_slider->value());
}
void Equalizer::closeEvent(QCloseEvent* e) {
QString name = ui_->preset->currentText();
if (!presets_.contains(name)) return;
if (presets_[name] == current_params()) return;
SavePreset();
}
Equalizer::Params::Params() : preamp(0) {
for (int i = 0; i < Equalizer::kBands; ++i) gain[i] = 0;
}
Equalizer::Params::Params(int g0, int g1, int g2, int g3, int g4, int g5, int g6, int g7, int g8, int g9, int pre)
: preamp(pre) {
gain[0] = g0;
gain[1] = g1;
gain[2] = g2;
gain[3] = g3;
gain[4] = g4;
gain[5] = g5;
gain[6] = g6;
gain[7] = g7;
gain[8] = g8;
gain[9] = g9;
}
bool Equalizer::Params::operator ==(const Equalizer::Params& other) const {
if (preamp != other.preamp) return false;
for (int i = 0; i < Equalizer::kBands; ++i) {
if (gain[i] != other.gain[i]) return false;
}
return true;
}
bool Equalizer::Params::operator !=(const Equalizer::Params& other) const {
return ! (*this == other);
}
void Equalizer::StereoSliderChanged(int value) {
emit StereoBalanceChanged(stereo_balance());
Save();
}
QDataStream &operator<<(QDataStream& s, const Equalizer::Params& p) {
s << p.preamp;
for (int i = 0; i < Equalizer::kBands; ++i) s << p.gain[i];
return s;
}
QDataStream &operator>>(QDataStream& s, Equalizer::Params& p) {
s >> p.preamp;
for (int i = 0; i < Equalizer::kBands; ++i) s >> p.gain[i];
return s;
}

101
src/equalizer/equalizer.h Normal file
View File

@@ -0,0 +1,101 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef EQUALIZER_H
#define EQUALIZER_H
#include "config.h"
#include <QDialog>
#include <QMetaType>
#include <QMap>
class EqualizerSlider;
class Ui_Equalizer;
class Equalizer : public QDialog {
Q_OBJECT
public:
Equalizer(QWidget* parent = nullptr);
~Equalizer();
static const int kBands = 10;
static const char *kGainText[kBands];
static const char *kSettingsGroup;
struct Params {
Params();
Params(int g0, int g1, int g2, int g3, int g4, int g5, int g6, int g7, int g8, int g9, int pre = 0);
bool operator ==(const Params &other) const;
bool operator !=(const Params &other) const;
int preamp;
int gain[kBands];
};
bool is_enabled() const;
int preamp_value() const;
QList<int> gain_values() const;
Params current_params() const;
float stereo_balance() const;
signals:
void EnabledChanged(bool enabled);
void ParametersChanged(int preamp, const QList<int> &band_gains);
void StereoBalanceChanged(float balance);
protected:
void closeEvent(QCloseEvent *);
private slots:
void ParametersChanged();
void PresetChanged(const QString &name);
void PresetChanged(int index);
void SavePreset();
void DelPreset();
void Save();
void StereoSliderChanged(int value);
private:
EqualizerSlider *AddSlider(const QString &label);
void LoadDefaultPresets();
void AddPreset(const QString &name, const Params &params);
void ReloadSettings();
QString SaveCurrentPreset();
private:
Ui_Equalizer *ui_;
bool loading_;
QString last_preset_;
EqualizerSlider *preamp_;
EqualizerSlider *gain_[kBands];
QMap<QString, Params> presets_;
};
Q_DECLARE_METATYPE(Equalizer::Params);
QDataStream &operator<<(QDataStream &s, const Equalizer::Params &p);
QDataStream &operator>>(QDataStream &s, Equalizer::Params &p);
#endif // EQUALIZER_H

163
src/equalizer/equalizer.ui Normal file
View File

@@ -0,0 +1,163 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Equalizer</class>
<widget class="QDialog" name="Equalizer">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>435</width>
<height>265</height>
</rect>
</property>
<property name="windowTitle">
<string>Equalizer</string>
</property>
<property name="windowIcon">
<iconset resource="../../data/data.qrc">
<normaloff>:/icons/64x64/strawberry.png</normaloff>:/icons/64x64/strawberry.png</iconset>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Preset:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="preset">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="preset_save">
<property name="toolTip">
<string>Save preset</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="preset_del">
<property name="toolTip">
<string>Delete preset</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="enable">
<property name="text">
<string>Enable equalizer</string>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="slider_container" native="true">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2"/>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="slider_label_layout">
<item>
<widget class="QLabel">
<property name="text">
<string>Left</string>
</property>
<property name="alignment">
<enum>Qt::AlignLeft | Qt::AlignBottom</enum>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
</widget>
</item>
<item>
<widget class="QLabel">
<property name="text">
<string>Balance</string>
</property>
<property name="alignment">
<enum>Qt::AlignCenter</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel">
<property name="text">
<string>Right</string>
</property>
<property name="alignment">
<enum>Qt::AlignRight | Qt::AlignBottom</enum>
</property>
<property name="font">
<font>
<pointsize>11</pointsize>
</font>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QSlider" name="balance_slider">
<property name="maximum">
<number>100</number>
</property>
<property name="minimum">
<number>-100</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksBelow</enum>
</property>
<property name="tickInterval">
<number>10</number>
</property>
<property name="singleStep">
<number>10</number>
</property>
<property name="pageStep">
<number>100</number>
</property>
</widget>
</item>
</layout>
</widget>
<tabstops>
<tabstop>preset</tabstop>
<tabstop>preset_save</tabstop>
</tabstops>
<resources>
<include location="../../data/data.qrc"/>
</resources>
<connections/>
</ui>

View File

@@ -0,0 +1,47 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "config.h"
#include "equalizerslider.h"
#include "ui_equalizerslider.h"
EqualizerSlider::EqualizerSlider(const QString &label, QWidget *parent)
: QWidget(parent),
ui_(new Ui_EqualizerSlider)
{
ui_->setupUi(this);
ui_->label->setText(label);
connect(ui_->slider, SIGNAL(valueChanged(int)), SIGNAL(ValueChanged(int)));
}
EqualizerSlider::~EqualizerSlider() {
delete ui_;
}
int EqualizerSlider::value() const {
return ui_->slider->value();
}
void EqualizerSlider::set_value(int value) {
ui_->slider->setValue(value);
}

View File

@@ -0,0 +1,50 @@
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Strawberry is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef EQUALISERSLIDER_H
#define EQUALISERSLIDER_H
#include "config.h"
#include <QWidget>
class Ui_EqualizerSlider;
// Contains the slider and the label
class EqualizerSlider : public QWidget {
Q_OBJECT
public:
EqualizerSlider(const QString &label, QWidget *parent = nullptr);
~EqualizerSlider();
int value() const;
void set_value(int value);
signals:
void ValueChanged(int value);
private:
Ui_EqualizerSlider *ui_;
};
#endif // EQUALISERSLIDER_H

View File

@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>EqualizerSlider</class>
<widget class="QWidget" name="EqualizerSlider">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>33</width>
<height>224</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>100</height>
</size>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="StickySlider" name="slider">
<property name="minimum">
<number>-100</number>
</property>
<property name="maximum">
<number>100</number>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sticky_center" stdset="0">
<number>0</number>
</property>
<property name="sticky_threshold" stdset="0">
<number>10</number>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label">
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>StickySlider</class>
<extends>QSlider</extends>
<header>widgets/stickyslider.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>