Skip to content

Commit

Permalink
Add support of UTF-8
Browse files Browse the repository at this point in the history
It's a better way.
  • Loading branch information
fakeboboliu authored Jul 17, 2018
1 parent ad3d52d commit 7735347
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions taglib.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,28 @@ type TagName int
// Tag names
const (
Album TagName = iota
Artist
Bitrate
Channels
Comments
Genre
Length
Samplerate
Title
Track
Year
Artist
Bitrate
Channels
Comments
Genre
Length
Samplerate
Title
Track
Year
)

var (
ErrInvalid = errors.New("invalid file")
glock = sync.Mutex{}
)

func init() {
// Make everything utf-8
C.taglib_id3v2_set_default_text_encoding(3)
}

// Returns a string with this tag's comment.
func (file *File) Tag(tagname TagName) (tagvalue string) {
switch tagname {
Expand Down Expand Up @@ -71,7 +76,7 @@ func (file *File) Tag(tagname TagName) (tagvalue string) {
}

// Sets the tag.
func (file *File) SetTag(tagname TagName, tagvalue []byte) {
func (file *File) SetTag(tagname TagName, tagvalue string) {
switch tagname {
case Album:
file.SetAlbum(tagvalue)
Expand All @@ -84,12 +89,12 @@ func (file *File) SetTag(tagname TagName, tagvalue []byte) {
case Title:
file.SetTitle(tagvalue)
case Track:
intValue, convErr := strconv.Atoi(string(tagvalue))
intValue, convErr := strconv.Atoi(tagvalue)
if convErr == nil {
file.SetTrack(intValue)
}
case Year:
intValue, convErr := strconv.Atoi(string(tagvalue))
intValue, convErr := strconv.Atoi(tagvalue)
if convErr == nil {
file.SetYear(intValue)
}
Expand All @@ -105,9 +110,6 @@ type File struct {
// Reads and parses a music file. Returns an error if the provided filename is
// not a valid file.
func Read(filename string) (*File, error) {
// Make everything utf-8
C.taglib_id3v2_set_default_text_encoding(3)

glock.Lock()
defer glock.Unlock()

Expand Down Expand Up @@ -254,46 +256,47 @@ func (file *File) Save() error {
}

// Sets the tag's title.
func (file *File) SetTitle(b []byte) {
func (file *File) SetTitle(s string) {
glock.Lock()
defer glock.Unlock()
cs := GetCCharPointer(b)
cs := GetCCharPointer(s)
defer C.free(unsafe.Pointer(cs))
C.taglib_tag_set_title(file.tag, cs)

}

// Sets the tag's artist.
func (file *File) SetArtist(b []byte) {
func (file *File) SetArtist(s string) {
glock.Lock()
defer glock.Unlock()
cs := GetCCharPointer(b)
cs := GetCCharPointer(s)
defer C.free(unsafe.Pointer(cs))
C.taglib_tag_set_artist(file.tag, cs)
}

// Sets the tag's album.
func (file *File) SetAlbum(b []byte) {
func (file *File) SetAlbum(s string) {
glock.Lock()
defer glock.Unlock()
cs := GetCCharPointer(b)
cs := GetCCharPointer(s)
defer C.free(unsafe.Pointer(cs))
C.taglib_tag_set_album(file.tag, cs)
}

// Sets the tag's comment.
func (file *File) SetComment(b []byte) {
func (file *File) SetComment(s string) {
glock.Lock()
defer glock.Unlock()
cs := GetCCharPointer(b)
cs := GetCCharPointer(s)
defer C.free(unsafe.Pointer(cs))
C.taglib_tag_set_comment(file.tag, cs)
}

// Sets the tag's genre.
func (file *File) SetGenre(b []byte) {
func (file *File) SetGenre(s string) {
glock.Lock()
defer glock.Unlock()
cs := GetCCharPointer(b)
cs := GetCCharPointer(s)
defer C.free(unsafe.Pointer(cs))
C.taglib_tag_set_genre(file.tag, cs)
}
Expand All @@ -314,8 +317,8 @@ func (file *File) SetTrack(i int) {
C.taglib_tag_set_track(file.tag, ci)
}

func GetCCharPointer(b []byte) *C.char {
func GetCCharPointer(s string) *C.char {
// Add a 0x00 to end
b = append(b, 0)
b := append([]byte(s), 0)
return (*C.char)(C.CBytes(b))
}

0 comments on commit 7735347

Please sign in to comment.