NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / audiographer / audiographer / sndfile / sndfile_writer.h
1 #ifndef AUDIOGRAPHER_SNDFILE_WRITER_H
2 #define AUDIOGRAPHER_SNDFILE_WRITER_H
3
4 #include <string>
5
6 #include <boost/format.hpp>
7
8 #include "audiographer/flag_debuggable.h"
9 #include "audiographer/sink.h"
10 #include "audiographer/types.h"
11 #include "audiographer/sndfile/sndfile_base.h"
12 #include "audiographer/broadcast_info.h"
13
14 #include "pbd/signals.h"
15
16 namespace AudioGrapher
17 {
18
19 /** Writer for audio files using libsndfile.
20   * Only short, int and float are valid template parameters
21   */
22 template <typename T = DefaultSampleType>
23 class SndfileWriter
24   : public virtual SndfileBase
25   , public Sink<T>
26   , public Throwing<>
27   , public FlagDebuggable<>
28 {
29   public:
30         SndfileWriter (std::string const & path, int format, ChannelCount channels, framecnt_t samplerate, boost::shared_ptr<BroadcastInfo> broadcast_info)
31           : SndfileHandle (path, Write, format, channels, samplerate)
32           , path (path)
33         {
34                 init();
35
36                 if (broadcast_info) {
37                         broadcast_info->write_to_file (this);
38                 }
39         }
40
41         virtual ~SndfileWriter () {}
42
43         SndfileWriter (SndfileWriter const & other)
44                 : SndfileHandle (other)
45         {
46                 init();
47         }
48
49         using SndfileHandle::operator=;
50
51         framecnt_t get_frames_written() const { return frames_written; }
52         void       reset_frames_written_count() { frames_written = 0; }
53
54         /// Writes data to file
55         void process (ProcessContext<T> const & c)
56         {
57                 check_flags (*this, c);
58
59                 if (throw_level (ThrowStrict) && c.channels() != channels()) {
60                         throw Exception (*this, boost::str (boost::format
61                                 ("Wrong number of channels given to process(), %1% instead of %2%")
62                                 % c.channels() % channels()));
63                 }
64
65                 framecnt_t const written = write (c.data(), c.frames());
66                 frames_written += written;
67
68                 if (throw_level (ThrowProcess) && written != c.frames()) {
69                         throw Exception (*this, boost::str (boost::format
70                                 ("Could not write data to output file (%1%)")
71                                 % strError()));
72                 }
73
74                 if (c.has_flag(ProcessContext<T>::EndOfInput)) {
75                         writeSync();
76                         FileWritten (path);
77                 }
78         }
79
80         using Sink<T>::process;
81
82         PBD::Signal1<void, std::string> FileWritten;
83
84   protected:
85         /// SndfileHandle has to be constructed directly by deriving classes
86         SndfileWriter ()
87         {
88                 init();
89         }
90
91         void init()
92         {
93                 frames_written = 0;
94                 add_supported_flag (ProcessContext<T>::EndOfInput);
95         }
96
97   protected:
98         std::string path;
99         framecnt_t frames_written;
100 };
101
102 } // namespace
103
104 #endif // AUDIOGRAPHER_SNDFILE_WRITER_H