forked from tidalcycles/Clean-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeta.py
More file actions
executable file
·117 lines (96 loc) · 4.1 KB
/
meta.py
File metadata and controls
executable file
·117 lines (96 loc) · 4.1 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
# Script for generating clean metadata files for superdirt and friends.
# (c) 2021 Alex McLean and contributors. Distributed under the terms of
# GPLv3, see LICENSE for details.
import argparse
import pathlib
import os,sys
import json
import itertools
import getpass
version = "clean-0.1"
def merge(a, b):
if isinstance(a, dict) and isinstance(b, dict):
d = dict(a)
d.update({k: merge(a.get(k, None), b[k]) for k in b})
return d
if isinstance(a, list) and isinstance(b, list):
return [merge(x, y) for x, y in itertools.zip_longest(a, b)]
return a if b is None else b
def isSound(path):
if os.path.isfile(path):
extension = os.path.splitext(path)[1]
if extension in ['.wav','.mp3']:
return True
return False
def readMeta(shortname, defaultMeta, pack_folder, subfolder, write):
subpath = os.path.join(pack_folder, subfolder)
metafile = os.path.join(pack_folder, shortname + ".cleanmeta")
sounds = [fn for fn in os.listdir(subpath) if isSound(os.path.join(subpath, fn))]
sounds.sort()
defaultMeta['sounds'] = list(map(lambda x: {'filename': os.path.join(subfolder,x), 'shortname': os.path.splitext(x)[0], 'description': '', 'type': 'sample'}, sounds))
if (os.path.exists(metafile)):
with open(metafile) as json_file:
meta = json.load(json_file)
meta = merge(defaultMeta,meta)
else:
meta = defaultMeta
if write:
with open(metafile, 'w') as json_file:
json.dump(meta, json_file, sort_keys=True, indent=2)
return(meta)
def getArgs():
parser = argparse.ArgumentParser(description='Create metadata for a sample folder.')
parser.add_argument('pack_folder', type=str)
parser.add_argument('--quiet', default=False, action="store_true")
parser.add_argument('--sample-subfolder', dest="subfolder", default="", type=str)
parser.add_argument('--shortname', type=str,
help='Identifier for the sampleset. Used to name the metadata file and to reference the sampleset elsewhere. Defaults to the name of the containing folder.'
)
parser.add_argument('--maintainer', type=str,
help='Name of maintainer'
)
parser.add_argument('--email', type=str,
help='Email of maintainer'
)
parser.add_argument('--copyright', type=str,
help='Copyright statement, e.g. (c) Gregory Coleman, 1969'
)
parser.add_argument('--license', type=str,
help='e.g. CC0 for creative commons zero (public domain)'
)
parser.add_argument('--provenance', type=str,
help='Where the samples came from, e.g. a freesound.org URL'
)
parser.add_argument('--description', type=str,
help='Description of a sample pack'
)
parser.add_argument('--write', default=False,
help='write/re-write the metadata with default values', action="store_true"
)
args = parser.parse_args()
if (not os.path.exists(args.pack_folder)):
sys.exit("Couldn't open '" + args.pack_folder + "'")
subpath = os.path.join(args.pack_folder, args.subfolder)
if (not os.path.exists(subpath)):
sys.exit("Couldn't open '" + subpath + "'")
return args
args = getArgs()
defaultMeta = {
'metadata-format': version,
'license': args.license if args.license else "unknown",
'copyright': args.copyright if args.copyright else "unknown",
'provenance': args.provenance if args.provenance else "",
'maintainer': args.maintainer if args.maintainer else getpass.getuser(),
'email': args.email if args.email else "",
'description': args.description if args.description else "",
}
pack_folder = args.pack_folder
if args.shortname:
shortname = args.shortname
else:
shortname = os.path.basename(os.path.normpath(os.path.join(args.pack_folder, args.subfolder)))
meta = readMeta(shortname, defaultMeta, pack_folder, args.subfolder, args.write)
if not args.quiet:
print(json.dumps(meta, sort_keys=True, indent=2))
exit(0)