2006-02-25 07:52:07 +01:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro
|
2006-01-16 22:02:54 +01:00
|
|
|
// 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.
|
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
/// @file audio_provider.cpp
|
|
|
|
/// @brief Baseclass for audio providers
|
|
|
|
/// @ingroup audio_input
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-12-22 16:46:26 +01:00
|
|
|
#include <cstdint>
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2008-03-05 03:19:50 +01:00
|
|
|
#include "audio_provider_avs.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_provider_convert.h"
|
2012-12-22 18:30:47 +01:00
|
|
|
#include "audio_provider_dummy.h"
|
2008-09-23 22:02:21 +02:00
|
|
|
#include "audio_provider_ffmpegsource.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_provider_hd.h"
|
2012-08-18 05:13:39 +02:00
|
|
|
#include "audio_provider_lock.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "audio_provider_pcm.h"
|
|
|
|
#include "audio_provider_ram.h"
|
2012-12-22 16:46:26 +01:00
|
|
|
|
|
|
|
#include "audio_controller.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "compat.h"
|
2011-09-28 21:49:37 +02:00
|
|
|
#include "dialog_progress.h"
|
|
|
|
#include "frame_main.h"
|
2010-05-21 03:13:36 +02:00
|
|
|
#include "main.h"
|
2012-12-22 16:46:26 +01:00
|
|
|
#include "utils.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
|
2012-06-13 17:58:21 +02:00
|
|
|
#include <libaegisub/log.h>
|
|
|
|
|
2010-12-08 04:36:10 +01:00
|
|
|
void AudioProvider::GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const {
|
2012-12-22 16:46:26 +01:00
|
|
|
GetAudio(buf, start, count);
|
2009-04-29 21:30:02 +02:00
|
|
|
|
2007-01-23 05:42:08 +01:00
|
|
|
if (volume == 1.0) return;
|
2012-12-22 16:46:26 +01:00
|
|
|
if (bytes_per_sample != 2)
|
|
|
|
throw agi::InternalError("GetAudioWithVolume called on unconverted audio stream", 0);
|
2007-01-23 05:42:08 +01:00
|
|
|
|
2012-12-22 16:46:26 +01:00
|
|
|
short *buffer = static_cast<int16_t *>(buf);
|
|
|
|
for (size_t i = 0; i < (size_t)count; ++i)
|
|
|
|
buffer[i] = mid<int>(-0x8000, buffer[i] * volume + 0.5, 0x7FFF);
|
2007-01-23 05:42:08 +01:00
|
|
|
}
|
|
|
|
|
2012-11-25 16:45:54 +01:00
|
|
|
void AudioProvider::ZeroFill(void *buf, int64_t count) const {
|
|
|
|
if (bytes_per_sample == 1)
|
|
|
|
// 8 bit formats are usually unsigned with bias 127
|
|
|
|
memset(buf, 127, count * channels);
|
|
|
|
else
|
|
|
|
// While everything else is signed
|
|
|
|
memset(buf, 0, count * bytes_per_sample * channels);
|
|
|
|
}
|
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void AudioProvider::GetAudio(void *buf, int64_t start, int64_t count) const {
|
2012-11-25 16:45:54 +01:00
|
|
|
if (start < 0) {
|
|
|
|
ZeroFill(buf, std::min(-start, count));
|
|
|
|
buf = static_cast<char *>(buf) + -start * bytes_per_sample * channels;
|
|
|
|
count += start;
|
|
|
|
start = 0;
|
2012-08-18 05:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (start + count > num_samples) {
|
|
|
|
int64_t zero_count = std::min(count, start + count - num_samples);
|
|
|
|
count -= zero_count;
|
2012-11-25 16:45:54 +01:00
|
|
|
ZeroFill(static_cast<char *>(buf) + count * bytes_per_sample * channels, zero_count);
|
2012-08-18 05:13:44 +02:00
|
|
|
}
|
|
|
|
|
2012-11-25 16:45:54 +01:00
|
|
|
if (count <= 0) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
FillBuffer(buf, start, count);
|
|
|
|
}
|
|
|
|
catch (AudioDecodeError const& e) {
|
|
|
|
LOG_E("audio_provider") << e.GetChainedMessage();
|
|
|
|
ZeroFill(buf, count);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// FIXME: Poor error handling though better than none, to patch issue #800.
|
|
|
|
// Just return blank audio if real provider fails.
|
|
|
|
LOG_E("audio_provider") << "Unknown audio decoding error";
|
|
|
|
ZeroFill(buf, count);
|
|
|
|
return;
|
2012-09-02 06:43:06 +02:00
|
|
|
}
|
2012-08-18 05:13:44 +02:00
|
|
|
}
|
|
|
|
|
2012-12-22 16:46:26 +01:00
|
|
|
namespace {
|
|
|
|
struct provider_creator {
|
|
|
|
bool found_file;
|
|
|
|
bool found_audio;
|
2010-08-02 08:32:01 +02:00
|
|
|
std::string msg;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-12-22 16:46:26 +01:00
|
|
|
provider_creator() : found_file(false) , found_audio(false) { }
|
|
|
|
|
|
|
|
template<typename Factory>
|
|
|
|
AudioProvider *try_create(std::string const& name, Factory&& create) {
|
2010-08-02 08:32:01 +02:00
|
|
|
try {
|
2012-12-22 16:46:26 +01:00
|
|
|
AudioProvider *provider = create();
|
|
|
|
if (provider)
|
|
|
|
LOG_I("audio_provider") << "Using audio provider: " << name;
|
|
|
|
return provider;
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
|
|
|
catch (agi::FileNotFoundError const& err) {
|
2012-12-22 16:46:26 +01:00
|
|
|
msg += name + ": " + err.GetMessage() + " not found.\n";
|
|
|
|
}
|
|
|
|
catch (agi::AudioDataNotFoundError const& err) {
|
|
|
|
found_file = true;
|
|
|
|
msg += name + ": " + err.GetChainedMessage() + "\n";
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
2012-01-08 02:33:39 +01:00
|
|
|
catch (agi::AudioOpenError const& err) {
|
2012-12-22 16:46:26 +01:00
|
|
|
found_audio = true;
|
2012-01-08 02:33:39 +01:00
|
|
|
found_file = true;
|
2012-12-22 16:46:26 +01:00
|
|
|
msg += name + ": " + err.GetChainedMessage() + "\n";
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
2012-12-22 16:46:26 +01:00
|
|
|
|
|
|
|
return nullptr;
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
2012-12-22 16:46:26 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioProvider *AudioProviderFactory::GetProvider(wxString const& filename) {
|
|
|
|
provider_creator creator;
|
|
|
|
AudioProvider *provider = nullptr;
|
|
|
|
|
2012-12-22 18:30:47 +01:00
|
|
|
provider = creator.try_create("Dummy audio provider", [&]() { return new DummyAudioProvider(filename); });
|
|
|
|
|
2012-12-22 16:46:26 +01:00
|
|
|
// Try a PCM provider first
|
2012-12-22 18:30:47 +01:00
|
|
|
if (!provider && !OPT_GET("Provider/Audio/PCM/Disable")->GetBool())
|
2012-12-22 16:46:26 +01:00
|
|
|
provider = creator.try_create("PCM audio provider", [&]() { return CreatePCMAudioProvider(filename); });
|
2012-06-13 17:58:21 +02:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
if (!provider) {
|
|
|
|
std::vector<std::string> list = GetClasses(OPT_GET("Audio/Provider")->GetString());
|
2012-01-08 02:33:39 +01:00
|
|
|
if (list.empty()) throw agi::NoAudioProvidersError("No audio providers are available.", 0);
|
2010-08-02 08:32:01 +02:00
|
|
|
|
2012-12-22 16:46:26 +01:00
|
|
|
for (auto const& name : list) {
|
|
|
|
provider = creator.try_create(name, [&]() { return Create(name, filename); });
|
|
|
|
if (provider) break;
|
2008-07-04 05:24:25 +02:00
|
|
|
}
|
2008-01-19 03:18:08 +01:00
|
|
|
}
|
2012-06-13 17:58:21 +02:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
if (!provider) {
|
2012-12-22 16:46:26 +01:00
|
|
|
if (creator.found_audio)
|
|
|
|
throw agi::AudioProviderOpenError(creator.msg, 0);
|
|
|
|
if (creator.found_file)
|
|
|
|
throw agi::AudioDataNotFoundError(creator.msg, 0);
|
|
|
|
throw agi::FileNotFoundError(from_wx(filename));
|
2006-02-25 07:52:07 +01:00
|
|
|
}
|
2012-01-08 02:33:39 +01:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
bool needsCache = provider->NeedsCache();
|
2006-02-25 07:52:07 +01:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
// Give it a converter if needed
|
2008-07-16 15:22:06 +02:00
|
|
|
if (provider->GetBytesPerSample() != 2 || provider->GetSampleRate() < 32000 || provider->GetChannels() != 1)
|
|
|
|
provider = CreateConvertAudioProvider(provider);
|
2008-01-19 03:38:31 +01:00
|
|
|
|
2006-02-25 08:41:18 +01:00
|
|
|
// Change provider to RAM/HD cache if needed
|
2012-12-22 16:46:26 +01:00
|
|
|
int cache = OPT_GET("Audio/Cache/Type")->GetInt();
|
|
|
|
if (!cache || !needsCache)
|
2012-08-18 05:13:39 +02:00
|
|
|
return new LockAudioProvider(provider);
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2011-12-22 22:17:30 +01:00
|
|
|
DialogProgress progress(wxGetApp().frame, _("Load audio"));
|
2011-09-28 21:49:37 +02:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
// Convert to RAM
|
2011-09-28 21:49:37 +02:00
|
|
|
if (cache == 1) return new RAMAudioProvider(provider, &progress);
|
2007-01-05 19:27:15 +01:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
// Convert to HD
|
2011-09-28 21:49:37 +02:00
|
|
|
if (cache == 2) return new HDAudioProvider(provider, &progress);
|
2007-01-05 19:27:15 +01:00
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
throw agi::AudioCacheOpenError("Unknown caching method", 0);
|
2010-08-02 08:32:01 +02:00
|
|
|
}
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2010-08-02 08:31:38 +02:00
|
|
|
void AudioProviderFactory::RegisterProviders() {
|
2008-03-05 03:19:50 +01:00
|
|
|
#ifdef WITH_AVISYNTH
|
2010-08-02 08:31:38 +02:00
|
|
|
Register<AvisynthAudioProvider>("Avisynth");
|
2008-03-05 03:19:50 +01:00
|
|
|
#endif
|
2011-12-22 22:25:49 +01:00
|
|
|
#ifdef WITH_FFMS2
|
2010-08-02 08:31:38 +02:00
|
|
|
Register<FFmpegSourceAudioProvider>("FFmpegSource");
|
2008-09-23 22:02:21 +02:00
|
|
|
#endif
|
2008-03-05 03:19:50 +01:00
|
|
|
}
|
|
|
|
|
2012-11-13 17:51:01 +01:00
|
|
|
template<> AudioProviderFactory::map *FactoryBase<AudioProvider *(*)(wxString)>::classes = nullptr;
|