From 88915d15d42ff85d9ed08777c60a25328b45acff Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Sun, 29 Jan 2023 14:29:12 +0100 Subject: [PATCH] Properly handle float32 samples for analyzer --- audio/format/analyzer.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/audio/format/analyzer.go b/audio/format/analyzer.go index a4602b9..19df2fc 100644 --- a/audio/format/analyzer.go +++ b/audio/format/analyzer.go @@ -3,8 +3,10 @@ package format import ( "git.gammaspectra.live/S.O.N.G/Kirika/audio" "io" + "runtime" "sync" "sync/atomic" + "unsafe" ) const chanBuf = 16 @@ -37,13 +39,17 @@ func NewAnalyzerChannel(source audio.Source, err error) (audio.Source, AnalyzerC go func() { defer close(channel) - if _, ok := sources[1].(audio.TypedSource[float32]); ok { - for block := range sources[1].ToInt32(source.GetBitDepth()).GetBlocks() { + if float32Source, ok := sources[1].(audio.TypedSource[float32]); ok { + for block := range float32Source.GetBlocks() { + //Convert float32 to int32 representation with 32 bit depth, so it hashes the same way + buf := make([]int32, len(block)) + copy(buf, unsafe.Slice((*int32)(unsafe.Pointer(&block[0])), len(block))) + runtime.KeepAlive(block) channel <- &AnalyzerPacket{ - Samples: block, + Samples: buf, Channels: source.GetChannels(), SampleRate: source.GetSampleRate(), - BitDepth: source.GetBitDepth(), + BitDepth: 32, } } } else {