-
Notifications
You must be signed in to change notification settings - Fork 2
/
readers.go
113 lines (101 loc) · 3.16 KB
/
readers.go
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
// {{{ Copyright (c) Paul R. Tagliamonte <[email protected]>, 2020
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. }}}
package sdr
// Readers represents a collection of Readers.
type Readers []Reader
// SampleRate returns the number of samples per second in the stream.
func (rs Readers) SampleRate() uint {
if len(rs) == 0 {
return 0
}
ret := rs[0].SampleRate()
for _, r := range rs {
if r.SampleRate() != ret {
return 0
}
}
return ret
}
// SampleFormat returns the IQ Format of the Readers.
func (rs Readers) SampleFormat() SampleFormat {
if len(rs) == 0 {
return SampleFormat(0)
}
ret := rs[0].SampleFormat()
for _, r := range rs {
if r.SampleFormat() != ret {
return SampleFormat(0)
}
}
return ret
}
// WrapErr will apply the function 'fn' to each Reader in this collection,
// and return a new slice of those new Reader objects. If any error is
// encountered, that error is returned.
func (rs Readers) WrapErr(fn func(Reader) (Reader, error)) (Readers, error) {
var (
err error
ret = make(Readers, len(rs))
)
for i := range rs {
ret[i], err = fn(rs[i])
if err != nil {
return nil, err
}
}
return ret, nil
}
// Wrap will apply the function 'fn' to each Reader in this collection,
// and return a new slice of those new Reader objects.
func (rs Readers) Wrap(fn func(Reader) Reader) Readers {
ret := make(Readers, len(rs))
for i := range rs {
ret[i] = fn(rs[i])
}
return ret
}
// ReadClosers is a collection of ReadCloser objects.
type ReadClosers []ReadCloser
// SampleRate returns the number of IQ samples per second.
func (rcs ReadClosers) SampleRate() uint {
return rcs.Readers().SampleRate()
}
// SampleFormat returns the IQ format of the Readers.
func (rcs ReadClosers) SampleFormat() SampleFormat {
return rcs.Readers().SampleFormat()
}
// Readers will return the ReadClosers as a Reader slice.
func (rcs ReadClosers) Readers() Readers {
ret := make(Readers, len(rcs))
for i := range rcs {
ret[i] = rcs[i]
}
return ret
}
// Close will close all the ReadClosers.
func (rcs ReadClosers) Close() error {
for _, rc := range rcs {
if err := rc.Close(); err != nil {
return err
}
}
return nil
}
// vim: foldmethod=marker