-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathtimeside-waveforms
More file actions
128 lines (101 loc) · 4.45 KB
/
Copy pathtimeside-waveforms
File metadata and controls
128 lines (101 loc) · 4.45 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
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2009-2013 Guillaume Pellerin <yomguy@parisson.com>
# This file is part of TimeSide.
# TimeSide is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# TimeSide 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 General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with TimeSide. If not, see <http://www.gnu.org/licenses/>.
# Author: Guillaume Pellerin <yomguy@parisson.com>
version = '0.5'
import os
import sys
import timeside.core
class GrapherScheme:
def __init__(self):
self.color_scheme = {
'waveform': [
# Four (R,G,B) tuples for three main color channels for the spectral centroid method
(0,0,255), (0,255,255), (255,255,0), (255,0,0)
],
'spectrogram': [
(0, 0, 0), (58/4,68/4,65/4), (80/2,100/2,153/2),
(90,180,100), (224,224,44), (255,60,30), (255,255,255)
]}
# Width of the image
self.width = 925
# Height of the image
self.height = 67
# Background color
self.bg_color = (-1, -1, -1, -1)
# Force computation. By default, the class doesn't overwrite existing image files.
self.force = True
class Media2Waveform(object):
def __init__(self, media_dir, img_dir):
self.root_dir = os.path.join(os.path.dirname(__file__), media_dir)
self.img_dir = os.path.join(os.path.dirname(__file__), img_dir)
self.scheme = GrapherScheme()
self.width = self.scheme.width
self.height = self.scheme.height
self.bg_color = self.scheme.bg_color
self.color_scheme = self.scheme.color_scheme
self.force = self.scheme.force
self.media_list = self.get_media_list()
if not os.path.exists(self.img_dir):
os.makedirs(self.img_dir)
self.path_dict = self.get_path_dict()
def get_media_list(self):
media_list = []
for root, dirs, files in os.walk(self.root_dir):
if root:
for media_file in files:
name, ext = os.path.splitext(media_file)
if ext:
media_list.append(os.path.join(root, media_file))
return media_list
def get_path_dict(self):
path_dict = {}
for media in self.media_list:
filename = os.path.basename(media)
img_file = filename.replace('.', '_') + '.png'
path_dict[media] = os.path.join(self.img_dir, img_file)
return path_dict
def process(self):
waveform = timeside.core.get_processor('waveform_simple')(width=self.width, height=self.height,
bg_color=self.bg_color, color_scheme=self.color_scheme)
for source, image in self.path_dict.items():
if not os.path.exists(image) or self.force:
print('Processing ', source)
audio = os.path.join(os.path.dirname(__file__), source)
decoder = timeside.core.get_processor('aubio_decoder')(audio)
(decoder | waveform).run()
dir_name, img_name = os.path.split(image)
img_root, img_ext = os.path.splitext(img_name)
img_name = '_'.join([img_root, str(self.width),
str(self.height)]
)+img_ext
filename = os.path.join(dir_name, )
#waveform.graph.filename = image
print('Rendering %s '% filename)
#print('frames per pixel = ', waveform.graph.samples_per_pixel)
waveform.render(output=image)
if __name__ == '__main__':
if len(sys.argv) <= 1:
print("""
timeside-waveforms : TimeSide simple batch waveform generator
usage : timeside-waveforms /path/to/media_dir /path/to/img_dir
infos : https://github.com/Ircam-WAM/TimeSide/
version : %s
""" % version)
else:
media_dir = sys.argv[-2]
img_dir = sys.argv[-1]
m = Media2Waveform(media_dir, img_dir)
m.process()