Skip to content

Commit 6eaf45f

Browse files
committed
make chanLen exported
1 parent 74293a7 commit 6eaf45f

File tree

13 files changed

+60
-49
lines changed

13 files changed

+60
-49
lines changed

float32.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

float64.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

int16.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

int32.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

int64.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

int8.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

signal.go

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,14 @@ func FloatingAsFloating(src Floating, dst Floating) int {
175175
mustSameChannels(src.Channels(), dst.Channels())
176176
// cap length to destination capacity.
177177
length := min(src.Len(), dst.Len())
178+
if length == 0 {
179+
return 0
180+
}
178181
// determine the multiplier for bit depth conversion
179182
for pos := 0; pos < length; pos++ {
180183
dst.SetSample(pos, src.Sample(pos))
181184
}
182-
return chanLen(length, dst.Channels())
185+
return min(src.Length(), dst.Length())
183186
}
184187

185188
// FloatingAsSigned converts floating-point samples into signed fixed-point
@@ -204,7 +207,7 @@ func FloatingAsSigned(src Floating, dst Signed) int {
204207
dst.SetSample(pos, int64(sample)*(msv+1))
205208
}
206209
}
207-
return chanLen(length, dst.Channels())
210+
return min(src.Length(), dst.Length())
208211
}
209212

210213
// FloatingAsUnsigned converts floating-point samples into unsigned
@@ -228,7 +231,7 @@ func FloatingAsUnsigned(src Floating, dst Unsigned) int {
228231
dst.SetSample(pos, uint64(sample)*(msv+1)+(msv+1))
229232
}
230233
}
231-
return chanLen(length, dst.Channels())
234+
return min(src.Length(), dst.Length())
232235
}
233236

234237
// SignedAsFloating converts signed fixed-point samples into floating-point
@@ -252,7 +255,7 @@ func SignedAsFloating(src Signed, dst Floating) int {
252255
dst.SetSample(pos, float64(sample)/(msv+1))
253256
}
254257
}
255-
return chanLen(length, dst.Channels())
258+
return min(src.Length(), dst.Length())
256259
}
257260

258261
// SignedAsSigned appends signed fixed-point samples to the signed
@@ -273,7 +276,7 @@ func SignedAsSigned(src Signed, dst Signed) int {
273276
for pos := 0; pos < length; pos++ {
274277
dst.SetSample(pos, src.Sample(pos)/scale)
275278
}
276-
return chanLen(length, dst.Channels())
279+
return min(src.Length(), dst.Length())
277280
}
278281

279282
// upscale
@@ -285,7 +288,7 @@ func SignedAsSigned(src Signed, dst Signed) int {
285288
dst.SetSample(pos, src.Sample(pos)*scale)
286289
}
287290
}
288-
return chanLen(length, dst.Channels())
291+
return min(src.Length(), dst.Length())
289292
}
290293

291294
// SignedAsUnsigned converts signed fixed-point samples into unsigned
@@ -309,7 +312,7 @@ func SignedAsUnsigned(src Signed, dst Unsigned) int {
309312
for pos := 0; pos < length; pos++ {
310313
dst.SetSample(pos, uint64(src.Sample(pos)/scale)+msv+1)
311314
}
312-
return chanLen(length, dst.Channels())
315+
return min(src.Length(), dst.Length())
313316
}
314317

315318
// upscale
@@ -321,7 +324,7 @@ func SignedAsUnsigned(src Signed, dst Unsigned) int {
321324
dst.SetSample(pos, uint64(src.Sample(pos)*scale)+msv+1)
322325
}
323326
}
324-
return chanLen(length, dst.Channels())
327+
return min(src.Length(), dst.Length())
325328
}
326329

327330
// UnsignedAsFloating converts unsigned fixed-point samples into
@@ -344,7 +347,7 @@ func UnsignedAsFloating(src Unsigned, dst Floating) int {
344347
dst.SetSample(pos, (float64(sample)-(msv+1))/(msv+1))
345348
}
346349
}
347-
return chanLen(length, dst.Channels())
350+
return min(src.Length(), dst.Length())
348351
}
349352

350353
// UnsignedAsSigned converts unsigned fixed-point samples into signed
@@ -367,7 +370,7 @@ func UnsignedAsSigned(src Unsigned, dst Signed) int {
367370
for pos := 0; pos < length; pos++ {
368371
dst.SetSample(pos, int64(src.Sample(pos)-(msv+1))/scale)
369372
}
370-
return chanLen(length, dst.Channels())
373+
return min(src.Length(), dst.Length())
371374
}
372375

373376
// upscale
@@ -379,7 +382,7 @@ func UnsignedAsSigned(src Unsigned, dst Signed) int {
379382
dst.SetSample(pos, sample*scale)
380383
}
381384
}
382-
return chanLen(length, dst.Channels())
385+
return min(src.Length(), dst.Length())
383386
}
384387

385388
// UnsignedAsUnsigned appends unsigned fixed-point samples to the unsigned
@@ -400,7 +403,7 @@ func UnsignedAsUnsigned(src, dst Unsigned) int {
400403
for pos := 0; pos < length; pos++ {
401404
dst.SetSample(pos, src.Sample(pos)/scale)
402405
}
403-
return chanLen(length, dst.Channels())
406+
return min(src.Length(), dst.Length())
404407
}
405408

406409
// upscale
@@ -414,7 +417,7 @@ func UnsignedAsUnsigned(src, dst Unsigned) int {
414417
dst.SetSample(pos, sample*scale)
415418
}
416419
}
417-
return chanLen(length, dst.Channels())
420+
return min(src.Length(), dst.Length())
418421
}
419422

420423
// BitDepth returns bit depth of the buffer.
@@ -427,12 +430,6 @@ func (c channels) Channels() int {
427430
return int(c)
428431
}
429432

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-
436433
func capFloat(v float64) float64 {
437434
if v > 1 {
438435
return 1
@@ -468,18 +465,26 @@ func mustSameCapacity(c1, c2 int) {
468465
}
469466
}
470467

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 {
472471
return int(math.Ceil(float64(sliceLen) / float64(channels)))
473472
}
474473

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+
475480
// WriteInt writes values from provided slice into the buffer.
476481
// Returns a number of samples written per channel.
477482
func WriteInt(src []int, dst Signed) int {
478483
length := min(dst.Len(), len(src))
479484
for pos := 0; pos < length; pos++ {
480485
dst.SetSample(pos, int64(src[pos]))
481486
}
482-
return chanLen(length, dst.Channels())
487+
return ChannelLength(length, dst.Channels())
483488
}
484489

485490
// WriteStripedInt writes values from provided slice into the buffer.
@@ -516,7 +521,7 @@ func WriteUint(src []uint, dst Unsigned) int {
516521
for pos := 0; pos < length; pos++ {
517522
dst.SetSample(pos, uint64(src[pos]))
518523
}
519-
return chanLen(length, dst.Channels())
524+
return ChannelLength(length, dst.Channels())
520525
}
521526

522527
// WriteStripedUint writes values from provided slice into the buffer.
@@ -553,7 +558,7 @@ func ReadInt(src Signed, dst []int) int {
553558
for pos := 0; pos < length; pos++ {
554559
dst[pos] = int(src.Sample(pos))
555560
}
556-
return chanLen(length, src.Channels())
561+
return ChannelLength(length, src.Channels())
557562
}
558563

559564
// ReadStripedInt reads values from the buffer into provided slice. The
@@ -581,7 +586,7 @@ func ReadUint(src Unsigned, dst []uint) int {
581586
for pos := 0; pos < length; pos++ {
582587
dst[pos] = uint(src.Sample(pos))
583588
}
584-
return chanLen(length, src.Channels())
589+
return ChannelLength(length, src.Channels())
585590
}
586591

587592
// ReadStripedUint reads values from the buffer into provided slice. The

signal_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,15 @@ func TestWrite(t *testing.T) {
343343
allocator := signal.Allocator{
344344
Capacity: 1,
345345
Length: 1,
346-
Channels: 2,
346+
Channels: 3,
347347
}
348348
t.Run("striped int 8-bits overflow", func() func(t *testing.T) {
349349
buf := allocator.Int64(signal.BitDepth8)
350350
length := signal.WriteStripedInt(
351351
[][]int{
352352
{math.MaxInt32},
353353
{math.MinInt32},
354+
{},
354355
},
355356
buf)
356357
return testOk(
@@ -361,6 +362,7 @@ func TestWrite(t *testing.T) {
361362
data: [][]int64{
362363
{math.MaxInt8},
363364
{math.MinInt8},
365+
{0},
364366
},
365367
},
366368
)
@@ -382,6 +384,7 @@ func TestWrite(t *testing.T) {
382384
data: [][]int64{
383385
{math.MaxInt8},
384386
{math.MinInt8},
387+
{0},
385388
},
386389
},
387390
)
@@ -392,6 +395,7 @@ func TestWrite(t *testing.T) {
392395
[][]uint{
393396
{math.MaxUint32},
394397
{0},
398+
{},
395399
},
396400
buf,
397401
)
@@ -403,6 +407,7 @@ func TestWrite(t *testing.T) {
403407
data: [][]uint64{
404408
{math.MaxUint8},
405409
{0},
410+
{0},
406411
},
407412
},
408413
)
@@ -424,6 +429,7 @@ func TestWrite(t *testing.T) {
424429
data: [][]uint64{
425430
{math.MaxUint8},
426431
{0},
432+
{0},
427433
},
428434
},
429435
)

templates/signal.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func Read{{ .Name }}(src {{ .Interface }}, dst []{{ .Builtin }}) int {
8585
for pos := 0; pos < length; pos++ {
8686
dst[pos] = {{if eq .Interface "Floating"}}{{ .Builtin }}(src.Sample(pos)){{else}}{{ .Builtin }}({{ .MaxBitDepth }}.{{ .Interface }}Value(src.Sample(pos))){{end}}
8787
}
88-
return chanLen(length, src.Channels())
88+
return ChannelLength(length, src.Channels())
8989
}
9090

9191
// ReadStriped{{ .Name }} reads values from the buffer into provided slice. The
@@ -114,7 +114,7 @@ func Write{{ .Name }}(src []{{ .Builtin }}, dst {{ .Interface }}) int {
114114
for pos := 0; pos < length; pos++ {
115115
dst.SetSample(pos, {{ .SampleType }}(src[pos]))
116116
}
117-
return chanLen(length, dst.Channels())
117+
return ChannelLength(length, dst.Channels())
118118
}
119119

120120
// WriteStriped{{ .Name }} writes values from provided slice into the buffer.

0 commit comments

Comments
 (0)