-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconstant.go
More file actions
310 lines (256 loc) · 7.62 KB
/
constant.go
File metadata and controls
310 lines (256 loc) · 7.62 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package resampler
import (
"fmt"
"sync"
)
// constantRateResampler implements fixed-ratio resampling.
// It uses a multi-stage pipeline approach similar to libsoxr,
// combining different algorithms for optimal performance.
type constantRateResampler struct {
config Config
ratio float64
pipeline *Pipeline
// Per-channel state
channels []*channelResampler
// Shared resources
mu sync.RWMutex
}
// channelResampler holds per-channel state.
type channelResampler struct {
stages []Stage
buffers []*RingBuffer
}
// newConstantRateResampler creates a new constant-rate resampler.
func newConstantRateResampler(config *Config, ratio float64) (*constantRateResampler, error) {
r := &constantRateResampler{
config: *config,
ratio: ratio,
channels: make([]*channelResampler, config.Channels),
}
// Build the processing pipeline based on quality and ratio
pipeline, err := buildPipeline(config, ratio)
if err != nil {
return nil, fmt.Errorf("failed to build pipeline: %w", err)
}
r.pipeline = pipeline
// Initialize per-channel state
for i := range config.Channels {
ch := &channelResampler{
stages: make([]Stage, len(pipeline.stages)),
buffers: make([]*RingBuffer, len(pipeline.stages)+1),
}
// Create stage instances for this channel
for j, stageSpec := range pipeline.stages {
stage, err := createStage(stageSpec, config)
if err != nil {
return nil, fmt.Errorf("failed to create stage %d: %w", j, err)
}
ch.stages[j] = stage
}
// Create buffers between stages
for j := 0; j <= len(pipeline.stages); j++ {
bufferSize := defaultBufferSize
if config.MaxInputSize > 0 && j == 0 {
bufferSize = config.MaxInputSize * bufferSizeMultiplier
}
ch.buffers[j] = NewRingBuffer(bufferSize)
}
r.channels[i] = ch
}
return r, nil
}
// Process resamples a mono audio channel.
func (r *constantRateResampler) Process(input []float64) ([]float64, error) {
if len(r.channels) == 0 {
return nil, fmt.Errorf("no channels initialized")
}
// Use first channel for mono processing
return r.processChannel(0, input)
}
// ProcessFloat32 resamples float32 audio data.
// Internally converts to float64 for processing, then converts back.
// This approach maintains numerical precision during resampling while
// supporting float32 I/O. The conversion overhead is minimal compared
// to the filter computation (~5% of total time for typical buffers).
// Native float32 processing would require duplicating the engine with
// generic types, which is a trade-off between code complexity and performance.
func (r *constantRateResampler) ProcessFloat32(input []float32) ([]float32, error) {
// Convert to float64 for high-precision internal processing
input64 := make([]float64, len(input))
for i, v := range input {
input64[i] = float64(v)
}
output64, err := r.Process(input64)
if err != nil {
return nil, err
}
output32 := make([]float32, len(output64))
for i, v := range output64 {
output32[i] = float32(v)
}
return output32, nil
}
// ProcessMulti processes multiple audio channels.
// When EnableParallel is true in config, channels are processed concurrently.
// Otherwise, channels are processed sequentially.
func (r *constantRateResampler) ProcessMulti(input [][]float64) ([][]float64, error) {
if len(input) != r.config.Channels {
return nil, fmt.Errorf("expected %d channels, got %d", r.config.Channels, len(input))
}
output := make([][]float64, len(input))
// Sequential processing (default or when parallel disabled)
if !r.config.EnableParallel || len(input) <= 1 {
for ch := range input {
result, err := r.processChannel(ch, input[ch])
if err != nil {
return nil, fmt.Errorf("channel %d: %w", ch, err)
}
output[ch] = result
}
return output, nil
}
// Parallel processing: process channels concurrently
var wg sync.WaitGroup
errChan := make(chan error, len(input))
for ch := range input {
wg.Add(1)
go func(channel int) {
defer wg.Done()
result, err := r.processChannel(channel, input[channel])
if err != nil {
errChan <- fmt.Errorf("channel %d: %w", channel, err)
return
}
output[channel] = result
}(ch)
}
wg.Wait()
close(errChan)
// Check for errors
for err := range errChan {
if err != nil {
return nil, err
}
}
return output, nil
}
// processChannel processes a single channel through the pipeline.
func (r *constantRateResampler) processChannel(channel int, input []float64) ([]float64, error) {
if channel >= len(r.channels) {
return nil, fmt.Errorf("channel %d out of range", channel)
}
ch := r.channels[channel]
// Add input to first buffer
ch.buffers[0].Write(input)
// Process through each stage
for i, stage := range ch.stages {
inputBuffer := ch.buffers[i]
outputBuffer := ch.buffers[i+1]
// Process available input
for inputBuffer.Available() >= stage.GetMinInput() {
// Get input chunk
chunk := inputBuffer.Read(stage.GetMinInput())
// Process through stage
output, err := stage.Process(chunk)
if err != nil {
return nil, fmt.Errorf("stage %d processing error: %w", i, err)
}
// Write to next buffer
outputBuffer.Write(output)
}
}
// Read final output
finalBuffer := ch.buffers[len(ch.buffers)-1]
return finalBuffer.ReadAll(), nil
}
// Flush returns any remaining samples.
func (r *constantRateResampler) Flush() ([]float64, error) {
if len(r.channels) == 0 {
return []float64{}, nil
}
// Flush first channel for mono
ch := r.channels[0]
// Flush each stage
for i, stage := range ch.stages {
outputBuffer := ch.buffers[i+1]
// Flush the stage
output, err := stage.Flush()
if err != nil {
return nil, fmt.Errorf("stage %d flush error: %w", i, err)
}
if len(output) > 0 {
outputBuffer.Write(output)
}
}
// Return all remaining output
finalBuffer := ch.buffers[len(ch.buffers)-1]
return finalBuffer.ReadAll(), nil
}
// GetLatency returns the total pipeline latency in samples.
func (r *constantRateResampler) GetLatency() int {
if r.pipeline == nil || len(r.channels) == 0 {
return 0
}
// Use the first channel's stages to calculate latency
ch := r.channels[0]
if ch == nil || len(ch.stages) == 0 {
return 0
}
totalLatency := 0
for _, stage := range ch.stages {
// Account for stage processing latency and ratio change
stageLatency := int(float64(stage.GetLatency()) * stage.GetRatio())
totalLatency += stageLatency
}
return totalLatency
}
// Reset clears all internal state.
func (r *constantRateResampler) Reset() {
r.mu.Lock()
defer r.mu.Unlock()
for _, ch := range r.channels {
// Reset all stages
for _, stage := range ch.stages {
stage.Reset()
}
// Clear all buffers
for _, buffer := range ch.buffers {
buffer.Clear()
}
}
}
// GetRatio returns the resampling ratio.
func (r *constantRateResampler) GetRatio() float64 {
return r.ratio
}
// GetInfo returns information about the resampler.
func (r *constantRateResampler) GetInfo() Info {
info := Info{
Algorithm: "multi-stage",
Latency: r.GetLatency(),
}
// Calculate total memory usage
var memUsage int64
for _, ch := range r.channels {
for _, buffer := range ch.buffers {
memUsage += int64(buffer.Capacity() * bytesPerFloat64)
}
for _, stage := range ch.stages {
memUsage += stage.GetMemoryUsage()
}
}
info.MemoryUsage = memUsage
// Get stage information from first channel
if len(r.channels) > 0 && len(r.channels[0].stages) > 0 {
// Report info from primary stage
primaryStage := r.channels[0].stages[0]
info.FilterLength = primaryStage.GetFilterLength()
info.Phases = primaryStage.GetPhases()
// Check for SIMD
if simd := primaryStage.GetSIMDInfo(); simd != "" {
info.SIMDEnabled = true
info.SIMDType = simd
}
}
return info
}