-
-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathwav_sink.cpp
210 lines (164 loc) · 5.26 KB
/
wav_sink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "roc_sndio/wav_sink.h"
#include "roc_audio/pcm_format.h"
#include "roc_audio/sample_format.h"
#include "roc_core/endian_ops.h"
#include "roc_core/log.h"
#include "roc_core/panic.h"
namespace roc {
namespace sndio {
namespace {
const size_t DefaultChans = 2;
const size_t DefaultRate = 44100;
} // namespace
WavSink::WavSink(core::IArena& arena, const Config& config)
: output_file_(NULL)
, valid_(false) {
if (config.latency != 0) {
roc_log(LogError, "wav sink: setting io latency not supported");
return;
}
sample_spec_ = config.sample_spec;
if (sample_spec_.sample_format() != audio::SampleFormat_Invalid
&& (sample_spec_.sample_format() != audio::SampleFormat_Pcm
|| sample_spec_.pcm_format() != audio::Sample_RawFormat)) {
roc_log(LogError, "wav sink: sample format can be only \"-\" or \"%s\"",
audio::pcm_format_to_str(audio::Sample_RawFormat));
return;
}
if (sample_spec_.sample_format() == audio::SampleFormat_Invalid) {
sample_spec_.set_sample_format(audio::SampleFormat_Pcm);
sample_spec_.set_pcm_format(audio::Sample_RawFormat);
}
if (sample_spec_.sample_rate() == 0) {
sample_spec_.set_sample_rate(DefaultRate);
}
if (!sample_spec_.channel_set().is_valid()) {
sample_spec_.channel_set().set_layout(audio::ChanLayout_Surround);
sample_spec_.channel_set().set_order(audio::ChanOrder_Smpte);
sample_spec_.channel_set().set_channel_range(0, DefaultChans, true);
}
header_.reset(new (header_)
WavHeader(sample_spec_.num_channels(), sample_spec_.sample_rate(),
sizeof(audio::sample_t) * 8));
valid_ = true;
}
WavSink::~WavSink() {
close_();
}
bool WavSink::is_valid() const {
return valid_;
}
bool WavSink::open(const char* path) {
roc_panic_if(!valid_);
if (!open_(path)) {
return false;
}
return true;
}
ISink* WavSink::to_sink() {
return this;
}
ISource* WavSink::to_source() {
return NULL;
}
DeviceType WavSink::type() const {
return DeviceType_Sink;
}
DeviceState WavSink::state() const {
return DeviceState_Active;
}
void WavSink::pause() {
// no-op
}
bool WavSink::resume() {
return true;
}
bool WavSink::restart() {
return true;
}
audio::SampleSpec WavSink::sample_spec() const {
if (!output_file_) {
roc_panic("wav sink: not opened");
}
return sample_spec_;
}
core::nanoseconds_t WavSink::latency() const {
return 0;
}
bool WavSink::has_latency() const {
return false;
}
bool WavSink::has_clock() const {
return false;
}
void WavSink::write(audio::Frame& frame) {
if (!output_file_) {
roc_panic("wav sink: not opened");
}
const audio::sample_t* samples = frame.raw_samples();
size_t n_samples = frame.num_raw_samples();
if (n_samples > 0) {
if (fseek(output_file_, 0, SEEK_SET)) {
roc_log(LogError, "wav sink: failed to seek to the beginning of the file: %s",
core::errno_to_str(errno).c_str());
}
const WavHeader::WavHeaderData& wav_header =
header_->update_and_get_header(n_samples);
if (fwrite(&wav_header, sizeof(wav_header), 1, output_file_) != 1) {
roc_log(LogError, "wav sink: failed to write header: %s",
core::errno_to_str(errno).c_str());
}
if (fseek(output_file_, 0, SEEK_END)) {
roc_log(LogError,
"wav sink: failed to seek to append position of the file: %s",
core::errno_to_str(errno).c_str());
}
if (fwrite(samples, sizeof(audio::sample_t), n_samples, output_file_)
!= n_samples) {
roc_log(LogError, "wav sink: failed to write samples: %s",
core::errno_to_str(errno).c_str());
}
if (fflush(output_file_)) {
roc_log(LogError, "wav sink: failed to flush data to the file: %s",
core::errno_to_str(errno).c_str());
}
}
}
bool WavSink::open_(const char* path) {
if (output_file_) {
roc_panic("wav sink: already opened");
}
output_file_ = fopen(path, "w");
if (!output_file_) {
roc_log(LogDebug, "wav sink: can't open output file: %s",
core::errno_to_str(errno).c_str());
return false;
}
roc_log(LogInfo,
"wav sink: opened output file:"
" path=%s out_bits=%lu out_rate=%lu out_ch=%lu",
path, (unsigned long)header_->bits_per_sample(),
(unsigned long)header_->sample_rate(),
(unsigned long)header_->num_channels());
return true;
}
void WavSink::close_() {
if (!output_file_) {
return;
}
roc_log(LogDebug, "wav sink: closing output file");
if (fclose(output_file_)) {
roc_panic("wav sink: can't close output file: %s",
core::errno_to_str(errno).c_str());
}
output_file_ = NULL;
}
} // namespace sndio
} // namespace roc