a6f338487a96fd6dec37517c2b929e7e636f8dbc
[ardour.git] / libs / audiographer / audiographer / sndfile / sndfile_writer.h
1 #ifndef AUDIOGRAPHER_SNDFILE_WRITER_H
2 #define AUDIOGRAPHER_SNDFILE_WRITER_H
3
4 #include <boost/signals2.hpp>
5 #include <boost/format.hpp>
6 #include <string>
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, nframes_t samplerate, boost::shared_ptr<BroadcastInfo> broadcast_info)
31           : SndfileHandle (path, Write, format, channels, samplerate)
32           , path (path)
33         {
34                 add_supported_flag (ProcessContext<T>::EndOfInput);
35
36                 if (broadcast_info) {
37                         broadcast_info->write_to_file (this);
38                 }
39         }
40         
41         virtual ~SndfileWriter () {}
42         
43         SndfileWriter (SndfileWriter const & other) : SndfileHandle (other) {}
44         using SndfileHandle::operator=;
45         
46         /// Writes data to file
47         void process (ProcessContext<T> const & c)
48         {
49                 check_flags (*this, c);
50                 
51                 if (throw_level (ThrowStrict) && c.channels() != channels()) {
52                         throw Exception (*this, boost::str (boost::format
53                                 ("Wrong number of channels given to process(), %1% instead of %2%")
54                                 % c.channels() % channels()));
55                 }
56                 
57                 nframes_t written = write (c.data(), c.frames());
58                 if (throw_level (ThrowProcess) && written != c.frames()) {
59                         throw Exception (*this, boost::str (boost::format
60                                 ("Could not write data to output file (%1%)")
61                                 % strError()));
62                 }
63
64                 if (c.has_flag(ProcessContext<T>::EndOfInput)) {
65                         writeSync();
66                         FileWritten (path);
67                 }
68         }
69         
70         using Sink<T>::process;
71         
72         PBD::Signal1<void, std::string> FileWritten;
73
74   protected:
75         /// SndfileHandle has to be constructed directly by deriving classes
76         SndfileWriter ()
77         {
78                 add_supported_flag (ProcessContext<T>::EndOfInput);
79         }
80         
81   protected:
82         std::string path;
83 };
84
85 } // namespace
86
87 #endif // AUDIOGRAPHER_SNDFILE_WRITER_H