Ardour  9.0-pre0-582-g084a23a80d
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 
7 #include "audiographer/sink.h"
8 #include "audiographer/types.h"
11 
12 #include "pbd/compose.h"
13 #include "pbd/signals.h"
14 
15 namespace AudioGrapher
16 {
17 
21 template <typename T = DefaultSampleType>
23  : public virtual SndfileBase
24  , public Sink<T>
25  , public Throwing<>
26  , public FlagDebuggable<>
27 {
28  public:
29  SndfileWriter (std::string const & path, int format, ChannelCount channels, samplecnt_t samplerate, std::shared_ptr<BroadcastInfo> broadcast_info)
31  , path (path)
32  {
33  init();
34 
35  if (broadcast_info) {
36  broadcast_info->write_to_file (this);
37  }
38  }
39 
40  virtual ~SndfileWriter () {}
41 
42  using SndfileHandle::operator=;
43 
46 
48  virtual void process (ProcessContext<T> const & c)
49  {
50  check_flags (*this, c);
51 
52  if (throw_level (ThrowStrict) && c.channels() != channels()) {
53  throw Exception (*this, string_compose
54  ("Wrong number of channels given to process(), %1 instead of %2",
55  c.channels(), channels()));
56  }
57 
58  samplecnt_t const written = write (c.data(), c.samples());
59  samples_written += written;
60 
61  if (throw_level (ThrowProcess) && written != c.samples()) {
62  std::stringstream reason;
63  reason << "Could not write data to output file ("
64  << strError()
65  << ")";
66 
67  throw Exception (*this, reason.str());
68  }
69 
71  writeSync();
72  FileWritten (path);
73  }
74  }
75 
76  using Sink<T>::process;
77 
78  PBD::Signal<void(std::string)> FileWritten;
79 
80  protected:
83  {
84  init();
85  }
86 
87  virtual void init()
88  {
89  if (SF_ERR_NO_ERROR != SndfileHandle::error ()) {
90  std::stringstream reason;
91  reason << "Could not write data to output file ("
92  << path
93  << ")";
94 
95  throw Exception (*this, reason.str());
96  }
97  samples_written = 0;
99  }
100 
101  protected:
102  std::string path;
104 
105  private:
106  SndfileWriter (SndfileWriter const & other) {}
107 };
108 
109 } // namespace
110 
111 #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::Signal< void(std::string)> FileWritten
SndfileWriter(SndfileWriter const &other)
bool throw_level(ThrowLevel level)
Definition: throwing.h:47
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:246
@ ThrowProcess
Process cycle level stuff.
Definition: throwing.h:23
@ ThrowStrict
Stricter checks than ThrowProcess, less than ThrowSample.
Definition: throwing.h:24