def pitchshift_stream(sound_array, n, window_size=2**13, h=2**11): """ Changes the pitch of a sound by n semitones. Notes ----- This application has 2 sink_window agents and 3 streams x, y, z. Stretch agent: The first agent gets input x and outputs y which stretches the data in stream x. The stretching code is from Zulko, pianoputer. Speed up agent: The next agent gets input y and outputs z which speeds up y by the specified factor. This agent interpolates the data in y to the number of points determined by factor. """ factor = 2**(1.0 * n / 12.0) f = 1.0 / factor # Declare streams x = StreamArray('x', dtype=np.int16) y = StreamArray('y', dtype=np.int16) z = StreamArray('z', dtype=np.int16) # Define the stretch agent stretch_object = Stretch(in_stream=x, out_stream=y, factor=factor, window_size=window_size, h=h) sink_window(func=stretch_object.stretch, in_stream=x, window_size=window_size + h, step_size=int(h * f)) # Define the speedup agent. def f(window, out_stream): indices = np.arange(0, window_size, factor) out_stream.extend( np.int16(np.interp(indices, np.arange(window_size), window))) sink_window(func=f, in_stream=y, window_size=window_size, step_size=window_size, out_stream=z) # Partition sound_array into sound bites. Extend the # input with a sequence of sound bites and run each # sound bite until the sound_array data is finished. sound_bite_size = 2**14 for i in range(0, sound_array.size, sound_bite_size): # sound_bite = sound_array[i:i+sound_bite_size] x.extend(sound_array[i:i + sound_bite_size]) run() # Process any data in sound_array that wasn't processed # in the for loop. x.extend(sound_array[i:]) # Return the result. return z.recent[:z.stop]
def f(in_streams, out_streams, index, even, odd): def g(v): if (0 < index) and (index < N - 1): if v % 2 == 0: odd[index] = (even[index - 1] + even[index] + even[index + 1]) / 3.0 else: even[index] = (odd[index - 1] + odd[index] + odd[index + 1]) / 3.0 return v + 1 def r(lst, state): if state < M: return lst[0], state + 1 else: return _no_value, state for out_stream in out_streams: out_stream.extend([0]) synch_stream = Stream('synch_stream') zip_map(r, in_streams, synch_stream, state=0, name='zip_map_' + str(index)) map_element(g, synch_stream, out_streams[0], name='grid' + str(index)) run()
def test_r_sub(self): x = Stream() y = Stream() r_sub(x, y, 2) x.extend(list(range(5))) run() assert recent_values(y) == [-2, -1, 0, 1, 2]
def test_f_add(self): x = Stream() K = 5 y = f_add(x, K) x.extend(list(range(3))) run() assert recent_values(y) == [5, 6, 7]
def test_source_file(self, filename='tests/test_source_file_name.txt'): s = Stream('s') with open(filename, 'r') as input_file: for line in input_file: s.append(int(line)) run() assert recent_values(s) == [1, 2, 3]
def test_class(self): class C(object): def __init__(self): return def f(self, value): if value > 0: return self.pos(value) else: return self.neg(value) def pos(self, value): return value * value def neg(self, value): return value + value s = Stream('s') t = Stream('t') c = C() @map_e def g(v): return c.f(v) g(in_stream=s, out_stream=t) s.extend(list(range(-4, 4))) run() assert (recent_values(t) == [-8, -6, -4, -2, 0, 1, 4, 9])
def test_(self): def copy(list_of_in_lists, state): return ([ in_list.list[in_list.start:in_list.stop] for in_list in list_of_in_lists ], state, [in_list.stop for in_list in list_of_in_lists]) input_stream_0 = Stream('input_stream_0', num_in_memory=32) input_stream_1 = Stream('input_stream_1', num_in_memory=32) output_stream_0 = Stream('output_stream_0', num_in_memory=32) output_stream_1 = Stream('output_stream_1', num_in_memory=32) A = Agent(in_streams=[input_stream_0, input_stream_1], out_streams=[output_stream_0, output_stream_1], transition=copy, name='A') input_stream_0.extend(list(range(10))) run() assert (output_stream_0.stop == 10) assert (output_stream_1.stop == 0) assert (output_stream_0.recent[:10] == list(range(10))) assert (input_stream_0.start == {A: 10}) assert (input_stream_1.start == {A: 0}) input_stream_1.extend(list(range(10, 25, 1))) run() assert (output_stream_0.stop == 10) assert (output_stream_1.stop == 15) assert (output_stream_0.recent[:10] == list(range(10))) assert (output_stream_1.recent[:15] == list(range(10, 25, 1))) assert (input_stream_0.start == {A: 10}) assert (input_stream_1.start == {A: 15})
def test_r_mul(self): x = Stream() y = Stream() a = r_mul(x, y, 2) x.extend(list(range(5))) run() assert recent_values(y) == [0, 2, 4, 6, 8]
def count_pos_and_non_pos(self, count, lst): """ Parameters ---------- count : list list with at least 2 elements lst : list The input list Return ------ count[0]: number of non-positive values in lst count[1]: number of positive values in lst. """ @sink_e def f(v, count): if v > 0: count[1] += 1 else: count[0] += 1 x = Stream('x') f(x, count=count) x.extend(lst) run()
def test_echo(self): spoken = Stream('spoken') heard = make_echo(spoken, D=1, A=0.5) spoken.extend([64, 32, 16, 8, 4, 2, 1, 0, 0, 0, 0]) run() assert recent_values(heard) == [ 64.0, 64.0, 48.0, 32.0, 20.0, 12.0, 7.0, 3.5, 1.75, 0.875, 0.4375 ]
def test_r_add(self): x = Stream() y = Stream() z = 5 r_add(x, y, z) x.extend(list(range(3))) run() assert recent_values(y) == [5, 6, 7]
def test_multiply_operator_with_arrays(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) z = y * x x.extend(np.arange(3)) y.extend(np.arange(100, 105, 2)) run() assert np.array_equal(recent_values(z), np.array([0, 102, 208]))
def test_minus_operator_with_arrays(self): x = StreamArray(dtype=int) y = StreamArray(dtype=int) z = y - x x.extend(np.arange(3)) y.extend(np.arange(100, 105, 2)) run() assert np.array_equal(recent_values(z), np.array([100, 101, 102]))
def shortest_path(d): R = range(len(d)) actors = [[[[] for k in R] for j in R] for i in R] for i in R: for j in R: for k in R: actors[i][j][k] = triangle_inequality(d, i, j, k) actors[i][j][k].actors = actors activate(actors[i][j][k]) run()
def sort_simple(the_list): initial_list = list(the_list) N = len(the_list) actors = [sort_pair(the_list, my_index) for my_index in range(N - 1)] for actor in actors: actor.actors = actors activate(actor) run() initial_list.sort() assert the_list == initial_list
def SORT_2(the_list, cutoffs): the_list = [min(the_list)] + the_list + [max(the_list)] M = len(cutoffs) signals = [Shared() for i in range(len(the_list) + 1)] actors = [ sort_2(cutoffs[k - 1], cutoffs[k], the_list, signals, name=str(k)) for k in range(1, M) ] run() return the_list[1:-1]
def test_None_in_stream(self): x = Stream('x', discard_None=False) y = Stream(name='y', discard_None=False) z = Stream(name='z') map_element(lambda v: v, x, y) map_element(lambda v: v, x, z) x.extend([0, None, 1, None, 2, _no_value, 3]) run() assert (recent_values(y) == [0, None, 1, None, 2, 3]) assert (recent_values(z) == [0, 1, 2, 3])
def test_initial_value(self): def double(v): return v * 2 x = Stream('x') y = Stream(name='y', initial_value=[0] * 5) a = map_element(func=double, in_stream=x, out_stream=y) x.extend(list(range(5))) run() assert recent_values(y) == [0] * 5 + [0, 2, 4, 6, 8]
def test_minus_operator_with_arrays_and_dimension(self): x = StreamArray(dimension=3, dtype=int) y = StreamArray(dimension=3, dtype=int) z = y - x A = np.array([[10, 20, 30], [40, 50, 60]]) B = np.array([[100, 100, 100], [200, 200, 200], [300, 300, 00]]) x.extend(A) y.extend(B) run() assert np.array_equal(recent_values(z), np.array([[90, 80, 70], [160, 150, 140]]))
def test_multiple_relations_2(self): @map_e def double(v): return v * 2 x = Stream('x', [10, 11]) y = Stream('y') double(x, y) double(x, y) x.extend(list(range(5))) run()
def test_sink_2(self): x = Stream() y = Stream() @sink_e def f(v, out_stream): out_stream.append(v + 100) f(x, out_stream=y) x.extend(list(range(5))) run()
def test_sink_1(self): x = Stream() y = Stream() @sink_w def f(v, out_stream): out_stream.append(sum(v) + 10) f(x, window_size=2, step_size=1, out_stream=y) x.extend(list(range(5))) run()
def SORT_1(the_list, cutoffs): the_list = [min(the_list)] + the_list + [max(the_list)] M = len(cutoffs) actors = [None] + [ sort_1(cutoffs[k - 1], cutoffs[k], the_list) for k in range(1, M) ] + [None] for k in range(1, M): actors[k].left_actor = actors[k - 1] actors[k].right_actor = actors[k + 1] run() return the_list[1:-1]
def test_map_window_list_1(self): x = Stream() y = Stream() @map_wl def f(window): return [2 * v for v in window] f(x, y, window_size=2, step_size=2) x.extend(list(range(10))) run() assert recent_values(y) == list(range(0, 20, 2))
def test_map_window_with_state(self): x = Stream() y = Stream() @map_w def f(window, state): return sum(window) + state, state + 1 f(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(10))) run() assert recent_values(y) == [1, 6, 11, 16, 21]
def test_filter_number_1(self): input_list = list(range(-5, 5)) in_stream = Stream('input') out_stream = Stream('output') def f(v): return v > 0 filter_element(f, in_stream, out_stream) in_stream.extend(input_list) run() assert recent_values(out_stream) == list(filter(f, input_list))
def test_map_with_keyword_arg(self): x = Stream() y = Stream() @map_e def f(v, k): return v + k f(x, y, k=10) x.extend(list(range(5))) run() assert recent_values(y) == [10, 11, 12, 13, 14]
def test_operators_on_three_streams(self): x = Stream('x') y = Stream('y') z = x + y w = x - y + z DATA = list(range(5)) x.extend(DATA) y.extend(DATA) run() assert recent_values(z) == [2 * v for v in DATA] assert recent_values(w) == [2 * v for v in DATA]
def test_map_list_with_arrays(self): from IoTPy.agent_types.op import map_list x = StreamArray(dtype=int) y = StreamArray(dtype=int) def f(A): return 2 * A map_list(f, x, y) x.extend(np.arange(5)) run() assert np.array_equal(recent_values(y), 2 * np.arange(5))
def test_map_window_list_2(self): x = Stream() y = Stream() @map_wl def f(window, state): return [v + 10 * state for v in window], state + 1 f(x, y, window_size=2, step_size=2, state=0) x.extend(list(range(10))) run() assert recent_values(y) == [0, 1, 12, 13, 24, 25, 36, 37, 48, 49]