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

View file

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

View file

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

View file

@ -37,7 +37,7 @@
///////////
// Headers
#include <wx/wxprec.h>
#include "audio_player.h"
#include "audio_player_portaudio.h"
#include "audio_provider.h"
@ -55,6 +55,7 @@ AudioPlayer::~AudioPlayer() {
if (displayTimer) {
displayTimer->Stop();
}
CloseStream();
}
@ -108,3 +109,20 @@ END_EVENT_TABLE()
void AudioPlayer::OnStopAudio(wxCommandEvent &event) {
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();
virtual ~AudioPlayer();
virtual void OpenStream() {}
virtual void CloseStream() {}
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
@ -83,6 +86,8 @@ public:
void SetDisplayTimer(wxTimer *timer);
virtual wxMutex *GetMutex();
static AudioPlayer* GetAudioPlayer();
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);
protected:
void OpenStream();
void CloseStream();
public:
PortAudioPlayer();
~PortAudioPlayer();
void OpenStream();
void CloseStream();
void Play(__int64 start,__int64 count);
void Stop(bool timerToo=true);
bool IsPlaying() { return playing; }

View file

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

View file

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

View file

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

View file

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