Skip to content

Commit

Permalink
Merge pull request #19 from braheezy/valid-qoa-files
Browse files Browse the repository at this point in the history
Validate QOA files with magic word
  • Loading branch information
braheezy authored Mar 6, 2024
2 parents 470802c + 20fa98b commit 644167c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/play.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"fmt"
"io"
"os"
"time"
Expand All @@ -24,6 +25,26 @@ func init() {
rootCmd.AddCommand(playCmd)
}

func isValidQOAFile(inputFile string) (bool, error) {
// Read first 4 bytes of the file
fileBytes := make([]byte, 4)
file, err := os.Open(inputFile)
if err != nil {
return false, err
}
defer file.Close()

_, err = file.Read(fileBytes)
if err != nil && err != io.EOF {
return false, err
}

// Check if the first 4 bytes are magic word `qoaf`
if string(fileBytes) != "qoaf" {
return false, fmt.Errorf("no magic word 'qoaf' found in %s", inputFile)
}
return true, nil
}
func playQOA(inputFiles []string) {

// Prepare an Oto context (this will use your default audio device)
Expand All @@ -38,6 +59,11 @@ func playQOA(inputFiles []string) {
}

for _, inputFile := range inputFiles {
_, err := isValidQOAFile(inputFile)
if err != nil {
logger.Fatalf("Error validating QOA file: %v", err)
}

qoaBytes, err := os.ReadFile(inputFile)
if err != nil {
logger.Fatalf("Error reading QOA file: %v", err)
Expand Down

0 comments on commit 644167c

Please sign in to comment.