-
Notifications
You must be signed in to change notification settings - Fork 6
/
audiotags.go
117 lines (102 loc) · 3.36 KB
/
audiotags.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
/***************************************************************************
copyright : (C) 2014 by Nick Sellen
email : code@nicksellen.co.uk
***************************************************************************/
/***************************************************************************
* This library is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License version *
* 2.1 as published by the Free Software Foundation. *
* *
* This library is distributed in the hope that it will be useful, but *
* WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
***************************************************************************/
package audiotags
/*
#cgo pkg-config: taglib
#cgo LDFLAGS: -lstdc++
#include "audiotags.h"
#include <stdlib.h>
*/
import "C"
import (
"strings"
"unsafe"
)
import "fmt"
type File C.TagLib_File
type AudioProperties struct {
Length, Bitrate, Samplerate, Channels int
}
func Open(filename string) (*File, error) {
fp := C.CString(filename)
defer C.free(unsafe.Pointer(fp))
f := (C.audiotags_file_new(fp))
if f == nil {
return nil, fmt.Errorf("cannot process %s", filename)
}
return (*File)(f), nil
}
func Read(filename string) (map[string]string, *AudioProperties, error) {
f, err := Open(filename)
if err != nil {
return nil, nil, err
}
defer f.Close()
return f.ReadTags(), f.ReadAudioProperties(), nil
}
func ReadTags(filename string) (map[string]string, error) {
f, err := Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return f.ReadTags(), nil
}
func ReadAudioProperties(filename string) (*AudioProperties, error) {
f, err := Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return f.ReadAudioProperties(), nil
}
func (f *File) Close() {
C.audiotags_file_close((*C.TagLib_File)(f))
}
func (f *File) ReadTags() map[string]string {
id := mapsNextId
mapsNextId++
m := make(map[string]string)
maps[id] = m
C.audiotags_file_properties((*C.TagLib_File)(f), C.int(id))
delete(maps, id)
return m
}
func (f *File) ReadAudioProperties() *AudioProperties {
ap := C.audiotags_file_audioproperties((*C.TagLib_File)(f))
if ap == nil {
return nil
}
p := AudioProperties{}
p.Length = int(C.audiotags_audioproperties_length(ap))
p.Bitrate = int(C.audiotags_audioproperties_bitrate(ap))
p.Samplerate = int(C.audiotags_audioproperties_samplerate(ap))
p.Channels = int(C.audiotags_audioproperties_channels(ap))
return &p
}
var maps = make(map[int]map[string]string)
var mapsNextId = 0
//export go_map_put
func go_map_put(id C.int, key *C.char, val *C.char) {
m := maps[int(id)]
k := strings.ToLower(C.GoString(key))
v := C.GoString(val)
m[k] = v
}