2006-02-25 09:39:15 +01:00
|
|
|
// Copyright (c) 2005-2006, Rodrigo Braz Monteiro, Fredrik Mellbin
|
2006-02-25 07:52:07 +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_avs.cpp
|
|
|
|
/// @brief Avisynth-based audio provider
|
|
|
|
/// @ingroup audio_input
|
|
|
|
///
|
2006-02-25 07:52:07 +01:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2008-01-22 03:54:16 +01:00
|
|
|
#ifdef WITH_AVISYNTH
|
2008-03-05 03:19:50 +01:00
|
|
|
#include "audio_provider_avs.h"
|
2012-01-08 02:33:39 +01:00
|
|
|
|
|
|
|
#include "audio_controller.h"
|
2013-01-07 02:50:09 +01:00
|
|
|
#include "options.h"
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "utils.h"
|
2006-02-25 07:52:07 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/access.h>
|
|
|
|
#include <libaegisub/charset_conv.h>
|
|
|
|
#include <libaegisub/fs.h>
|
2013-01-30 04:35:37 +01:00
|
|
|
#include <libaegisub/path.h>
|
2012-12-22 21:07:14 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
AvisynthAudioProvider::AvisynthAudioProvider(agi::fs::path const& filename) {
|
2012-12-22 21:07:14 +01:00
|
|
|
this->filename = filename;
|
2012-01-08 02:33:39 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::acs::CheckFileRead(filename);
|
2012-01-08 02:33:39 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::lock_guard<std::mutex> lock(avs_wrapper.GetMutex());
|
2012-12-22 21:07:14 +01:00
|
|
|
|
|
|
|
try {
|
2012-01-18 21:08:32 +01:00
|
|
|
IScriptEnvironment *env = avs_wrapper.GetEnv();
|
|
|
|
|
2012-01-08 02:33:39 +01:00
|
|
|
// Include
|
2013-01-04 16:01:50 +01:00
|
|
|
if (agi::fs::HasExtension(filename, "avs"))
|
|
|
|
LoadFromClip(env->Invoke("Import", env->SaveString(agi::fs::ShortName(filename).c_str())));
|
2012-01-08 02:33:39 +01:00
|
|
|
// Use DirectShowSource
|
2007-06-23 08:46:42 +02:00
|
|
|
else {
|
2012-01-08 02:33:39 +01:00
|
|
|
const char * argnames[3] = { 0, "video", "audio" };
|
2013-01-04 16:01:50 +01:00
|
|
|
AVSValue args[3] = { env->SaveString(agi::fs::ShortName(filename).c_str()), false, true };
|
2012-01-08 02:33:39 +01:00
|
|
|
|
|
|
|
// Load DirectShowSource.dll from app dir if it exists
|
2013-01-30 04:35:37 +01:00
|
|
|
agi::fs::path dsspath(config::path->Decode("?data/DirectShowSource.dll"));
|
2013-01-04 16:01:50 +01:00
|
|
|
if (agi::fs::FileExists(dsspath))
|
|
|
|
env->Invoke("LoadPlugin", env->SaveString(agi::fs::ShortName(dsspath).c_str()));
|
2012-01-08 02:33:39 +01:00
|
|
|
|
|
|
|
// Load audio with DSS if it exists
|
2012-12-22 21:07:14 +01:00
|
|
|
if (env->FunctionExists("DirectShowSource"))
|
|
|
|
LoadFromClip(env->Invoke("DirectShowSource", AVSValue(args, 3), argnames));
|
2012-01-08 02:33:39 +01:00
|
|
|
// Otherwise fail
|
2012-12-22 21:07:14 +01:00
|
|
|
else
|
2012-01-08 02:33:39 +01:00
|
|
|
throw agi::AudioProviderOpenError("No suitable audio source filter found. Try placing DirectShowSource.dll in the Aegisub application directory.", 0);
|
2007-06-23 08:46:42 +02:00
|
|
|
}
|
2012-01-08 02:33:39 +01:00
|
|
|
}
|
|
|
|
catch (AvisynthError &err) {
|
|
|
|
std::string errmsg(err.msg);
|
|
|
|
if (errmsg.find("filter graph manager won't talk to me") != errmsg.npos)
|
|
|
|
throw agi::AudioDataNotFoundError("Avisynth error: " + errmsg, 0);
|
|
|
|
else
|
|
|
|
throw agi::AudioProviderOpenError("Avisynth error: " + errmsg, 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
|
|
|
|
2012-12-22 21:07:14 +01:00
|
|
|
void AvisynthAudioProvider::LoadFromClip(AVSValue clip) {
|
2006-02-25 07:52:07 +01:00
|
|
|
// Check if it has audio
|
2012-12-22 21:07:14 +01:00
|
|
|
VideoInfo vi = clip.AsClip()->GetVideoInfo();
|
2012-01-08 02:33:39 +01:00
|
|
|
if (!vi.HasAudio()) throw agi::AudioDataNotFoundError("No audio found.", 0);
|
2006-02-25 07:52:07 +01:00
|
|
|
|
2012-01-18 21:08:32 +01:00
|
|
|
IScriptEnvironment *env = avs_wrapper.GetEnv();
|
|
|
|
|
2006-02-25 07:52:07 +01:00
|
|
|
// Convert to one channel
|
2012-12-22 21:07:14 +01:00
|
|
|
AVSValue script = env->Invoke(OPT_GET("Audio/Downmixer")->GetString().c_str(), clip);
|
2006-02-25 07:52:07 +01:00
|
|
|
|
|
|
|
// Convert to 16 bits per sample
|
|
|
|
script = env->Invoke("ConvertAudioTo16bit", script);
|
2008-01-19 03:38:31 +01:00
|
|
|
vi = script.AsClip()->GetVideoInfo();
|
2006-02-25 07:52:07 +01:00
|
|
|
|
|
|
|
// Convert sample rate
|
2010-05-21 03:13:36 +02:00
|
|
|
int setsample = OPT_GET("Provider/Audio/AVS/Sample Rate")->GetInt();
|
2012-12-22 21:07:14 +01:00
|
|
|
if (setsample == 0 && vi.SamplesPerSecond() < 32000)
|
|
|
|
setsample = 44100;
|
2006-02-25 07:52:07 +01:00
|
|
|
if (setsample != 0) {
|
|
|
|
AVSValue args[2] = { script, setsample };
|
2012-12-22 21:07:14 +01:00
|
|
|
script = env->Invoke("ResampleAudio", AVSValue(args, 2));
|
2006-02-25 07:52:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set clip
|
|
|
|
PClip tempclip = script.AsClip();
|
|
|
|
vi = tempclip->GetVideoInfo();
|
|
|
|
|
|
|
|
// Read properties
|
|
|
|
channels = vi.AudioChannels();
|
|
|
|
num_samples = vi.num_audio_samples;
|
|
|
|
sample_rate = vi.SamplesPerSecond();
|
|
|
|
bytes_per_sample = vi.BytesPerAudioSample();
|
2012-06-13 17:58:28 +02:00
|
|
|
float_samples = false;
|
2006-02-25 07:52:07 +01:00
|
|
|
|
2012-12-22 21:07:14 +01:00
|
|
|
this->clip = tempclip;
|
2006-02-25 07:52:07 +01:00
|
|
|
}
|
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void AvisynthAudioProvider::FillBuffer(void *buf, int64_t start, int64_t count) const {
|
2012-12-22 21:07:14 +01:00
|
|
|
clip->GetAudio(buf, start, count, avs_wrapper.GetEnv());
|
2006-02-25 07:52:07 +01:00
|
|
|
}
|
|
|
|
#endif
|