Fix some weirdness in the AVS audio provider

This commit is contained in:
Thomas Goyne 2012-12-22 12:07:14 -08:00
parent 46d2819312
commit 433368dc58
3 changed files with 28 additions and 54 deletions

View File

@ -35,12 +35,6 @@
#include "config.h" #include "config.h"
#ifdef WITH_AVISYNTH #ifdef WITH_AVISYNTH
#include <Mmreg.h>
#include <ctime>
#include <wx/filename.h>
#include "audio_provider_avs.h" #include "audio_provider_avs.h"
#include "audio_controller.h" #include "audio_controller.h"
@ -50,28 +44,23 @@
#include "standard_paths.h" #include "standard_paths.h"
#include "utils.h" #include "utils.h"
/// @brief Constructor #include <wx/filename.h>
/// @param _filename
/// AvisynthAudioProvider::AvisynthAudioProvider(wxString filename) {
AvisynthAudioProvider::AvisynthAudioProvider(wxString filename) this->filename = filename;
: filename(filename)
{ wxMutexLocker lock(avs_wrapper.GetMutex());
wxFileName fn(filename);
if (!fn.FileExists())
throw agi::FileNotFoundError(from_wx(filename));
try { try {
AVSValue script;
wxMutexLocker lock(avs_wrapper.GetMutex());
wxFileName fn(filename);
if (!fn.FileExists())
throw agi::FileNotFoundError(STD_STR(filename));
IScriptEnvironment *env = avs_wrapper.GetEnv(); IScriptEnvironment *env = avs_wrapper.GetEnv();
// Include // Include
if (filename.EndsWith(".avs")) { if (filename.EndsWith(".avs"))
char *fname = env->SaveString(fn.GetShortPath().mb_str(csConvLocal)); LoadFromClip(env->Invoke("Import", env->SaveString(fn.GetShortPath().mb_str(csConvLocal))));
script = env->Invoke("Import", fname);
}
// Use DirectShowSource // Use DirectShowSource
else { else {
const char * argnames[3] = { 0, "video", "audio" }; const char * argnames[3] = { 0, "video", "audio" };
@ -79,21 +68,16 @@ AvisynthAudioProvider::AvisynthAudioProvider(wxString filename)
// Load DirectShowSource.dll from app dir if it exists // Load DirectShowSource.dll from app dir if it exists
wxFileName dsspath(StandardPaths::DecodePath("?data/DirectShowSource.dll")); wxFileName dsspath(StandardPaths::DecodePath("?data/DirectShowSource.dll"));
if (dsspath.FileExists()) { if (dsspath.FileExists())
env->Invoke("LoadPlugin",env->SaveString(dsspath.GetShortPath().mb_str(csConvLocal))); env->Invoke("LoadPlugin", env->SaveString(dsspath.GetShortPath().mb_str(csConvLocal)));
}
// Load audio with DSS if it exists // Load audio with DSS if it exists
if (env->FunctionExists("DirectShowSource")) { if (env->FunctionExists("DirectShowSource"))
script = env->Invoke("DirectShowSource", AVSValue(args,3),argnames); LoadFromClip(env->Invoke("DirectShowSource", AVSValue(args, 3), argnames));
}
// Otherwise fail // Otherwise fail
else { else
throw agi::AudioProviderOpenError("No suitable audio source filter found. Try placing DirectShowSource.dll in the Aegisub application directory.", 0); throw agi::AudioProviderOpenError("No suitable audio source filter found. Try placing DirectShowSource.dll in the Aegisub application directory.", 0);
}
} }
LoadFromClip(script);
} }
catch (AvisynthError &err) { catch (AvisynthError &err) {
std::string errmsg(err.msg); std::string errmsg(err.msg);
@ -104,22 +88,15 @@ AvisynthAudioProvider::AvisynthAudioProvider(wxString filename)
} }
} }
/// @brief Read from environment void AvisynthAudioProvider::LoadFromClip(AVSValue clip) {
/// @param _clip
///
void AvisynthAudioProvider::LoadFromClip(AVSValue _clip) {
AVSValue script;
// Check if it has audio // Check if it has audio
VideoInfo vi = _clip.AsClip()->GetVideoInfo(); VideoInfo vi = clip.AsClip()->GetVideoInfo();
if (!vi.HasAudio()) throw agi::AudioDataNotFoundError("No audio found.", 0); if (!vi.HasAudio()) throw agi::AudioDataNotFoundError("No audio found.", 0);
IScriptEnvironment *env = avs_wrapper.GetEnv(); IScriptEnvironment *env = avs_wrapper.GetEnv();
// Convert to one channel // Convert to one channel
char buffer[1024]; AVSValue script = env->Invoke(OPT_GET("Audio/Downmixer")->GetString().c_str(), clip);
strcpy(buffer,lagi_wxString(OPT_GET("Audio/Downmixer")->GetString()).mb_str(csConvLocal));
script = env->Invoke(buffer, _clip);
// Convert to 16 bits per sample // Convert to 16 bits per sample
script = env->Invoke("ConvertAudioTo16bit", script); script = env->Invoke("ConvertAudioTo16bit", script);
@ -127,10 +104,11 @@ void AvisynthAudioProvider::LoadFromClip(AVSValue _clip) {
// Convert sample rate // Convert sample rate
int setsample = OPT_GET("Provider/Audio/AVS/Sample Rate")->GetInt(); int setsample = OPT_GET("Provider/Audio/AVS/Sample Rate")->GetInt();
if (vi.SamplesPerSecond() < 32000) setsample = 44100; if (setsample == 0 && vi.SamplesPerSecond() < 32000)
setsample = 44100;
if (setsample != 0) { if (setsample != 0) {
AVSValue args[2] = { script, setsample }; AVSValue args[2] = { script, setsample };
script = env->Invoke("ResampleAudio", AVSValue(args,2)); script = env->Invoke("ResampleAudio", AVSValue(args, 2));
} }
// Set clip // Set clip
@ -144,10 +122,10 @@ void AvisynthAudioProvider::LoadFromClip(AVSValue _clip) {
bytes_per_sample = vi.BytesPerAudioSample(); bytes_per_sample = vi.BytesPerAudioSample();
float_samples = false; float_samples = false;
clip = tempclip; this->clip = tempclip;
} }
void AvisynthAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const { void AvisynthAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
clip->GetAudio(buf,start,count,avs_wrapper.GetEnv()); clip->GetAudio(buf, start, count, avs_wrapper.GetEnv());
} }
#endif #endif

View File

@ -40,17 +40,13 @@
class AvisynthAudioProvider : public AudioProvider { class AvisynthAudioProvider : public AudioProvider {
AviSynthWrapper avs_wrapper; AviSynthWrapper avs_wrapper;
wxString filename;
PClip clip; PClip clip;
void LoadFromClip(AVSValue clip); void LoadFromClip(AVSValue clip);
void FillBuffer(void *buf, int64_t start, int64_t count) const; void FillBuffer(void *buf, int64_t start, int64_t count) const;
public: public:
AvisynthAudioProvider(wxString _filename); AvisynthAudioProvider(wxString filename);
wxString GetFilename() const { return filename; }
bool AreSamplesNativeEndian() const { return true; } bool AreSamplesNativeEndian() const { return true; }
bool NeedsCache() const { return true; } bool NeedsCache() const { return true; }

View File

@ -57,10 +57,10 @@ protected:
public: public:
virtual ~AudioProvider() { } virtual ~AudioProvider() { }
virtual wxString GetFilename() const { return filename; };
void GetAudio(void *buf, int64_t start, int64_t count) const; void GetAudio(void *buf, int64_t start, int64_t count) const;
void GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const; void GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const;
wxString GetFilename() const { return filename; };
int64_t GetNumSamples() const { return num_samples; } int64_t GetNumSamples() const { return num_samples; }
int GetSampleRate() const { return sample_rate; } int GetSampleRate() const { return sample_rate; }
int GetBytesPerSample() const { return bytes_per_sample; } int GetBytesPerSample() const { return bytes_per_sample; }