forked from wtolson/go-taglib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtaglib.go
192 lines (149 loc) · 3.73 KB
/
taglib.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Go wrapper for taglib
package taglib
// #cgo LDFLAGS: -ltag_c
// #include <stdlib.h>
// #include <taglib/tag_c.h>
import "C"
import (
"errors"
"sync"
"time"
"unsafe"
)
var (
ErrInvalid = errors.New("invalid file")
glock = sync.Mutex{}
)
type File struct {
fp *C.TagLib_File
tag *C.TagLib_Tag
props *C.TagLib_AudioProperties
}
// Reads and parses a music file. Returns an error if the provided filename is
// not a valid file.
func Read(filename string) (*File, error) {
glock.Lock()
defer glock.Unlock()
cs := C.CString(filename)
defer C.free(unsafe.Pointer(cs))
fp := C.taglib_file_new(cs)
if fp == nil || C.taglib_file_is_valid(fp) == 0 {
return nil, ErrInvalid
}
return &File{
fp: fp,
tag: C.taglib_file_tag(fp),
props: C.taglib_file_audioproperties(fp),
}, nil
}
// Close and free the file.
func (file *File) Close() {
glock.Lock()
defer glock.Unlock()
if file.fp == nil {
return
}
C.taglib_file_free(file.fp)
file.fp = nil
file.tag = nil
file.props = nil
}
func convertAndFree(cs *C.char) string {
if cs == nil {
return ""
}
defer C.free(unsafe.Pointer(cs))
return C.GoString(cs)
}
// Returns a string with this tag's title.
func (file *File) Title() string {
glock.Lock()
defer glock.Unlock()
return convertAndFree(C.taglib_tag_title(file.tag))
}
// Sets the tag's title string
// don't forget to call the "Save" method
func (file *File) SetTitle(title string) {
glock.Lock()
defer glock.Unlock()
C.taglib_tag_set_title(file.tag, C.CString(title))
}
// Returns a string with this tag's artist.
func (file *File) Artist() string {
glock.Lock()
defer glock.Unlock()
return convertAndFree(C.taglib_tag_artist(file.tag))
}
// Sets the tag's artist string
// don't forget to call the "Save" method
func (file *File) SetArtist(artist string) {
glock.Lock()
defer glock.Unlock()
C.taglib_tag_set_artist(file.tag, C.CString(artist))
}
// Returns a string with this tag's album name.
func (file *File) Album() string {
glock.Lock()
defer glock.Unlock()
return convertAndFree(C.taglib_tag_album(file.tag))
}
// Returns a string with this tag's comment.
func (file *File) Comment() string {
glock.Lock()
defer glock.Unlock()
return convertAndFree(C.taglib_tag_comment(file.tag))
}
// Returns a string with this tag's genre.
func (file *File) Genre() string {
glock.Lock()
defer glock.Unlock()
return convertAndFree(C.taglib_tag_genre(file.tag))
}
// Returns the tag's year or 0 if year is not set.
func (file *File) Year() int {
glock.Lock()
defer glock.Unlock()
return int(C.taglib_tag_year(file.tag))
}
// Returns the tag's track number or 0 if track number is not set.
func (file *File) Track() int {
glock.Lock()
defer glock.Unlock()
return int(C.taglib_tag_track(file.tag))
}
// Returns the length of the file.
func (file *File) Length() time.Duration {
glock.Lock()
defer glock.Unlock()
length := C.taglib_audioproperties_length(file.props)
return time.Duration(length) * time.Second
}
// Returns the bitrate of the file in kb/s.
func (file *File) Bitrate() int {
glock.Lock()
defer glock.Unlock()
return int(C.taglib_audioproperties_bitrate(file.props))
}
// Returns the sample rate of the file in Hz.
func (file *File) Samplerate() int {
glock.Lock()
defer glock.Unlock()
return int(C.taglib_audioproperties_samplerate(file.props))
}
// Returns the number of channels in the audio stream.
func (file *File) Channels() int {
glock.Lock()
defer glock.Unlock()
return int(C.taglib_audioproperties_channels(file.props))
}
// Saves any changes to the file file (such as title or artist)
func (file *File) Save() {
glock.Lock()
defer glock.Unlock()
C.taglib_file_save(file.fp)
}
func init() {
glock.Lock()
defer glock.Unlock()
C.taglib_set_string_management_enabled(0)
}