-
Notifications
You must be signed in to change notification settings - Fork 1
/
add.go
187 lines (162 loc) · 5.04 KB
/
add.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
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
// {{{ Copyright (c) Paul R. Tagliamonte <paul@k3xec.com>, 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 stream
import (
"fmt"
"hz.tools/sdr"
"hz.tools/sdr/internal/simd"
)
// Add will take any number of Readers, and mix all those Readers into a single
// Reader. This is likely not generally useful for generating data to transmit,
// but could be very useful for testing.
//
// Each of the readers must be the same SampleFormat and SampleRate or
// an error will be returned.
//
// The samples, when added must not exceed +1 or go below -1. This causes
// clipping and clipping is bad. If you're adding two waveforms, be absolutely
// sure you've adjusted the gain correctly on the readers with the stream.Gain
// reader.
func Add(readers ...sdr.Reader) (sdr.Reader, error) {
switch len(readers) {
case 0:
return nil, fmt.Errorf("stream.Add: No readers passed")
case 1:
return readers[0], nil
}
var (
sampleFormat = readers[0].SampleFormat()
sampleRate = readers[0].SampleRate()
)
switch sampleFormat {
case sdr.SampleFormatC64:
case sdr.SampleFormatI16:
case sdr.SampleFormatI8:
default:
return nil, sdr.ErrSampleFormatUnknown
}
for _, reader := range readers {
if reader.SampleFormat() != sampleFormat {
return nil, fmt.Errorf("stream.Add: Readers are not all the same format")
}
if reader.SampleRate() != sampleRate {
return nil, fmt.Errorf("stream.Add: Readers are not all the same rate")
}
}
return &addReader{
sampleFormat: sampleFormat,
sampleRate: sampleRate,
readers: readers,
}, nil
}
type addReader struct {
sampleFormat sdr.SampleFormat
sampleRate uint
readers []sdr.Reader
err error
}
func (ar *addReader) SampleFormat() sdr.SampleFormat {
return ar.sampleFormat
}
func (ar *addReader) SampleRate() uint {
return ar.sampleRate
}
func (ar *addReader) AddI8(out sdr.SamplesI8, buffers ...sdr.Samples) {
for _, bufS := range buffers {
buf := bufS.(sdr.SamplesI8)
for idx, sample := range buf {
out[idx][0] += sample[0]
out[idx][1] += sample[1]
}
}
}
func (ar *addReader) AddI16(out sdr.SamplesI16, buffers ...sdr.Samples) {
for _, bufS := range buffers {
buf := bufS.(sdr.SamplesI16)
for idx, sample := range buf {
out[idx][0] += sample[0]
out[idx][1] += sample[1]
}
}
}
func (ar *addReader) AddC64(out sdr.SamplesC64, buffers ...sdr.Samples) {
for _, buf := range buffers {
simd.AddComplex(out, buf.(sdr.SamplesC64), out)
}
}
func (ar *addReader) Read(s sdr.Samples) (int, error) {
// TODO(paultag): SIMD moderate hanging fruit
// TODO(paultag): SampleFormatU8 support here
if ar.err != nil {
return 0, ar.err
}
switch s.Format() {
case sdr.SampleFormatC64:
case sdr.SampleFormatI16:
case sdr.SampleFormatI8:
default:
return 0, sdr.ErrSampleFormatUnknown
}
buffers := make([]sdr.Samples, len(ar.readers))
for i, reader := range ar.readers {
var err error
buffers[i], err = sdr.MakeSamples(s.Format(), s.Length())
// See note below on error handling.
if err != nil {
ar.err = err
return 0, err
}
_, err = sdr.ReadFull(reader, buffers[i])
// here we know the number of phasors read must match the buffer
// length, or we get an error. as a result, we can avoid storing
// the number of samples read.
if err != nil {
ar.err = err
// TODO(paultag): This should likely not be 0, perhaps we need
// to take the number read from any buffer? Max read from any?
// Least read from any? This is going to cause alignment
// headaches regardless.
return 0, err
}
}
switch samples := s.(type) {
case sdr.SamplesC64:
for si := range samples {
samples[si] = complex(0, 0)
}
ar.AddC64(samples, buffers...)
return len(samples), nil
case sdr.SamplesI16:
for si := range samples {
samples[si] = [2]int16{0, 0}
}
ar.AddI16(samples, buffers...)
return len(samples), nil
case sdr.SamplesI8:
for si := range samples {
samples[si] = [2]int8{0, 0}
}
ar.AddI8(samples, buffers...)
return len(samples), nil
default:
return 0, sdr.ErrSampleFormatUnknown
}
}
// vim: foldmethod=marker