From 767409e346bc1d0f80f835ecbfd955b91cd3ae1d Mon Sep 17 00:00:00 2001 From: "bjorn.olievier" Date: Fri, 16 May 2008 07:41:03 +0000 Subject: [PATCH] Fixed problem where TransportView stopped updating when next track was started. Added TrackPtr parameter to some audio events for this. Square temporarily broken. --- src/core/PlaybackQueue.cpp | 5 +-- src/core/audio/AudioStream.cpp | 10 +++--- src/core/audio/AudioStream.h | 7 ++++- src/core/audio/IAudioSource.h | 2 +- src/core/audio/Transport.cpp | 53 ++++++++++++++++++++------------ src/core/audio/Transport.h | 23 ++++++++------ src/cube/TransportController.cpp | 27 ++++++++++------ src/cube/TransportController.hpp | 21 +++++++------ src/cube/TransportView.cpp | 8 ++--- src/square/ConsoleUI.cpp | 10 +++--- 10 files changed, 97 insertions(+), 69 deletions(-) diff --git a/src/core/PlaybackQueue.cpp b/src/core/PlaybackQueue.cpp index 61bbccb1d..0baebf997 100644 --- a/src/core/PlaybackQueue.cpp +++ b/src/core/PlaybackQueue.cpp @@ -98,13 +98,10 @@ void PlaybackQueue::Play(){ TrackPtr track(this->CurrentTrack()); if(track){ - // If current track exists - utfstring path(track->GetValue("path")); - this->Stop(); this->playing = true; - this->transport.Start(path); + this->transport.Start(track); this->paused = false; } diff --git a/src/core/audio/AudioStream.cpp b/src/core/audio/AudioStream.cpp index f28b3caf0..9bc4b1c09 100644 --- a/src/core/audio/AudioStream.cpp +++ b/src/core/audio/AudioStream.cpp @@ -5,11 +5,12 @@ #include #include +using namespace musik::core; using namespace musik::core::audio; unsigned long AudioStream::streamsCreated = 0; -AudioStream::AudioStream(IAudioSource* source, IAudioOutput* output, Transport* owner) +AudioStream::AudioStream(IAudioSource* source, IAudioOutput* output, Transport* owner, TrackPtr track) : audioSource(source) , transport(owner) , playState(PlayStateUnknown) @@ -19,6 +20,7 @@ AudioStream::AudioStream(IAudioSource* source, IAudioOutput* output, Transport* , isFinished(false) , isLast(false) , samplesOut(0) +, track(track) { this->output = output; this->output->SetCallback(this); @@ -136,7 +138,7 @@ bool AudioStream::GetBuffer(float * pAudioBuffer, unsigned long NumSamples) //used for repeatnone where this is the end of line. if(pos >= len && this->isLast) { - transport->EventPlaybackStoppedOk(); + transport->EventPlaybackStoppedOk(this->track); this->playState = PlayStateStopped; } @@ -219,7 +221,7 @@ bool AudioStream::SetPositionMs(unsigned long ms) if(this->fadeState != FadeStateNone) { - this->volume = 1.0; + this->volume = 1.0; this->fadeState = FadeStateNone; } @@ -242,7 +244,7 @@ utfstring AudioStream::ToString() const { std::utfstringstream ss; - ss << this->streamId << " " << this->audioSource->GetSource(); + ss << this->streamId << " " << this->track->GetValue("path"); return ss.str(); } \ No newline at end of file diff --git a/src/core/audio/AudioStream.h b/src/core/audio/AudioStream.h index 651bf8ef2..ce99a58ef 100644 --- a/src/core/audio/AudioStream.h +++ b/src/core/audio/AudioStream.h @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -22,7 +23,7 @@ public: static const unsigned long UnknownLength = ULONG_MAX; - AudioStream(IAudioSource* source, IAudioOutput* output, Transport *owner); + AudioStream(IAudioSource* source, IAudioOutput* output, Transport *owner, musik::core::TrackPtr track); ~AudioStream(); bool Start(); @@ -39,6 +40,8 @@ public: bool SetPositionMs(unsigned long ms); bool isFinished; + musik::core::TrackPtr Track() const { return this->track; }; + unsigned long GetStreamId() const { return this->streamId; }; utfstring ToString() const; @@ -48,6 +51,8 @@ private: IAudioOutput* output; AudioPacketizer packetizer; + musik::core::TrackPtr track; + PlayState playState; FadeState fadeState; diff --git a/src/core/audio/IAudioSource.h b/src/core/audio/IAudioSource.h index 2b89260a2..eae0ddfdb 100644 --- a/src/core/audio/IAudioSource.h +++ b/src/core/audio/IAudioSource.h @@ -16,7 +16,7 @@ public: virtual bool GetFormat(unsigned long * SampleRate, unsigned long * Ch public: virtual bool GetBuffer(float ** ppBuffer, unsigned long * NumSamples) = 0; // return false to signal that we are done decoding. public: virtual bool Open(const utfchar* source) = 0; -public: virtual const utfchar* GetSource() const = 0; +public: virtual const utfchar* GetSource() const = 0; // TODO: remove? }; class IAudioSourceSupplier diff --git a/src/core/audio/Transport.cpp b/src/core/audio/Transport.cpp index 9ca2f0e50..e139215e1 100644 --- a/src/core/audio/Transport.cpp +++ b/src/core/audio/Transport.cpp @@ -41,10 +41,13 @@ #include #include + +using namespace musik::core; using namespace musik::core::audio; -Transport::Transport() : - currVolume(100) //TODO: preference or previous value +Transport::Transport() +: currVolume(100) //TODO: preference or previous value +, activeStream(NULL) { this->registeredSourceSuppliers = PluginFactory::Instance().QueryInterface< IAudioSourceSupplier, @@ -63,15 +66,17 @@ Transport::~Transport() delete this->openStreams[i]; } this->openStreams.clear(); + + this->activeStream = NULL; } -void Transport::Start(const utfstring path) +void Transport::Start(TrackPtr trackPtr) { this->RemoveFinishedStreams(); bool success = false; - AudioStream * audioStream = this->CreateStream(path.c_str()); + AudioStream * audioStream = this->CreateStream(trackPtr); if (audioStream != NULL) { @@ -87,21 +92,24 @@ void Transport::Start(const utfstring path) if (success) { - this->EventPlaybackStartedOk(); + this->activeStream = audioStream; + this->EventPlaybackStartedOk(trackPtr); } else { - this->EventPlaybackStartedFail(); + this->EventPlaybackStartedFail(trackPtr); } } void Transport::Stop() { + //TODO: review + this->RemoveFinishedStreams(); if (this->openStreams.empty()) { - this->EventPlaybackStoppedFail(); +// this->EventPlaybackStoppedFail(); //TODO: still necessary ? return; } @@ -109,15 +117,18 @@ void Transport::Stop() if (stream->Stop()) { + if (stream == this->activeStream) + this->activeStream = NULL; + + this->EventPlaybackStoppedOk(stream->Track()); + delete stream; this->openStreams.erase(this->openStreams.begin()); - - this->EventPlaybackStoppedOk(); } else { - this->EventPlaybackStoppedFail(); + this->EventPlaybackStoppedFail(stream->Track()); } } @@ -175,14 +186,14 @@ void Transport::SetTrackPosition(unsigned long position) unsigned long Transport::TrackPosition() const { - if (this->openStreams.size() > 0) return this->openStreams[0]->PositionMs(); - else return 0; + if (this->activeStream) return this->activeStream->PositionMs(); + else return 0; } unsigned long Transport::TrackLength() const { - if (this->openStreams.size() > 0) return this->openStreams[0]->LengthMs(); - else return 0; + if (this->activeStream) return this->openStreams[0]->LengthMs(); + else return 0; } void Transport::SetVolume(short volume) @@ -226,7 +237,7 @@ AudioStreamOverview Transport::StreamsOverview() const return overview; } -AudioStream* Transport::CreateStream(const utfstring sourcePath) +AudioStream* Transport::CreateStream(TrackPtr trackPtr) { boost::shared_ptr supplier; IAudioSource* audioSource; @@ -238,14 +249,16 @@ AudioStream* Transport::CreateStream(const utfstring sourcePath) } IAudioOutput* audioOutput = - this->registeredOutputSuppliers[0]->CreateAudioOutput(); //TODO: deal with multiple outputs simultaneously + this->registeredOutputSuppliers[0]->CreateAudioOutput(); + + const utfchar* filePath = trackPtr->GetValue("path"); SourceSupplierList::const_iterator it; for(it = this->registeredSourceSuppliers.begin(); it != this->registeredSourceSuppliers.end(); it++) { supplier = *it; - if (supplier->CanHandle(sourcePath.c_str())) + if (supplier->CanHandle(filePath)) { break; } @@ -255,9 +268,9 @@ AudioStream* Transport::CreateStream(const utfstring sourcePath) { audioSource = supplier->CreateAudioSource(); - if (audioSource != NULL && audioSource->Open(sourcePath.c_str())) + if (audioSource != NULL && audioSource->Open(filePath)) { - audioStream = new AudioStream(audioSource, audioOutput, this); + audioStream = new AudioStream(audioSource, audioOutput, this, trackPtr); } } @@ -277,4 +290,4 @@ void Transport::RemoveFinishedStreams() --i; } } -} +} \ No newline at end of file diff --git a/src/core/audio/Transport.h b/src/core/audio/Transport.h index 30738936c..5445aeefb 100644 --- a/src/core/audio/Transport.h +++ b/src/core/audio/Transport.h @@ -41,6 +41,7 @@ #include #include +#include namespace musik { namespace core { namespace audio { @@ -64,14 +65,15 @@ public: Transport(); ~Transport(); - ///\brief Start a new audiostream based on a file path or URL. - void Start(const utfstring path); + ///\brief Start a new audiostream based on a Track. + ///\param trackPtr Pointer to the track + void Start(TrackPtr trackPtr); ///\brief Stop the active stream. All resources used are released. - void Stop(); + void Stop(); ///\brief Pause the active stream. All resources stay in use. - bool Pause(); + bool Pause(); ///\brief Resume the active stream - bool Resume(); + bool Resume(); ///\brief Jump to a given position in the active stream. ///\param position New position in milliseconds @@ -102,8 +104,9 @@ private: OutputSupplierList registeredOutputSuppliers; std::vector openStreams; + AudioStream* activeStream; - AudioStream* CreateStream(const utfstring sourceString); + AudioStream* CreateStream(TrackPtr trackPtr); void RemoveFinishedStreams(); short currVolume; @@ -111,13 +114,13 @@ private: // Signals public: ///\brief Emitted when Start() completed successfully - sigslot::signal0<> EventPlaybackStartedOk; + sigslot::signal1 EventPlaybackStartedOk; ///\brief Emitted when Start() failed - sigslot::signal0<> EventPlaybackStartedFail; + sigslot::signal1 EventPlaybackStartedFail; ///\brief Emitted when Stop() completed successfully - sigslot::signal0<> EventPlaybackStoppedOk; + sigslot::signal1 EventPlaybackStoppedOk; ///\brief Emitted when Stop() failed - sigslot::signal0<> EventPlaybackStoppedFail; + sigslot::signal1 EventPlaybackStoppedFail; ///\brief Emitted when Pause() completed successfully sigslot::signal0<> EventPlaybackPausedOk; diff --git a/src/cube/TransportController.cpp b/src/cube/TransportController.cpp index f59fb39cb..ec9a2d42a 100644 --- a/src/cube/TransportController.cpp +++ b/src/cube/TransportController.cpp @@ -193,11 +193,11 @@ void TransportController::OnPlaybackSliderMouseUp(Window* windows, MouseEventFla this->playbackSliderMouseDown = false; } -void TransportController::OnPlaybackStarted() +void TransportController::OnPlaybackStarted(musik::core::TrackPtr track) { if(!win32cpp::ApplicationThread::InMainThread()) { - win32cpp::ApplicationThread::Call0(this, &TransportController::OnPlaybackStarted); + win32cpp::ApplicationThread::Call1(this, &TransportController::OnPlaybackStarted, track); return; } @@ -205,16 +205,20 @@ void TransportController::OnPlaybackStarted() this->transportView.playButton->SetCaption(_T("Pause")); this->transportView.timeDurationLabel->SetCaption(this->FormatTime(musik::core::PlaybackQueue::Instance().Transport().TrackLength())); - + // this->transportView.timeDurationLabel->SetCaption(track->GetValue("duration")); + this->transportView.playbackSlider->SetPosition(0); + this->playbackSliderTimer.Start(); + + this->displayedTrack = track; } -void TransportController::OnPlaybackStopped() +void TransportController::OnPlaybackStopped(musik::core::TrackPtr track) { if(!win32cpp::ApplicationThread::InMainThread()) { - win32cpp::ApplicationThread::Call0(this, &TransportController::OnPlaybackStopped); + win32cpp::ApplicationThread::Call1(this, &TransportController::OnPlaybackStopped, track); return; } @@ -222,11 +226,14 @@ void TransportController::OnPlaybackStopped() this->paused = false; this->transportView.playButton->SetCaption(_T("Play")); - this->transportView.playbackSlider->SetPosition(0); - this->playbackSliderTimer.Stop(); - - this->transportView.timeElapsedLabel->SetCaption(_T("0:00")); - this->transportView.timeDurationLabel->SetCaption(_T("0:00")); + if (this->displayedTrack->id == track->id) // For out of order signals + { + this->transportView.playbackSlider->SetPosition(0); + this->playbackSliderTimer.Stop(); + + this->transportView.timeElapsedLabel->SetCaption(_T("0:00")); + this->transportView.timeDurationLabel->SetCaption(_T("0:00")); + } } void TransportController::OnPlaybackPaused() diff --git a/src/cube/TransportController.hpp b/src/cube/TransportController.hpp index 9e3bc523b..25a419d37 100644 --- a/src/cube/TransportController.hpp +++ b/src/cube/TransportController.hpp @@ -58,8 +58,8 @@ public: /*ctor*/ TransportController(TransportView& transportView); protected: - void OnViewCreated(Window* window); - void OnViewResized(Window* window, Size size); + void OnViewCreated(Window* window); + void OnViewResized(Window* window, Size size); void OnPlayPressed(Button* button); void OnStopPressed(Button* button); @@ -68,12 +68,12 @@ protected: void OnVolumeSliderChange(Trackbar* trackbar); void OnTrackChange(musik::core::TrackPtr track); 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); + void OnPlaybackSliderTimerTimedOut(); + void OnPlaybackSliderMouseDown(Window* windows, MouseEventFlags flags, Point point); + void OnPlaybackSliderMouseUp(Window* windows, MouseEventFlags flags, Point point); - void OnPlaybackStarted(); - void OnPlaybackStopped(); + void OnPlaybackStarted(musik::core::TrackPtr track); + void OnPlaybackStopped(musik::core::TrackPtr track); void OnPlaybackPaused(); void OnPlaybackResumed(); @@ -82,10 +82,11 @@ protected: void OnPlaybackSliderMouseUp(Window* windows, MouseEventFlags fla bool paused; bool playing; -protected: Timer playbackSliderTimer; -protected: bool playbackSliderMouseDown; + Timer playbackSliderTimer; + bool playbackSliderMouseDown; -protected: win32cpp::uistring FormatTime(unsigned long ms); + musik::core::TrackPtr displayedTrack; + win32cpp::uistring FormatTime(unsigned long ms); }; ////////////////////////////////////////////////////////////////////////////// diff --git a/src/cube/TransportView.cpp b/src/cube/TransportView.cpp index d65271dad..2fa530441 100644 --- a/src/cube/TransportView.cpp +++ b/src/cube/TransportView.cpp @@ -60,10 +60,10 @@ void TransportView::OnCreated() topRowLayout->SetDefaultChildFill(false); topRowLayout->SetDefaultChildAlignment(ChildAlignMiddle); - this->prevButton = topRowLayout->AddChild(new Button(_T("prev"))); - this->playButton = topRowLayout->AddChild(new Button(_T("play"))); - this->stopButton = topRowLayout->AddChild(new Button(_T("stop"))); - this->nextButton = topRowLayout->AddChild(new Button(_T("next"))); + this->prevButton = topRowLayout->AddChild(new Button(_T("Prev"))); + this->playButton = topRowLayout->AddChild(new Button(_T("Play"))); + this->stopButton = topRowLayout->AddChild(new Button(_T("Stop"))); + this->nextButton = topRowLayout->AddChild(new Button(_T("Next"))); // this->prevButton->Resize(50, 28); this->playButton->Resize(50, 28); diff --git a/src/square/ConsoleUI.cpp b/src/square/ConsoleUI.cpp index 3e9a22321..f3dea78b9 100644 --- a/src/square/ConsoleUI.cpp +++ b/src/square/ConsoleUI.cpp @@ -49,11 +49,11 @@ ConsoleUI::ConsoleUI() : shouldQuit(false) , audioEventHandler(this) { - this->transport.EventPlaybackStartedOk.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStartedOk); - this->transport.EventPlaybackStartedFail.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStartedFail); +// this->transport.EventPlaybackStartedOk.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStartedOk); +// this->transport.EventPlaybackStartedFail.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStartedFail); - this->transport.EventPlaybackStoppedOk.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStoppedOk); - this->transport.EventPlaybackStoppedFail.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStoppedFail); +// this->transport.EventPlaybackStoppedOk.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStoppedOk); +// this->transport.EventPlaybackStoppedFail.connect(&audioEventHandler, &DummyAudioEventHandler::OnPlaybackStoppedFail); this->transport.EventVolumeChangedOk.connect(&audioEventHandler, &DummyAudioEventHandler::OnVolumeChangedOk); this->transport.EventVolumeChangedFail.connect(&audioEventHandler, &DummyAudioEventHandler::OnVolumeChangedFail); @@ -171,7 +171,7 @@ void ConsoleUI::PlayFile(Args args) for (int i = 0; i < repeat; ++i) { - transport.Start(filename.c_str()); +// transport.Start(filename.c_str()); //TODO: fix to use TrackPtr if (delay) { Sleep(delay);