2006-02-25 07:52:07 +01:00
|
|
|
// Copyright (c) 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.h
|
|
|
|
/// @brief Declaration of base-class for audio providers
|
|
|
|
/// @ingroup main_headers audio_input
|
|
|
|
///
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2006-02-25 05:54:21 +01:00
|
|
|
#pragma once
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
#include <libaegisub/exception.h>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <libaegisub/fs_fwd.h>
|
|
|
|
|
2014-04-22 21:34:20 +02:00
|
|
|
#include <atomic>
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2009-07-30 06:24:23 +02:00
|
|
|
|
2006-02-25 08:41:18 +01:00
|
|
|
class AudioProvider {
|
2006-02-25 07:52:07 +01:00
|
|
|
protected:
|
2006-01-16 22:02:54 +01:00
|
|
|
int channels;
|
2009-07-30 06:24:23 +02:00
|
|
|
|
2012-06-13 17:58:28 +02:00
|
|
|
/// for one channel, ie. number of PCM frames
|
|
|
|
int64_t num_samples;
|
2014-04-22 21:34:20 +02:00
|
|
|
std::atomic<int64_t> decoded_samples;
|
2006-01-16 22:02:54 +01:00
|
|
|
int sample_rate;
|
|
|
|
int bytes_per_sample;
|
2012-06-13 17:58:28 +02:00
|
|
|
bool float_samples;
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path filename;
|
2006-02-25 05:54:21 +01:00
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
virtual void FillBuffer(void *buf, int64_t start, int64_t count) const = 0;
|
|
|
|
|
2012-11-25 16:45:54 +01:00
|
|
|
void ZeroFill(void *buf, int64_t count) const;
|
|
|
|
|
2006-01-16 22:02:54 +01:00
|
|
|
public:
|
2014-04-25 19:01:07 +02:00
|
|
|
virtual ~AudioProvider() = default;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2012-08-18 05:13:44 +02:00
|
|
|
void GetAudio(void *buf, int64_t start, int64_t count) const;
|
2010-12-08 04:36:10 +01:00
|
|
|
void GetAudioWithVolume(void *buf, int64_t start, int64_t count, double volume) const;
|
2006-01-16 22:02:54 +01:00
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
agi::fs::path GetFilename() const { return filename; }
|
|
|
|
int64_t GetNumSamples() const { return num_samples; }
|
2014-04-22 21:34:20 +02:00
|
|
|
int64_t GetDecodedSamples() const { return decoded_samples; }
|
2013-01-04 16:01:50 +01:00
|
|
|
int GetSampleRate() const { return sample_rate; }
|
|
|
|
int GetBytesPerSample() const { return bytes_per_sample; }
|
|
|
|
int GetChannels() const { return channels; }
|
|
|
|
bool AreSamplesFloat() const { return float_samples; }
|
2012-12-22 22:44:54 +01:00
|
|
|
|
2010-08-02 08:32:01 +02:00
|
|
|
/// @brief Does this provider benefit from external caching?
|
|
|
|
virtual bool NeedsCache() const { return false; }
|
2007-01-23 05:42:08 +01:00
|
|
|
};
|
|
|
|
|
2013-09-27 05:18:29 +02:00
|
|
|
/// Helper base class for an audio provider which wraps another provider
|
|
|
|
class AudioProviderWrapper : public AudioProvider {
|
|
|
|
protected:
|
|
|
|
std::unique_ptr<AudioProvider> source;
|
|
|
|
public:
|
|
|
|
AudioProviderWrapper(std::unique_ptr<AudioProvider> src)
|
|
|
|
: source(std::move(src))
|
|
|
|
{
|
|
|
|
channels = source->GetChannels();
|
|
|
|
num_samples = source->GetNumSamples();
|
2014-04-22 21:34:20 +02:00
|
|
|
decoded_samples = source->GetDecodedSamples();
|
2013-09-27 05:18:29 +02:00
|
|
|
sample_rate = source->GetSampleRate();
|
|
|
|
bytes_per_sample = source->GetBytesPerSample();
|
|
|
|
float_samples = source->AreSamplesFloat();
|
|
|
|
filename = source->GetFilename();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-25 17:51:38 +01:00
|
|
|
namespace agi { class BackgroundRunner; }
|
|
|
|
|
2014-03-24 03:30:03 +01:00
|
|
|
struct AudioProviderFactory {
|
|
|
|
static std::vector<std::string> GetClasses();
|
2012-01-08 02:33:33 +01:00
|
|
|
|
|
|
|
/// Get a provider for the file
|
|
|
|
/// @param filename URI to open
|
2014-03-25 17:51:38 +01:00
|
|
|
static std::unique_ptr<AudioProvider> GetProvider(agi::fs::path const& filename, agi::BackgroundRunner *br);
|
2008-03-07 03:32:29 +01:00
|
|
|
};
|
2010-08-02 08:32:01 +02:00
|
|
|
|
2011-12-28 22:27:06 +01:00
|
|
|
DEFINE_BASE_EXCEPTION_NOINNER(AudioProviderError, agi::Exception)
|
2010-08-02 08:32:01 +02:00
|
|
|
/// Error of some sort occurred while decoding a frame
|
2011-12-28 22:27:06 +01:00
|
|
|
DEFINE_SIMPLE_EXCEPTION_NOINNER(AudioDecodeError, AudioProviderError, "audio/error")
|