Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
sndfile_source.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023 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_sndfile/roc_sndio/sndfile_source.h
10 //! @brief Sndfile source.
11 
12 #ifndef ROC_SNDIO_SNDFILE_SOURCE_H_
13 #define ROC_SNDIO_SNDFILE_SOURCE_H_
14 
15 #include <sndfile.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/isource.h"
26 
27 namespace roc {
28 namespace sndio {
29 
30 //! Sndfile source.
31 //! @remarks
32 //! Reads samples from input file.
33 //! Supports multiple drivers for different file types.
34 class SndfileSource : public ISource, private core::NonCopyable<> {
35 public:
36  //! Initialize.
37  SndfileSource(core::IArena& arena, const Config& config);
38 
39  virtual ~SndfileSource();
40 
41  //! Check if the object was successfully constructed.
42  bool is_valid() const;
43 
44  //! Open input file or device.
45  //!
46  //! @b Parameters
47  //! - @p driver is input format name;
48  //! - @p path is input file name, "-" for stdin.
49  //!
50  //! @remarks
51  //! If @p driver is NULL, default is used.
52  bool open(const char* driver, const char* path);
53 
54  //! Cast IDevice to ISink.
55  virtual ISink* to_sink();
56 
57  //! Cast IDevice to ISink.
58  virtual ISource* to_source();
59 
60  //! Get device type.
61  virtual DeviceType type() const;
62 
63  //! Get device state.
64  virtual DeviceState state() const;
65 
66  //! Pause reading.
67  virtual void pause();
68 
69  //! Resume paused reading.
70  virtual bool resume();
71 
72  //! Restart reading from the beginning.
73  virtual bool restart();
74 
75  //! Get sample specification of the source.
76  virtual audio::SampleSpec sample_spec() const;
77 
78  //! Get latency of the source.
79  virtual core::nanoseconds_t latency() const;
80 
81  //! Check if the source supports latency reports.
82  virtual bool has_latency() const;
83 
84  //! Check if the source has own clock.
85  virtual bool has_clock() const;
86 
87  //! Adjust source clock to match consumer clock.
89 
90  //! Read frame.
91  virtual bool read(audio::Frame&);
92 
93 private:
94  bool open_();
95  void close_();
96 
97  bool seek_(size_t offset);
98 
99  audio::SampleSpec sample_spec_;
100 
101  SNDFILE* file_;
102  SF_INFO file_info_;
103  core::StringBuffer path_;
104  bool valid_;
105 };
106 
107 } // namespace sndio
108 } // namespace roc
109 
110 #endif // ROC_SNDIO_SNDFILE_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
virtual ISink * to_sink()
Cast IDevice to ISink.
virtual bool restart()
Restart reading from the beginning.
bool is_valid() const
Check if the object was successfully constructed.
virtual void reclock(core::nanoseconds_t timestamp)
Adjust source clock to match consumer clock.
virtual ISource * to_source()
Cast IDevice to ISink.
virtual audio::SampleSpec sample_spec() const
Get sample specification of the source.
virtual DeviceState state() const
Get device state.
virtual bool has_latency() const
Check if the source supports latency reports.
bool open(const char *driver, const char *path)
Open input file or device.
virtual bool resume()
Resume paused reading.
SndfileSource(core::IArena &arena, const Config &config)
Initialize.
virtual DeviceType type() const
Get device type.
virtual core::nanoseconds_t latency() const
Get latency of the source.
virtual bool read(audio::Frame &)
Read frame.
virtual bool has_clock() const
Check if the source has own clock.
virtual void pause()
Pause reading.
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
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.