Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
audiowmark
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Stefan Westerfeld
audiowmark
Commits
21248bdc
Commit
21248bdc
authored
Nov 13, 2019
by
Stefan Westerfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add 24-bit support for stdout wav stream.
Signed-off-by:
Stefan Westerfeld
<
stefan@space.twc.de
>
parent
66628a94
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
7 deletions
+56
-7
teststream.cc
src/teststream.cc
+56
-7
No files found.
src/teststream.cc
View file @
21248bdc
...
...
@@ -63,9 +63,22 @@ public:
class
StdoutWavOutputStream
:
public
AudioOutputStream
{
std
::
string
m_error_blurb
;
int
m_bit_depth
=
0
;
size_t
m_close_padding
=
0
;
enum
class
State
{
NEW
,
OPEN
,
CLOSED
};
State
m_state
=
State
::
NEW
;
public
:
~
StdoutWavOutputStream
();
bool
open
(
int
n_channels
,
int
sample_rate
,
int
bit_depth
,
size_t
n_frames
);
bool
write_frames
(
const
std
::
vector
<
float
>&
frames
);
void
close
();
const
char
*
error_blurb
()
const
{
...
...
@@ -151,6 +164,11 @@ SFInputStream::close()
}
}
StdoutWavOutputStream
::~
StdoutWavOutputStream
()
{
close
();
}
static
void
header_append_str
(
vector
<
unsigned
char
>&
bytes
,
const
string
&
str
)
{
...
...
@@ -177,7 +195,9 @@ header_append_u16 (vector<unsigned char>& bytes, uint16_t u)
bool
StdoutWavOutputStream
::
open
(
int
n_channels
,
int
sample_rate
,
int
bit_depth
,
size_t
n_frames
)
{
if
(
bit_depth
!=
16
)
assert
(
m_state
==
State
::
NEW
);
if
(
bit_depth
!=
16
&&
bit_depth
!=
24
)
{
m_error_blurb
=
"StdoutWavOutputStream::open: unsupported bit depth"
;
return
false
;
...
...
@@ -186,8 +206,11 @@ StdoutWavOutputStream::open (int n_channels, int sample_rate, int bit_depth, siz
size_t
data_size
=
n_frames
*
n_channels
*
((
bit_depth
+
7
)
/
8
);
m_close_padding
=
data_size
&
1
;
// padding to ensure even data size
size_t
aligned_data_size
=
data_size
+
m_close_padding
;
header_append_str
(
header_bytes
,
"RIFF"
);
header_append_u32
(
header_bytes
,
36
+
data_size
);
header_append_u32
(
header_bytes
,
36
+
aligned_
data_size
);
header_append_str
(
header_bytes
,
"WAVE"
);
// subchunk 1
...
...
@@ -205,13 +228,17 @@ StdoutWavOutputStream::open (int n_channels, int sample_rate, int bit_depth, siz
header_append_u32
(
header_bytes
,
data_size
);
fwrite
(
&
header_bytes
[
0
],
1
,
header_bytes
.
size
(),
stdout
);
m_bit_depth
=
bit_depth
;
m_state
=
State
::
OPEN
;
return
true
;
}
bool
StdoutWavOutputStream
::
write_frames
(
const
vector
<
float
>&
samples
)
{
vector
<
unsigned
char
>
output_bytes
(
samples
.
size
()
*
sizeof
(
short
));
vector
<
unsigned
char
>
output_bytes
(
samples
.
size
()
*
(
m_bit_depth
/
8
));
for
(
size_t
i
=
0
;
i
<
samples
.
size
();
i
++
)
{
...
...
@@ -221,14 +248,36 @@ StdoutWavOutputStream::write_frames (const vector<float>& samples)
const
int
sample
=
lrint
(
bound
<
double
>
(
min_value
,
samples
[
i
]
*
norm
,
max_value
));
// write short little endian value
output_bytes
[
i
*
2
]
=
sample
>>
16
;
output_bytes
[
i
*
2
+
1
]
=
sample
>>
24
;
if
(
m_bit_depth
==
16
)
{
// write 16-bit little endian value
output_bytes
[
i
*
2
]
=
sample
>>
16
;
output_bytes
[
i
*
2
+
1
]
=
sample
>>
24
;
}
else
if
(
m_bit_depth
==
24
)
{
// write 24-bit little endian value
output_bytes
[
i
*
3
]
=
sample
>>
8
;
output_bytes
[
i
*
3
+
1
]
=
sample
>>
16
;
output_bytes
[
i
*
3
+
2
]
=
sample
>>
24
;
}
}
fwrite
(
&
output_bytes
[
0
],
1
,
output_bytes
.
size
(),
stdout
);
return
true
;
}
void
StdoutWavOutputStream
::
close
()
{
if
(
m_state
==
State
::
OPEN
)
{
for
(
size_t
i
=
0
;
i
<
m_close_padding
;
i
++
)
fputc
(
0
,
stdout
);
m_state
=
State
::
CLOSED
;
}
}
int
main
(
int
argc
,
char
**
argv
)
{
...
...
@@ -241,7 +290,7 @@ main (int argc, char **argv)
fprintf
(
stderr
,
"teststream: open input failed: %s
\n
"
,
in
.
error_blurb
());
return
1
;
}
if
(
!
out
.
open
(
in
.
n_channels
(),
in
.
sample_rate
(),
16
,
in
.
n_frames
()))
if
(
!
out
.
open
(
in
.
n_channels
(),
in
.
sample_rate
(),
24
,
in
.
n_frames
()))
{
fprintf
(
stderr
,
"teststream: open output failed: %s
\n
"
,
out
.
error_blurb
());
return
1
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment