From 68431d0c1a44c44d55f579448a21b0a60abb5520 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Sat, 25 Feb 2006 04:54:21 +0000 Subject: [PATCH] Very early abstraction of audio classes Originally committed to SVN as r167. --- core/audio_player.cpp | 85 ++++++++++++++++++++++++++++++++++++ core/audio_player.h | 84 +++++++++++++++++++++++++++++++++++ core/audio_provider.cpp | 23 ---------- core/audio_provider.h | 34 +++++++-------- core/video_provider_lavc.cpp | 6 +-- 5 files changed, 187 insertions(+), 45 deletions(-) create mode 100644 core/audio_player.cpp create mode 100644 core/audio_player.h diff --git a/core/audio_player.cpp b/core/audio_player.cpp new file mode 100644 index 000000000..737b95411 --- /dev/null +++ b/core/audio_player.cpp @@ -0,0 +1,85 @@ +// Copyright (c) 2005, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +/////////// +// Headers +#include +#include "audio_player.h" +#include "audio_provider.h" + + +/////////////// +// Constructor +AudioPlayer::AudioPlayer() { + provider = NULL; +} + + +////////////// +// Destructor +AudioPlayer::~AudioPlayer() { +} + + +//////////////// +// Set provider +void AudioPlayer::SetProvider(AudioProvider *_provider) { + provider = _provider; +} + + +///////////////////// +// Ask to stop later +void AudioPlayer::RequestStop() { + wxCommandEvent event(wxEVT_STOP_AUDIO, 1000); + event.SetEventObject(this); + wxMutexGuiEnter(); + AddPendingEvent(event); + wxMutexGuiLeave(); +} + + +///////// +// Event +DEFINE_EVENT_TYPE(wxEVT_STOP_AUDIO) + +BEGIN_EVENT_TABLE(AudioPlayer, wxEvtHandler) + EVT_COMMAND (1000, wxEVT_STOP_AUDIO, AudioPlayer::OnStopAudio) +END_EVENT_TABLE() + +void AudioPlayer::OnStopAudio(wxCommandEvent &event) { + Stop(false); +} diff --git a/core/audio_player.h b/core/audio_player.h new file mode 100644 index 000000000..9e93fb380 --- /dev/null +++ b/core/audio_player.h @@ -0,0 +1,84 @@ +// Copyright (c) 2005, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +#pragma once + + +/////////// +// Headers +#include + + +////////////// +// Prototypes +class AudioProvider; + + +/////////////////////////// +// Audio Player base class +class AudioPlayer : public wxEvtHandler { +private: + void OnStopAudio(wxCommandEvent &event); + +protected: + AudioProvider *provider; + +public: + AudioPlayer(); + virtual ~AudioPlayer(); + + virtual void Play(__int64 start,__int64 count)=0; // Play sample range + virtual void Stop(bool timerToo=true)=0; // Stop playing + virtual void RequestStop(); // Request it to stop playing in a thread-safe way + + virtual void SetVolume(double volume)=0; + virtual double GetVolume()=0; + + void SetProvider(AudioProvider *provider); + + virtual __int64 GetEndPosition()=0; + virtual __int64 GetCurrentPosition()=0; + virtual void SetEndPosition(__int64 pos)=0; + virtual void SetCurrentPosition(__int64 pos)=0; + + DECLARE_EVENT_TABLE() +}; + + +///////// +// Event +DECLARE_EVENT_TYPE(wxEVT_STOP_AUDIO, -1) + diff --git a/core/audio_provider.cpp b/core/audio_provider.cpp index 38d6e7fb5..1a2240452 100644 --- a/core/audio_provider.cpp +++ b/core/audio_provider.cpp @@ -581,26 +581,3 @@ void AudioProvider::CloseStream() { Pa_CloseStream(stream); } catch (...) {} } - -///////////////////// -// Ask to stop later -void AudioProvider::RequestStop() { - wxCommandEvent event(wxEVT_STOP_AUDIO, 1000); - event.SetEventObject(this); - wxMutexGuiEnter(); - AddPendingEvent(event); - wxMutexGuiLeave(); -} - - -///////// -// Event -DEFINE_EVENT_TYPE(wxEVT_STOP_AUDIO) - -BEGIN_EVENT_TABLE(AudioProvider, wxEvtHandler) - EVT_COMMAND (1000, wxEVT_STOP_AUDIO, AudioProvider::OnStopAudio) -END_EVENT_TABLE() - -void AudioProvider::OnStopAudio(wxCommandEvent &event) { - Stop(false); -} diff --git a/core/audio_provider.h b/core/audio_provider.h index e6c93920d..6dfa18568 100644 --- a/core/audio_provider.h +++ b/core/audio_provider.h @@ -34,16 +34,16 @@ // -#ifndef AUDIO_PROVIDER_H -#define AUDIO_PROVIDER_H +#pragma once /////////// // Headers #include -#include "avisynth_wrap.h" #include #include +#include "avisynth_wrap.h" +#include "audio_player.h" ////////////// @@ -63,7 +63,7 @@ enum AudioProviderType { //////////////////////// // Audio provider class -class AudioProvider : public wxEvtHandler, public AviSynthWrapper { +class AudioProvider : public AviSynthWrapper, public AudioPlayer { private: static int pa_refcount; @@ -94,11 +94,11 @@ private: void ConvertToDiskCache(PClip &tempclip); void LoadFromClip(AVSValue clip); void OpenAVSAudio(); - void OnStopAudio(wxCommandEvent &event); static wxString DiskCachePath(); static wxString DiskCacheName(); void SetFile(); void Unload(); + public: wxMutex PAMutex; volatile bool stopping; @@ -112,7 +112,6 @@ public: volatile __int64 realPlayPos; volatile __int64 startMS; void *stream; - clock_t span; AudioProvider(wxString _filename, AudioDisplay *_display); ~AudioProvider(); @@ -122,21 +121,18 @@ public: void GetAudio(void *buf, __int64 start, __int64 count); void GetWaveForm(int *min,int *peak,__int64 start,int w,int h,int samples,float scale); - void Play(__int64 start,__int64 count); - void Stop(bool timerToo=true); - void RequestStop(); - int GetChannels(); __int64 GetNumSamples(); int GetSampleRate(); - DECLARE_EVENT_TABLE() + void Play(__int64 start,__int64 count); + void Stop(bool timerToo=true); + + __int64 GetEndPosition() { return endPos; } + __int64 GetCurrentPosition() { return realPlayPos; } + void SetEndPosition(__int64 pos) { endPos = pos; } + void SetCurrentPosition(__int64 pos) { playPos = pos; realPlayPos = pos; } + + void SetVolume(double vol) { volume = vol; } + double GetVolume() { return volume; } }; - - -///////// -// Event -DECLARE_EVENT_TYPE(wxEVT_STOP_AUDIO, -1) - - -#endif diff --git a/core/video_provider_lavc.cpp b/core/video_provider_lavc.cpp index 85c5931a5..9dcf95a93 100644 --- a/core/video_provider_lavc.cpp +++ b/core/video_provider_lavc.cpp @@ -109,9 +109,6 @@ void LAVCVideoProvider::LoadVideo(wxString filename) { } if (vidStream == -1) throw _T("Could not find a video stream"); - // Check length - if (stream->duration <= 0) throw _T("Returned invalid stream length"); - // Find codec codec = avcodec_find_decoder(codecContext->codec_id); if (!codec) throw _T("Could not find suitable video decoder"); @@ -123,6 +120,9 @@ void LAVCVideoProvider::LoadVideo(wxString filename) { result = avcodec_open(codecContext,codec); if (result < 0) throw _T("Failed to open video decoder"); + // Check length + if (stream->duration <= 0) throw _T("Returned invalid stream length"); + // Set size dar = double(GetSourceWidth()) / GetSourceHeight(); UpdateDisplaySize();