From 9ac1f64bb90b416f2ee440d95455c7f1a1cb202f Mon Sep 17 00:00:00 2001 From: WeebDataHoarder <57538841+WeebDataHoarder@users.noreply.github.com> Date: Sun, 29 Jan 2023 14:34:26 +0100 Subject: [PATCH] Use proper float32 analyzer on cmd/decode, add SHA256 and CRC32 hashes --- cmd/decode/decode.go | 62 +++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/cmd/decode/decode.go b/cmd/decode/decode.go index 6bbd965..9f33872 100644 --- a/cmd/decode/decode.go +++ b/cmd/decode/decode.go @@ -1,20 +1,16 @@ package main import ( - "crypto/md5" "encoding/hex" "flag" "git.gammaspectra.live/S.O.N.G/Kirika/audio" "git.gammaspectra.live/S.O.N.G/Kirika/audio/format" "git.gammaspectra.live/S.O.N.G/Kirika/audio/format/guess" "git.gammaspectra.live/S.O.N.G/Kirika/hasher" - "hash" "log" "os" - "runtime" "strconv" "time" - "unsafe" ) func main() { @@ -49,7 +45,7 @@ func main() { source, analyzer, err := guess.OpenAnalyzer(fp, decoders) if err != nil { - source, err = guess.Open(fp, decoders) + source, analyzer, err = format.NewAnalyzerChannel(guess.Open(fp, decoders)) } if err != nil { @@ -57,26 +53,18 @@ func main() { return } - var h *hasher.Hasher + analyzers := analyzer.Split(3) - if analyzer != nil { - h = hasher.NewHasher(analyzer, hasher.HashtypeMd5) - } - - var rawFloat32Hasher hash.Hash + hCRC32 := hasher.NewHasher(analyzers[0], hasher.HashtypeCrc32) + hSHA256 := hasher.NewHasher(analyzers[1], hasher.HashtypeSha256) + hMD5 := hasher.NewHasher(analyzers[2], hasher.HashtypeMd5) var sampleCount, blockCount uint64 if f32Source, ok := source.(audio.TypedSource[float32]); ok { - rawFloat32Hasher = md5.New() log.Printf("Decoding as float32 @ %dHz %d channel(s)", f32Source.GetSampleRate(), f32Source.GetChannels()) for b := range f32Source.GetBlocks() { blockCount++ sampleCount += uint64(len(b)) - - func() { - defer runtime.KeepAlive(b) - rawFloat32Hasher.Write(unsafe.Slice((*byte)(unsafe.Pointer(&b[0])), len(b)*int(unsafe.Sizeof(float32(0))))) - }() } sampleCount /= uint64(f32Source.GetChannels()) } else if i16Source, ok := source.(audio.TypedSource[int16]); ok { @@ -94,17 +82,11 @@ func main() { } sampleCount /= uint64(i32Source.GetChannels()) } else { - rawFloat32Hasher = md5.New() f32Source = source.ToFloat32() log.Printf("Decoding as float32 (generic) %d-bit @ %dHz %d channel(s)", f32Source.GetBitDepth(), f32Source.GetSampleRate(), f32Source.GetChannels()) for b := range f32Source.GetBlocks() { blockCount++ sampleCount += uint64(len(b)) - - func() { - defer runtime.KeepAlive(b) - rawFloat32Hasher.Write(unsafe.Slice((*byte)(unsafe.Pointer(&b[0])), len(b)*int(unsafe.Sizeof(float32(0))))) - }() } sampleCount /= uint64(f32Source.GetChannels()) } @@ -115,24 +97,22 @@ func main() { } log.Printf("Duration %s", ((time.Duration(sampleCount) * time.Second) / time.Duration(source.GetSampleRate())).String()) - if h != nil { - h.Wait() - log.Printf("MD5 hash: %s", hex.EncodeToString(h.GetResult())) - if _, ok := source.(audio.TypedSource[float32]); ok { - log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_f32le -f md5 -", strconv.Quote(*inputFile)) - } else { - switch source.GetBitDepth() { - case 8, 16, 24, 32: - log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s%dle -f md5 -", strconv.Quote(*inputFile), source.GetBitDepth()) - default: - log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s32le -f md5 -", strconv.Quote(*inputFile)) - } - } - } else if rawFloat32Hasher != nil { - log.Printf("MD5 hash: %s", hex.EncodeToString(rawFloat32Hasher.Sum(nil))) - if _, ok := source.(audio.TypedSource[float32]); ok { - log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_f32le -f md5 -", strconv.Quote(*inputFile)) - log.Printf("\tNote that you might need to specify a decoder with -c:a before -i to perfectly match the hash on lossy decoders, for example, libopus instead of opus") + hCRC32.Wait() + hSHA256.Wait() + hMD5.Wait() + + log.Printf("CRC32 hash: %s", hex.EncodeToString(hCRC32.GetResult())) + log.Printf("SHA256 hash: %s", hex.EncodeToString(hSHA256.GetResult())) + log.Printf("MD5 hash: %s", hex.EncodeToString(hMD5.GetResult())) + if _, ok := source.(audio.TypedSource[float32]); ok { + log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_f32le -f md5 -", strconv.Quote(*inputFile)) + log.Printf("\tNote that you might need to specify a decoder with -c:a before -i to perfectly match the hash on lossy decoders, for example, libopus instead of opus") + } else { + switch source.GetBitDepth() { + case 8, 16, 24, 32: + log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s%dle -f md5 -", strconv.Quote(*inputFile), source.GetBitDepth()) + default: + log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s32le -f md5 -", strconv.Quote(*inputFile)) } }