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 Streaming 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_audio/sample_spec.h"
18 #include "roc_core/array.h"
19 #include "roc_core/iarena.h"
20 #include "roc_core/noncopyable.h"
21 #include "roc_core/stddefs.h"
22 #include "roc_core/string_buffer.h"
23 #include "roc_packet/units.h"
24 #include "roc_sndio/config.h"
25 #include "roc_sndio/driver.h"
26 #include "roc_sndio/isource.h"
27 
28 namespace roc {
29 namespace sndio {
30 
31 //! SoX source.
32 //! @remarks
33 //! Reads samples from input file or device.
34 //! Supports multiple drivers for different file types and audio systems.
35 class SoxSource : public ISource, private core::NonCopyable<> {
36 public:
37  //! Initialize.
38  SoxSource(core::IArena& arena, const Config& config, DriverType type);
39 
40  virtual ~SoxSource();
41 
42  //! Check if the object was successfully constructed.
43  bool is_valid() const;
44 
45  //! Open input file or device.
46  //!
47  //! @b Parameters
48  //! - @p driver is input driver name;
49  //! - @p path is input file or device name, "-" for stdin.
50  //!
51  //! @remarks
52  //! If @p driver or @p path are NULL, defaults are used.
53  bool open(const char* driver, const char* path);
54 
55  //! Cast IDevice to ISink.
56  virtual ISink* to_sink();
57 
58  //! Cast IDevice to ISink.
59  virtual ISource* to_source();
60 
61  //! Get device type.
62  virtual DeviceType type() const;
63 
64  //! Get device state.
65  virtual DeviceState state() const;
66 
67  //! Pause reading.
68  virtual void pause();
69 
70  //! Resume paused reading.
71  virtual bool resume();
72 
73  //! Restart reading from the beginning.
74  virtual bool restart();
75 
76  //! Get sample specification of the source.
77  virtual audio::SampleSpec sample_spec() const;
78 
79  //! Get latency of the source.
80  virtual core::nanoseconds_t latency() const;
81 
82  //! Check if the source supports latency reports.
83  virtual bool has_latency() const;
84 
85  //! Check if the source has own clock.
86  virtual bool has_clock() const;
87 
88  //! Adjust source clock to match consumer clock.
90 
91  //! Read frame.
92  virtual bool read(audio::Frame&);
93 
94 private:
95  bool setup_names_(const char* driver, const char* path);
96  bool setup_buffer_();
97 
98  bool open_();
99  void close_();
100 
101  bool seek_(uint64_t offset);
102 
103  core::StringBuffer driver_name_;
104  core::StringBuffer input_name_;
105 
107  size_t buffer_size_;
108  core::nanoseconds_t frame_length_;
109  audio::SampleSpec sample_spec_;
110 
111  sox_format_t* input_;
112  sox_signalinfo_t in_signal_;
113 
114  bool is_file_;
115  bool eof_;
116  bool paused_;
117  bool valid_;
118 };
119 
120 } // namespace sndio
121 } // namespace roc
122 
123 #endif // ROC_SNDIO_SOX_SOURCE_H_
Dynamic array.
Audio frame.
Definition: frame.h:25
Sample specification. Describes sample rate and channels.
Definition: sample_spec.h:30
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Sink interface.
Definition: isink.h:22
Source interface.
Definition: isource.h:23
bool open(const char *driver, const char *path)
Open input file or device.
virtual void pause()
Pause reading.
virtual DeviceType type() const
Get device type.
virtual DeviceState state() const
Get device state.
SoxSource(core::IArena &arena, const Config &config, DriverType type)
Initialize.
bool is_valid() const
Check if the object was successfully constructed.
virtual bool has_clock() const
Check if the source has own clock.
virtual ISource * to_source()
Cast IDevice to ISink.
virtual bool restart()
Restart reading from the beginning.
virtual audio::SampleSpec sample_spec() const
Get sample specification of the source.
virtual core::nanoseconds_t latency() const
Get latency of the source.
virtual bool read(audio::Frame &)
Read frame.
virtual bool has_latency() const
Check if the source supports latency reports.
virtual void reclock(core::nanoseconds_t timestamp)
Adjust source clock to match consumer clock.
virtual bool resume()
Resume paused reading.
virtual ISink * to_sink()
Cast IDevice to ISink.
Driver types.
Memory arena interface.
Source interface.
nanoseconds_t timestamp(clock_t clock)
Get current timestamp in nanoseconds.
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:58
DeviceType
Device type.
Definition: device_type.h:19
DriverType
Driver type.
Definition: driver.h:27
DeviceState
Device state.
Definition: device_state.h:19
Root namespace.
Non-copyable object.
Sink and source config.
Sample specifications.
Commonly used types and functions.
String buffer.
Sink and source config.
Definition: config.h:29
Various units used in packets.