瀏覽代碼

Bridge audio message waveforms in both directions

Tulir Asokan 3 年之前
父節點
當前提交
7ae5791aa3
共有 1 個文件被更改,包括 31 次插入2 次删除
  1. 31 2
      portal.go

+ 31 - 2
portal.go

@@ -1978,8 +1978,17 @@ func (portal *Portal) convertMediaMessage(intent *appservice.IntentAPI, source *
 	audioMessage, ok := msg.(*waProto.AudioMessage)
 	audioMessage, ok := msg.(*waProto.AudioMessage)
 	extraContent := map[string]interface{}{}
 	extraContent := map[string]interface{}{}
 	if ok {
 	if ok {
+		var waveform []int
+		if audioMessage.Waveform != nil {
+			waveform = make([]int, len(audioMessage.Waveform))
+			for i, part := range audioMessage.Waveform {
+				// TODO is 4 the right multiplier?
+				waveform[i] = int(part) * 4
+			}
+		}
 		extraContent["org.matrix.msc1767.audio"] = map[string]interface{}{
 		extraContent["org.matrix.msc1767.audio"] = map[string]interface{}{
 			"duration": int(audioMessage.GetSeconds()) * 1000,
 			"duration": int(audioMessage.GetSeconds()) * 1000,
+			"waveform": waveform,
 		}
 		}
 		if audioMessage.GetPtt() {
 		if audioMessage.GetPtt() {
 			extraContent["org.matrix.msc3245.voice"] = map[string]interface{}{}
 			extraContent["org.matrix.msc3245.voice"] = map[string]interface{}{}
@@ -2202,6 +2211,26 @@ func parseGeoURI(uri string) (lat, long float64, err error) {
 	return
 	return
 }
 }
 
 
+func getUnstableWaveform(content map[string]interface{}) []byte {
+	audioInfo, ok := content["org.matrix.msc1767.audio"].(map[string]interface{})
+	if !ok {
+		return nil
+	}
+	waveform, ok := audioInfo["waveform"].([]interface{})
+	if !ok {
+		return nil
+	}
+	output := make([]byte, len(waveform))
+	var val float64
+	for i, part := range waveform {
+		val, ok = part.(float64)
+		if ok {
+			output[i] = byte(val / 4)
+		}
+	}
+	return output
+}
+
 func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waProto.Message, *User) {
 func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waProto.Message, *User) {
 	content, ok := evt.Content.Parsed.(*event.MessageEventContent)
 	content, ok := evt.Content.Parsed.(*event.MessageEventContent)
 	if !ok {
 	if !ok {
@@ -2321,8 +2350,8 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waP
 			FileLength:    proto.Uint64(uint64(media.FileLength)),
 			FileLength:    proto.Uint64(uint64(media.FileLength)),
 		}
 		}
 		_, isMSC3245Voice := evt.Content.Raw["org.matrix.msc3245.voice"]
 		_, isMSC3245Voice := evt.Content.Raw["org.matrix.msc3245.voice"]
-		_, isMSC2516Voice := evt.Content.Raw["org.matrix.msc2516.voice"]
-		if isMSC3245Voice || isMSC2516Voice {
+		if isMSC3245Voice {
+			msg.AudioMessage.Waveform = getUnstableWaveform(evt.Content.Raw)
 			msg.AudioMessage.Ptt = proto.Bool(true)
 			msg.AudioMessage.Ptt = proto.Bool(true)
 			// hacky hack to add the codecs param that whatsapp seems to require
 			// hacky hack to add the codecs param that whatsapp seems to require
 			msg.AudioMessage.Mimetype = proto.String(addCodecToMime(content.GetInfo().MimeType, "opus"))
 			msg.AudioMessage.Mimetype = proto.String(addCodecToMime(content.GetInfo().MimeType, "opus"))