Finished separation of audio provider and player

Originally committed to SVN as r171.
This commit is contained in:
Rodrigo Braz Monteiro 2006-02-25 07:41:18 +00:00
parent 93b35641a4
commit 18d948ef97
10 changed files with 89 additions and 42 deletions

View File

@ -283,7 +283,7 @@ void AudioBox::OnVerticalZoom(wxScrollEvent &event) {
float value = pow(float(pos)/50.0f,3); float value = pow(float(pos)/50.0f,3);
audioDisplay->SetScale(value); audioDisplay->SetScale(value);
if (VerticalLink->GetValue()) { if (VerticalLink->GetValue()) {
audioDisplay->provider->SetVolume(value); audioDisplay->player->SetVolume(value);
VolumeBar->SetValue(pos); VolumeBar->SetValue(pos);
} }
} }
@ -296,7 +296,7 @@ void AudioBox::OnVolume(wxScrollEvent &event) {
int pos = event.GetPosition(); int pos = event.GetPosition();
if (pos < 1) pos = 1; if (pos < 1) pos = 1;
if (pos > 100) pos = 100; if (pos > 100) pos = 100;
audioDisplay->provider->SetVolume(pow(float(pos)/50.0f,3)); audioDisplay->player->SetVolume(pow(float(pos)/50.0f,3));
} }
} }
@ -309,7 +309,7 @@ void AudioBox::OnVerticalLink(wxCommandEvent &event) {
if (pos > 100) pos = 100; if (pos > 100) pos = 100;
float value = pow(float(pos)/50.0f,3); float value = pow(float(pos)/50.0f,3);
if (VerticalLink->GetValue()) { if (VerticalLink->GetValue()) {
audioDisplay->provider->SetVolume(value); audioDisplay->player->SetVolume(value);
VolumeBar->SetValue(pos); VolumeBar->SetValue(pos);
} }
VolumeBar->Enable(!VerticalLink->GetValue()); VolumeBar->Enable(!VerticalLink->GetValue());

View File

@ -79,6 +79,7 @@ AudioDisplay::AudioDisplay(wxWindow *parent,VideoDisplay *display)
oldCurPos = 0; oldCurPos = 0;
scale = 1.0f; scale = 1.0f;
provider = NULL; provider = NULL;
player = NULL;
video = display; video = display;
hold = 0; hold = 0;
hasFocus = (wxWindow::FindFocus() == this); hasFocus = (wxWindow::FindFocus() == this);
@ -99,11 +100,13 @@ AudioDisplay::AudioDisplay(wxWindow *parent,VideoDisplay *display)
////////////// //////////////
// Destructor // Destructor
AudioDisplay::~AudioDisplay() { AudioDisplay::~AudioDisplay() {
if (provider) delete provider; if (player) player->CloseStream();
if (origImage) delete origImage; delete provider;
if (spectrumDisplay) delete spectrumDisplay; delete player;
if (peak) delete peak; delete origImage;
if (min) delete min; delete spectrumDisplay;
delete peak;
delete min;
} }
@ -740,20 +743,30 @@ void AudioDisplay::SetScale(float _scale) {
////////////////// //////////////////
// Load from file // Load from file
void AudioDisplay::SetFile(wxString file) { void AudioDisplay::SetFile(wxString file) {
// Unload
if (file.IsEmpty()) { if (file.IsEmpty()) {
if (provider) if (player) player->CloseStream();
delete provider; delete provider;
delete player;
provider = NULL; provider = NULL;
player = NULL;
Reset(); Reset();
loaded = false; loaded = false;
} }
// Load
else { else {
SetFile(_T("")); SetFile(_T(""));
try { try {
//provider = new AudioProvider(file, this); // Get provider
provider = AudioProvider::GetAudioProvider(file, this); provider = AudioProvider::GetAudioProvider(file, this);
// Get player
player = AudioPlayer::GetAudioPlayer();
player->SetDisplayTimer(&UpdateTimer);
player->SetProvider(provider);
player->OpenStream();
loaded = true; loaded = true;
// Add to recent // Add to recent
@ -860,16 +873,16 @@ void AudioDisplay::Play(int start,int end) {
if (end < start) end = start; if (end < start) end = start;
// Call play // Call play
provider->Play(start,end-start); player->Play(start,end-start);
} }
//////// ////////
// Stop // Stop
void AudioDisplay::Stop() { void AudioDisplay::Stop() {
if (!provider) return; if (!player) return;
provider->Stop(); player->Stop();
} }
@ -1122,7 +1135,7 @@ void AudioDisplay::OnMouseEvent(wxMouseEvent& event) {
} }
// Cursor drawing // Cursor drawing
if (!provider->IsPlaying()) { if (!player->IsPlaying()) {
// Draw bg // Draw bg
wxClientDC dc(this); wxClientDC dc(this);
dc.BeginDrawing(); dc.BeginDrawing();
@ -1186,7 +1199,7 @@ void AudioDisplay::OnMouseEvent(wxMouseEvent& event) {
if (syl != -1) { if (syl != -1) {
int start = karaoke->syllables.at(syl).position * 10 + dialogue->Start.GetMS(); int start = karaoke->syllables.at(syl).position * 10 + dialogue->Start.GetMS();
int count = karaoke->syllables.at(syl).length * 10; int count = karaoke->syllables.at(syl).length * 10;
provider->Play(GetSampleAtMS(start),GetSampleAtMS(count)); player->Play(GetSampleAtMS(start),GetSampleAtMS(count));
} }
} }
} }
@ -1207,7 +1220,7 @@ void AudioDisplay::OnMouseEvent(wxMouseEvent& event) {
if (event.ButtonDown(wxMOUSE_BTN_RIGHT)) { if (event.ButtonDown(wxMOUSE_BTN_RIGHT)) {
curEndMS = GetMSAtX(x); curEndMS = GetMSAtX(x);
mod = true; mod = true;
provider->SetEndPosition(GetSampleAtX(x)); player->SetEndPosition(GetSampleAtX(x));
} }
// Modified, commit changes // Modified, commit changes
@ -1382,7 +1395,7 @@ void AudioDisplay::OnMouseEvent(wxMouseEvent& event) {
// Update stuff // Update stuff
if (updated) { if (updated) {
provider->SetEndPosition(GetSampleAtX(selEnd)); player->SetEndPosition(GetSampleAtX(selEnd));
wxCursor cursor(wxCURSOR_SIZEWE); wxCursor cursor(wxCURSOR_SIZEWE);
SetCursor(cursor); SetCursor(cursor);
UpdateImage(true); UpdateImage(true);
@ -1450,12 +1463,12 @@ void AudioDisplay::OnSize(wxSizeEvent &event) {
// Timer event // Timer event
void AudioDisplay::OnUpdateTimer(wxTimerEvent &event) { void AudioDisplay::OnUpdateTimer(wxTimerEvent &event) {
// Get lock and check if it's OK // Get lock and check if it's OK
if (provider->GetMutex()) { if (player->GetMutex()) {
wxMutexLocker locker(*provider->GetMutex()); wxMutexLocker locker(*player->GetMutex());
if (!locker.IsOk()) return; if (!locker.IsOk()) return;
} }
if (!provider->IsPlaying()) return; if (!player->IsPlaying()) return;
// Get DCs // Get DCs
//wxMutexGuiEnter(); //wxMutexGuiEnter();
@ -1469,14 +1482,14 @@ void AudioDisplay::OnUpdateTimer(wxTimerEvent &event) {
// Draw cursor // Draw cursor
int curpos = -1; int curpos = -1;
if (provider->IsPlaying()) { if (player->IsPlaying()) {
if (provider->GetCurrentPosition() > provider->GetStartPosition() && provider->GetCurrentPosition() < provider->GetEndPosition()) { if (player->GetCurrentPosition() > player->GetStartPosition() && player->GetCurrentPosition() < player->GetEndPosition()) {
dc.SetPen(wxPen(Options.AsColour(_T("Audio Play cursor")))); dc.SetPen(wxPen(Options.AsColour(_T("Audio Play cursor"))));
curpos = GetXAtSample(provider->GetCurrentPosition()); curpos = GetXAtSample(player->GetCurrentPosition());
dc.DrawLine(curpos,0,curpos,h); dc.DrawLine(curpos,0,curpos,h);
} }
else if (provider->GetCurrentPosition() > provider->GetEndPosition() + 8192) { else if (player->GetCurrentPosition() > player->GetEndPosition() + 8192) {
provider->Stop(); player->Stop();
} }
} }
oldCurPos = curpos; oldCurPos = curpos;
@ -1521,7 +1534,7 @@ void AudioDisplay::OnKeyDown(wxKeyEvent &event) {
// Play // Play
if (Hotkeys.IsPressed(_T("Audio Play")) || Hotkeys.IsPressed(_T("Audio Play Alt"))) { if (Hotkeys.IsPressed(_T("Audio Play")) || Hotkeys.IsPressed(_T("Audio Play Alt"))) {
if (provider->IsPlaying()) Stop(); if (player->IsPlaying()) Stop();
else { else {
int start=0,end=0; int start=0,end=0;
GetTimesSelection(start,end); GetTimesSelection(start,end);

View File

@ -42,6 +42,7 @@
// Headers // Headers
#include <wx/wxprec.h> #include <wx/wxprec.h>
#include "audio_provider.h" #include "audio_provider.h"
#include "audio_player.h"
////////////// //////////////
@ -52,6 +53,7 @@ class AudioBox;
class AudioKaraoke; class AudioKaraoke;
class VideoDisplay; class VideoDisplay;
///////////////// /////////////////
// Display class // Display class
class AudioDisplay: public wxWindow { class AudioDisplay: public wxWindow {
@ -109,10 +111,12 @@ private:
void UpdatePosition(int pos,bool IsSample=false); void UpdatePosition(int pos,bool IsSample=false);
public: public:
AudioProvider *provider;
AudioPlayer *player;
bool NeedCommit; bool NeedCommit;
bool loaded; bool loaded;
int w,h; int w,h;
AudioProvider *provider;
AudioBox *box; AudioBox *box;
AudioKaraoke *karaoke; AudioKaraoke *karaoke;
wxScrollBar *ScrollBar; wxScrollBar *ScrollBar;

View File

@ -37,7 +37,7 @@
/////////// ///////////
// Headers // Headers
#include <wx/wxprec.h> #include <wx/wxprec.h>
#include "audio_player.h" #include "audio_player_portaudio.h"
#include "audio_provider.h" #include "audio_provider.h"
@ -55,6 +55,7 @@ AudioPlayer::~AudioPlayer() {
if (displayTimer) { if (displayTimer) {
displayTimer->Stop(); displayTimer->Stop();
} }
CloseStream();
} }
@ -108,3 +109,20 @@ END_EVENT_TABLE()
void AudioPlayer::OnStopAudio(wxCommandEvent &event) { void AudioPlayer::OnStopAudio(wxCommandEvent &event) {
Stop(false); Stop(false);
} }
//////////////
// Get player
AudioPlayer* AudioPlayer::GetAudioPlayer() {
// Prepare
AudioPlayer *player;
// Get player
player = new PortAudioPlayer;
// Got player?
if (!player) throw _T("Unable to create audio player.");
// Return
return player;
}

View File

@ -63,6 +63,9 @@ public:
AudioPlayer(); AudioPlayer();
virtual ~AudioPlayer(); virtual ~AudioPlayer();
virtual void OpenStream() {}
virtual void CloseStream() {}
virtual void Play(__int64 start,__int64 count)=0; // Play sample range virtual void Play(__int64 start,__int64 count)=0; // Play sample range
virtual void Stop(bool timerToo=true)=0; // Stop playing virtual void Stop(bool timerToo=true)=0; // Stop playing
virtual void RequestStop(); // Request it to stop playing in a thread-safe way virtual void RequestStop(); // Request it to stop playing in a thread-safe way
@ -83,6 +86,8 @@ public:
void SetDisplayTimer(wxTimer *timer); void SetDisplayTimer(wxTimer *timer);
virtual wxMutex *GetMutex(); virtual wxMutex *GetMutex();
static AudioPlayer* GetAudioPlayer();
DECLARE_EVENT_TABLE() DECLARE_EVENT_TABLE()
}; };

View File

@ -65,14 +65,13 @@ private:
static int PortAudioPlayer::paCallback(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData); static int PortAudioPlayer::paCallback(void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, PaTimestamp outTime, void *userData);
protected:
void OpenStream();
void CloseStream();
public: public:
PortAudioPlayer(); PortAudioPlayer();
~PortAudioPlayer(); ~PortAudioPlayer();
void OpenStream();
void CloseStream();
void Play(__int64 start,__int64 count); void Play(__int64 start,__int64 count);
void Stop(bool timerToo=true); void Stop(bool timerToo=true);
bool IsPlaying() { return playing; } bool IsPlaying() { return playing; }

View File

@ -177,8 +177,19 @@ AudioProvider *AudioProvider::GetAudioProvider(wxString filename, AudioDisplay *
throw _T("Could not initialize any audio provider."); throw _T("Could not initialize any audio provider.");
} }
// Set up provider // Change provider to RAM/HD cache if needed
provider->SetDisplayTimer(&display->UpdateTimer); if (false) {
AudioProvider *final = NULL;
//final = new RAMAudioProvider(provider);
//final = new HDAudioProvider(provider);
// Reassign
if (final) {
delete provider;
provider = final;
}
}
// Return // Return
return provider; return provider;

View File

@ -43,7 +43,7 @@
#include <fstream> #include <fstream>
#include <time.h> #include <time.h>
#include "avisynth_wrap.h" #include "avisynth_wrap.h"
#include "audio_player_portaudio.h" #include "audio_player.h"
////////////// //////////////
@ -53,7 +53,7 @@ class AudioDisplay;
//////////////////////// ////////////////////////
// Audio provider class // Audio provider class
class AudioProvider : public PortAudioPlayer { class AudioProvider {
private: private:
void *raw; void *raw;
int raw_len; int raw_len;

View File

@ -54,7 +54,6 @@
////////////// //////////////
// Constructor // Constructor
AvisynthAudioProvider::AvisynthAudioProvider(wxString _filename) { AvisynthAudioProvider::AvisynthAudioProvider(wxString _filename) {
SetProvider(this);
type = AUDIO_PROVIDER_NONE; type = AUDIO_PROVIDER_NONE;
blockcache = NULL; blockcache = NULL;
blockcount = 0; blockcount = 0;
@ -64,7 +63,6 @@ AvisynthAudioProvider::AvisynthAudioProvider(wxString _filename) {
try { try {
OpenAVSAudio(); OpenAVSAudio();
OpenStream();
} }
catch (...) { catch (...) {
Unload(); Unload();
@ -76,7 +74,6 @@ AvisynthAudioProvider::AvisynthAudioProvider(wxString _filename) {
////////////// //////////////
// Destructor // Destructor
AvisynthAudioProvider::~AvisynthAudioProvider() { AvisynthAudioProvider::~AvisynthAudioProvider() {
CloseStream();
Unload(); Unload();
} }

View File

@ -887,8 +887,8 @@ void VideoDisplay::OnPlayTimer(wxTimerEvent &event) {
// Sync audio // Sync audio
if (nextFrame % 10 == 0) { if (nextFrame % 10 == 0) {
__int64 audPos = audio->GetSampleAtMS(VFR_Output.GetTimeAtFrame(nextFrame)); __int64 audPos = audio->GetSampleAtMS(VFR_Output.GetTimeAtFrame(nextFrame));
__int64 curPos = audio->provider->GetCurrentPosition(); __int64 curPos = audio->player->GetCurrentPosition();
if (abs(int(audPos-curPos)) > audio->provider->GetSampleRate() / 10) audio->provider->SetCurrentPosition(audPos); if (abs(int(audPos-curPos)) > audio->provider->GetSampleRate() / 10) audio->player->SetCurrentPosition(audPos);
} }
} }