Clean up AudioProvider a bit

Factor out some duplicated code and clean up some cruft.
This commit is contained in:
Thomas Goyne 2012-12-22 07:46:26 -08:00
parent 31feab4a8b
commit a4607ff6be
2 changed files with 52 additions and 62 deletions

View file

@ -35,45 +35,35 @@
#include "config.h" #include "config.h"
#include <wx/thread.h> #include <cstdint>
#include "audio_controller.h"
#ifdef WITH_AVISYNTH
#include "audio_provider_avs.h" #include "audio_provider_avs.h"
#endif
#include "audio_provider_convert.h" #include "audio_provider_convert.h"
#ifdef WITH_FFMS2
#include "audio_provider_ffmpegsource.h" #include "audio_provider_ffmpegsource.h"
#endif
#include "audio_provider_hd.h" #include "audio_provider_hd.h"
#include "audio_provider_lock.h" #include "audio_provider_lock.h"
#include "audio_provider_pcm.h" #include "audio_provider_pcm.h"
#include "audio_provider_ram.h" #include "audio_provider_ram.h"
#include "audio_controller.h"
#include "compat.h" #include "compat.h"
#include "dialog_progress.h" #include "dialog_progress.h"
#include "frame_main.h" #include "frame_main.h"
#include "main.h" #include "main.h"
#include "utils.h"
#include <libaegisub/log.h> #include <libaegisub/log.h>
void AudioProvider::GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const { void AudioProvider::GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const {
GetAudio(buf,start,count); GetAudio(buf, start, count);
if (volume == 1.0) return; if (volume == 1.0) return;
if (bytes_per_sample != 2)
throw agi::InternalError("GetAudioWithVolume called on unconverted audio stream", 0);
if (bytes_per_sample == 2) { short *buffer = static_cast<int16_t *>(buf);
// Read raw samples for (size_t i = 0; i < (size_t)count; ++i)
short *buffer = (short*) buf; buffer[i] = mid<int>(-0x8000, buffer[i] * volume + 0.5, 0x7FFF);
int value;
// Modify
for (size_t i = 0; i < (size_t)count; ++i) {
value = (int)(buffer[i]*volume+0.5);
if (value < -0x8000) value = -0x8000;
if (value > 0x7FFF) value = 0x7FFF;
buffer[i] = value;
}
}
} }
void AudioProvider::ZeroFill(void *buf, int64_t count) const { void AudioProvider::ZeroFill(void *buf, int64_t count) const {
@ -118,60 +108,64 @@ void AudioProvider::GetAudio(void *buf, int64_t start, int64_t count) const {
} }
} }
AudioProvider *AudioProviderFactory::GetProvider(wxString const& filename, int cache) { namespace {
AudioProvider *provider = 0; struct provider_creator {
bool found_file = false; bool found_file;
bool found_audio = false; bool found_audio;
std::string msg; std::string msg;
if (!OPT_GET("Provider/Audio/PCM/Disable")->GetBool()) { provider_creator() : found_file(false) , found_audio(false) { }
// Try a PCM provider first
template<typename Factory>
AudioProvider *try_create(std::string const& name, Factory&& create) {
try { try {
provider = CreatePCMAudioProvider(filename); AudioProvider *provider = create();
LOG_D("audio_provider") << "Using PCM provider"; if (provider)
LOG_I("audio_provider") << "Using audio provider: " << name;
return provider;
} }
catch (agi::FileNotFoundError const& err) { catch (agi::FileNotFoundError const& err) {
msg = "PCM audio provider: " + err.GetMessage() + " not found.\n"; msg += name + ": " + err.GetMessage() + " not found.\n";
}
catch (agi::AudioDataNotFoundError const& err) {
found_file = true;
msg += name + ": " + err.GetChainedMessage() + "\n";
} }
catch (agi::AudioOpenError const& err) { catch (agi::AudioOpenError const& err) {
found_audio = true;
found_file = true; found_file = true;
msg += err.GetChainedMessage() + "\n"; msg += name + ": " + err.GetChainedMessage() + "\n";
} }
return nullptr;
} }
};
}
AudioProvider *AudioProviderFactory::GetProvider(wxString const& filename) {
provider_creator creator;
AudioProvider *provider = nullptr;
// Try a PCM provider first
if (!OPT_GET("Provider/Audio/PCM/Disable")->GetBool())
provider = creator.try_create("PCM audio provider", [&]() { return CreatePCMAudioProvider(filename); });
if (!provider) { if (!provider) {
std::vector<std::string> list = GetClasses(OPT_GET("Audio/Provider")->GetString()); std::vector<std::string> list = GetClasses(OPT_GET("Audio/Provider")->GetString());
if (list.empty()) throw agi::NoAudioProvidersError("No audio providers are available.", 0); if (list.empty()) throw agi::NoAudioProvidersError("No audio providers are available.", 0);
for (size_t i = 0; i < list.size() ; ++i) { for (auto const& name : list) {
try { provider = creator.try_create(name, [&]() { return Create(name, filename); });
provider = Create(list[i], filename); if (provider) break;
if (provider) {
LOG_D("audio_provider") << "Using audio provider: " << list[i];
break;
}
}
catch (agi::FileNotFoundError const& err) {
msg += list[i] + ": " + err.GetMessage() + " not found.\n";
}
catch (agi::AudioDataNotFoundError const& err) {
found_file = true;
msg += list[i] + ": " + err.GetChainedMessage() + "\n";
}
catch (agi::AudioOpenError const& err) {
found_audio = true;
found_file = true;
msg += list[i] + ": " + err.GetChainedMessage() + "\n";
}
} }
} }
if (!provider) { if (!provider) {
if (found_audio) if (creator.found_audio)
throw agi::AudioProviderOpenError(msg, 0); throw agi::AudioProviderOpenError(creator.msg, 0);
if (found_file) if (creator.found_file)
throw agi::AudioDataNotFoundError(msg, 0); throw agi::AudioDataNotFoundError(creator.msg, 0);
throw agi::FileNotFoundError(STD_STR(filename)); throw agi::FileNotFoundError(from_wx(filename));
} }
bool needsCache = provider->NeedsCache(); bool needsCache = provider->NeedsCache();
@ -181,10 +175,9 @@ AudioProvider *AudioProviderFactory::GetProvider(wxString const& filename, int c
provider = CreateConvertAudioProvider(provider); provider = CreateConvertAudioProvider(provider);
// Change provider to RAM/HD cache if needed // Change provider to RAM/HD cache if needed
if (cache == -1) cache = OPT_GET("Audio/Cache/Type")->GetInt(); int cache = OPT_GET("Audio/Cache/Type")->GetInt();
if (!cache || !needsCache) { if (!cache || !needsCache)
return new LockAudioProvider(provider); return new LockAudioProvider(provider);
}
DialogProgress progress(wxGetApp().frame, _("Load audio")); DialogProgress progress(wxGetApp().frame, _("Load audio"));
@ -197,8 +190,6 @@ AudioProvider *AudioProviderFactory::GetProvider(wxString const& filename, int c
throw agi::AudioCacheOpenError("Unknown caching method", 0); throw agi::AudioCacheOpenError("Unknown caching method", 0);
} }
/// @brief Register all providers
///
void AudioProviderFactory::RegisterProviders() { void AudioProviderFactory::RegisterProviders() {
#ifdef WITH_AVISYNTH #ifdef WITH_AVISYNTH
Register<AvisynthAudioProvider>("Avisynth"); Register<AvisynthAudioProvider>("Avisynth");

View file

@ -78,8 +78,7 @@ public:
/// Get a provider for the file /// Get a provider for the file
/// @param filename URI to open /// @param filename URI to open
/// @param cache Caching mode static AudioProvider *GetProvider(wxString const& filename);
static AudioProvider *GetProvider(wxString const& filename, int cache=-1);
}; };
DEFINE_BASE_EXCEPTION_NOINNER(AudioProviderError, agi::Exception) DEFINE_BASE_EXCEPTION_NOINNER(AudioProviderError, agi::Exception)