Skip to content

Commit

Permalink
msgconv/from-matrix: fix reading duration and converting waveform
Browse files Browse the repository at this point in the history
Fixes #757
  • Loading branch information
tulir committed Dec 19, 2024
1 parent c2f719e commit 8d49641
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions pkg/msgconv/from-matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,25 +502,29 @@ func parseGeoURI(uri string) (lat, long float64, err error) {
return
}

func getAudioInfo(content *event.MessageEventContent) (output []byte, Duration uint32) {
func getAudioInfo(content *event.MessageEventContent) (output []byte, duration uint32) {
duration = uint32(content.Info.Duration / 1000)
audioInfo := content.MSC1767Audio
if audioInfo == nil {
return nil, uint32(content.Info.Duration / 1000)
return
}
if duration == 0 && audioInfo.Duration != 0 {
duration = uint32(audioInfo.Duration / 1000)
}
waveform := audioInfo.Waveform
if len(waveform) == 0 {
return nil, uint32(audioInfo.Duration / 1000)
return
}

maxVal := slices.Max(waveform)
output = make([]byte, len(waveform))
if maxVal < 256 {
if maxVal <= 256 {
for i, part := range waveform {
output[i] = byte(part)
output[i] = byte(min(part, 255))
}
} else {
for i, part := range waveform {
output[i] = min(byte(part/4), 255)
output[i] = byte(min(part/4, 255))
}
}
return
Expand Down

0 comments on commit 8d49641

Please sign in to comment.