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 namespace AudioGrapher
16 /** Writer for audio files using libsndfile.
17 * Only short, int and float are valid template parameters
19 template <typename T = DefaultSampleType>
21 : public virtual SndfileBase
24 , public FlagDebuggable<>
27 SndfileWriter (std::string const & path, int format, ChannelCount channels, nframes_t samplerate)
28 : SndfileHandle (path, Write, format, channels, samplerate)
31 add_supported_flag (ProcessContext<T>::EndOfInput);
34 virtual ~SndfileWriter () {}
36 SndfileWriter (SndfileWriter const & other) : SndfileHandle (other) {}
37 using SndfileHandle::operator=;
39 /// Writes data to file
40 void process (ProcessContext<T> const & c)
42 check_flags (*this, c);
44 if (throw_level (ThrowStrict) && c.channels() != channels()) {
45 throw Exception (*this, boost::str (boost::format
46 ("Wrong number of channels given to process(), %1% instead of %2%")
47 % c.channels() % channels()));
50 nframes_t written = write (c.data(), c.frames());
51 if (throw_level (ThrowProcess) && written != c.frames()) {
52 throw Exception (*this, boost::str (boost::format
53 ("Could not write data to output file (%1%)")
57 if (c.has_flag(ProcessContext<T>::EndOfInput)) {
63 using Sink<T>::process;
65 boost::signals2::signal<void (std::string)> FileWritten;
68 /// SndfileHandle has to be constructed directly by deriving classes
71 add_supported_flag (ProcessContext<T>::EndOfInput);
80 #endif // AUDIOGRAPHER_SNDFILE_WRITER_H