Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate QOA files with magic word #19

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading