Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
sox_source.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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_source.h
10 //! @brief SoX source.
11 
12 #ifndef ROC_SNDIO_SOX_SOURCE_H_
13 #define ROC_SNDIO_SOX_SOURCE_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/isource.h"
24 
25 namespace roc {
26 namespace sndio {
27 
28 //! SoX source.
29 //! @remarks
30 //! Reads samples from input file or device.
31 //! Supports multiple drivers for different file types and audio systems.
32 class SoxSource : public ISource, private core::NonCopyable<> {
33 public:
34  //! Initialize.
35  SoxSource(core::IAllocator& allocator, const Config& config);
36 
37  virtual ~SoxSource();
38 
39  //! Check if the object was successfully constructed.
40  bool valid() const;
41 
42  //! Open input file or device.
43  //!
44  //! @b Parameters
45  //! - @p driver is input driver name;
46  //! - @p input is input file or device name, "-" for stdin.
47  //!
48  //! @remarks
49  //! If @p driver or @p input are NULL, defaults are used.
50  bool open(const char* driver, const char* input);
51 
52  //! Get sample rate of an input file or a device.
53  virtual size_t sample_rate() const;
54 
55  //! Check if the source has own clock.
56  virtual bool has_clock() const;
57 
58  //! Get current source state.
59  virtual State state() const;
60 
61  //! Wait until the source state becomes active.
62  virtual void wait_active() const;
63 
64  //! Read frame.
65  virtual bool read(audio::Frame&);
66 
67 private:
68  bool prepare_();
69  bool open_(const char* driver, const char* input);
70  void close_();
71 
72  sox_format_t* input_;
73  sox_signalinfo_t in_signal_;
74  size_t n_channels_;
75 
76  core::IAllocator& allocator_;
77 
79  const size_t buffer_size_;
80 
81  bool is_file_;
82  bool eof_;
83  bool valid_;
84 };
85 
86 } // namespace sndio
87 } // namespace roc
88 
89 #endif // ROC_SNDIO_SOX_SOURCE_H_
virtual size_t sample_rate() const
Get sample rate of an input file or a device.
Memory allocator interface.
Definition: iallocator.h:23
SoX source.
Definition: sox_source.h:32
State
Source state.
Definition: isource.h:32
virtual bool has_clock() const
Check if the source has own clock.
SoxSource(core::IAllocator &allocator, const Config &config)
Initialize.
Root namespace.
virtual State state() const
Get current source state.
Sink and source config.
Audio frame.
Definition: frame.h:22
Commonly used types and functions.
Sink and source config.
Definition: config.h:22
bool open(const char *driver, const char *input)
Open input file or device.
Source interface.
Base class for non-copyable objects.
Definition: noncopyable.h:23
virtual bool read(audio::Frame &)
Read frame.
virtual void wait_active() const
Wait until the source state becomes active.
Various units used in packets.
Unique ownrship pointer.
Non-copyable object.
bool valid() const
Check if the object was successfully constructed.
Source interface.
Definition: isource.h:21
Memory allocator interface.