Ardour  8.7-15-gadf511264b
sndfile_writer.h
Go to the documentation of this file.
1 #ifndef AUDIOGRAPHER_SNDFILE_WRITER_H
2 #define AUDIOGRAPHER_SNDFILE_WRITER_H
3 
4 #include <string>
5 
6 #include <boost/format.hpp>
7 
9 #include "audiographer/sink.h"
10 #include "audiographer/types.h"
13 
14 #include "pbd/signals.h"
15 
16 namespace AudioGrapher
17 {
18 
22 template <typename T = DefaultSampleType>
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, samplecnt_t samplerate, std::shared_ptr<BroadcastInfo> broadcast_info)
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  using SndfileHandle::operator=;
44 
47 
49  virtual void process (ProcessContext<T> const & c)
50  {
51  check_flags (*this, c);
52 
53  if (throw_level (ThrowStrict) && c.channels() != channels()) {
54  throw Exception (*this, boost::str (boost::format
55  ("Wrong number of channels given to process(), %1% instead of %2%")
56  % c.channels() % channels()));
57  }
58 
59  samplecnt_t const written = write (c.data(), c.samples());
60  samples_written += written;
61 
62  if (throw_level (ThrowProcess) && written != c.samples()) {
63  throw Exception (*this, boost::str (boost::format
64  ("Could not write data to output file (%1%)")
65  % strError()));
66  }
67 
69  writeSync();
70  FileWritten (path);
71  }
72  }
73 
74  using Sink<T>::process;
75 
76  PBD::Signal1<void, std::string> FileWritten;
77 
78  protected:
81  {
82  init();
83  }
84 
85  virtual void init()
86  {
87  if (SF_ERR_NO_ERROR != SndfileHandle::error ()) {
88  throw Exception (*this, boost::str (boost::format
89  ("Could not create output file (%1%)") % path));
90  }
91  samples_written = 0;
93  }
94 
95  protected:
96  std::string path;
98 
99  private:
100  SndfileWriter (SndfileWriter const & other) {}
101 };
102 
103 } // namespace
104 
105 #endif // AUDIOGRAPHER_SNDFILE_WRITER_H
A debugging class for nodes that support a certain set of flags.
void check_flags(SelfType &self, ProcessContext< ContextType > context)
Prints debug output if context contains flags that are not supported by this class.
void add_supported_flag(Flag flag)
Adds a flag to the set of flags supported.
ChannelCount const & channels() const
bool has_flag(Flag flag) const
T const * data() const
data points to the array of data to process
samplecnt_t const & samples() const
samples tells how many samples the array pointed by data contains
Base class for all classes using libsndfile.
Definition: sndfile_base.h:13
int samplerate(void) const
Definition: sndfile.hh:103
const char * strError(void) const
sf_count_t write(const short *ptr, sf_count_t items)
int channels(void) const
Definition: sndfile.hh:102
int format(void) const
Definition: sndfile.hh:101
SndfileWriter()
SndfileHandle has to be constructed directly by deriving classes.
SndfileWriter(std::string const &path, int format, ChannelCount channels, samplecnt_t samplerate, std::shared_ptr< BroadcastInfo > broadcast_info)
virtual void process(ProcessContext< T > const &c)
Writes data to file.
samplecnt_t get_samples_written() const
PBD::Signal1< void, std::string > FileWritten
SndfileWriter(SndfileWriter const &other)
bool throw_level(ThrowLevel level)
Definition: throwing.h:47
@ ThrowProcess
Process cycle level stuff.
Definition: throwing.h:23
@ ThrowStrict
Stricter checks than ThrowProcess, less than ThrowSample.
Definition: throwing.h:24