2019-03-12 08:52:44 +03:00
|
|
|
package pcm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/yobert/alsa"
|
|
|
|
)
|
|
|
|
|
2019-03-13 05:49:53 +03:00
|
|
|
// Resample resamples pcm data from fromBuf to 'toRate' Hz and returns the resulting pcm.
|
|
|
|
// If an error occurs, an error will be returned along with the original fromBuf's data
|
2019-03-12 08:52:44 +03:00
|
|
|
// Notes:
|
2019-03-13 05:49:53 +03:00
|
|
|
// - Currently only downsampling is implemented and fromBuf's rate must be divisible by toRate or an error will occur.
|
|
|
|
// - If the number of bytes in fromBuf.Data is not divisible by the decimation factor (ratioFrom), the remaining bytes will
|
2019-03-12 08:52:44 +03:00
|
|
|
// not be included in the result. Eg. input of length 480002 downsampling 6:1 will result in output length 80000.
|
2019-03-13 05:49:53 +03:00
|
|
|
func Resample(fromBuf alsa.Buffer, toRate int) ([]byte, error) {
|
|
|
|
fromRate := fromBuf.Format.Rate
|
2019-03-12 08:52:44 +03:00
|
|
|
if fromRate == toRate {
|
2019-03-13 05:49:53 +03:00
|
|
|
return fromBuf.Data, nil
|
2019-03-12 08:52:44 +03:00
|
|
|
} else if fromRate < 0 {
|
2019-03-13 05:49:53 +03:00
|
|
|
return fromBuf.Data, fmt.Errorf("Unable to convert from: %v Hz", fromRate)
|
2019-03-12 08:52:44 +03:00
|
|
|
} else if toRate < 0 {
|
2019-03-13 05:49:53 +03:00
|
|
|
return fromBuf.Data, fmt.Errorf("Unable to convert to: %v Hz", toRate)
|
2019-03-12 08:52:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The number of bytes in a sample.
|
|
|
|
var sampleLen int
|
2019-03-13 05:49:53 +03:00
|
|
|
switch fromBuf.Format.SampleFormat {
|
|
|
|
case alsa.S32_LE:
|
|
|
|
sampleLen = 4 * fromBuf.Format.Channels
|
|
|
|
case alsa.S16_LE:
|
|
|
|
sampleLen = 2 * fromBuf.Format.Channels
|
2019-03-12 08:52:44 +03:00
|
|
|
default:
|
2019-03-13 05:49:53 +03:00
|
|
|
return fromBuf.Data, fmt.Errorf("Unhandled ALSA format: %v", fromBuf.Format.SampleFormat)
|
2019-03-12 08:52:44 +03:00
|
|
|
}
|
2019-03-13 05:49:53 +03:00
|
|
|
inPcmLen := len(fromBuf.Data)
|
2019-03-12 08:52:44 +03:00
|
|
|
|
|
|
|
// Calculate sample rate ratio ratioFrom:ratioTo.
|
|
|
|
rateGcd := gcd(toRate, fromRate)
|
|
|
|
ratioFrom := fromRate / rateGcd
|
|
|
|
ratioTo := toRate / rateGcd
|
|
|
|
|
|
|
|
// ratioTo = 1 is the only number that will result in an even sampling.
|
|
|
|
if ratioTo != 1 {
|
2019-03-13 05:49:53 +03:00
|
|
|
return fromBuf.Data, fmt.Errorf("%v:%v is an unhandled from:to rate ratio. must be n:1 for some rate n", ratioFrom, ratioTo)
|
2019-03-12 08:52:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newLen := inPcmLen / ratioFrom
|
|
|
|
result := make([]byte, 0, newLen)
|
|
|
|
|
2019-03-13 05:49:53 +03:00
|
|
|
// For each new sample to be generated, loop through the respective 'ratioFrom' samples in 'fromBuf.Data' to add them
|
2019-03-12 08:52:44 +03:00
|
|
|
// up and average them. The result is the new sample.
|
|
|
|
for i := 0; i < newLen/sampleLen; i++ {
|
|
|
|
var sum int
|
|
|
|
for j := 0; j < ratioFrom; j++ {
|
2019-03-13 05:49:53 +03:00
|
|
|
switch fromBuf.Format.SampleFormat {
|
|
|
|
case alsa.S32_LE:
|
|
|
|
sum += int(int32(binary.LittleEndian.Uint32(fromBuf.Data[(i*ratioFrom*sampleLen)+(j*sampleLen) : (i*ratioFrom*sampleLen)+((j+1)*sampleLen)])))
|
|
|
|
case alsa.S16_LE:
|
|
|
|
sum += int(int16(binary.LittleEndian.Uint16(fromBuf.Data[(i*ratioFrom*sampleLen)+(j*sampleLen) : (i*ratioFrom*sampleLen)+((j+1)*sampleLen)])))
|
2019-03-12 08:52:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
avg := sum / ratioFrom
|
|
|
|
bAvg := make([]byte, sampleLen)
|
2019-03-13 05:49:53 +03:00
|
|
|
switch fromBuf.Format.SampleFormat {
|
|
|
|
case alsa.S32_LE:
|
2019-03-12 08:52:44 +03:00
|
|
|
binary.LittleEndian.PutUint32(bAvg, uint32(avg))
|
2019-03-13 05:49:53 +03:00
|
|
|
case alsa.S16_LE:
|
2019-03-12 08:52:44 +03:00
|
|
|
binary.LittleEndian.PutUint16(bAvg, uint16(avg))
|
|
|
|
}
|
|
|
|
result = append(result, bAvg...)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StereoToMono returns raw mono audio data generated from only the left channel from
|
|
|
|
// the given stereo recording (ALSA buffer)
|
|
|
|
// if an error occurs, an error will be returned along with the original stereo data.
|
|
|
|
func StereoToMono(stereoBuf alsa.Buffer) ([]byte, error) {
|
2019-03-13 05:49:53 +03:00
|
|
|
if stereoBuf.Format.Channels == 1 {
|
2019-03-12 08:52:44 +03:00
|
|
|
return stereoBuf.Data, nil
|
2019-03-13 05:49:53 +03:00
|
|
|
} else if stereoBuf.Format.Channels != 2 {
|
|
|
|
return stereoBuf.Data, fmt.Errorf("Audio is not stereo or mono, it has %v channels", stereoBuf.Format.Channels)
|
2019-03-12 08:52:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var stereoSampleBytes int
|
|
|
|
switch stereoBuf.Format.SampleFormat {
|
|
|
|
case alsa.S32_LE:
|
|
|
|
stereoSampleBytes = 8
|
|
|
|
case alsa.S16_LE:
|
|
|
|
stereoSampleBytes = 4
|
|
|
|
default:
|
|
|
|
return stereoBuf.Data, fmt.Errorf("Unhandled ALSA format %v", stereoBuf.Format.SampleFormat)
|
|
|
|
}
|
|
|
|
|
|
|
|
recLength := len(stereoBuf.Data)
|
|
|
|
mono := make([]byte, recLength/2)
|
|
|
|
|
|
|
|
// Convert to mono: for each byte in the stereo recording, if it's in the first half of a stereo sample
|
|
|
|
// (left channel), add it to the new mono audio data.
|
|
|
|
var inc int
|
|
|
|
for i := 0; i < recLength; i++ {
|
|
|
|
if i%stereoSampleBytes < stereoSampleBytes/2 {
|
|
|
|
mono[inc] = stereoBuf.Data[i]
|
|
|
|
inc++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return mono, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// gcd is used for calculating the greatest common divisor of two positive integers, a and b.
|
|
|
|
// assumes given a and b are positive.
|
|
|
|
func gcd(a, b int) int {
|
|
|
|
if b != 0 {
|
|
|
|
return gcd(b, a%b)
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|