annotate test/test_process.py @ 82:a11b57e9fb0b

naming: module methods snake_case
author Chris Cannam
date Wed, 21 Jan 2015 12:43:50 +0000
parents b2afd385586f
children f0c18ba7b54e
rev   line source
Chris@61 1
Chris@61 2 import vamp
Chris@61 3 import numpy as np
Chris@61 4
Chris@82 5 plugin_key = "vamp-test-plugin:vamp-test-plugin"
Chris@82 6 plugin_key_freq = "vamp-test-plugin:vamp-test-plugin-freq"
Chris@61 7
Chris@61 8 rate = 44100
Chris@61 9
Chris@68 10 # Throughout this file we have the assumption that the plugin gets run with a
Chris@68 11 # blocksize of 1024, and with a step of 1024 for the time-domain version or 512
Chris@68 12 # for the frequency-domain one. That is certainly expected to be the norm for a
Chris@68 13 # plugin like this that declares no preference, and the Python Vamp module is
Chris@76 14 # expected to follow the norm.
Chris@66 15
Chris@68 16 blocksize = 1024
Chris@68 17
Chris@68 18 def input_data(n):
Chris@68 19 # start at 1, not 0 so that all elts are non-zero
Chris@68 20 return np.arange(n) + 1
Chris@68 21
Chris@68 22 def test_process_n():
Chris@68 23 buf = input_data(blocksize)
Chris@82 24 results = list(vamp.process(buf, rate, plugin_key, "input-summary"))
Chris@68 25 assert len(results) == 1
Chris@68 26
Chris@68 27 def test_process_freq_n():
Chris@68 28 buf = input_data(blocksize)
Chris@82 29 results = list(vamp.process(buf, rate, plugin_key_freq, "input-summary", {}))
Chris@68 30 assert len(results) == 2 # one complete block starting at zero, one half-full
Chris@68 31
Chris@74 32 def test_process_default_output():
Chris@74 33 # If no output is specified, we should get the first one (instants)
Chris@74 34 buf = input_data(blocksize)
Chris@82 35 results = list(vamp.process(buf, rate, plugin_key, "", {}))
Chris@74 36 assert len(results) == 10
Chris@74 37 for i in range(len(results)):
Chris@74 38 expectedTime = vamp.vampyhost.RealTime('seconds', i * 1.5)
Chris@76 39 actualTime = results[i]["timestamp"]
Chris@74 40 assert expectedTime == actualTime
Chris@76 41
Chris@68 42 def test_process_summary_param():
Chris@68 43 buf = input_data(blocksize * 10)
Chris@82 44 results = list(vamp.process(buf, rate, plugin_key, "input-summary", { "produce_output": 0 }))
Chris@76 45 assert len(results) == 0
Chris@76 46
Chris@76 47 def test_process_multi_summary_param():
Chris@76 48 buf = input_data(blocksize * 10)
Chris@82 49 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key, [ "input-summary" ], { "produce_output": 0 }))
Chris@68 50 assert len(results) == 0
Chris@68 51
Chris@68 52 def test_process_summary_param_bool():
Chris@68 53 buf = input_data(blocksize * 10)
Chris@82 54 results = list(vamp.process(buf, rate, plugin_key, "input-summary", { "produce_output": False }))
Chris@76 55 assert len(results) == 0
Chris@76 56
Chris@76 57 def test_process_multi_summary_param_bool():
Chris@76 58 buf = input_data(blocksize * 10)
Chris@82 59 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key, [ "input-summary" ], { "produce_output": False }))
Chris@68 60 assert len(results) == 0
Chris@68 61
Chris@68 62 def test_process_summary():
Chris@68 63 buf = input_data(blocksize * 10)
Chris@82 64 results = list(vamp.process(buf, rate, plugin_key, "input-summary", {}))
Chris@76 65 assert len(results) == 10
Chris@76 66 for i in range(len(results)):
Chris@76 67 #
Chris@76 68 # each feature has a single value, equal to the number of non-zero elts
Chris@76 69 # in the input block (which is all of them, i.e. the blocksize) plus
Chris@76 70 # the first elt (which is i * blockSize + 1)
Chris@76 71 #
Chris@76 72 expected = blocksize + i * blocksize + 1
Chris@76 73 actual = results[i]["values"][0]
Chris@76 74 assert actual == expected
Chris@76 75
Chris@76 76 def test_process_multi_summary():
Chris@76 77 buf = input_data(blocksize * 10)
Chris@82 78 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key, [ "input-summary" ], {}))
Chris@68 79 assert len(results) == 10
Chris@68 80 for i in range(len(results)):
Chris@69 81 #
Chris@68 82 # each feature has a single value, equal to the number of non-zero elts
Chris@68 83 # in the input block (which is all of them, i.e. the blocksize) plus
Chris@68 84 # the first elt (which is i * blockSize + 1)
Chris@69 85 #
Chris@68 86 expected = blocksize + i * blocksize + 1
Chris@70 87 actual = results[i]["input-summary"]["values"][0]
Chris@68 88 assert actual == expected
Chris@68 89
Chris@68 90 def test_process_freq_summary():
Chris@68 91 buf = input_data(blocksize * 10)
Chris@82 92 results = list(vamp.process(buf, rate, plugin_key_freq, "input-summary", {}))
Chris@68 93 assert len(results) == 20
Chris@68 94 for i in range(len(results)):
Chris@68 95 #
Chris@68 96 # sort of as above, but much much subtler:
Chris@68 97 #
Chris@68 98 # * the input block is converted to frequency domain but then converted
Chris@68 99 # back within the plugin, so the values being reported are time-domain
Chris@68 100 # ones but with windowing and FFT shift
Chris@68 101 #
Chris@68 102 # * the effect of FFT shift is that the first element in the
Chris@68 103 # re-converted frame is actually the one that was at the start of the
Chris@68 104 # second half of the original frame
Chris@68 105 #
Chris@68 106 # * and the last block is only half-full, so the "first" elt in that
Chris@68 107 # one, which actually comes from just after the middle of the block,
Chris@68 108 # will be zero
Chris@68 109 #
Chris@68 110 # * windowing does not affect the value of the first elt, because
Chris@68 111 # (before fft shift) it came from the peak of the window shape where
Chris@68 112 # the window value is 1
Chris@68 113 #
Chris@68 114 # * but windowing does affect the number of non-zero elts, because the
Chris@68 115 # asymmetric window used has one value very close to zero in it
Chris@68 116 #
Chris@68 117 # * the step size (the increment in input value from one block to the
Chris@68 118 # next) is only half the block size
Chris@68 119 #
Chris@68 120 expected = i * (blocksize/2) + blocksize/2 + 1 # "first" elt
Chris@68 121 if (i == len(results)-1):
Chris@68 122 expected = 0
Chris@68 123 expected = expected + blocksize - 1 # non-zero elts
Chris@76 124 actual = results[i]["values"][0]
Chris@76 125 eps = 1e-6
Chris@76 126 assert abs(actual - expected) < eps
Chris@76 127
Chris@76 128 def test_process_multi_freq_summary():
Chris@76 129 buf = input_data(blocksize * 10)
Chris@82 130 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key_freq, [ "input-summary" ], {}))
Chris@76 131 assert len(results) == 20
Chris@76 132 for i in range(len(results)):
Chris@76 133 expected = i * (blocksize/2) + blocksize/2 + 1 # "first" elt
Chris@76 134 if (i == len(results)-1):
Chris@76 135 expected = 0
Chris@76 136 expected = expected + blocksize - 1 # non-zero elts
Chris@70 137 actual = results[i]["input-summary"]["values"][0]
Chris@68 138 eps = 1e-6
Chris@68 139 assert abs(actual - expected) < eps
Chris@68 140
Chris@69 141 def test_process_timestamps():
Chris@69 142 buf = input_data(blocksize * 10)
Chris@82 143 results = list(vamp.process(buf, rate, plugin_key, "input-timestamp", {}))
Chris@76 144 assert len(results) == 10
Chris@76 145 for i in range(len(results)):
Chris@76 146 # The timestamp should be the frame number of the first frame in the
Chris@76 147 # input buffer
Chris@76 148 expected = i * blocksize
Chris@76 149 actual = results[i]["values"][0]
Chris@76 150 assert actual == expected
Chris@76 151
Chris@76 152 def test_process_multi_timestamps():
Chris@76 153 buf = input_data(blocksize * 10)
Chris@82 154 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key, [ "input-timestamp" ]))
Chris@69 155 assert len(results) == 10
Chris@69 156 for i in range(len(results)):
Chris@69 157 # The timestamp should be the frame number of the first frame in the
Chris@69 158 # input buffer
Chris@69 159 expected = i * blocksize
Chris@70 160 actual = results[i]["input-timestamp"]["values"][0]
Chris@69 161 assert actual == expected
Chris@69 162
Chris@69 163 def test_process_freq_timestamps():
Chris@69 164 buf = input_data(blocksize * 10)
Chris@82 165 results = list(vamp.process(buf, rate, plugin_key_freq, "input-timestamp", {}))
Chris@76 166 assert len(results) == 20
Chris@76 167 for i in range(len(results)):
Chris@76 168 # The timestamp should be the frame number of the frame just beyond
Chris@76 169 # half-way through the input buffer
Chris@76 170 expected = i * (blocksize/2) + blocksize/2
Chris@76 171 actual = results[i]["values"][0]
Chris@76 172 assert actual == expected
Chris@76 173
Chris@76 174 def test_process_multi_freq_timestamps():
Chris@76 175 buf = input_data(blocksize * 10)
Chris@82 176 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key_freq, [ "input-timestamp" ], {}))
Chris@69 177 assert len(results) == 20
Chris@69 178 for i in range(len(results)):
Chris@69 179 # The timestamp should be the frame number of the frame just beyond
Chris@69 180 # half-way through the input buffer
Chris@69 181 expected = i * (blocksize/2) + blocksize/2
Chris@70 182 actual = results[i]["input-timestamp"]["values"][0]
Chris@69 183 assert actual == expected
Chris@74 184
Chris@74 185 def test_process_multiple_outputs():
Chris@74 186 buf = input_data(blocksize * 10)
Chris@82 187 results = list(vamp.process_multiple_outputs(buf, rate, plugin_key, [ "input-summary", "input-timestamp" ], {}))
Chris@74 188 assert len(results) == 20
Chris@74 189 si = 0
Chris@74 190 ti = 0
Chris@74 191 for r in results:
Chris@74 192 assert "input-summary" in r or "input-timestamp" in r
Chris@74 193 if "input-summary" in r:
Chris@74 194 expected = blocksize + si * blocksize + 1
Chris@74 195 actual = r["input-summary"]["values"][0]
Chris@74 196 assert actual == expected
Chris@74 197 si = si + 1
Chris@74 198 if "input-timestamp" in r:
Chris@74 199 expected = ti * blocksize
Chris@74 200 actual = r["input-timestamp"]["values"][0]
Chris@74 201 assert actual == expected
Chris@74 202 ti = ti + 1