Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
sox_sink.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Roc authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_sndio/target_sox/roc_sndio/sox_sink.h
10 //! @brief SoX sink.
11 
12 #ifndef ROC_SNDIO_SOX_SINK_H_
13 #define ROC_SNDIO_SOX_SINK_H_
14 
15 #include <sox.h>
16 
17 #include "roc_core/iallocator.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/stddefs.h"
20 #include "roc_core/unique_ptr.h"
21 #include "roc_packet/units.h"
22 #include "roc_sndio/config.h"
23 #include "roc_sndio/isink.h"
24 
25 namespace roc {
26 namespace sndio {
27 
28 //! SoX sink.
29 //! @remarks
30 //! Writes samples to output file or device.
31 //! Supports multiple drivers for different file types and audio systems.
32 class SoxSink : public ISink, public core::NonCopyable<> {
33 public:
34  //! Initialize.
35  SoxSink(core::IAllocator& allocator, const Config& config);
36 
37  virtual ~SoxSink();
38 
39  //! Check if the object was successfully constructed.
40  bool valid() const;
41 
42  //! Open output file or device.
43  //!
44  //! @b Parameters
45  //! - @p driver is output driver name;
46  //! - @p output is output file or device name, "-" for stdout.
47  //!
48  //! @remarks
49  //! If @p driver or @p output are NULL, defaults are used.
50  bool open(const char* driver, const char* output);
51 
52  //! Get sample rate of the sink.
53  virtual size_t sample_rate() const;
54 
55  //! Check if the sink has own clock.
56  virtual bool has_clock() const;
57 
58  //! Write audio frame.
59  virtual void write(audio::Frame& frame);
60 
61 private:
62  bool prepare_();
63  bool open_(const char* driver, const char* output);
64  void write_(const sox_sample_t* samples, size_t n_samples);
65  void close_();
66 
67  sox_format_t* output_;
68  sox_signalinfo_t out_signal_;
69 
70  core::IAllocator& allocator_;
71 
73  const size_t buffer_size_;
74 
75  bool is_file_;
76  bool valid_;
77 };
78 
79 } // namespace sndio
80 } // namespace roc
81 
82 #endif // ROC_SNDIO_SOX_SINK_H_
Sink interface.
Definition: isink.h:21
Memory allocator interface.
Definition: iallocator.h:23
Sink interface.
bool valid() const
Check if the object was successfully constructed.
Root namespace.
Sink and source config.
virtual void write(audio::Frame &frame)
Write audio frame.
Audio frame.
Definition: frame.h:22
Commonly used types and functions.
bool open(const char *driver, const char *output)
Open output file or device.
Sink and source config.
Definition: config.h:22
Base class for non-copyable objects.
Definition: noncopyable.h:23
virtual size_t sample_rate() const
Get sample rate of the sink.
SoX sink.
Definition: sox_sink.h:32
virtual bool has_clock() const
Check if the sink has own clock.
SoxSink(core::IAllocator &allocator, const Config &config)
Initialize.
Various units used in packets.
Unique ownrship pointer.
Non-copyable object.
Memory allocator interface.