- Fix process callbakc handling during export
[ardour.git] / libs / audiographer / src / sndfile_writer.cc
1 #include "audiographer/sndfile_writer.h"
2 #include "audiographer/exception.h"
3
4 #include <cstring>
5
6 #include <boost/format.hpp>
7
8 namespace AudioGrapher
9 {
10
11 using std::string;
12 using boost::str;
13 using boost::format;
14
15 template <typename T>
16 SndfileWriter<T>::SndfileWriter (ChannelCount channels, nframes_t samplerate, int format, string const & path) :
17   SndfileBase (channels, samplerate, format, path)
18 {
19         // init write function
20         init ();
21 }
22
23 template <>
24 void
25 SndfileWriter<float>::init ()
26 {
27         write_func = &sf_write_float;
28 }
29
30 template <>
31 void
32 SndfileWriter<int>::init ()
33 {
34         write_func = &sf_write_int;
35 }
36
37 template <>
38 void
39 SndfileWriter<short>::init ()
40 {
41         write_func = &sf_write_short;
42 }
43
44 template <typename T>
45 void
46 SndfileWriter<T>::process (ProcessContext<T> const & c)
47 {
48         if (c.channels() != sf_info.channels) {
49                 throw Exception (*this, str (boost::format(
50                         "Wrong number of channels given to process(), %1% instead of %2%")
51                         % c.channels() % sf_info.channels));
52         }
53         
54         char errbuf[256];
55         nframes_t written = (*write_func) (sndfile, c.data(), c.frames());
56         if (written != c.frames()) {
57                 sf_error_str (sndfile, errbuf, sizeof (errbuf) - 1);
58                 throw Exception (*this, str ( format("Could not write data to output file (%1%)") % errbuf));
59         }
60
61         if (c.has_flag(ProcessContext<T>::EndOfInput)) {
62                 sf_write_sync (sndfile);
63                 FileWritten (path);
64                 if (debug_level (DebugProcess)) {
65                         debug_stream() << str ( format("Finished writing file %1%") % path) << std::endl;
66                 }
67         }
68 }
69
70 template class SndfileWriter<short>;
71 template class SndfileWriter<int>;
72 template class SndfileWriter<float>;
73
74 } // namespace