-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclip.go
More file actions
96 lines (79 loc) · 1.73 KB
/
clip.go
File metadata and controls
96 lines (79 loc) · 1.73 KB
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
package main
import (
"path"
"strings"
"unicode"
)
// Represents a music clip.
type Clip struct {
file string
tags [5]string
}
// Index for Clip.tags
const (
TAG_TRACK = iota
TAG_TITLE
TAG_ALBUM
TAG_ARTIST
TAG_GENRE
)
var tagStr []string = []string{"Track", "Title", "Album", "Artist", "Genre"}
func NewClip(file string) *Clip {
clip := new(Clip)
clip.file = file
clip.initTags()
return clip
}
// Rudimentary way to set clip tags based on file name:
// artist/album/01_title.ogg
// TODO: read I3D tags.
func (clip *Clip) initTags() {
// if file starts with number,
// use it as TRACK tag.
file := clip.file
base := path.Base(file)
i := 0
for _, chr := range base {
if !unicode.IsDigit(chr) {
break
}
i++
}
clip.tags[TAG_TRACK] = base[:i]
// TITLE tag is filename without extension
// or leading track number.
ext := path.Ext(base)
title := base[i : len(base)-len(ext)]
clip.tags[TAG_TITLE] = strings.Trim(title, " ")
// ALBUM tag is clip's parent directory
parent1, _ := path.Split(file)
clip.tags[TAG_ALBUM] = path.Base(parent1)
// ARTIST tag is albums' parent directory
parent2, _ := path.Split(parent1[:len(parent1)-1])
clip.tags[TAG_ARTIST] = path.Base(parent2)
}
func (clip *Clip) File() string {
return clip.file
}
func (clip *Clip) Track() string {
return clip.tags[TAG_TRACK]
}
func (clip *Clip) Title() string {
return clip.tags[TAG_TITLE]
}
func (clip *Clip) Album() string {
return clip.tags[TAG_ALBUM]
}
func (clip *Clip) Artist() string {
return clip.tags[TAG_ARTIST]
}
func (clip *Clip) Genre() string {
return clip.tags[TAG_GENRE]
}
func (clip *Clip) String() string {
str := clip.file + "\n"
for i, tag := range clip.tags {
str += "\t" + tagStr[i] + ":" + tag + "\n"
}
return str
}