1
0
mirror of https://github.com/Mrs4s/MiraiGo.git synced 2025-05-04 19:17:38 +08:00
MiraiGo/utils/audio.go
2020-08-09 13:01:46 +08:00

32 lines
644 B
Go

package utils
// GetAmrDuration - return .amr file duration form bytesData
func GetAmrDuration(bytesData []byte) int32 {
duration := -1
packedSize := [16]int{12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0}
length := len(bytesData)
pos := 6
var datas []byte
frameCount := 0
packedPos := -1
for pos <= length {
datas = bytesData[pos : pos+1]
if len(datas) != 1 {
if length > 0 {
duration = (length - 6) / 650
} else {
duration = 0
}
break
}
packedPos = int((datas[0] >> 3) & 0x0F)
pos += packedSize[packedPos] + 1
frameCount++
}
duration += frameCount * 20
return int32(duration/1000 + 1)
}