-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwavsink.cpp
More file actions
148 lines (139 loc) · 4.69 KB
/
Copy pathwavsink.cpp
File metadata and controls
148 lines (139 loc) · 4.69 KB
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
#include <cstdio>
#include <iostream>
#include <sstream>
#include <sys/stat.h>
#if defined(_MSC_VER ) || defined(__MINGW32__)
#include <io.h>
#else
#include <unistd.h>
#endif
#include "wavsource.h"
#include "wavsink.h"
WaveSink::WaveSink(FILE *fp,
uint64_t duration,
const AudioStreamBasicDescription &asbd,
uint32_t chanmask)
: m_file(fp), m_bytes_written(0), m_closed(false),
m_seekable(false), m_chanmask(chanmask), m_asbd(asbd)
{
struct stat stb = { 0 };
if (fstat(fileno(fp), &stb))
util::throw_crt_error("fstat()");
m_seekable = ((stb.st_mode & S_IFMT) == S_IFREG);
std::string header = buildHeader();
uint32_t hdrsize = header.size();
uint32_t riffsize = ~0, datasize = ~0;
m_rf64 = m_seekable;
if (duration != ~0ULL) {
uint64_t datasize64 = duration * m_bytes_per_frame;
uint64_t riffsize64 = hdrsize + datasize64 + 20;
if (riffsize64 >> 32 == 0) {
datasize = static_cast<uint32_t>(datasize64);
riffsize = static_cast<uint32_t>(riffsize64);
m_rf64 = false;
}
}
write("RIFF", 4);
write(&riffsize, 4);
write("WAVE", 4);
if (m_rf64) {
write("JUNK", 4);
static const char filler[32] = { 0x1c, 0 };
write(filler, 32);
}
write("fmt ", 4);
write(&hdrsize, 4);
write(header.c_str(), hdrsize);
write("data", 4);
write(&datasize, 4);
m_data_pos = 28 + hdrsize + (m_rf64 ? 36 : 0);
if (!m_seekable) std::fflush(fp);
}
std::string WaveSink::buildHeader()
{
std::ostringstream oss;
std::stringbuf *os = oss.rdbuf();
int bpc = ((m_asbd.mBitsPerChannel + 7) & ~7) / 8;
m_bytes_per_frame = bpc * m_asbd.mChannelsPerFrame;
// wFormatTag
uint16_t fmt = (m_asbd.mChannelsPerFrame > 2
|| m_asbd.mBitsPerChannel > 16
|| (m_asbd.mBitsPerChannel & 7))
? 0xfffe : 1;
put(os, fmt);
// nChannels
put(os, static_cast<uint16_t>(m_asbd.mChannelsPerFrame));
// nSamplesPerSec
put(os, static_cast<uint32_t>(m_asbd.mSampleRate));
// nAvgBytesPerSec
put(os, static_cast<uint32_t>(m_asbd.mSampleRate * m_bytes_per_frame));
// nBlockAlign
put(os, static_cast<uint16_t>(m_bytes_per_frame));
// wBitsPerSample
put(os, static_cast<uint16_t>(bpc << 3));
// cbSize
if (fmt == 0xfffe) {
// WAVEFORMATEXTENSIBLE
put(os, static_cast<uint16_t>(22));
// Samples
put(os, static_cast<uint16_t>(m_asbd.mBitsPerChannel));
// dwChannelMask
put(os, m_chanmask);
// SubFormat
if (m_asbd.mFormatFlags & kAudioFormatFlagIsFloat)
put(os, wave::ksFormatSubTypeFloat);
else
put(os, wave::ksFormatSubTypePCM);
}
return oss.str();
}
void WaveSink::writeSamples(const void *data, size_t length, size_t nsamples)
{
uint8_t *bp = static_cast<uint8_t *>(const_cast<void*>(data));
std::vector<uint8_t> buf;
if (m_bytes_per_frame < m_asbd.mBytesPerFrame) {
unsigned obpc = m_asbd.mBytesPerFrame / m_asbd.mChannelsPerFrame;
unsigned nbpc = m_bytes_per_frame / m_asbd.mChannelsPerFrame;
util::pack(bp, &length, obpc, nbpc);
}
if (m_asbd.mBitsPerChannel <= 8 &&
m_asbd.mFormatFlags & kAudioFormatFlagIsSignedInteger) {
buf.resize(length);
bp = &buf[0];
std::memcpy(bp, data, length);
for (size_t i = 0; i < length; ++i)
bp[i] ^= 0x80;
}
write(bp, length);
m_bytes_written += length;
if (!m_seekable) std::fflush(m_file);
}
void WaveSink::finishWrite()
{
if (m_closed) return;
m_closed = true;
if (m_bytes_written & 1) write("\0", 1);
if (!m_seekable) return;
uint64_t datasize64 = m_bytes_written;
uint64_t riffsize64 = datasize64 + m_data_pos - 8;
if (riffsize64 >> 32 == 0) {
if (std::fseek(m_file, m_data_pos - 4, SEEK_SET) == 0) {
uint32_t size32 = static_cast<uint32_t>(datasize64);
write(&size32, 4);
if (std::fseek(m_file, 4, SEEK_SET) == 0) {
size32 = static_cast<uint32_t>(riffsize64);
write(&size32, 4);
}
}
} else if (m_rf64) {
std::rewind(m_file);
write("RF64", 4);
std::fseek(m_file, 8, SEEK_CUR);
write("ds64", 4);
std::fseek(m_file, 4, SEEK_CUR);
write(&riffsize64, 8);
write(&datasize64, 8);
uint64_t nsamples = m_bytes_written / m_bytes_per_frame;
write(&nsamples, 8);
}
}