Re-integrate export-optimization branch.
[ardour.git] / libs / audiographer / src / sndfile_base.cc
1 #include "audiographer/sndfile_base.h"
2 #include "audiographer/exception.h"
3
4 #include <boost/format.hpp>
5
6 namespace AudioGrapher
7 {
8
9 using std::string;
10 using boost::str;
11 using boost::format;
12
13 /* SndfileWriterBase */
14
15 SndfileBase::SndfileBase (ChannelCount channels, nframes_t samplerate, int format, string const & path)
16   : path (path)
17 {
18         char errbuf[256];
19
20         sf_info.channels = channels;
21         sf_info.samplerate = samplerate;
22         sf_info.format = format;
23
24         if (!sf_format_check (&sf_info)) {
25                 throw Exception (*this, "Invalid format in constructor");
26         }
27
28         if (path.length() == 0) {
29                 throw Exception (*this, "No output file specified");
30         }
31
32         /* TODO add checks that the directory path exists, and also
33            check if we are overwriting an existing file...
34         */
35
36         // Open file
37         if (path.compare ("temp")) {
38                 if ((sndfile = sf_open (path.c_str(), SFM_WRITE, &sf_info)) == 0) {
39                         sf_error_str (0, errbuf, sizeof (errbuf) - 1);
40                         throw Exception (*this, str (boost::format ("Cannot open output file \"%1%\" (%2%)") % path % errbuf));
41                 }
42         } else {
43                 FILE * file;
44                 if (!(file = tmpfile ())) {
45                         throw Exception (*this, "Cannot open tempfile");
46                 }
47                 sndfile = sf_open_fd (fileno(file), SFM_RDWR, &sf_info, true);
48         }
49 }
50
51 SndfileBase::~SndfileBase ()
52 {
53         sf_close (sndfile);
54 }
55
56 } // namespace