From 8bcbb9b4015f589de510d53b4707df8485d9d963 Mon Sep 17 00:00:00 2001 From: Logiase Date: Sun, 9 Aug 2020 13:01:46 +0800 Subject: [PATCH] update voice --- client/client.go | 3 ++- utils/audio.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 utils/audio.go diff --git a/client/client.go b/client/client.go index 2c091351..e8abf332 100644 --- a/client/client.go +++ b/client/client.go @@ -429,8 +429,9 @@ func (c *QQClient) uploadPrivateImage(target int64, img []byte, count int) (*mes return e, nil } -func (c *QQClient) UploadGroupPtt(groupCode int64, voice []byte, voiceLength int32) (*message.GroupVoiceElement, error) { +func (c *QQClient) UploadGroupPtt(groupCode int64, voice []byte) (*message.GroupVoiceElement, error) { h := md5.Sum(voice) + voiceLength := utils.GetAmrDuration(voice) seq, pkt := c.buildGroupPttStorePacket(groupCode, h[:], int32(len(voice)), voiceLength) r, err := c.sendAndWait(seq, pkt) if err != nil { diff --git a/utils/audio.go b/utils/audio.go new file mode 100644 index 00000000..c567ef0a --- /dev/null +++ b/utils/audio.go @@ -0,0 +1,31 @@ +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) +}