From 05488a5bd70b69a67c45aaebd3cccebc61f643fd Mon Sep 17 00:00:00 2001 From: "bjorn.olievier" Date: Wed, 14 May 2008 22:32:36 +0000 Subject: [PATCH] Made position slider & time indicators update during playback. Removed some pass-through methods from PlaybackQueue and added getter for Transport. Updated all references. --- src/core/PlaybackQueue.cpp | 13 ------ src/core/PlaybackQueue.h | 10 +++-- src/core/audio/Transport.cpp | 20 +++++++--- src/core/audio/Transport.h | 6 ++- src/cube/TransportController.cpp | 68 ++++++++++++++++++++++++++++++-- src/cube/TransportController.hpp | 9 +++++ src/cube/TransportView.cpp | 2 +- src/square/ConsoleUI.cpp | 2 +- src/win32cpp/Trackbar.hpp | 1 + 9 files changed, 102 insertions(+), 29 deletions(-) diff --git a/src/core/PlaybackQueue.cpp b/src/core/PlaybackQueue.cpp index 1bc463782..e487b59be 100644 --- a/src/core/PlaybackQueue.cpp +++ b/src/core/PlaybackQueue.cpp @@ -198,17 +198,4 @@ void PlaybackQueue::Play(tracklist::IRandomAccess &tracklist){ this->currentTrack.reset(); this->nowPlaying->CopyTracks(tracklist); this->Play(); -} - -short PlaybackQueue::Volume() const{ - return this->transport.Volume(); -} - -void PlaybackQueue::SetVolume(short volume){ - this->transport.ChangeVolume(volume); -} - -void PlaybackQueue::JumpToPosition(short relativePosition) -{ - this->transport.JumpToPosition(relativePosition); } \ No newline at end of file diff --git a/src/core/PlaybackQueue.h b/src/core/PlaybackQueue.h index ac7cb4b50..cffc9d52a 100644 --- a/src/core/PlaybackQueue.h +++ b/src/core/PlaybackQueue.h @@ -86,6 +86,12 @@ class PlaybackQueue : public sigslot::has_slots<>{ ////////////////////////////////////////// static PlaybackQueue& Instance(){ return sInstance; }; + ////////////////////////////////////////// + ///\brief + ///Get a hold of the Transport + ////////////////////////////////////////// + musik::core::audio::Transport& Transport() { return this->transport; }; + // Now Playing control tracklist::Standard::Ptr NowPlayingTracklist(); void Play(tracklist::IRandomAccess &tracklist); @@ -97,10 +103,6 @@ class PlaybackQueue : public sigslot::has_slots<>{ void Previous(); void Stop(); - short Volume() const; - void SetVolume(short volume); - void JumpToPosition(short relativePosition); - musik::core::TrackPtr CurrentTrack(); // Public signals diff --git a/src/core/audio/Transport.cpp b/src/core/audio/Transport.cpp index adc718822..ef1bf5926 100644 --- a/src/core/audio/Transport.cpp +++ b/src/core/audio/Transport.cpp @@ -121,16 +121,26 @@ void Transport::Stop(size_t idx) } } -void Transport::JumpToPosition(short relativePosition) +void Transport::JumpToPosition(unsigned long position) { AudioStream* stream = this->openStreams[0]; - unsigned long posMS = stream->GetLength() * relativePosition / 100; - - stream->SetPosition(posMS); + stream->SetPosition(position); } -void Transport::ChangeVolume(short volume) +unsigned long Transport::FirstTrackPosition() const +{ + if (this->openStreams.size() > 0) return this->openStreams[0]->GetPosition(); + else return 0; +} + +unsigned long Transport::FirstTrackLength() const +{ + if (this->openStreams.size() > 0) return this->openStreams[0]->GetLength(); + else return 0; +} + +void Transport::SetVolume(short volume) { if (volume < 0 || volume > 100) { diff --git a/src/core/audio/Transport.h b/src/core/audio/Transport.h index a10ca20f5..780d58fe6 100644 --- a/src/core/audio/Transport.h +++ b/src/core/audio/Transport.h @@ -59,9 +59,11 @@ public: ~Transport(); public: void Start(const utfstring path); public: void Stop(size_t idx); -public: void JumpToPosition(short relativePosition); +public: void JumpToPosition(unsigned long position); +public: unsigned long FirstTrackPosition() const; +public: unsigned long FirstTrackLength() const; -public: void ChangeVolume(short volume); +public: void SetVolume(short volume); public: short Volume() const { return currVolume; }; public: size_t NumOfStreams() const; diff --git a/src/cube/TransportController.cpp b/src/cube/TransportController.cpp index af98fe404..4c37bf7df 100644 --- a/src/cube/TransportController.cpp +++ b/src/cube/TransportController.cpp @@ -37,6 +37,9 @@ ////////////////////////////////////////////////////////////////////////////// #include + +#include + #include #include @@ -48,6 +51,8 @@ using namespace musik::cube; /*ctor*/ TransportController::TransportController(TransportView& transportView) : transportView(transportView) +, playbackSliderTimer(500) +, playbackSliderMouseDown(false) { this->transportView.Created.connect( this, &TransportController::OnViewCreated); @@ -74,12 +79,22 @@ void TransportController::OnViewCreated(Window* window) this, &TransportController::OnVolumeSliderChange); this->transportView.volumeSlider->SetPosition( - musik::core::PlaybackQueue::Instance().Volume()); + musik::core::PlaybackQueue::Instance().Transport().Volume()); musik::core::PlaybackQueue::Instance().CurrentTrackChanged.connect(this,&TransportController::OnTrackChange); this->transportView.playbackSlider->Repositioned.connect( this, &TransportController::OnPlaybackSliderChange); + + this->transportView.playbackSlider->MouseButtonDown.connect( + this, &TransportController::OnPlaybackSliderMouseDown); + + this->transportView.playbackSlider->MouseButtonUp.connect( + this, &TransportController::OnPlaybackSliderMouseUp); + + this->playbackSliderTimer.ConnectToWindow(this->transportView.playbackSlider); + + this->playbackSliderTimer.OnTimout.connect(this, &TransportController::OnPlaybackSliderTimerTimedOut); } void TransportController::OnViewResized(Window* window, Size size) @@ -94,6 +109,8 @@ void TransportController::OnPlayPressed(Button* button) void TransportController::OnStopPressed(Button* button) { musik::core::PlaybackQueue::Instance().Stop(); + this->transportView.playbackSlider->SetPosition(0); + this->playbackSliderTimer.Stop(); } void TransportController::OnNextPressed(Button* button) @@ -108,7 +125,7 @@ void TransportController::OnPreviousPressed(Button* button) void TransportController::OnVolumeSliderChange(Trackbar* trackbar) { - musik::core::PlaybackQueue::Instance().SetVolume(transportView.volumeSlider->Position()); + musik::core::PlaybackQueue::Instance().Transport().SetVolume(transportView.volumeSlider->Position()); } void TransportController::OnTrackChange(musik::core::TrackPtr track){ @@ -133,9 +150,54 @@ void TransportController::OnTrackChange(musik::core::TrackPtr track){ this->transportView.titleLabel->SetCaption(title); this->transportView.artistLabel->SetCaption(artist); + this->transportView.timeDurationLabel->SetCaption(this->FormatTime(musik::core::PlaybackQueue::Instance().Transport().FirstTrackLength())); + + this->transportView.playbackSlider->SetPosition(0); + this->playbackSliderTimer.Start(); } void TransportController::OnPlaybackSliderChange(Trackbar *trackBar) { - musik::core::PlaybackQueue::Instance().JumpToPosition(trackBar->Position()); + unsigned long lengthMs = musik::core::PlaybackQueue::Instance().Transport().FirstTrackLength(); + unsigned long newPosMs = lengthMs * trackBar->Position() / trackBar->Range(); + + //this->playbackSliderTimer.Stop(); + musik::core::PlaybackQueue::Instance().Transport().JumpToPosition(newPosMs); + //this->playbackSliderTimer.Start(); +} + +void TransportController::OnPlaybackSliderTimerTimedOut() +{ + unsigned long currPosMs = musik::core::PlaybackQueue::Instance().Transport().FirstTrackPosition(); + unsigned long lengthMs = musik::core::PlaybackQueue::Instance().Transport().FirstTrackLength(); + unsigned long sliderRange = this->transportView.playbackSlider->Range(); + + this->transportView.timeElapsedLabel->SetCaption(this->FormatTime(currPosMs)); + + if (!this->playbackSliderMouseDown) + { + this->transportView.playbackSlider->SetPosition(sliderRange * currPosMs / lengthMs); + } +} + +void TransportController::OnPlaybackSliderMouseDown(Window* windows, MouseEventFlags flags, Point point) +{ + this->playbackSliderMouseDown = true; +} + +void TransportController::OnPlaybackSliderMouseUp(Window* windows, MouseEventFlags flags, Point point) +{ + this->playbackSliderMouseDown = false; +} + +win32cpp::uistring TransportController::FormatTime(unsigned long ms) +{ + unsigned long seconds = ms / 1000 % 60; + unsigned long minutes = ms / 1000 / 60; + + boost::basic_format format(_T("%1%:%2$02d")); + format % minutes; + format % seconds; + + return format.str(); } \ No newline at end of file diff --git a/src/cube/TransportController.hpp b/src/cube/TransportController.hpp index 2a21b9cb7..0c7191911 100644 --- a/src/cube/TransportController.hpp +++ b/src/cube/TransportController.hpp @@ -42,6 +42,7 @@ #include #include +#include ////////////////////////////////////////////////////////////////////////////// @@ -65,8 +66,16 @@ protected: void OnPreviousPressed(Button* button); protected: void OnVolumeSliderChange(Trackbar* trackbar); protected: void OnTrackChange(musik::core::TrackPtr track); protected: void OnPlaybackSliderChange(Trackbar* trackBar); +protected: void OnPlaybackSliderTimerTimedOut(); +protected: void OnPlaybackSliderMouseDown(Window* windows, MouseEventFlags flags, Point point); +protected: void OnPlaybackSliderMouseUp(Window* windows, MouseEventFlags flags, Point point); protected: TransportView& transportView; + +protected: Timer playbackSliderTimer; +protected: bool playbackSliderMouseDown; + +protected: win32cpp::uistring FormatTime(unsigned long ms); }; ////////////////////////////////////////////////////////////////////////////// diff --git a/src/cube/TransportView.cpp b/src/cube/TransportView.cpp index 246afe794..0c9e9a965 100644 --- a/src/cube/TransportView.cpp +++ b/src/cube/TransportView.cpp @@ -96,7 +96,7 @@ void TransportView::OnCreated() // bottom row layout this->timeElapsedLabel = bottomRowLayout->AddChild(new Label(_T("0:00"))); - this->playbackSlider = bottomRowLayout->AddChild(new Trackbar(0, 100)); + this->playbackSlider = bottomRowLayout->AddChild(new Trackbar(0, 1000)); this->timeDurationLabel = bottomRowLayout->AddChild(new Label(_T("0:00"))); // this->playbackSlider->Resize(100, 20); diff --git a/src/square/ConsoleUI.cpp b/src/square/ConsoleUI.cpp index 960b12015..406e5b2b1 100644 --- a/src/square/ConsoleUI.cpp +++ b/src/square/ConsoleUI.cpp @@ -247,7 +247,7 @@ void ConsoleUI::SetVolume(Args args) void ConsoleUI::SetVolume(short volume) { - transport.ChangeVolume(volume); + transport.SetVolume(volume); } void ConsoleUI::Quit() diff --git a/src/win32cpp/Trackbar.hpp b/src/win32cpp/Trackbar.hpp index b1b848315..fcee57adb 100644 --- a/src/win32cpp/Trackbar.hpp +++ b/src/win32cpp/Trackbar.hpp @@ -79,6 +79,7 @@ public: /*ctor*/ Trackbar( public: // methods void SetRange(short minValue, short maxValue); + int Range() const { return this->maxValue - this->minValue; } int MinValue() const { return this->minValue; } int MaxValue() const { return this->maxValue; } void SetTickFrequency(short tickFrequency = 0);