mirror of
https://github.com/kotatogram/kotatogram-desktop
synced 2025-08-31 06:35:14 +00:00
Alpha 1.0.8: seek in voice messages (by waveform).
This commit is contained in:
@@ -1686,12 +1686,12 @@ public:
|
||||
QByteArray buffer;
|
||||
buffer.reserve(AudioVoiceMsgBufferSize);
|
||||
int64 countbytes = sampleSize * duration(), processed = 0, sumbytes = 0;
|
||||
if (duration() < WaveformSamplesCount) {
|
||||
if (duration() < Media::Player::kWaveformSamplesCount) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QVector<uint16> peaks;
|
||||
peaks.reserve(WaveformSamplesCount);
|
||||
peaks.reserve(Media::Player::kWaveformSamplesCount);
|
||||
|
||||
int32 fmt = format();
|
||||
uint16 peak = 0;
|
||||
@@ -1716,7 +1716,7 @@ public:
|
||||
}
|
||||
|
||||
i += sizeof(uchar);
|
||||
sumbytes += WaveformSamplesCount;
|
||||
sumbytes += Media::Player::kWaveformSamplesCount;
|
||||
if (sumbytes >= countbytes) {
|
||||
sumbytes -= countbytes;
|
||||
peaks.push_back(peak);
|
||||
@@ -1731,7 +1731,7 @@ public:
|
||||
}
|
||||
|
||||
i += sizeof(uint16);
|
||||
sumbytes += sizeof(uint16) * WaveformSamplesCount;
|
||||
sumbytes += sizeof(uint16) * Media::Player::kWaveformSamplesCount;
|
||||
if (sumbytes >= countbytes) {
|
||||
sumbytes -= countbytes;
|
||||
peaks.push_back(peak);
|
||||
@@ -1741,7 +1741,7 @@ public:
|
||||
}
|
||||
processed += sampleSize * samples;
|
||||
}
|
||||
if (sumbytes > 0 && peaks.size() < WaveformSamplesCount) {
|
||||
if (sumbytes > 0 && peaks.size() < Media::Player::kWaveformSamplesCount) {
|
||||
peaks.push_back(peak);
|
||||
}
|
||||
|
||||
|
@@ -28,6 +28,7 @@ namespace Player {
|
||||
|
||||
constexpr auto kDefaultFrequency = 48000; // 48 kHz
|
||||
constexpr auto kTogetherLimit = 4;
|
||||
constexpr auto kWaveformSamplesCount = 100;
|
||||
|
||||
class Fader;
|
||||
class Loaders;
|
||||
|
@@ -396,9 +396,9 @@ void Instance::Inner::onStop(bool needResult) {
|
||||
qint32 samples = d->fullSamples;
|
||||
if (samples && !d->waveform.isEmpty()) {
|
||||
int64 count = d->waveform.size(), sum = 0;
|
||||
if (count >= WaveformSamplesCount) {
|
||||
if (count >= Player::kWaveformSamplesCount) {
|
||||
QVector<uint16> peaks;
|
||||
peaks.reserve(WaveformSamplesCount);
|
||||
peaks.reserve(Player::kWaveformSamplesCount);
|
||||
|
||||
uint16 peak = 0;
|
||||
for (int32 i = 0; i < count; ++i) {
|
||||
@@ -406,7 +406,7 @@ void Instance::Inner::onStop(bool needResult) {
|
||||
if (peak < sample) {
|
||||
peak = sample;
|
||||
}
|
||||
sum += WaveformSamplesCount;
|
||||
sum += Player::kWaveformSamplesCount;
|
||||
if (sum >= count) {
|
||||
sum -= count;
|
||||
peaks.push_back(peak);
|
||||
|
@@ -141,7 +141,7 @@ void CoverWidget::handleSeekProgress(float64 progress) {
|
||||
if (_seekPositionMs != positionMs) {
|
||||
_seekPositionMs = positionMs;
|
||||
updateTimeLabel();
|
||||
instance()->startSeeking();
|
||||
instance()->startSeeking(AudioMsgId::Type::Song);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,7 +157,7 @@ void CoverWidget::handleSeekFinished(float64 progress) {
|
||||
Media::Player::mixer()->seek(type, qRound(progress * state.duration));
|
||||
}
|
||||
|
||||
instance()->stopSeeking();
|
||||
instance()->stopSeeking(type);
|
||||
}
|
||||
|
||||
void CoverWidget::resizeEvent(QResizeEvent *e) {
|
||||
@@ -239,7 +239,7 @@ void CoverWidget::handleSongUpdate(const TrackState &state) {
|
||||
|
||||
auto stopped = (IsStopped(state.state) || state.state == State::Finishing);
|
||||
auto showPause = !stopped && (state.state == State::Playing || state.state == State::Resuming || state.state == State::Starting);
|
||||
if (instance()->isSeeking()) {
|
||||
if (instance()->isSeeking(AudioMsgId::Type::Song)) {
|
||||
showPause = true;
|
||||
}
|
||||
auto buttonState = [audio = state.id.audio(), showPause] {
|
||||
|
@@ -49,9 +49,7 @@ void finish() {
|
||||
|
||||
Instance::Instance() {
|
||||
subscribe(Media::Player::Updated(), [this](const AudioMsgId &audioId) {
|
||||
if (audioId.type() == AudioMsgId::Type::Song) {
|
||||
handleSongUpdate(audioId);
|
||||
}
|
||||
handleSongUpdate(audioId);
|
||||
});
|
||||
auto observeEvents = Notify::PeerUpdate::Flag::SharedMediaChanged;
|
||||
subscribe(Notify::PeerUpdated(), Notify::PeerUpdatedHandler(observeEvents, [this](const Notify::PeerUpdate &update) {
|
||||
@@ -79,27 +77,33 @@ void Instance::notifyPeerUpdated(const Notify::PeerUpdate &update) {
|
||||
}
|
||||
|
||||
void Instance::handleSongUpdate(const AudioMsgId &audioId) {
|
||||
emitUpdate([&audioId](const AudioMsgId &playing) {
|
||||
emitUpdate(audioId.type(), [&audioId](const AudioMsgId &playing) {
|
||||
return (audioId == playing);
|
||||
});
|
||||
}
|
||||
|
||||
void Instance::setCurrent(const AudioMsgId &audioId) {
|
||||
if (_current != audioId) {
|
||||
_current = audioId;
|
||||
_isPlaying = false;
|
||||
if (audioId.type() == AudioMsgId::Type::Song) {
|
||||
if (_current != audioId) {
|
||||
_current = audioId;
|
||||
_isPlaying = false;
|
||||
|
||||
auto history = _history, migrated = _migrated;
|
||||
auto item = _current ? App::histItemById(_current.contextId()) : nullptr;
|
||||
if (item) {
|
||||
_history = item->history()->peer->migrateTo() ? App::history(item->history()->peer->migrateTo()) : item->history();
|
||||
_migrated = _history->peer->migrateFrom() ? App::history(_history->peer->migrateFrom()) : nullptr;
|
||||
} else {
|
||||
_history = _migrated = nullptr;
|
||||
auto history = _history, migrated = _migrated;
|
||||
auto item = _current ? App::histItemById(_current.contextId()) : nullptr;
|
||||
if (item) {
|
||||
_history = item->history()->peer->migrateTo() ? App::history(item->history()->peer->migrateTo()) : item->history();
|
||||
_migrated = _history->peer->migrateFrom() ? App::history(_history->peer->migrateFrom()) : nullptr;
|
||||
} else {
|
||||
_history = _migrated = nullptr;
|
||||
}
|
||||
_songChangedNotifier.notify(true);
|
||||
if (_history != history || _migrated != migrated) {
|
||||
rebuildPlaylist();
|
||||
}
|
||||
}
|
||||
_songChangedNotifier.notify(true);
|
||||
if (_history != history || _migrated != migrated) {
|
||||
rebuildPlaylist();
|
||||
} else if (audioId.type() == AudioMsgId::Type::Voice) {
|
||||
if (_currentVoice != audioId) {
|
||||
_currentVoice = audioId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -173,12 +177,12 @@ void Instance::play(const AudioMsgId &audioId) {
|
||||
}
|
||||
}
|
||||
|
||||
void Instance::pause() {
|
||||
auto state = mixer()->currentState(AudioMsgId::Type::Song);
|
||||
void Instance::pause(AudioMsgId::Type type) {
|
||||
auto state = mixer()->currentState(type);
|
||||
if (state.id) {
|
||||
if (!IsStopped(state.state)) {
|
||||
if (state.state == State::Starting || state.state == State::Resuming || state.state == State::Playing || state.state == State::Finishing) {
|
||||
mixer()->pauseresume(AudioMsgId::Type::Song);
|
||||
mixer()->pauseresume(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,7 +214,7 @@ void Instance::previous() {
|
||||
}
|
||||
|
||||
void Instance::playPauseCancelClicked() {
|
||||
if (isSeeking()) {
|
||||
if (isSeeking(AudioMsgId::Type::Song)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -221,32 +225,40 @@ void Instance::playPauseCancelClicked() {
|
||||
if (audio && audio->loading()) {
|
||||
audio->cancel();
|
||||
} else if (showPause) {
|
||||
pause();
|
||||
pause(AudioMsgId::Type::Song);
|
||||
} else {
|
||||
play();
|
||||
}
|
||||
}
|
||||
|
||||
void Instance::startSeeking() {
|
||||
_seeking = _current;
|
||||
pause();
|
||||
emitUpdate([](const AudioMsgId &playing) { return true; });
|
||||
void Instance::startSeeking(AudioMsgId::Type type) {
|
||||
if (type == AudioMsgId::Type::Song) {
|
||||
_seeking = _current;
|
||||
} else if (type == AudioMsgId::Type::Voice) {
|
||||
_seekingVoice = _currentVoice;
|
||||
}
|
||||
pause(type);
|
||||
emitUpdate(type, [](const AudioMsgId &playing) { return true; });
|
||||
}
|
||||
|
||||
void Instance::stopSeeking() {
|
||||
_seeking = AudioMsgId();
|
||||
emitUpdate([](const AudioMsgId &playing) { return true; });
|
||||
void Instance::stopSeeking(AudioMsgId::Type type) {
|
||||
if (type == AudioMsgId::Type::Song) {
|
||||
_seeking = AudioMsgId();
|
||||
} else if (type == AudioMsgId::Type::Voice) {
|
||||
_seekingVoice = AudioMsgId();
|
||||
}
|
||||
emitUpdate(type, [](const AudioMsgId &playing) { return true; });
|
||||
}
|
||||
|
||||
void Instance::documentLoadProgress(DocumentData *document) {
|
||||
emitUpdate([document](const AudioMsgId &audioId) {
|
||||
emitUpdate(document->song() ? AudioMsgId::Type::Song : AudioMsgId::Type::Voice, [document](const AudioMsgId &audioId) {
|
||||
return (audioId.audio() == document);
|
||||
});
|
||||
}
|
||||
|
||||
template <typename CheckCallback>
|
||||
void Instance::emitUpdate(CheckCallback check) {
|
||||
auto state = mixer()->currentState(AudioMsgId::Type::Song);
|
||||
void Instance::emitUpdate(AudioMsgId::Type type, CheckCallback check) {
|
||||
auto state = mixer()->currentState(type);
|
||||
if (!state.id || !check(state.id)) {
|
||||
return;
|
||||
}
|
||||
@@ -254,18 +266,20 @@ void Instance::emitUpdate(CheckCallback check) {
|
||||
setCurrent(state.id);
|
||||
_updatedNotifier.notify(state, true);
|
||||
|
||||
if (_isPlaying && state.state == State::StoppedAtEnd) {
|
||||
if (_repeatEnabled) {
|
||||
mixer()->play(_current);
|
||||
} else {
|
||||
next();
|
||||
if (type == AudioMsgId::Type::Song) {
|
||||
if (_isPlaying && state.state == State::StoppedAtEnd) {
|
||||
if (_repeatEnabled) {
|
||||
mixer()->play(_current);
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
}
|
||||
auto isPlaying = !IsStopped(state.state);
|
||||
if (_isPlaying != isPlaying) {
|
||||
_isPlaying = isPlaying;
|
||||
if (_isPlaying) {
|
||||
preloadNext();
|
||||
auto isPlaying = !IsStopped(state.state);
|
||||
if (_isPlaying != isPlaying) {
|
||||
_isPlaying = isPlaying;
|
||||
if (_isPlaying) {
|
||||
preloadNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -39,7 +39,7 @@ struct TrackState;
|
||||
class Instance : private base::Subscriber {
|
||||
public:
|
||||
void play();
|
||||
void pause();
|
||||
void pause(AudioMsgId::Type type);
|
||||
void stop();
|
||||
void playPause();
|
||||
void next();
|
||||
@@ -60,11 +60,16 @@ public:
|
||||
_repeatChangedNotifier.notify();
|
||||
}
|
||||
|
||||
bool isSeeking() const {
|
||||
return (_seeking == _current);
|
||||
bool isSeeking(AudioMsgId::Type type) const {
|
||||
if (type == AudioMsgId::Type::Song) {
|
||||
return (_seeking == _current);
|
||||
} else if (type == AudioMsgId::Type::Voice) {
|
||||
return (_seekingVoice == _currentVoice);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void startSeeking();
|
||||
void stopSeeking();
|
||||
void startSeeking(AudioMsgId::Type type);
|
||||
void stopSeeking(AudioMsgId::Type type);
|
||||
|
||||
const QList<FullMsgId> &playlist() const {
|
||||
return _playlist;
|
||||
@@ -111,7 +116,7 @@ private:
|
||||
void handleLogout();
|
||||
|
||||
template <typename CheckCallback>
|
||||
void emitUpdate(CheckCallback check);
|
||||
void emitUpdate(AudioMsgId::Type type, CheckCallback check);
|
||||
|
||||
AudioMsgId _current, _seeking;
|
||||
History *_history = nullptr;
|
||||
@@ -122,6 +127,8 @@ private:
|
||||
QList<FullMsgId> _playlist;
|
||||
bool _isPlaying = false;
|
||||
|
||||
AudioMsgId _currentVoice, _seekingVoice;
|
||||
|
||||
base::Observable<bool> _usePanelPlayer;
|
||||
base::Observable<bool> _titleButtonOver;
|
||||
base::Observable<bool> _playerWidgetOver;
|
||||
|
@@ -195,7 +195,7 @@ void Widget::handleSeekProgress(float64 progress) {
|
||||
_seekPositionMs = positionMs;
|
||||
updateTimeLabel();
|
||||
|
||||
instance()->startSeeking();
|
||||
instance()->startSeeking(AudioMsgId::Type::Song);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,7 +211,7 @@ void Widget::handleSeekFinished(float64 progress) {
|
||||
mixer()->seek(type, qRound(progress * state.duration));
|
||||
}
|
||||
|
||||
instance()->stopSeeking();
|
||||
instance()->stopSeeking(AudioMsgId::Type::Song);
|
||||
}
|
||||
|
||||
void Widget::resizeEvent(QResizeEvent *e) {
|
||||
@@ -312,7 +312,7 @@ void Widget::handleSongUpdate(const TrackState &state) {
|
||||
|
||||
auto stopped = (IsStopped(state.state) || state.state == State::Finishing);
|
||||
auto showPause = !stopped && (state.state == State::Playing || state.state == State::Resuming || state.state == State::Starting);
|
||||
if (instance()->isSeeking()) {
|
||||
if (instance()->isSeeking(AudioMsgId::Type::Song)) {
|
||||
showPause = true;
|
||||
}
|
||||
auto buttonState = [audio = state.id.audio(), showPause] {
|
||||
|
Reference in New Issue
Block a user