1 #ifndef AUDIOGRAPHER_SNDFILE_WRITER_H
2 #define AUDIOGRAPHER_SNDFILE_WRITER_H
4 #include <boost/signals2.hpp>
5 #include <boost/format.hpp>
8 #include "audiographer/flag_debuggable.h"
9 #include "audiographer/sink.h"
10 #include "audiographer/types.h"
11 #include "audiographer/sndfile/sndfile_base.h"
13 #include "pbd/signals.h"
15 namespace AudioGrapher
18 /** Writer for audio files using libsndfile.
19 * Only short, int and float are valid template parameters
21 template <typename T = DefaultSampleType>
23 : public virtual SndfileBase
26 , public FlagDebuggable<>
29 SndfileWriter (std::string const & path, int format, ChannelCount channels, nframes_t samplerate)
30 : SndfileHandle (path, Write, format, channels, samplerate)
33 add_supported_flag (ProcessContext<T>::EndOfInput);
36 virtual ~SndfileWriter () {}
38 SndfileWriter (SndfileWriter const & other) : SndfileHandle (other) {}
39 using SndfileHandle::operator=;
41 /// Writes data to file
42 void process (ProcessContext<T> const & c)
44 check_flags (*this, c);
46 if (throw_level (ThrowStrict) && c.channels() != channels()) {
47 throw Exception (*this, boost::str (boost::format
48 ("Wrong number of channels given to process(), %1% instead of %2%")
49 % c.channels() % channels()));
52 nframes_t written = write (c.data(), c.frames());
53 if (throw_level (ThrowProcess) && written != c.frames()) {
54 throw Exception (*this, boost::str (boost::format
55 ("Could not write data to output file (%1%)")
59 if (c.has_flag(ProcessContext<T>::EndOfInput)) {
65 using Sink<T>::process;
67 PBD::Signal1<void, std::string> FileWritten;
70 /// SndfileHandle has to be constructed directly by deriving classes
73 add_supported_flag (ProcessContext<T>::EndOfInput);
82 #endif // AUDIOGRAPHER_SNDFILE_WRITER_H