2017-02-22 21:10:57 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms and conditions defined in
|
|
|
|
* file 'LICENSE.md', which is part of this source code package.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package sampling
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// ResampleBytes resamples the raw data which is in 8-bit (byte) format as a different
|
2017-02-22 21:10:57 +00:00
|
|
|
// bit count per sample, up to 32 bits (uint32).
|
|
|
|
func ResampleBytes(data []byte, bitsPerSample int) []uint32 {
|
2018-12-09 19:28:50 +02:00
|
|
|
var samples []uint32
|
2017-02-22 21:10:57 +00:00
|
|
|
|
|
|
|
bitsLeftPerSample := bitsPerSample
|
|
|
|
var sample uint32
|
|
|
|
var remainder byte
|
|
|
|
remainderBits := 0
|
|
|
|
|
|
|
|
index := 0
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i < len(data) {
|
|
|
|
// Start with the remainder.
|
|
|
|
if remainderBits > 0 {
|
|
|
|
take := remainderBits
|
|
|
|
if bitsLeftPerSample < take {
|
|
|
|
take = bitsLeftPerSample
|
|
|
|
}
|
|
|
|
|
|
|
|
sample = (sample << uint(take)) | uint32(remainder>>uint(8-take))
|
|
|
|
remainderBits -= take
|
|
|
|
if remainderBits > 0 {
|
|
|
|
remainder = remainder << uint(take)
|
|
|
|
} else {
|
|
|
|
remainder = 0
|
|
|
|
}
|
|
|
|
bitsLeftPerSample -= take
|
2019-07-14 23:18:40 +02:00
|
|
|
|
2017-02-22 21:10:57 +00:00
|
|
|
if bitsLeftPerSample == 0 {
|
|
|
|
//samples[index] = sample
|
|
|
|
samples = append(samples, sample)
|
|
|
|
bitsLeftPerSample = bitsPerSample
|
|
|
|
sample = 0
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Take next byte
|
|
|
|
b := data[i]
|
|
|
|
i++
|
|
|
|
|
|
|
|
// 8 bits.
|
|
|
|
take := 8
|
|
|
|
if bitsLeftPerSample < take {
|
|
|
|
take = bitsLeftPerSample
|
|
|
|
}
|
|
|
|
remainderBits = 8 - take
|
|
|
|
sample = (sample << uint(take)) | uint32(b>>uint(remainderBits))
|
|
|
|
|
|
|
|
if take < 8 {
|
|
|
|
remainder = b << uint(take)
|
|
|
|
}
|
|
|
|
|
|
|
|
bitsLeftPerSample -= take
|
|
|
|
if bitsLeftPerSample == 0 {
|
|
|
|
//samples[index] = sample
|
|
|
|
samples = append(samples, sample)
|
|
|
|
bitsLeftPerSample = bitsPerSample
|
|
|
|
sample = 0
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take care of remaining samples (if enough data available).
|
|
|
|
for remainderBits >= bitsPerSample {
|
|
|
|
take := remainderBits
|
|
|
|
if bitsLeftPerSample < take {
|
|
|
|
take = bitsLeftPerSample
|
|
|
|
}
|
|
|
|
|
|
|
|
sample = (sample << uint(take)) | uint32(remainder>>uint(8-take))
|
|
|
|
remainderBits -= take
|
|
|
|
if remainderBits > 0 {
|
|
|
|
remainder = remainder << uint(take)
|
|
|
|
} else {
|
|
|
|
remainder = 0
|
|
|
|
}
|
|
|
|
bitsLeftPerSample -= take
|
|
|
|
if bitsLeftPerSample == 0 {
|
|
|
|
//samples[index] = sample
|
|
|
|
samples = append(samples, sample)
|
|
|
|
bitsLeftPerSample = bitsPerSample
|
|
|
|
sample = 0
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return samples
|
|
|
|
}
|
|
|
|
|
2018-12-09 20:22:33 +02:00
|
|
|
// ResampleUint32 resamples the raw data which is in <=32-bit (uint32) format as a different
|
2017-02-22 21:10:57 +00:00
|
|
|
// bit count per sample, up to 32 bits (uint32).
|
2017-02-24 17:38:41 +00:00
|
|
|
//
|
|
|
|
// bitsPerOutputSample is the number of bits for each output sample (up to 32)
|
|
|
|
// bitsPerInputSample is the number of bits used in each input sample (up to 32)
|
|
|
|
func ResampleUint32(data []uint32, bitsPerInputSample int, bitsPerOutputSample int) []uint32 {
|
2018-12-09 19:28:50 +02:00
|
|
|
var samples []uint32
|
2017-02-22 21:10:57 +00:00
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
bitsLeftPerSample := bitsPerOutputSample
|
2017-02-22 21:10:57 +00:00
|
|
|
var sample uint32
|
|
|
|
var remainder uint32
|
|
|
|
remainderBits := 0
|
|
|
|
|
|
|
|
index := 0
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for i < len(data) {
|
|
|
|
// Start with the remainder.
|
|
|
|
if remainderBits > 0 {
|
|
|
|
take := remainderBits
|
|
|
|
if bitsLeftPerSample < take {
|
|
|
|
take = bitsLeftPerSample
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
sample = (sample << uint(take)) | uint32(remainder>>uint(bitsPerInputSample-take))
|
2017-02-22 21:10:57 +00:00
|
|
|
remainderBits -= take
|
|
|
|
if remainderBits > 0 {
|
|
|
|
remainder = remainder << uint(take)
|
|
|
|
} else {
|
|
|
|
remainder = 0
|
|
|
|
}
|
|
|
|
bitsLeftPerSample -= take
|
|
|
|
if bitsLeftPerSample == 0 {
|
|
|
|
//samples[index] = sample
|
|
|
|
samples = append(samples, sample)
|
2017-02-24 17:38:41 +00:00
|
|
|
bitsLeftPerSample = bitsPerOutputSample
|
2017-02-22 21:10:57 +00:00
|
|
|
sample = 0
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Take next byte
|
|
|
|
b := data[i]
|
|
|
|
i++
|
|
|
|
|
|
|
|
// 32 bits.
|
2017-02-24 17:38:41 +00:00
|
|
|
take := bitsPerInputSample
|
2017-02-22 21:10:57 +00:00
|
|
|
if bitsLeftPerSample < take {
|
|
|
|
take = bitsLeftPerSample
|
|
|
|
}
|
2017-02-24 17:38:41 +00:00
|
|
|
remainderBits = bitsPerInputSample - take
|
2017-02-22 21:10:57 +00:00
|
|
|
sample = (sample << uint(take)) | uint32(b>>uint(remainderBits))
|
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
if take < bitsPerInputSample {
|
2017-02-22 21:10:57 +00:00
|
|
|
remainder = b << uint(take)
|
|
|
|
}
|
|
|
|
|
|
|
|
bitsLeftPerSample -= take
|
|
|
|
if bitsLeftPerSample == 0 {
|
|
|
|
//samples[index] = sample
|
|
|
|
samples = append(samples, sample)
|
2017-02-24 17:38:41 +00:00
|
|
|
bitsLeftPerSample = bitsPerOutputSample
|
2017-02-22 21:10:57 +00:00
|
|
|
sample = 0
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take care of remaining samples (if enough data available).
|
2017-02-24 17:38:41 +00:00
|
|
|
for remainderBits >= bitsPerOutputSample {
|
2017-02-22 21:10:57 +00:00
|
|
|
take := remainderBits
|
|
|
|
if bitsLeftPerSample < take {
|
|
|
|
take = bitsLeftPerSample
|
|
|
|
}
|
|
|
|
|
2017-02-24 17:38:41 +00:00
|
|
|
sample = (sample << uint(take)) | uint32(remainder>>uint(bitsPerInputSample-take))
|
2017-02-22 21:10:57 +00:00
|
|
|
remainderBits -= take
|
|
|
|
if remainderBits > 0 {
|
|
|
|
remainder = remainder << uint(take)
|
|
|
|
} else {
|
|
|
|
remainder = 0
|
|
|
|
}
|
|
|
|
bitsLeftPerSample -= take
|
|
|
|
if bitsLeftPerSample == 0 {
|
|
|
|
samples = append(samples, sample)
|
2017-02-24 17:38:41 +00:00
|
|
|
bitsLeftPerSample = bitsPerOutputSample
|
2017-02-22 21:10:57 +00:00
|
|
|
sample = 0
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 12:56:27 +00:00
|
|
|
// If there are partial output samples, pad with 0s.
|
|
|
|
if bitsLeftPerSample > 0 && bitsLeftPerSample < bitsPerOutputSample {
|
|
|
|
sample <<= uint(bitsLeftPerSample)
|
|
|
|
samples = append(samples, sample)
|
|
|
|
}
|
|
|
|
|
2017-02-22 21:10:57 +00:00
|
|
|
return samples
|
|
|
|
}
|