summaryrefslogtreecommitdiff
path: root/libs/audiographer/audiographer/sndfile/sndfile_reader.h
blob: b3e84ac40a9d8e910b05414f58eeac80481039d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef AUDIOGRAPHER_SNDFILE_READER_H
#define AUDIOGRAPHER_SNDFILE_READER_H

#include "audiographer/utils/listed_source.h"
#include "audiographer/process_context.h"
#include "audiographer/sndfile/sndfile_base.h"

namespace AudioGrapher
{

/** Reader for audio files using libsndfile.
  * Only short, int and float are valid template parameters
  */
template<typename T = DefaultSampleType>
class SndfileReader
  : public virtual SndfileBase
  , public ListedSource<T>
  , public Throwing<>
{
  public:

	SndfileReader (std::string const & path) : SndfileHandle (path) {}
	virtual ~SndfileReader () {}

	SndfileReader (SndfileReader const & other) : SndfileHandle (other) {}
	using SndfileHandle::operator=;

	/** Read data into buffer in \a context, only the data is modified (not sample count)
	 *  Note that the data read is output to the outputs, as well as read into the context
	 *  \return number of samples read
	 */
	samplecnt_t read (ProcessContext<T> & context)
	{
		if (throw_level (ThrowStrict) && context.channels() != channels() ) {
			throw Exception (*this, boost::str (boost::format
				("Wrong number of channels given to process(), %1% instead of %2%")
				% context.channels() % channels()));
		}

		samplecnt_t const samples_read = SndfileHandle::read (context.data(), context.samples());
		ProcessContext<T> c_out = context.beginning (samples_read);

		if (samples_read < context.samples()) {
			c_out.set_flag (ProcessContext<T>::EndOfInput);
		}
		this->output (c_out);
		return samples_read;
	}

  protected:
	/// SndfileHandle has to be constructed directly by deriving classes
	SndfileReader () {}
};

} // namespace

#endif // AUDIOGRAPHER_SNDFILE_READER_H