Unify playback speed settings serialization.

This commit is contained in:
John Preston 2023-03-15 09:15:40 +04:00
parent f9066235ca
commit 4351baffb3
4 changed files with 81 additions and 43 deletions

View file

@ -15,6 +15,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/platform/base_platform_info.h" #include "base/platform/base_platform_info.h"
#include "webrtc/webrtc_create_adm.h" #include "webrtc/webrtc_create_adm.h"
#include "media/player/media_player_instance.h" #include "media/player/media_player_instance.h"
#include "media/audio/media_audio.h"
#include "ui/gl/gl_detection.h" #include "ui/gl/gl_detection.h"
#include "calls/group/calls_group_common.h" #include "calls/group/calls_group_common.h"
#include "spellcheck/spellcheck_types.h" #include "spellcheck/spellcheck_types.h"
@ -118,6 +119,10 @@ void LogPosition(const WindowPosition &position, const QString &name) {
return position; return position;
} }
float64 Settings::PlaybackSpeed::Default() {
return Media::Audio::kSpedUpDefault;
}
Settings::Settings() Settings::Settings()
: _sendSubmitWay(Ui::InputSubmitSettings::Enter) : _sendSubmitWay(Ui::InputSubmitSettings::Enter)
, _floatPlayerColumn(Window::Column::Second) , _floatPlayerColumn(Window::Column::Second)
@ -215,7 +220,7 @@ QByteArray Settings::serialize() const {
<< qint32(_askDownloadPath ? 1 : 0) << qint32(_askDownloadPath ? 1 : 0)
<< _downloadPath.current() << _downloadPath.current()
<< _downloadPathBookmark << _downloadPathBookmark
<< qint32(_nonDefaultVoicePlaybackSpeed ? 1 : 0) << qint32(1)
<< qint32(_soundNotify ? 1 : 0) << qint32(_soundNotify ? 1 : 0)
<< qint32(_desktopNotify ? 1 : 0) << qint32(_desktopNotify ? 1 : 0)
<< qint32(_flashBounceNotify ? 1 : 0) << qint32(_flashBounceNotify ? 1 : 0)
@ -247,7 +252,7 @@ QByteArray Settings::serialize() const {
<< qint32(_suggestEmoji ? 1 : 0) << qint32(_suggestEmoji ? 1 : 0)
<< qint32(_suggestStickersByEmoji ? 1 : 0) << qint32(_suggestStickersByEmoji ? 1 : 0)
<< qint32(_spellcheckerEnabled.current() ? 1 : 0) << qint32(_spellcheckerEnabled.current() ? 1 : 0)
<< qint32(SerializePlaybackSpeed(_videoPlaybackSpeed.current())) << qint32(SerializePlaybackSpeed(_videoPlaybackSpeed))
<< _videoPipGeometry << _videoPipGeometry
<< qint32(_dictionariesEnabled.current().size()); << qint32(_dictionariesEnabled.current().size());
for (const auto i : _dictionariesEnabled.current()) { for (const auto i : _dictionariesEnabled.current()) {
@ -295,7 +300,7 @@ QByteArray Settings::serialize() const {
<< qint32(_disableOpenGL ? 1 : 0) << qint32(_disableOpenGL ? 1 : 0)
<< _photoEditorBrush << _photoEditorBrush
<< qint32(_groupCallNoiseSuppression ? 1 : 0) << qint32(_groupCallNoiseSuppression ? 1 : 0)
<< qint32(_voicePlaybackSpeed * 100) << qint32(SerializePlaybackSpeed(_voicePlaybackSpeed))
<< qint32(_closeToTaskbar.current() ? 1 : 0) << qint32(_closeToTaskbar.current() ? 1 : 0)
<< _customDeviceModel.current() << _customDeviceModel.current()
<< qint32(_playerRepeatMode.current()) << qint32(_playerRepeatMode.current())
@ -354,7 +359,7 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
qint32 askDownloadPath = _askDownloadPath ? 1 : 0; qint32 askDownloadPath = _askDownloadPath ? 1 : 0;
QString downloadPath = _downloadPath.current(); QString downloadPath = _downloadPath.current();
QByteArray downloadPathBookmark = _downloadPathBookmark; QByteArray downloadPathBookmark = _downloadPathBookmark;
qint32 nonDefaultVoicePlaybackSpeed = _nonDefaultVoicePlaybackSpeed ? 1 : 0; qint32 nonDefaultVoicePlaybackSpeed = 1;
qint32 soundNotify = _soundNotify ? 1 : 0; qint32 soundNotify = _soundNotify ? 1 : 0;
qint32 desktopNotify = _desktopNotify ? 1 : 0; qint32 desktopNotify = _desktopNotify ? 1 : 0;
qint32 flashBounceNotify = _flashBounceNotify ? 1 : 0; qint32 flashBounceNotify = _flashBounceNotify ? 1 : 0;
@ -384,8 +389,8 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
qint32 suggestEmoji = _suggestEmoji ? 1 : 0; qint32 suggestEmoji = _suggestEmoji ? 1 : 0;
qint32 suggestStickersByEmoji = _suggestStickersByEmoji ? 1 : 0; qint32 suggestStickersByEmoji = _suggestStickersByEmoji ? 1 : 0;
qint32 spellcheckerEnabled = _spellcheckerEnabled.current() ? 1 : 0; qint32 spellcheckerEnabled = _spellcheckerEnabled.current() ? 1 : 0;
qint32 videoPlaybackSpeed = Core::Settings::SerializePlaybackSpeed(_videoPlaybackSpeed.current()); qint32 videoPlaybackSpeed = SerializePlaybackSpeed(_videoPlaybackSpeed);
qint32 voicePlaybackSpeed = _voicePlaybackSpeed * 100; qint32 voicePlaybackSpeed = SerializePlaybackSpeed(_voicePlaybackSpeed);
QByteArray videoPipGeometry = _videoPipGeometry; QByteArray videoPipGeometry = _videoPipGeometry;
qint32 dictionariesEnabledCount = 0; qint32 dictionariesEnabledCount = 0;
std::vector<int> dictionariesEnabled; std::vector<int> dictionariesEnabled;
@ -742,16 +747,9 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
_suggestStickersByEmoji = (suggestStickersByEmoji == 1); _suggestStickersByEmoji = (suggestStickersByEmoji == 1);
_spellcheckerEnabled = (spellcheckerEnabled == 1); _spellcheckerEnabled = (spellcheckerEnabled == 1);
_videoPlaybackSpeed = DeserializePlaybackSpeed(videoPlaybackSpeed); _videoPlaybackSpeed = DeserializePlaybackSpeed(videoPlaybackSpeed);
{ _voicePlaybackSpeed = DeserializePlaybackSpeed(voicePlaybackSpeed);
// Restore settings from 3.0.1 version. if (nonDefaultVoicePlaybackSpeed != 1) {
if (voicePlaybackSpeed == 100) { _voicePlaybackSpeed.enabled = false;
_nonDefaultVoicePlaybackSpeed = false;
_voicePlaybackSpeed = 2.0;
} else {
_nonDefaultVoicePlaybackSpeed =
(nonDefaultVoicePlaybackSpeed == 1);
_voicePlaybackSpeed = voicePlaybackSpeed / 100.;
}
} }
_videoPipGeometry = (videoPipGeometry); _videoPipGeometry = (videoPipGeometry);
_dictionariesEnabled = std::move(dictionariesEnabled); _dictionariesEnabled = std::move(dictionariesEnabled);
@ -1101,7 +1099,6 @@ void Settings::resetOnLastLogout() {
_downloadPath = QString(); _downloadPath = QString();
_downloadPathBookmark = QByteArray(); _downloadPathBookmark = QByteArray();
_nonDefaultVoicePlaybackSpeed = false;
_soundNotify = true; _soundNotify = true;
_desktopNotify = true; _desktopNotify = true;
_flashBounceNotify = true; _flashBounceNotify = true;
@ -1145,8 +1142,8 @@ void Settings::resetOnLastLogout() {
_suggestStickersByEmoji = true; _suggestStickersByEmoji = true;
_suggestAnimatedEmoji = true; _suggestAnimatedEmoji = true;
_spellcheckerEnabled = true; _spellcheckerEnabled = true;
_videoPlaybackSpeed = 1.; _videoPlaybackSpeed = PlaybackSpeed();
_voicePlaybackSpeed = 1.; _voicePlaybackSpeed = PlaybackSpeed();
//_videoPipGeometry = QByteArray(); //_videoPipGeometry = QByteArray();
_dictionariesEnabled = std::vector<int>(); _dictionariesEnabled = std::vector<int>();
_autoDownloadDictionaries = true; _autoDownloadDictionaries = true;
@ -1180,6 +1177,34 @@ float64 Settings::DefaultDialogsWidthRatio() {
: kDefaultDialogsWidthRatio; : kDefaultDialogsWidthRatio;
} }
qint32 Settings::SerializePlaybackSpeed(PlaybackSpeed speed) {
using namespace Media::Audio;
const auto value = int(base::SafeRound(
std::clamp(speed.value, kSpeedMin, kSpeedMax) * 100));
return speed.enabled ? value : -value;
}
auto Settings::DeserializePlaybackSpeed(qint32 speed) -> PlaybackSpeed {
using namespace Media::Audio;
auto enabled = true;
const auto validate = [&](float64 result) {
return PlaybackSpeed{
.value = (result == 1.) ? kSpedUpDefault : result,
.enabled = enabled && (result != 1.),
};
};
if (speed >= 0 && speed < 10) {
// The old values in settings.
return validate((std::clamp(speed, 0, 6) + 2) / 4.);
} else if (speed < 0) {
speed = -speed;
enabled = false;
}
return validate(std::clamp(speed / 100., kSpeedMin, kSpeedMax));
}
void Settings::setTranslateButtonEnabled(bool value) { void Settings::setTranslateButtonEnabled(bool value) {
_translateButtonEnabled = value; _translateButtonEnabled = value;
} }

View file

@ -485,23 +485,37 @@ public:
return _autoDownloadDictionaries.changes(); return _autoDownloadDictionaries.changes();
} }
[[nodiscard]] float64 videoPlaybackSpeed() const { [[nodiscard]] float64 videoPlaybackSpeed(
return _videoPlaybackSpeed.current(); bool lastNonDefault = false) const {
return (_videoPlaybackSpeed.enabled || lastNonDefault)
? _videoPlaybackSpeed.value
: 1.;
} }
void setVideoPlaybackSpeed(float64 speed) { void setVideoPlaybackSpeed(float64 speed) {
_videoPlaybackSpeed = speed; if ((_videoPlaybackSpeed.enabled = (speed != 1.))) {
_videoPlaybackSpeed.value = speed;
}
} }
[[nodiscard]] float64 voicePlaybackSpeed( [[nodiscard]] float64 voicePlaybackSpeed(
bool lastNonDefault = false) const { bool lastNonDefault = false) const {
return (_nonDefaultVoicePlaybackSpeed || lastNonDefault) return (_voicePlaybackSpeed.enabled || lastNonDefault)
? _voicePlaybackSpeed ? _voicePlaybackSpeed.value
: 1.0; : 1.;
} }
void setVoicePlaybackSpeed(float64 speed) { void setVoicePlaybackSpeed(float64 speed) {
if ((_nonDefaultVoicePlaybackSpeed = (speed != 1.0))) { if ((_voicePlaybackSpeed.enabled = (speed != 1.0))) {
_voicePlaybackSpeed = speed; _voicePlaybackSpeed.value = speed;
} }
} }
// For legacy values read-write outside of Settings.
[[nodiscard]] qint32 videoPlaybackSpeedSerialized() const {
return SerializePlaybackSpeed(_videoPlaybackSpeed);
}
void setVideoPlaybackSpeedSerialized(qint32 value) {
_videoPlaybackSpeed = DeserializePlaybackSpeed(value);
}
[[nodiscard]] QByteArray videoPipGeometry() const { [[nodiscard]] QByteArray videoPipGeometry() const {
return _videoPipGeometry; return _videoPipGeometry;
} }
@ -795,17 +809,16 @@ public:
[[nodiscard]] static bool ThirdColumnByDefault(); [[nodiscard]] static bool ThirdColumnByDefault();
[[nodiscard]] static float64 DefaultDialogsWidthRatio(); [[nodiscard]] static float64 DefaultDialogsWidthRatio();
[[nodiscard]] static qint32 SerializePlaybackSpeed(float64 speed) {
return int(base::SafeRound(std::clamp(speed, 0.5, 2.0) * 100)); struct PlaybackSpeed {
} [[nodiscard]] static float64 Default();
[[nodiscard]] static float64 DeserializePlaybackSpeed(qint32 speed) {
if (speed < 10) { float64 value = Default();
// The old values in settings. bool enabled = false;
return (std::clamp(speed, 0, 6) + 2) / 4.; };
} else { [[nodiscard]] static qint32 SerializePlaybackSpeed(PlaybackSpeed speed);
return std::clamp(speed, 50, 200) / 100.; [[nodiscard]] static PlaybackSpeed DeserializePlaybackSpeed(
} qint32 speed);
}
void resetOnLastLogout(); void resetOnLastLogout();
@ -867,9 +880,8 @@ private:
bool _suggestAnimatedEmoji = true; bool _suggestAnimatedEmoji = true;
rpl::variable<bool> _cornerReaction = true; rpl::variable<bool> _cornerReaction = true;
rpl::variable<bool> _spellcheckerEnabled = true; rpl::variable<bool> _spellcheckerEnabled = true;
rpl::variable<float64> _videoPlaybackSpeed = 1.; PlaybackSpeed _videoPlaybackSpeed;
float64 _voicePlaybackSpeed = 2.; PlaybackSpeed _voicePlaybackSpeed;
bool _nonDefaultVoicePlaybackSpeed = false;
QByteArray _videoPipGeometry; QByteArray _videoPipGeometry;
rpl::variable<std::vector<int>> _dictionariesEnabled; rpl::variable<std::vector<int>> _dictionariesEnabled;
rpl::variable<bool> _autoDownloadDictionaries = true; rpl::variable<bool> _autoDownloadDictionaries = true;

View file

@ -143,7 +143,7 @@ void SessionSettings::addFromSerialized(const QByteArray &serialized) {
qint32 appSuggestStickersByEmoji = app.suggestStickersByEmoji() ? 1 : 0; qint32 appSuggestStickersByEmoji = app.suggestStickersByEmoji() ? 1 : 0;
qint32 appSpellcheckerEnabled = app.spellcheckerEnabled() ? 1 : 0; qint32 appSpellcheckerEnabled = app.spellcheckerEnabled() ? 1 : 0;
std::vector<std::pair<DocumentId, crl::time>> mediaLastPlaybackPosition; std::vector<std::pair<DocumentId, crl::time>> mediaLastPlaybackPosition;
qint32 appVideoPlaybackSpeed = Core::Settings::SerializePlaybackSpeed(app.videoPlaybackSpeed()); qint32 appVideoPlaybackSpeed = app.videoPlaybackSpeedSerialized();
QByteArray appVideoPipGeometry = app.videoPipGeometry(); QByteArray appVideoPipGeometry = app.videoPipGeometry();
std::vector<int> appDictionariesEnabled; std::vector<int> appDictionariesEnabled;
qint32 appAutoDownloadDictionaries = app.autoDownloadDictionaries() ? 1 : 0; qint32 appAutoDownloadDictionaries = app.autoDownloadDictionaries() ? 1 : 0;
@ -479,7 +479,7 @@ void SessionSettings::addFromSerialized(const QByteArray &serialized) {
app.setSuggestEmoji(appSuggestEmoji == 1); app.setSuggestEmoji(appSuggestEmoji == 1);
app.setSuggestStickersByEmoji(appSuggestStickersByEmoji == 1); app.setSuggestStickersByEmoji(appSuggestStickersByEmoji == 1);
app.setSpellcheckerEnabled(appSpellcheckerEnabled == 1); app.setSpellcheckerEnabled(appSpellcheckerEnabled == 1);
app.setVideoPlaybackSpeed(Core::Settings::DeserializePlaybackSpeed(appVideoPlaybackSpeed)); app.setVideoPlaybackSpeedSerialized(appVideoPlaybackSpeed);
app.setVideoPipGeometry(appVideoPipGeometry); app.setVideoPipGeometry(appVideoPipGeometry);
app.setDictionariesEnabled(std::move(appDictionariesEnabled)); app.setDictionariesEnabled(std::move(appDictionariesEnabled));
app.setAutoDownloadDictionaries(appAutoDownloadDictionaries == 1); app.setAutoDownloadDictionaries(appAutoDownloadDictionaries == 1);

View file

@ -37,6 +37,7 @@ class Instance;
inline constexpr auto kSpeedMin = 0.5; inline constexpr auto kSpeedMin = 0.5;
inline constexpr auto kSpeedMax = 2.5; inline constexpr auto kSpeedMax = 2.5;
inline constexpr auto kSpedUpDefault = 1.7;
// Thread: Main. // Thread: Main.
void Start(not_null<Instance*> instance); void Start(not_null<Instance*> instance);