comparison vamp/collect.py @ 94:c3318a95625b

Return step as well
author Chris Cannam
date Mon, 02 Feb 2015 16:32:44 +0000
parents 4bed6bf67243
children 3e5791890b65
comparison
equal deleted inserted replaced
93:4bed6bf67243 94:c3318a95625b
5 import process 5 import process
6 import frames 6 import frames
7 7
8 import numpy as np 8 import numpy as np
9 9
10 def get_feature_step_time(sample_rate, step_size, output_desc):
11 if output_desc["sample_type"] == vampyhost.ONE_SAMPLE_PER_STEP:
12 return vampyhost.frame_to_realtime(step_size, sample_rate)
13 elif output_desc["sample_type"] == vampyhost.FIXED_SAMPLE_RATE:
14 return vampyhost.RealTime('seconds', 1.0 / output_desc["sample_rate"])
15 else:
16 return 1
10 17
11 def timestamp_features(sample_rate, step_size, output_desc, features): 18 def timestamp_features(sample_rate, step_size, output_desc, features):
12 n = -1 19 n = -1
13 if output_desc["sample_type"] == vampyhost.ONE_SAMPLE_PER_STEP: 20 if output_desc["sample_type"] == vampyhost.ONE_SAMPLE_PER_STEP:
14 for f in features: 21 for f in features:
27 yield f 34 yield f
28 else: 35 else:
29 for f in features: 36 for f in features:
30 yield f 37 yield f
31 38
32
33 def fill_timestamps(results, sample_rate, step_size, output_desc): 39 def fill_timestamps(results, sample_rate, step_size, output_desc):
34 40
35 output = output_desc["identifier"] 41 output = output_desc["identifier"]
36 42
37 selected = [ r[output] for r in results ] 43 selected = [ r[output] for r in results ]
39 stamped = timestamp_features(sample_rate, step_size, output_desc, selected) 45 stamped = timestamp_features(sample_rate, step_size, output_desc, selected)
40 46
41 for s in stamped: 47 for s in stamped:
42 yield s 48 yield s
43 49
44
45 def deduce_shape(output_desc): 50 def deduce_shape(output_desc):
46 if output_desc["has_duration"]: 51 if output_desc["has_duration"]:
47 return "individual" 52 return "individual"
48 if output_desc["sample_type"] == vampyhost.VARIABLE_SAMPLE_RATE: 53 if output_desc["sample_type"] == vampyhost.VARIABLE_SAMPLE_RATE:
49 return "individual" 54 return "individual"
50 if not output_desc["has_fixed_bin_count"]: 55 if not output_desc["has_fixed_bin_count"]:
51 return "individual" 56 return "individual"
52 if output_desc["bin_count"] == 0: 57 if output_desc["bin_count"] == 0:
53 return "individual" 58 return "individual"
54 if output_desc["bin_count"] > 1: 59 if output_desc["bin_count"] == 1:
55 return "matrix" 60 return "vector"
56 return "vector" 61 return "matrix"
57
58 62
59 def process_and_reshape(data, sample_rate, key, output, parameters = {}): 63 def process_and_reshape(data, sample_rate, key, output, parameters = {}):
60 64
61 plugin, step_size, block_size = load.load_and_configure(data, sample_rate, key, parameters) 65 plugin, step_size, block_size = load.load_and_configure(data, sample_rate, key, parameters)
62 66
69 ff = frames.frames_from_array(data, step_size, block_size) 73 ff = frames.frames_from_array(data, step_size, block_size)
70 74
71 results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output]) 75 results = process.process_frames_with_plugin(ff, sample_rate, step_size, plugin, [output])
72 76
73 shape = deduce_shape(output_desc) 77 shape = deduce_shape(output_desc)
78 out_step = get_feature_step_time(sample_rate, step_size, output_desc)
74 79
75 if shape == "vector": 80 if shape == "vector":
76 rv = np.array([r[output]["values"][0] for r in results]) 81 rv = ( out_step,
82 np.array([r[output]["values"][0] for r in results]) )
77 elif shape == "matrix": 83 elif shape == "matrix":
78 rv = np.array( 84 rv = ( out_step,
79 [[r[output]["values"][i] for r in results] 85 np.array(
80 for i in range(0, output_desc["bin_count"]-1)]) 86 [[r[output]["values"][i] for r in results]
87 for i in range(0, output_desc["bin_count"])]) )
81 else: 88 else:
82 rv = list(fill_timestamps(results, sample_rate, step_size, output_desc)) 89 rv = list(fill_timestamps(results, sample_rate, step_size, output_desc))
83 90
84 plugin.unload() 91 plugin.unload()
85 return rv 92 return rv