Fix export, which has been broken since the boost::signals2 changes. Also update...
[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
13 namespace AudioGrapher
14 {
15
16 /** Writer for audio files using libsndfile.
17   * Only short, int and float are valid template parameters
18   */
19 template <typename T = DefaultSampleType>
20 class SndfileWriter
21   : public virtual SndfileBase
22   , public Sink<T>
23   , public Throwing<>
24   , public FlagDebuggable<>
25 {
26   public:
27         SndfileWriter (std::string const & path, int format, ChannelCount channels, nframes_t samplerate)
28           : SndfileHandle (path, Write, format, channels, samplerate)
29           , path (path)
30         {
31                 add_supported_flag (ProcessContext<T>::EndOfInput);
32         }
33         
34         virtual ~SndfileWriter () {}
35         
36         SndfileWriter (SndfileWriter const & other) : SndfileHandle (other) {}
37         using SndfileHandle::operator=;
38         
39         /// Writes data to file
40         void process (ProcessContext<T> const & c)
41         {
42                 check_flags (*this, c);
43                 
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()));
48                 }
49                 
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%)")
54                                 % strError()));
55                 }
56
57                 if (c.has_flag(ProcessContext<T>::EndOfInput)) {
58                         writeSync();
59                         FileWritten (path);
60                 }
61         }
62         
63         using Sink<T>::process;
64         
65         boost::signals2::signal<void (std::string)> FileWritten;
66
67   protected:
68         /// SndfileHandle has to be constructed directly by deriving classes
69         SndfileWriter ()
70         {
71                 add_supported_flag (ProcessContext<T>::EndOfInput);
72         }
73         
74   protected:
75         std::string path;
76 };
77
78 } // namespace
79
80 #endif // AUDIOGRAPHER_SNDFILE_WRITER_H