Bridge audio message waveforms in both directions

This commit is contained in:
Tulir Asokan 2022-02-10 12:50:04 +02:00
parent 94e880c2cc
commit 7ae5791aa3

View file

@ -1978,8 +1978,17 @@ func (portal *Portal) convertMediaMessage(intent *appservice.IntentAPI, source *
audioMessage, ok := msg.(*waProto.AudioMessage)
extraContent := map[string]interface{}{}
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{}{
"duration": int(audioMessage.GetSeconds()) * 1000,
"waveform": waveform,
}
if audioMessage.GetPtt() {
extraContent["org.matrix.msc3245.voice"] = map[string]interface{}{}
@ -2202,6 +2211,26 @@ func parseGeoURI(uri string) (lat, long float64, err error) {
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) {
content, ok := evt.Content.Parsed.(*event.MessageEventContent)
if !ok {
@ -2321,8 +2350,8 @@ func (portal *Portal) convertMatrixMessage(sender *User, evt *event.Event) (*waP
FileLength: proto.Uint64(uint64(media.FileLength)),
}
_, 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)
// hacky hack to add the codecs param that whatsapp seems to require
msg.AudioMessage.Mimetype = proto.String(addCodecToMime(content.GetInfo().MimeType, "opus"))