Ardour
9.0-pre0-427-gd2a3450e2f
interleaver.h
Go to the documentation of this file.
1
#ifndef AUDIOGRAPHER_INTERLEAVER_H
2
#define AUDIOGRAPHER_INTERLEAVER_H
3
4
#include "
audiographer/visibility.h
"
5
#include "
audiographer/types.h
"
6
#include "
audiographer/sink.h
"
7
#include "
audiographer/exception.h
"
8
#include "
audiographer/throwing.h
"
9
#include "
audiographer/utils/listed_source.h
"
10
11
#include <vector>
12
#include <cmath>
13
14
namespace
AudioGrapher
15
{
16
18
template
<
typename
T = DefaultSampleType>
19
class
/*LIBAUDIOGRAPHER_API*/
Interleaver
20
:
public
ListedSource
<T>
21
,
public
Throwing
<>
22
{
23
public
:
24
26
Interleaver
()
27
:
channels
(0)
28
,
max_samples
(0)
29
,
buffer
(0)
30
{}
31
32
~Interleaver
() {
reset
(); }
33
35
void
init
(
unsigned
int
num_channels,
samplecnt_t
max_samples_per_channel)
36
{
37
reset
();
38
channels
= num_channels;
39
max_samples
= max_samples_per_channel;
40
41
buffer
=
new
T[
channels
*
max_samples
];
42
43
for
(
unsigned
int
i = 0; i <
channels
; ++i) {
44
inputs
.push_back (
InputPtr
(
new
Input
(*
this
, i)));
45
}
46
}
47
51
typename
Source<T>::SinkPtr
input
(
unsigned
int
channel)
52
{
53
if
(
throw_level
(
ThrowObject
) && channel >=
channels
) {
54
throw
Exception
(*
this
,
"Channel out of range"
);
55
}
56
57
return
std::static_pointer_cast<Sink<T> > (
inputs
[channel]);
58
}
59
60
private
:
61
62
class
Input
:
public
Sink
<T>
63
{
64
public
:
65
Input
(
Interleaver
&
parent
,
unsigned
int
channel
)
66
:
samples_written
(0),
parent
(
parent
),
channel
(
channel
) {}
67
68
void
process
(
ProcessContext<T>
const
& c)
69
{
70
if
(
parent
.
throw_level
(
ThrowProcess
) && c.
channels
() > 1) {
71
throw
Exception
(*
this
,
"Data input has more than on channel"
);
72
}
73
if
(
parent
.
throw_level
(
ThrowStrict
) &&
samples_written
) {
74
throw
Exception
(*
this
,
"Input channels out of sync"
);
75
}
76
samples_written
= c.
samples
();
77
parent
.
write_channel
(c,
channel
);
78
}
79
80
using
Sink<T>::process
;
81
82
samplecnt_t
samples
() {
return
samples_written
; }
83
void
reset
() {
samples_written
= 0; }
84
85
private
:
86
samplecnt_t
samples_written
;
87
Interleaver
&
parent
;
88
unsigned
int
channel
;
89
};
90
91
void
reset
()
92
{
93
inputs
.clear();
94
delete
[]
buffer
;
95
buffer
= 0;
96
channels
= 0;
97
max_samples
= 0;
98
}
99
100
void
reset_channels
()
101
{
102
for
(
unsigned
int
i = 0; i <
channels
; ++i) {
103
inputs
[i]->reset();
104
}
105
106
}
107
108
void
write_channel
(
ProcessContext<T>
const
& c,
unsigned
int
channel)
109
{
110
if
(
throw_level
(
ThrowProcess
) && c.
samples
() >
max_samples
) {
111
reset_channels
();
112
throw
Exception
(*
this
,
"Too many samples given to an input"
);
113
}
114
115
for
(
unsigned
int
i = 0; i < c.
samples
(); ++i) {
116
buffer
[channel + (
channels
* i)] = c.
data
()[i];
117
}
118
119
samplecnt_t
const
ready_samples =
ready_to_output
();
120
if
(ready_samples) {
121
ProcessContext<T>
c_out (c,
buffer
, ready_samples,
channels
);
122
ListedSource<T>::output
(c_out);
123
reset_channels
();
124
}
125
}
126
127
samplecnt_t
ready_to_output
()
128
{
129
samplecnt_t
ready_samples =
inputs
[0]->samples();
130
if
(!ready_samples) {
return
0; }
131
132
for
(
unsigned
int
i = 1; i <
channels
; ++i) {
133
samplecnt_t
const
samples =
inputs
[i]->samples();
134
if
(!samples) {
return
0; }
135
if
(
throw_level
(
ThrowProcess
) && samples != ready_samples) {
136
init
(
channels
,
max_samples
);
137
throw
Exception
(*
this
,
"Samples count out of sync"
);
138
}
139
}
140
return
ready_samples *
channels
;
141
}
142
143
typedef
std::shared_ptr<Input>
InputPtr
;
144
std::vector<InputPtr>
inputs
;
145
146
unsigned
int
channels
;
147
samplecnt_t
max_samples
;
148
T *
buffer
;
149
};
150
151
}
// namespace
152
153
#endif
// AUDIOGRAPHER_INTERLEAVER_H
types.h
visibility.h
AudioGrapher::Interleaver::Input
Definition:
interleaver.h:63
AudioGrapher::Interleaver::Input::samples_written
samplecnt_t samples_written
Definition:
interleaver.h:86
AudioGrapher::Interleaver::Input::channel
unsigned int channel
Definition:
interleaver.h:88
AudioGrapher::Interleaver::Input::Input
Input(Interleaver &parent, unsigned int channel)
Definition:
interleaver.h:65
AudioGrapher::Interleaver::Input::process
void process(ProcessContext< T > const &c)
Definition:
interleaver.h:68
AudioGrapher::Interleaver::Input::parent
Interleaver & parent
Definition:
interleaver.h:87
AudioGrapher::Interleaver::Input::reset
void reset()
Definition:
interleaver.h:83
AudioGrapher::Interleaver::Input::samples
samplecnt_t samples()
Definition:
interleaver.h:82
AudioGrapher::Interleaver
Interleaves many streams of non-interleaved data into one interleaved stream.
Definition:
interleaver.h:22
AudioGrapher::Interleaver::ready_to_output
samplecnt_t ready_to_output()
Definition:
interleaver.h:127
AudioGrapher::Interleaver::init
void init(unsigned int num_channels, samplecnt_t max_samples_per_channel)
Inits the interleaver. Must be called before using. Not RT safe.
Definition:
interleaver.h:35
AudioGrapher::Interleaver::input
Source< T >::SinkPtr input(unsigned int channel)
Definition:
interleaver.h:51
AudioGrapher::Interleaver::write_channel
void write_channel(ProcessContext< T > const &c, unsigned int channel)
Definition:
interleaver.h:108
AudioGrapher::Interleaver::buffer
T * buffer
Definition:
interleaver.h:148
AudioGrapher::Interleaver::reset_channels
void reset_channels()
Definition:
interleaver.h:100
AudioGrapher::Interleaver::Interleaver
Interleaver()
Constructs an interleaver RT safe.
Definition:
interleaver.h:26
AudioGrapher::Interleaver::~Interleaver
~Interleaver()
Definition:
interleaver.h:32
AudioGrapher::Interleaver::max_samples
samplecnt_t max_samples
Definition:
interleaver.h:147
AudioGrapher::Interleaver::inputs
std::vector< InputPtr > inputs
Definition:
interleaver.h:144
AudioGrapher::Interleaver::reset
void reset()
Definition:
interleaver.h:91
AudioGrapher::Interleaver::InputPtr
std::shared_ptr< Input > InputPtr
Definition:
interleaver.h:143
AudioGrapher::Interleaver::channels
unsigned int channels
Definition:
interleaver.h:146
AudioGrapher::ListedSource
An generic Source that uses a std::list for managing outputs.
Definition:
listed_source.h:17
AudioGrapher::ListedSource::output
void output(ProcessContext< T > const &c)
Helper for derived classes.
Definition:
listed_source.h:28
AudioGrapher::ProcessContext< T >
AudioGrapher::ProcessContext::channels
ChannelCount const & channels() const
Definition:
process_context.h:93
AudioGrapher::ProcessContext::data
T const * data() const
data points to the array of data to process
Definition:
process_context.h:84
AudioGrapher::ProcessContext::samples
samplecnt_t const & samples() const
samples tells how many samples the array pointed by data contains
Definition:
process_context.h:88
AudioGrapher::Sink
Definition:
sink.h:17
AudioGrapher::Source::SinkPtr
std::shared_ptr< Sink< T > > SinkPtr
Definition:
audiographer/audiographer/source.h:23
AudioGrapher::Throwing
Definition:
throwing.h:44
AudioGrapher::Throwing::throw_level
bool throw_level(ThrowLevel level)
Definition:
throwing.h:47
exception.h
listed_source.h
AudioGrapher
Definition:
analysis_graph.h:33
AudioGrapher::ThrowObject
@ ThrowObject
Object level stuff, ctors, initalizers etc.
Definition:
throwing.h:22
AudioGrapher::ThrowProcess
@ ThrowProcess
Process cycle level stuff.
Definition:
throwing.h:23
AudioGrapher::ThrowStrict
@ ThrowStrict
Stricter checks than ThrowProcess, less than ThrowSample.
Definition:
throwing.h:24
AudioGrapher::samplecnt_t
int64_t samplecnt_t
Definition:
audiographer/audiographer/types.h:11
Select::Exception
@ Exception
Definition:
libs/pbd/pbd/selectable.h:35
sink.h
throwing.h
libs
audiographer
audiographer
general
interleaver.h
Generated on Mon Nov 11 2024 08:49:01 for Ardour by
1.9.1