Skip to content

Commit

Permalink
Add OGG -> QOA support
Browse files Browse the repository at this point in the history
Haven't figure out QOA-> OGG because there's no clean Go library to do it...might need our own
  • Loading branch information
braheezy committed Sep 3, 2023
1 parent b2c2792 commit 1b66767
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ goqoa
dist/
/*.mp3
/*.qoa
/*.ogg
28 changes: 27 additions & 1 deletion cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/braheezy/goqoa/pkg/qoa"
"github.com/go-audio/audio"
"github.com/go-audio/wav"
"github.com/jfreymuth/oggvorbis"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -39,7 +40,7 @@ var convertCmd = &cobra.Command{
DisableFlagsInUseLine: true,
}

var supportedFormats = []string{".qoa", ".wav", ".mp3"}
var supportedFormats = []string{".qoa", ".wav", ".mp3", ".ogg"}

func init() {
rootCmd.AddCommand(convertCmd)
Expand Down Expand Up @@ -107,6 +108,27 @@ func convertAudio(inputFile, outputFile string) {

case ".mp3":
decodedData, q = decodeMp3(&inputData)
case ".ogg":
fmt.Println("Input format is OGG")
oggReader := bytes.NewReader(inputData)
oggData, format, err := oggvorbis.ReadAll(oggReader)
if err != nil {
log.Fatalf("Error decoding OGG data: %v", err)
}

decodedData = make([]int16, len(oggData))
for i, val := range oggData {
// Scale to int16 range
decodedData[i] = int16(val * 32767.0)
}

// Set QOA metadata
numSamples := len(decodedData) / format.Channels
q = qoa.NewEncoder(
uint32(format.SampleRate),
uint32(format.Channels),
uint32(numSamples),
)
}

outExt := filepath.Ext(outputFile)
Expand Down Expand Up @@ -162,6 +184,10 @@ func convertAudio(inputFile, outputFile string) {

case ".mp3":
encodeMp3(outputFile, q, decodedData)
case ".ogg":
fmt.Println("Output format is OGG")
fmt.Println("And that's not supported yet...")
return
}

fmt.Printf("Conversion completed: %s -> %s\n", inputFile, outputFile)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/ebitengine/oto/v3 v3.0.0
github.com/go-audio/audio v1.0.0
github.com/go-audio/wav v1.1.0
github.com/jfreymuth/oggvorbis v1.0.5
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/tosone/minimp3 v1.0.2
Expand All @@ -17,6 +18,7 @@ require (
github.com/ebitengine/purego v0.5.0-alpha // indirect
github.com/go-audio/riff v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jfreymuth/vorbis v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.10.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ github.com/go-audio/wav v1.1.0 h1:jQgLtbqBzY7G+BM8fXF7AHUk1uHUviWS4X39d5rsL2g=
github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jfreymuth/oggvorbis v1.0.5 h1:u+Ck+R0eLSRhgq8WTmffYnrVtSztJcYrl588DM4e3kQ=
github.com/jfreymuth/oggvorbis v1.0.5/go.mod h1:1U4pqWmghcoVsCJJ4fRBKv9peUJMBHixthRlBeD6uII=
github.com/jfreymuth/vorbis v1.0.2 h1:m1xH6+ZI4thH927pgKD8JOH4eaGRm18rEE9/0WKjvNE=
github.com/jfreymuth/vorbis v1.0.2/go.mod h1:DoftRo4AznKnShRl1GxiTFCseHr4zR9BN3TWXyuzrqQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down

0 comments on commit 1b66767

Please sign in to comment.