more refactoring with glm

quietlight
Apr 30, 2026, 9:19 PM
2HAQZPV377VV26SMPSXSZR6CL7SS2GTNPR5COIAPN47NLJILRQGAC

Dependencies

  • [2] QVIGQOQZ more work on utils/ with glm
  • [3] LBWQJEDH minor refactor and more tests for utils/
  • [4] KZKLAINJ run out of space on nest, cleaned out
  • [5] LQLC7S3A trying gemini: Inconsistent Standards in @utils/ refactoring

Change contents

  • replacement in utils/wav_metadata.go at line 142
    [3.29717][3.29717:29787]()
    // Returns (sampleRate, duration, error). Does not parse INFO chunks.
    [3.29717]
    [3.29787]
    // Returns (sampleRate, duration, error). Delegates to parseWAVFromBytes and
    // extracts just the fields needed for batch processing.
  • replacement in utils/wav_metadata.go at line 145
    [3.29869][3.29869:31391]()
    if len(data) < 44 {
    return 0, 0, fmt.Errorf("file too small to be valid WAV")
    }
    // Verify RIFF header
    if string(data[0:4]) != "RIFF" {
    return 0, 0, fmt.Errorf("not a valid WAV file (missing RIFF header)")
    }
    // Verify WAVE format
    if string(data[8:12]) != "WAVE" {
    return 0, 0, fmt.Errorf("not a valid WAV file (missing WAVE format)")
    }
    var channels, bitsPerSample int
    // Parse chunks - stop after finding data chunk
    offset := 12
    for offset < len(data)-8 {
    chunkID := string(data[offset : offset+4])
    chunkSize := int(binary.LittleEndian.Uint32(data[offset+4 : offset+8]))
    offset += 8
    switch chunkID {
    case "fmt ":
    // Parse format chunk
    if chunkSize >= 16 && offset+16 <= len(data) {
    channels = int(binary.LittleEndian.Uint16(data[offset+2 : offset+4]))
    sampleRate = int(binary.LittleEndian.Uint32(data[offset+4 : offset+8]))
    bitsPerSample = int(binary.LittleEndian.Uint16(data[offset+14 : offset+16]))
    }
    case "data":
    // Found data chunk - calculate duration and return
    if sampleRate > 0 && channels > 0 && bitsPerSample > 0 {
    bytesPerSample := bitsPerSample / 8
    bytesPerSecond := sampleRate * channels * bytesPerSample
    if bytesPerSecond > 0 {
    duration = float64(chunkSize) / float64(bytesPerSecond)
    return sampleRate, duration, nil
    }
    }
    return 0, 0, fmt.Errorf("invalid WAV: fmt chunk missing or corrupt before data chunk")
    }
    // Move to next chunk (word-aligned)
    offset += chunkSize
    if chunkSize%2 != 0 {
    offset++
    }
    [3.29869]
    [3.31391]
    metadata, err := parseWAVFromBytes(data)
    if err != nil {
    return 0, 0, err
  • replacement in utils/wav_metadata.go at line 149
    [3.31394][3.31394:31565]()
    // Data chunk not found within 4KB - file may have large INFO chunks
    return 0, 0, fmt.Errorf("data chunk not found in first 4KB (try ParseWAVHeader for full parsing)")
    [3.31394]
    [3.31565]
    return metadata.SampleRate, metadata.Duration, nil
  • edit in utils/resample.go at line 9
    [3.74657][3.74657:74854]()
    return samples
    }
    // Calculate ratio: toRate/fromRate (e.g., 16000/250000 = 0.064)
    ratio := float64(toRate) / float64(fromRate)
    newLen := int(float64(len(samples)) * ratio)
    if newLen <= 0 {
  • edit in utils/resample.go at line 10
    [3.74871][3.74871:75351]()
    }
    result := make([]float64, newLen)
    for i := range newLen {
    // Source index in original samples (floating point)
    srcIdx := float64(i) / ratio
    idx0 := int(srcIdx)
    idx1 := idx0 + 1
    // Clamp to valid range
    if idx0 >= len(samples) {
    idx0 = len(samples) - 1
    }
    if idx1 >= len(samples) {
    idx1 = len(samples) - 1
    }
    // Linear interpolation between adjacent samples
    frac := srcIdx - float64(idx0)
    result[i] = samples[idx0]*(1-frac) + samples[idx1]*frac
  • replacement in utils/resample.go at line 11
    [3.75354][3.75354:75370]()
    return result
    [3.75354]
    [3.75370]
    // speed = fromRate/toRate: e.g. 250000/16000 = 15.625 (skip samples to downsample)
    return Resample(samples, float64(fromRate)/float64(toRate))
  • edit in utils/mapping.go at line 4
    [3.89580][3.89580:89596]()
    "database/sql"
  • edit in utils/mapping.go at line 89
    [3.92000][3.4817:4962]()
    }
    // DBQueryer is an interface satisfied by *sql.DB and *sql.Tx
    type DBQueryer interface {
    Query(query string, args ...any) (*sql.Rows, error)
  • replacement in utils/mapping.go at line 94
    [3.92217][3.4963:4983]()
    queryer DBQueryer,
    [3.92217]
    [3.92230]
    queryer DB,
  • replacement in utils/file_import.go at line 124
    [3.138445][3.138445:138596]()
    // DBQueryable is an interface satisfied by both *sql.DB and *sql.Tx
    // for running duplicate hash checks against either.
    type DBQueryable interface {
    [3.138445]
    [3.138596]
    // DB is an interface satisfied by both *sql.DB and *sql.Tx.
    // Used throughout utils for database queries that must work with either.
    type DB interface {
    Query(query string, args ...any) (*sql.Rows, error)
  • replacement in utils/file_import.go at line 134
    [3.138841][3.138841:138944]()
    func CheckDuplicateHash(q DBQueryable, hash string) (existingID string, isDuplicate bool, err error) {
    [3.138841]
    [3.138944]
    func CheckDuplicateHash(q DB, hash string) (existingID string, isDuplicate bool, err error) {
  • replacement in utils/cluster_import.go at line 377
    [3.184587][3.184587:184637](),[3.184637][2.902:981](),[2.981][3.184698:184729](),[3.184698][3.184698:184729]()
    var exists bool
    err = tx.QueryRowContext(ctx,
    "SELECT EXISTS(SELECT 1 FROM file WHERE xxh64_hash = ? AND active = true)",
    fd.Hash,
    ).Scan(&exists)
    [3.184587]
    [3.184729]
    _, isDuplicate, err := CheckDuplicateHash(tx, fd.Hash)
  • replacement in utils/cluster_import.go at line 387
    [3.184927][3.184927:184941]()
    if exists {
    [3.184927]
    [3.184941]
    if isDuplicate {