Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
wav_sink.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/wav_sink.h
10 //! @brief WAV sink.
11 
12 #ifndef ROC_SNDIO_WAV_SINK_H_
13 #define ROC_SNDIO_WAV_SINK_H_
14 
15 #include "roc_audio/sample_spec.h"
16 #include "roc_core/array.h"
17 #include "roc_core/iarena.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/optional.h"
20 #include "roc_core/stddefs.h"
21 #include "roc_packet/units.h"
22 #include "roc_sndio/config.h"
23 #include "roc_sndio/isink.h"
24 
25 #include "wav_header.h"
26 
27 namespace roc {
28 namespace sndio {
29 
30 //! WAV sink.
31 //! @remarks
32 //! Writes samples to output file.
33 class WavSink : public ISink, public core::NonCopyable<> {
34 public:
35  //! Initialize.
36  WavSink(core::IArena& arena, const Config& config);
37 
38  virtual ~WavSink();
39 
40  //! Check if the object was successfully constructed.
41  bool is_valid() const;
42 
43  //! Open output file.
44  //!
45  //! @b Parameters
46  //! - @p path is output file or device name, "-" for stdout.
47  //!
48  //! @remarks
49  //! If @p path is NULL, defaults are used.
50  bool open(const char* path);
51 
52  //! Cast IDevice to ISink.
53  virtual ISink* to_sink();
54 
55  //! Cast IDevice to ISink.
56  virtual ISource* to_source();
57 
58  //! Get device type.
59  virtual DeviceType type() const;
60 
61  //! Get device state.
62  virtual DeviceState state() const;
63 
64  //! Pause reading.
65  virtual void pause();
66 
67  //! Resume paused reading.
68  virtual bool resume();
69 
70  //! Restart reading from the beginning.
71  virtual bool restart();
72 
73  //! Get sample specification of the sink.
74  virtual audio::SampleSpec sample_spec() const;
75 
76  //! Get latency of the sink.
77  virtual core::nanoseconds_t latency() const;
78 
79  //! Check if the sink supports latency reports.
80  virtual bool has_latency() const;
81 
82  //! Check if the sink has own clock.
83  virtual bool has_clock() const;
84 
85  //! Write audio frame.
86  virtual void write(audio::Frame& frame);
87 
88 private:
89  bool open_(const char* path);
90  void close_();
91 
92  audio::SampleSpec sample_spec_;
93 
94  FILE* output_file_;
96 
97  bool valid_;
98 };
99 
100 } // namespace sndio
101 } // namespace roc
102 
103 #endif // ROC_SNDIO_WAV_SINK_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
Optionally constructed object.
Definition: optional.h:25
Sink interface.
Definition: isink.h:22
Source interface.
Definition: isource.h:23
bool open(const char *path)
Open output file.
virtual void write(audio::Frame &frame)
Write audio frame.
virtual audio::SampleSpec sample_spec() const
Get sample specification of the sink.
virtual bool has_latency() const
Check if the sink supports latency reports.
virtual bool has_clock() const
Check if the sink has own clock.
virtual DeviceState state() const
Get device state.
virtual bool resume()
Resume paused reading.
virtual ISink * to_sink()
Cast IDevice to ISink.
WavSink(core::IArena &arena, const Config &config)
Initialize.
virtual ISource * to_source()
Cast IDevice to ISink.
virtual void pause()
Pause reading.
virtual core::nanoseconds_t latency() const
Get latency of the sink.
virtual bool restart()
Restart reading from the beginning.
bool is_valid() const
Check if the object was successfully constructed.
virtual DeviceType type() const
Get device type.
Memory arena interface.
Sink interface.
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.
Optionally constructed object.
Sink and source config.
Sample specifications.
Commonly used types and functions.
Sink and source config.
Definition: config.h:29
Various units used in packets.
WAV header.