@@ -175,11 +175,14 @@ func FloatingAsFloating(src Floating, dst Floating) int {
175
175
mustSameChannels (src .Channels (), dst .Channels ())
176
176
// cap length to destination capacity.
177
177
length := min (src .Len (), dst .Len ())
178
+ if length == 0 {
179
+ return 0
180
+ }
178
181
// determine the multiplier for bit depth conversion
179
182
for pos := 0 ; pos < length ; pos ++ {
180
183
dst .SetSample (pos , src .Sample (pos ))
181
184
}
182
- return chanLen ( length , dst .Channels ())
185
+ return min ( src . Length () , dst .Length ())
183
186
}
184
187
185
188
// FloatingAsSigned converts floating-point samples into signed fixed-point
@@ -204,7 +207,7 @@ func FloatingAsSigned(src Floating, dst Signed) int {
204
207
dst .SetSample (pos , int64 (sample )* (msv + 1 ))
205
208
}
206
209
}
207
- return chanLen ( length , dst .Channels ())
210
+ return min ( src . Length () , dst .Length ())
208
211
}
209
212
210
213
// FloatingAsUnsigned converts floating-point samples into unsigned
@@ -228,7 +231,7 @@ func FloatingAsUnsigned(src Floating, dst Unsigned) int {
228
231
dst .SetSample (pos , uint64 (sample )* (msv + 1 )+ (msv + 1 ))
229
232
}
230
233
}
231
- return chanLen ( length , dst .Channels ())
234
+ return min ( src . Length () , dst .Length ())
232
235
}
233
236
234
237
// SignedAsFloating converts signed fixed-point samples into floating-point
@@ -252,7 +255,7 @@ func SignedAsFloating(src Signed, dst Floating) int {
252
255
dst .SetSample (pos , float64 (sample )/ (msv + 1 ))
253
256
}
254
257
}
255
- return chanLen ( length , dst .Channels ())
258
+ return min ( src . Length () , dst .Length ())
256
259
}
257
260
258
261
// SignedAsSigned appends signed fixed-point samples to the signed
@@ -273,7 +276,7 @@ func SignedAsSigned(src Signed, dst Signed) int {
273
276
for pos := 0 ; pos < length ; pos ++ {
274
277
dst .SetSample (pos , src .Sample (pos )/ scale )
275
278
}
276
- return chanLen ( length , dst .Channels ())
279
+ return min ( src . Length () , dst .Length ())
277
280
}
278
281
279
282
// upscale
@@ -285,7 +288,7 @@ func SignedAsSigned(src Signed, dst Signed) int {
285
288
dst .SetSample (pos , src .Sample (pos )* scale )
286
289
}
287
290
}
288
- return chanLen ( length , dst .Channels ())
291
+ return min ( src . Length () , dst .Length ())
289
292
}
290
293
291
294
// SignedAsUnsigned converts signed fixed-point samples into unsigned
@@ -309,7 +312,7 @@ func SignedAsUnsigned(src Signed, dst Unsigned) int {
309
312
for pos := 0 ; pos < length ; pos ++ {
310
313
dst .SetSample (pos , uint64 (src .Sample (pos )/ scale )+ msv + 1 )
311
314
}
312
- return chanLen ( length , dst .Channels ())
315
+ return min ( src . Length () , dst .Length ())
313
316
}
314
317
315
318
// upscale
@@ -321,7 +324,7 @@ func SignedAsUnsigned(src Signed, dst Unsigned) int {
321
324
dst .SetSample (pos , uint64 (src .Sample (pos )* scale )+ msv + 1 )
322
325
}
323
326
}
324
- return chanLen ( length , dst .Channels ())
327
+ return min ( src . Length () , dst .Length ())
325
328
}
326
329
327
330
// UnsignedAsFloating converts unsigned fixed-point samples into
@@ -344,7 +347,7 @@ func UnsignedAsFloating(src Unsigned, dst Floating) int {
344
347
dst .SetSample (pos , (float64 (sample )- (msv + 1 ))/ (msv + 1 ))
345
348
}
346
349
}
347
- return chanLen ( length , dst .Channels ())
350
+ return min ( src . Length () , dst .Length ())
348
351
}
349
352
350
353
// UnsignedAsSigned converts unsigned fixed-point samples into signed
@@ -367,7 +370,7 @@ func UnsignedAsSigned(src Unsigned, dst Signed) int {
367
370
for pos := 0 ; pos < length ; pos ++ {
368
371
dst .SetSample (pos , int64 (src .Sample (pos )- (msv + 1 ))/ scale )
369
372
}
370
- return chanLen ( length , dst .Channels ())
373
+ return min ( src . Length () , dst .Length ())
371
374
}
372
375
373
376
// upscale
@@ -379,7 +382,7 @@ func UnsignedAsSigned(src Unsigned, dst Signed) int {
379
382
dst .SetSample (pos , sample * scale )
380
383
}
381
384
}
382
- return chanLen ( length , dst .Channels ())
385
+ return min ( src . Length () , dst .Length ())
383
386
}
384
387
385
388
// UnsignedAsUnsigned appends unsigned fixed-point samples to the unsigned
@@ -400,7 +403,7 @@ func UnsignedAsUnsigned(src, dst Unsigned) int {
400
403
for pos := 0 ; pos < length ; pos ++ {
401
404
dst .SetSample (pos , src .Sample (pos )/ scale )
402
405
}
403
- return chanLen ( length , dst .Channels ())
406
+ return min ( src . Length () , dst .Length ())
404
407
}
405
408
406
409
// upscale
@@ -414,7 +417,7 @@ func UnsignedAsUnsigned(src, dst Unsigned) int {
414
417
dst .SetSample (pos , sample * scale )
415
418
}
416
419
}
417
- return chanLen ( length , dst .Channels ())
420
+ return min ( src . Length () , dst .Length ())
418
421
}
419
422
420
423
// BitDepth returns bit depth of the buffer.
@@ -427,12 +430,6 @@ func (c channels) Channels() int {
427
430
return int (c )
428
431
}
429
432
430
- // ChannelPos calculates sample position in the buffer based on channel and
431
- // postition in the channel.
432
- func (c channels ) ChannelPos (channel , pos int ) int {
433
- return int (c )* pos + channel
434
- }
435
-
436
433
func capFloat (v float64 ) float64 {
437
434
if v > 1 {
438
435
return 1
@@ -468,18 +465,26 @@ func mustSameCapacity(c1, c2 int) {
468
465
}
469
466
}
470
467
471
- func chanLen (sliceLen , channels int ) int {
468
+ // ChannelLength calculates a channel length for provided buffer length and
469
+ // number of channels.
470
+ func ChannelLength (sliceLen , channels int ) int {
472
471
return int (math .Ceil (float64 (sliceLen ) / float64 (channels )))
473
472
}
474
473
474
+ // ChannelPos calculates sample position in the buffer based on channel and
475
+ // postition in the channel.
476
+ func (c channels ) ChannelPos (channel , pos int ) int {
477
+ return int (c )* pos + channel
478
+ }
479
+
475
480
// WriteInt writes values from provided slice into the buffer.
476
481
// Returns a number of samples written per channel.
477
482
func WriteInt (src []int , dst Signed ) int {
478
483
length := min (dst .Len (), len (src ))
479
484
for pos := 0 ; pos < length ; pos ++ {
480
485
dst .SetSample (pos , int64 (src [pos ]))
481
486
}
482
- return chanLen (length , dst .Channels ())
487
+ return ChannelLength (length , dst .Channels ())
483
488
}
484
489
485
490
// WriteStripedInt writes values from provided slice into the buffer.
@@ -516,7 +521,7 @@ func WriteUint(src []uint, dst Unsigned) int {
516
521
for pos := 0 ; pos < length ; pos ++ {
517
522
dst .SetSample (pos , uint64 (src [pos ]))
518
523
}
519
- return chanLen (length , dst .Channels ())
524
+ return ChannelLength (length , dst .Channels ())
520
525
}
521
526
522
527
// WriteStripedUint writes values from provided slice into the buffer.
@@ -553,7 +558,7 @@ func ReadInt(src Signed, dst []int) int {
553
558
for pos := 0 ; pos < length ; pos ++ {
554
559
dst [pos ] = int (src .Sample (pos ))
555
560
}
556
- return chanLen (length , src .Channels ())
561
+ return ChannelLength (length , src .Channels ())
557
562
}
558
563
559
564
// ReadStripedInt reads values from the buffer into provided slice. The
@@ -581,7 +586,7 @@ func ReadUint(src Unsigned, dst []uint) int {
581
586
for pos := 0 ; pos < length ; pos ++ {
582
587
dst [pos ] = uint (src .Sample (pos ))
583
588
}
584
- return chanLen (length , src .Channels ())
589
+ return ChannelLength (length , src .Channels ())
585
590
}
586
591
587
592
// ReadStripedUint reads values from the buffer into provided slice. The
0 commit comments