Use proper float32 analyzer on cmd/decode, add SHA256 and CRC32 hashes
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DataHoarder 2023-01-29 14:34:26 +01:00
parent 88915d15d4
commit 9ac1f64bb9
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -1,20 +1,16 @@
package main package main
import ( import (
"crypto/md5"
"encoding/hex" "encoding/hex"
"flag" "flag"
"git.gammaspectra.live/S.O.N.G/Kirika/audio" "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"
"git.gammaspectra.live/S.O.N.G/Kirika/audio/format/guess" "git.gammaspectra.live/S.O.N.G/Kirika/audio/format/guess"
"git.gammaspectra.live/S.O.N.G/Kirika/hasher" "git.gammaspectra.live/S.O.N.G/Kirika/hasher"
"hash"
"log" "log"
"os" "os"
"runtime"
"strconv" "strconv"
"time" "time"
"unsafe"
) )
func main() { func main() {
@ -49,7 +45,7 @@ func main() {
source, analyzer, err := guess.OpenAnalyzer(fp, decoders) source, analyzer, err := guess.OpenAnalyzer(fp, decoders)
if err != nil { if err != nil {
source, err = guess.Open(fp, decoders) source, analyzer, err = format.NewAnalyzerChannel(guess.Open(fp, decoders))
} }
if err != nil { if err != nil {
@ -57,26 +53,18 @@ func main() {
return return
} }
var h *hasher.Hasher analyzers := analyzer.Split(3)
if analyzer != nil { hCRC32 := hasher.NewHasher(analyzers[0], hasher.HashtypeCrc32)
h = hasher.NewHasher(analyzer, hasher.HashtypeMd5) hSHA256 := hasher.NewHasher(analyzers[1], hasher.HashtypeSha256)
} hMD5 := hasher.NewHasher(analyzers[2], hasher.HashtypeMd5)
var rawFloat32Hasher hash.Hash
var sampleCount, blockCount uint64 var sampleCount, blockCount uint64
if f32Source, ok := source.(audio.TypedSource[float32]); ok { if f32Source, ok := source.(audio.TypedSource[float32]); ok {
rawFloat32Hasher = md5.New()
log.Printf("Decoding as float32 @ %dHz %d channel(s)", f32Source.GetSampleRate(), f32Source.GetChannels()) log.Printf("Decoding as float32 @ %dHz %d channel(s)", f32Source.GetSampleRate(), f32Source.GetChannels())
for b := range f32Source.GetBlocks() { for b := range f32Source.GetBlocks() {
blockCount++ blockCount++
sampleCount += uint64(len(b)) 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()) sampleCount /= uint64(f32Source.GetChannels())
} else if i16Source, ok := source.(audio.TypedSource[int16]); ok { } else if i16Source, ok := source.(audio.TypedSource[int16]); ok {
@ -94,17 +82,11 @@ func main() {
} }
sampleCount /= uint64(i32Source.GetChannels()) sampleCount /= uint64(i32Source.GetChannels())
} else { } else {
rawFloat32Hasher = md5.New()
f32Source = source.ToFloat32() f32Source = source.ToFloat32()
log.Printf("Decoding as float32 (generic) %d-bit @ %dHz %d channel(s)", f32Source.GetBitDepth(), f32Source.GetSampleRate(), f32Source.GetChannels()) log.Printf("Decoding as float32 (generic) %d-bit @ %dHz %d channel(s)", f32Source.GetBitDepth(), f32Source.GetSampleRate(), f32Source.GetChannels())
for b := range f32Source.GetBlocks() { for b := range f32Source.GetBlocks() {
blockCount++ blockCount++
sampleCount += uint64(len(b)) 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()) sampleCount /= uint64(f32Source.GetChannels())
} }
@ -115,24 +97,22 @@ func main() {
} }
log.Printf("Duration %s", ((time.Duration(sampleCount) * time.Second) / time.Duration(source.GetSampleRate())).String()) log.Printf("Duration %s", ((time.Duration(sampleCount) * time.Second) / time.Duration(source.GetSampleRate())).String())
if h != nil { hCRC32.Wait()
h.Wait() hSHA256.Wait()
log.Printf("MD5 hash: %s", hex.EncodeToString(h.GetResult())) hMD5.Wait()
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("CRC32 hash: %s", hex.EncodeToString(hCRC32.GetResult()))
} else { log.Printf("SHA256 hash: %s", hex.EncodeToString(hSHA256.GetResult()))
switch source.GetBitDepth() { log.Printf("MD5 hash: %s", hex.EncodeToString(hMD5.GetResult()))
case 8, 16, 24, 32: if _, ok := source.(audio.TypedSource[float32]); ok {
log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s%dle -f md5 -", strconv.Quote(*inputFile), source.GetBitDepth()) log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_f32le -f md5 -", strconv.Quote(*inputFile))
default: 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")
log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s32le -f md5 -", strconv.Quote(*inputFile)) } else {
} switch source.GetBitDepth() {
} case 8, 16, 24, 32:
} else if rawFloat32Hasher != nil { log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s%dle -f md5 -", strconv.Quote(*inputFile), source.GetBitDepth())
log.Printf("MD5 hash: %s", hex.EncodeToString(rawFloat32Hasher.Sum(nil))) default:
if _, ok := source.(audio.TypedSource[float32]); ok { log.Printf("\tCheck via $ ffmpeg -hide_banner -loglevel error -i %s -vn -c:a pcm_s32le -f md5 -", strconv.Quote(*inputFile))
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")
} }
} }