/
id3tag.go
93 lines (76 loc) · 1.97 KB
/
id3tag.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// id3tag package implements ID3 tag parsing functionality.
// Actually this is a wrapper around C libid3tag.
package id3tag
// #include <id3tag.h>
// #include <stdlib.h>
// #include "id3_hlp.h"
import "C"
import (
"unsafe"
"os"
)
// Tag struct incapulate parsed file's metadata.
type Tag struct {
cTag *_Ctype_struct_id3_tag
frames map[string]string
}
// Parse returns filled Tag object for the given music file.
func Parse(filename string) (tag *Tag, err os.Error) {
cFilename := C.CString(filename)
defer C.free(unsafe.Pointer(cFilename))
// Open file.
cId3File, err := C.id3_file_open(cFilename, C.ID3_FILE_MODE_READONLY)
if err != nil {
return nil, err
}
defer C.id3_file_close(cId3File)
tag = new(Tag)
tag.frames = make(map[string]string)
// Read tag.
tag.cTag, err = C.id3_file_tag(cId3File)
if err != nil {
return nil, err
}
// Tag struct will be released with file's one.
// Parse all frames.
for i := _Ctype_uint(0); i < tag.cTag.nframes; i++ {
cFrame := C.id3_hlp_get_tag_frame(tag.cTag, i)
cId := C.id3_hlp_get_frame_id(cFrame)
// XXX: As I understood cId memory will be GCed with id
// (they share same memory).
id := C.GoString(cId)
cVal := C.id3_hlp_get_frame_string(cFrame)
val := C.GoString(cVal)
tag.frames[id] = val
}
return tag, nil
}
// Artist returns name of the artist.
func (tag *Tag) Artist() string {
return tag.frames["TPE1"]
}
// Album returns name of the album.
func (tag *Tag) Album() string {
return tag.frames["TALB"]
}
// Artist returns track's title.
func (tag *Tag) Title() string {
return tag.frames["TIT2"]
}
// Number returns track's number.
func (tag *Tag) Number() string {
return tag.frames["TRCK"]
}
// Year returns track's year.
func (tag *Tag) Year() string {
year, present := tag.frames["TDRC"]
if !present {
year, _ = tag.frames["TYER"]
}
return year
}
// Comment returns track's comment string.
//func (tag *Tag) Comment() string {
// fmt.Printf("%v\n", tag.frames)
// return tag.frames["TCON"]
//}