From 8da39356dc33bb75caf534eac1e9b5b275c361ed Mon Sep 17 00:00:00 2001 From: John Preston Date: Tue, 12 Jul 2016 17:11:59 +0300 Subject: [PATCH] Divided song volume and video volume, video volume control implemented. --- Telegram/SourceFiles/facades.cpp | 6 +++++ Telegram/SourceFiles/facades.h | 3 +++ Telegram/SourceFiles/localstorage.cpp | 16 +++++++++--- Telegram/SourceFiles/media/media_audio.cpp | 18 +++++++++---- Telegram/SourceFiles/media/media_audio.h | 4 ++- .../SourceFiles/media/media_audio_loaders.cpp | 4 +-- .../media/view/media_clip_controller.cpp | 7 +++++ .../media/view/media_clip_controller.h | 1 + .../view/media_clip_volume_controller.cpp | 22 ++++++++++++++++ .../media/view/media_clip_volume_controller.h | 2 ++ Telegram/SourceFiles/mediaview.cpp | 26 ++++++++++++++----- Telegram/SourceFiles/mediaview.h | 1 + Telegram/SourceFiles/playerwidget.cpp | 8 +++--- Telegram/SourceFiles/settings.cpp | 2 -- Telegram/SourceFiles/settings.h | 2 -- 15 files changed, 96 insertions(+), 26 deletions(-) diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 8ba195c17..b0f0a17b2 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -538,6 +538,9 @@ struct Data { int32 DebugLoggingFlags = 0; + float64 SongVolume = 0.9; + float64 VideoVolume = 0.9; + // config int32 ChatSizeMax = 200; int32 MegagroupSizeMax = 1000; @@ -606,6 +609,9 @@ DefineVar(Global, bool, ScreenIsLocked); DefineVar(Global, int32, DebugLoggingFlags); +DefineVar(Global, float64, SongVolume); +DefineVar(Global, float64, VideoVolume); + // config DefineVar(Global, int32, ChatSizeMax); DefineVar(Global, int32, MegagroupSizeMax); diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index a95629dd3..4a14f5694 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -217,6 +217,9 @@ DeclareVar(bool, ScreenIsLocked); DeclareVar(int32, DebugLoggingFlags); +DeclareVar(float64, SongVolume); +DeclareVar(float64, VideoVolume); + // config DeclareVar(int32, ChatSizeMax); DeclareVar(int32, MegagroupSizeMax); diff --git a/Telegram/SourceFiles/localstorage.cpp b/Telegram/SourceFiles/localstorage.cpp index 6ab0612b8..9d30c85aa 100644 --- a/Telegram/SourceFiles/localstorage.cpp +++ b/Telegram/SourceFiles/localstorage.cpp @@ -541,6 +541,7 @@ namespace { dbiHiddenPinnedMessages = 0x39, dbiDialogsMode = 0x40, dbiModerateMode = 0x41, + dbiVideoVolume = 0x42, dbiEncryptedWithSalt = 333, dbiEncrypted = 444, @@ -1308,7 +1309,15 @@ namespace { stream >> v; if (!_checkStreamStatus(stream)) return false; - cSetSongVolume(snap(v / 1e6, 0., 1.)); + Global::SetSongVolume(snap(v / 1e6, 0., 1.)); + } break; + + case dbiVideoVolume: { + qint32 v; + stream >> v; + if (!_checkStreamStatus(stream)) return false; + + Global::SetVideoVolume(snap(v / 1e6, 0., 1.)); } break; default: @@ -1532,7 +1541,7 @@ namespace { _writeMap(WriteMapFast); } - uint32 size = 17 * (sizeof(quint32) + sizeof(qint32)); + uint32 size = 18 * (sizeof(quint32) + sizeof(qint32)); size += sizeof(quint32) + Serialize::stringSize(cAskDownloadPath() ? QString() : cDownloadPath()) + Serialize::bytearraySize(cAskDownloadPath() ? QByteArray() : cDownloadPathBookmark()); size += sizeof(quint32) + sizeof(qint32) + (cRecentEmojisPreload().isEmpty() ? cGetRecentEmojis().size() : cRecentEmojisPreload().size()) * (sizeof(uint64) + sizeof(ushort)); size += sizeof(quint32) + sizeof(qint32) + cEmojiVariants().size() * (sizeof(uint32) + sizeof(uint64)); @@ -1561,7 +1570,8 @@ namespace { data.stream << quint32(dbiDownloadPath) << (cAskDownloadPath() ? QString() : cDownloadPath()) << (cAskDownloadPath() ? QByteArray() : cDownloadPathBookmark()); data.stream << quint32(dbiCompressPastedImage) << qint32(cCompressPastedImage()); data.stream << quint32(dbiDialogLastPath) << cDialogLastPath(); - data.stream << quint32(dbiSongVolume) << qint32(qRound(cSongVolume() * 1e6)); + data.stream << quint32(dbiSongVolume) << qint32(qRound(Global::SongVolume() * 1e6)); + data.stream << quint32(dbiVideoVolume) << qint32(qRound(Global::VideoVolume() * 1e6)); data.stream << quint32(dbiAutoDownload) << qint32(cAutoDownloadPhoto()) << qint32(cAutoDownloadAudio()) << qint32(cAutoDownloadGif()); data.stream << quint32(dbiDialogsMode) << qint32(Global::DialogsModeEnabled() ? 1 : 0) << static_cast(Global::DialogsMode()); data.stream << quint32(dbiModerateMode) << qint32(Global::ModerateModeEnabled() ? 1 : 0); diff --git a/Telegram/SourceFiles/media/media_audio.cpp b/Telegram/SourceFiles/media/media_audio.cpp index 3cdee8d4e..b18b8ddb1 100644 --- a/Telegram/SourceFiles/media/media_audio.cpp +++ b/Telegram/SourceFiles/media/media_audio.cpp @@ -291,6 +291,7 @@ _loader(new AudioPlayerLoaders(&_loaderThread)) { connect(this, SIGNAL(unsuppressSong()), _fader, SLOT(onUnsuppressSong())); connect(this, SIGNAL(suppressAll()), _fader, SLOT(onSuppressAll())); connect(this, SIGNAL(songVolumeChanged()), _fader, SLOT(onSongVolumeChanged())); + connect(this, SIGNAL(videoVolumeChanged()), _fader, SLOT(onVideoVolumeChanged())); connect(this, SIGNAL(loaderOnStart(const AudioMsgId&,qint64)), _loader, SLOT(onStart(const AudioMsgId&,qint64))); connect(this, SIGNAL(loaderOnCancel(const AudioMsgId&)), _loader, SLOT(onCancel(const AudioMsgId&))); connect(&_faderThread, SIGNAL(started()), _fader, SLOT(onInit())); @@ -571,7 +572,7 @@ void AudioPlayer::pauseresume(AudioMsgId::Type type, bool fast) { float64 suppressGain = 1.; switch (type) { case AudioMsgId::Type::Voice: suppressGain = suppressAllGain; break; - case AudioMsgId::Type::Song: suppressGain = suppressSongGain * cSongVolume(); break; + case AudioMsgId::Type::Song: suppressGain = suppressSongGain * Global::SongVolume(); break; } switch (current->playbackState.state) { @@ -623,7 +624,7 @@ void AudioPlayer::seek(int64 position) { float64 suppressGain = 1.; switch (type) { case AudioMsgId::Type::Voice: suppressGain = suppressAllGain; break; - case AudioMsgId::Type::Song: suppressGain = suppressSongGain * cSongVolume(); break; + case AudioMsgId::Type::Song: suppressGain = suppressSongGain * Global::SongVolume(); break; } auto audio = current->audio; @@ -887,15 +888,17 @@ void AudioPlayerFader::onTimer() { if (emitSignals & EmitPositionUpdated) emit playPositionUpdated(data->audio); if (emitSignals & EmitNeedToPreload) emit needToPreload(data->audio); }; - auto suppressGainForMusic = suppressSongGain * cSongVolume(); + auto suppressGainForMusic = suppressSongGain * Global::SongVolume(); auto suppressGainForMusicChanged = suppressSongChanged || _songVolumeChanged; for (int i = 0; i < AudioSimultaneousLimit; ++i) { updatePlayback(AudioMsgId::Type::Voice, i, suppressAllGain, suppressAudioChanged); updatePlayback(AudioMsgId::Type::Song, i, suppressGainForMusic, suppressGainForMusicChanged); } - updatePlayback(AudioMsgId::Type::Video, 0, suppressGainForMusic, suppressGainForMusicChanged); + auto suppressGainForVideo = suppressSongGain * Global::VideoVolume(); + auto suppressGainForVideoChanged = suppressSongChanged || _videoVolumeChanged; + updatePlayback(AudioMsgId::Type::Video, 0, suppressGainForVideo, suppressGainForVideoChanged); - _songVolumeChanged = false; + _songVolumeChanged = _videoVolumeChanged = false; if (!hasFading) { if (!hasPlaying) { @@ -1067,6 +1070,11 @@ void AudioPlayerFader::onSongVolumeChanged() { onTimer(); } +void AudioPlayerFader::onVideoVolumeChanged() { + _videoVolumeChanged = true; + onTimer(); +} + void AudioPlayerFader::resumeDevice() { QMutexLocker lock(&_pauseMutex); _pauseFlag = false; diff --git a/Telegram/SourceFiles/media/media_audio.h b/Telegram/SourceFiles/media/media_audio.h index 529e730ce..a902861f2 100644 --- a/Telegram/SourceFiles/media/media_audio.h +++ b/Telegram/SourceFiles/media/media_audio.h @@ -100,6 +100,7 @@ signals: void suppressAll(); void songVolumeChanged(); + void videoVolumeChanged(); private: bool fadedStop(AudioMsgId::Type type, bool *fadedStart = 0); @@ -237,6 +238,7 @@ public slots: void onUnsuppressSong(); void onSuppressAll(); void onSongVolumeChanged(); + void onVideoVolumeChanged(); private: enum { @@ -252,7 +254,7 @@ private: QMutex _pauseMutex; bool _pauseFlag, _paused; - bool _suppressAll, _suppressAllAnim, _suppressSong, _suppressSongAnim, _songVolumeChanged; + bool _suppressAll, _suppressAllAnim, _suppressSong, _suppressSongAnim, _songVolumeChanged, _videoVolumeChanged; anim::fvalue _suppressAllGain, _suppressSongGain; uint64 _suppressAllStart, _suppressSongStart; diff --git a/Telegram/SourceFiles/media/media_audio_loaders.cpp b/Telegram/SourceFiles/media/media_audio_loaders.cpp index a45558a8d..f635c8326 100644 --- a/Telegram/SourceFiles/media/media_audio_loaders.cpp +++ b/Telegram/SourceFiles/media/media_audio_loaders.cpp @@ -294,8 +294,8 @@ void AudioPlayerLoaders::loadData(AudioMsgId audio, qint64 position) { switch (type) { case AudioMsgId::Type::Voice: alSourcef(m->source, AL_GAIN, internal::audioSuppressGain()); break; - case AudioMsgId::Type::Song: alSourcef(m->source, AL_GAIN, internal::audioSuppressSongGain() * cSongVolume()); break; - case AudioMsgId::Type::Video: alSourcef(m->source, AL_GAIN, internal::audioSuppressSongGain() * cSongVolume()); break; + case AudioMsgId::Type::Song: alSourcef(m->source, AL_GAIN, internal::audioSuppressSongGain() * Global::SongVolume()); break; + case AudioMsgId::Type::Video: alSourcef(m->source, AL_GAIN, internal::audioSuppressSongGain() * Global::VideoVolume()); break; } if (!internal::audioCheckError()) { setStoppedState(m, AudioPlayerStoppedAtError); diff --git a/Telegram/SourceFiles/media/view/media_clip_controller.cpp b/Telegram/SourceFiles/media/view/media_clip_controller.cpp index 4a89e0f6c..fc48fb16b 100644 --- a/Telegram/SourceFiles/media/view/media_clip_controller.cpp +++ b/Telegram/SourceFiles/media/view/media_clip_controller.cpp @@ -43,6 +43,9 @@ Controller::Controller(QWidget *parent) : TWidget(parent) _fadeAnimation->show(); _fadeAnimation->setFinishedCallback(func(this, &Controller::fadeFinished)); _fadeAnimation->setUpdatedCallback(func(this, &Controller::fadeUpdated)); + + _volumeController->setVolume(Global::VideoVolume()); + connect(_playPauseResume, SIGNAL(clicked()), this, SIGNAL(playPressed())); connect(_fullScreenToggle, SIGNAL(clicked()), this, SIGNAL(toFullScreenPressed())); connect(_playback, SIGNAL(seekProgress(int64)), this, SLOT(onSeekProgress(int64))); @@ -173,5 +176,9 @@ void Controller::paintEvent(QPaintEvent *e) { App::roundRect(p, rect(), st::medviewSaveMsg, MediaviewSaveCorners); } +void Controller::mousePressEvent(QMouseEvent *e) { + e->accept(); // Don't pass event to the MediaView. +} + } // namespace Clip } // namespace Media diff --git a/Telegram/SourceFiles/media/view/media_clip_controller.h b/Telegram/SourceFiles/media/view/media_clip_controller.h index 3cf6016f3..14a5a5c13 100644 --- a/Telegram/SourceFiles/media/view/media_clip_controller.h +++ b/Telegram/SourceFiles/media/view/media_clip_controller.h @@ -65,6 +65,7 @@ private slots: protected: void resizeEvent(QResizeEvent *e) override; void paintEvent(QPaintEvent *e) override; + void mousePressEvent(QMouseEvent *e) override; private: template diff --git a/Telegram/SourceFiles/media/view/media_clip_volume_controller.cpp b/Telegram/SourceFiles/media/view/media_clip_volume_controller.cpp index 57021db79..8a98dec90 100644 --- a/Telegram/SourceFiles/media/view/media_clip_volume_controller.cpp +++ b/Telegram/SourceFiles/media/view/media_clip_volume_controller.cpp @@ -29,6 +29,7 @@ namespace Clip { VolumeController::VolumeController(QWidget *parent) : TWidget(parent) { resize(st::mediaviewVolumeSize); setCursor(style::cur_pointer); + setMouseTracking(true); } void VolumeController::setVolume(float64 volume) { @@ -57,12 +58,33 @@ void VolumeController::paintEvent(QPaintEvent *e) { } void VolumeController::mouseMoveEvent(QMouseEvent *e) { + if (_downCoord < 0) { + return; + } + int delta = e->pos().x() - _downCoord; + int left = (width() - st::mediaviewVolumeIcon.width()) / 2; + float64 startFrom = snap((_downCoord - left) / float64(st::mediaviewVolumeIcon.width()), 0., 1.); + float64 add = delta / float64(4 * st::mediaviewVolumeIcon.width()); + auto newVolume = snap(startFrom + add, 0., 1.); + changeVolume(newVolume); } void VolumeController::mousePressEvent(QMouseEvent *e) { + _downCoord = snap(e->pos().x(), 0, width()); + int left = (width() - st::mediaviewVolumeIcon.width()) / 2; + auto newVolume = snap((_downCoord - left) / float64(st::mediaviewVolumeIcon.width()), 0., 1.); + changeVolume(newVolume); +} + +void VolumeController::changeVolume(float64 newVolume) { + if (newVolume != _volume) { + setVolume(newVolume); + emit volumeChanged(_volume); + } } void VolumeController::mouseReleaseEvent(QMouseEvent *e) { + _downCoord = -1; } void VolumeController::enterEvent(QEvent *e) { diff --git a/Telegram/SourceFiles/media/view/media_clip_volume_controller.h b/Telegram/SourceFiles/media/view/media_clip_volume_controller.h index 6d891d321..cf062ce09 100644 --- a/Telegram/SourceFiles/media/view/media_clip_volume_controller.h +++ b/Telegram/SourceFiles/media/view/media_clip_volume_controller.h @@ -47,8 +47,10 @@ private: update(); } void setOver(bool over); + void changeVolume(float64 newVolume); float64 _volume = 0.; + int _downCoord = -1; // < 0 means mouse is not pressed bool _over = false; FloatAnimation _a_over; diff --git a/Telegram/SourceFiles/mediaview.cpp b/Telegram/SourceFiles/mediaview.cpp index 7e5aee2f6..4811281ce 100644 --- a/Telegram/SourceFiles/mediaview.cpp +++ b/Telegram/SourceFiles/mediaview.cpp @@ -554,7 +554,9 @@ void MediaView::close() { } void MediaView::activateControls() { - if (!_menu) _controlsHideTimer.start(int(st::mvWaitHide)); + if (!_menu && !_mousePressed) { + _controlsHideTimer.start(int(st::mvWaitHide)); + } if (_controlsState == ControlsHiding || _controlsState == ControlsHidden) { _controlsState = ControlsShowing; _controlsAnimStarted = getms(); @@ -567,7 +569,7 @@ void MediaView::activateControls() { } void MediaView::onHideControls(bool force) { - if (!force && (!_dropdown.isHidden() || _menu)) return; + if (!force && (!_dropdown.isHidden() || _menu || _mousePressed)) return; if (_controlsState == ControlsHiding || _controlsState == ControlsHidden) return; _controlsState = ControlsHiding; _controlsAnimStarted = getms(); @@ -1301,7 +1303,8 @@ void MediaView::onVideoSeekFinished(int64 position) { } void MediaView::onVideoVolumeChanged(float64 volume) { - + Global::SetVideoVolume(volume); + emit audioPlayer()->videoVolumeChanged(); } void MediaView::onVideoToFullScreen() { @@ -2244,12 +2247,21 @@ bool MediaView::event(QEvent *e) { } bool MediaView::eventFilter(QObject *obj, QEvent *e) { - if (e->type() == QEvent::MouseMove && obj->isWidgetType()) { + auto type = e->type(); + if ((type == QEvent::MouseMove || type == QEvent::MouseButtonPress || type == QEvent::MouseButtonRelease) && obj->isWidgetType()) { if (isAncestorOf(static_cast(obj))) { - auto mousePosition = mapFromGlobal(static_cast(e)->globalPos()); - bool moved = (mousePosition != _lastMouseMovePos); + auto mouseEvent = static_cast(e); + auto mousePosition = mapFromGlobal(mouseEvent->globalPos()); + bool activate = (mousePosition != _lastMouseMovePos); _lastMouseMovePos = mousePosition; - if (moved) activateControls(); + if (type == QEvent::MouseButtonPress) { + _mousePressed = true; + activate = true; + } else if (type == QEvent::MouseButtonRelease) { + _mousePressed = false; + activate = true; + } + if (activate) activateControls(); } } return TWidget::eventFilter(obj, e); diff --git a/Telegram/SourceFiles/mediaview.h b/Telegram/SourceFiles/mediaview.h index 1e696c147..8076df4e8 100644 --- a/Telegram/SourceFiles/mediaview.h +++ b/Telegram/SourceFiles/mediaview.h @@ -280,6 +280,7 @@ private: uint64 _controlsAnimStarted = 0; QTimer _controlsHideTimer; anim::fvalue a_cOpacity; + bool _mousePressed = false; PopupMenu *_menu = nullptr; Dropdown _dropdown; diff --git a/Telegram/SourceFiles/playerwidget.cpp b/Telegram/SourceFiles/playerwidget.cpp index 1b222e511..af71285ac 100644 --- a/Telegram/SourceFiles/playerwidget.cpp +++ b/Telegram/SourceFiles/playerwidget.cpp @@ -79,7 +79,7 @@ void PlayerWidget::paintEvent(QPaintEvent *e) { p.setOpacity(o * 1. + (1. - o) * st::playerInactiveOpacity); int32 top = _volumeRect.y() + (_volumeRect.height() - st::playerVolume.pxHeight()) / 2; int32 left = _volumeRect.x() + (_volumeRect.width() - st::playerVolume.pxWidth()) / 2; - int32 mid = left + qRound(st::playerVolume.pxWidth() * cSongVolume()); + int32 mid = left + qRound(st::playerVolume.pxWidth() * Global::SongVolume()); int32 right = left + st::playerVolume.pxWidth(); if (rtl()) { left = width() - left; @@ -164,7 +164,7 @@ void PlayerWidget::mousePressEvent(QMouseEvent *e) { } else if (_over == OverVolume) { _down = OverVolume; _downCoord = pos.x() - _volumeRect.x(); - cSetSongVolume(snap((_downCoord - ((_volumeRect.width() - st::playerVolume.pxWidth()) / 2)) / float64(st::playerVolume.pxWidth()), 0., 1.)); + Global::SetSongVolume(snap((_downCoord - ((_volumeRect.width() - st::playerVolume.pxWidth()) / 2)) / float64(st::playerVolume.pxWidth()), 0., 1.)); emit audioPlayer()->songVolumeChanged(); rtlupdate(_volumeRect); } else if (_over == OverPlayback) { @@ -401,8 +401,8 @@ void PlayerWidget::updateSelected() { int32 delta = (pos.x() - _volumeRect.x()) - _downCoord; float64 startFrom = snap((_downCoord - ((_volumeRect.width() - st::playerVolume.pxWidth()) / 2)) / float64(st::playerVolume.pxWidth()), 0., 1.); float64 add = delta / float64(4 * st::playerVolume.pxWidth()), result = snap(startFrom + add, 0., 1.); - if (result != cSongVolume()) { - cSetSongVolume(result); + if (result != Global::SongVolume()) { + Global::SetSongVolume(result); emit audioPlayer()->songVolumeChanged(); rtlupdate(_volumeRect); } diff --git a/Telegram/SourceFiles/settings.cpp b/Telegram/SourceFiles/settings.cpp index 7e0ebb89e..030bf7a67 100644 --- a/Telegram/SourceFiles/settings.cpp +++ b/Telegram/SourceFiles/settings.cpp @@ -148,8 +148,6 @@ bool gDialogsReceived = false; int gOtherOnline = 0; -float64 gSongVolume = 0.9; - SavedPeers gSavedPeers; SavedPeersByTime gSavedPeersByTime; diff --git a/Telegram/SourceFiles/settings.h b/Telegram/SourceFiles/settings.h index 999065bbf..c27d862fe 100644 --- a/Telegram/SourceFiles/settings.h +++ b/Telegram/SourceFiles/settings.h @@ -291,8 +291,6 @@ DeclareSetting(bool, DialogsReceived); DeclareSetting(int, OtherOnline); -DeclareSetting(float64, SongVolume); - class PeerData; typedef QMap SavedPeers; typedef QMultiMap SavedPeersByTime;