Last active
November 8, 2020 05:50
-
-
Save mkb218/fb442d05488aee5cd4b4fafc7e8275e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "bytes" | |
import "io/ioutil" | |
import "fmt" | |
import "flag" | |
import "github.com/mkb218/gosndfile/sndfile" | |
type adapter struct { | |
br *bytes.Reader | |
index int64 | |
} | |
func main() { | |
flag.Parse() | |
fileBytes, err := ioutil.ReadFile(flag.Args()[0]) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
br := bytes.NewReader(fileBytes) | |
var virtualIo sndfile.VirtualIo | |
virtualIo.UserData = &adapter{br, 0} | |
virtualIo.GetLength = func(userdata interface{}) int64 { | |
a := userdata.(*adapter) | |
return int64(a.br.Len()) | |
} | |
virtualIo.Seek = func(offset int64, whence sndfile.Whence, userdata interface{}) int64 { | |
a := userdata.(*adapter) | |
var goWhence = int(whence) | |
var err error | |
a.index, err = a.br.Seek(offset, goWhence) | |
if err != nil { | |
panic(err) | |
} | |
return a.index | |
} | |
virtualIo.Read = func(output []byte, userdata interface{}) int64 { | |
a := userdata.(*adapter) | |
ret, err := a.br.Read(output) | |
if err != nil { | |
panic(err) | |
} | |
a.index += int64(ret) | |
return int64(ret) | |
} | |
virtualIo.Write = func(output []byte, userdata interface{}) int64 { | |
panic("read-only file") | |
} | |
virtualIo.Tell = func(userdata interface{}) int64 { | |
return userdata.(*adapter).index | |
} | |
var info sndfile.Info | |
_, err = sndfile.OpenVirtual(virtualIo, sndfile.Read, &info) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println(info) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment