def test(): from gum.lib.mock import Fake, Mock graph = Mock({"frames_info":(0, 0, [], []), "channels": [[(0, 0.5)]], "set_width": None, "scroll_left": None, "scroll_right": None}) graph.changed = Fake() selection = Mock({"pixels": [50, 100], "start_selection": None, "end_selection": None, "selected": True}) selection.changed = Fake() cursor = Mock({'pixel': 20}) cursor.changed = Fake() class FakeEditor(Fake): def __init__(self): self.filename_changed = Fake() self.error = Fake() notebook = EditorNotebook() win = EditorWindow(notebook) page = EditorPage(FakeEditor(), graph, selection, cursor) notebook.add_page(page) win.resize(700, 500) win.show_all() gtk.main() display_error("Title", "Text")
def test_Graph(): from gum.lib.mock import Mock, Fake import numpy sound = Mock({"numchan": 1}) sound.changed = Fake() sound.frames = numpy.array(list(range(1000)), DTYPE) c = Graph(sound) c.set_width(200) o = c.channels() class Foo: def foo(self): print("Changed.") f = Foo() c = Graph(sound) c.changed.connect(f.foo) c.set_width(200) o = c.channels() # stereo import numpy sound = Mock({"numchan": 2}) sound.changed = Fake() data = numpy.array([[1, 1], [2, 2], [3, 3]], DTYPE) sound.frames = data c = Graph(sound) o = c.channels() assert (len(o)) == 2
def test(): import time from gum.lib.mock import Fake class Empty: pass graph = Empty() graph.changed = Fake() graph.frmtopxl = lambda x: x player = Empty() player.start_playing = Fake() player.stop_playing = Fake() c = Cursor(graph, player) c.set_frame(5) assert c.pixel() == 5 player.position = 0 c._on_start_playing() time.sleep(0.2) assert c.pixel() == 0 c.set_frame(10) assert c.pixel() == 0 c._on_stop_playing() time.sleep(0.2) assert c.pixel() == 10
def test_Editor(): import gum from gum.lib.mock import Fake # Test opening a file editor = Editor(Fake(), Fake(), Fake(), Fake()) editor.open(gum.basedir + '/data/test/test1.wav') assert editor._sound != None
def test_selection_layer(layerclass): graph = Mock({"channels": [], "set_width": None, "frames_info": (0, 0, 0)}) graph.changed = Fake() selection = Mock({"pixels": (20, 100), "selected": True}) selection.changed = Fake() layered = LayeredGraphView(graph) layered.layers.append(layerclass(layered, selection)) return layered
def sine(): from math import sin sine = [sin(2 * 3.14 * 0.01 * x) for x in range(500)] channels = [[(i, i) for i in sine]] graph = Mock({"channels": channels, "set_width": None, "frames_info": (0, 0, 0)}) graph.changed = Fake() layered = LayeredGraphView(graph) layered.layers.append(WaveformLayer(layered, graph)) return layered
def randomized(): from random import random channels = [[((random() - 0.5) * 2, (random() - 0.5) * 2) for i in range(500)]] graph = Mock({"channels": channels, "set_width": None, "frames_info": (0, 0, 0)}) graph.changed = Fake() layered = LayeredGraphView(graph) layered.layers.append(WaveformLayer(layered, graph)) return layered
def test_middle(): from gum.lib.mock import Mock, Fake import numpy sound = Mock({"numchan": 1}) sound.changed = Fake() sound.frames = [] g = Graph(sound) for nframes, mid in [(4, 1.5), (9, 4), (10, 4.5)]: sound.frames = numpy.array(list(range(nframes))) g.set_sound(sound) assert g.middle() == mid
class FakeGraph(): density = 10 numframes = (lambda self: 5000) _view_start = 100.1 _view_end = 200.1 changed = Fake() def frmtopxl(self, f): return int(round(f - self._view_start) / self.density) def pxltofrm(self, p): return int(round(self._view_start + p * self.density))
def cursor(): graph = Mock({"channels": [], "set_width": None, "frames_info": (0, 0, 0)}) class Cursor: pass cursor = Cursor() cursor.changed = Fake() cursor.pixel = lambda: 50 layered = LayeredGraphView(graph) cursorlayer = CursorLayer(layered, cursor) cursorlayer.rgba = (1, 0, 0, 1) layered.layers.append(cursorlayer) return layered
def test_channels(): import numpy from gum.lib.mock import Mock, Fake sound = Mock({"numchan": 1}) sound.changed = Fake() sound.frames = numpy.array(list(range(1000000)), DTYPE) g = Graph(sound) for w in [1, 10, 11, 12, 13, 14, 15, 29, 54, 12.0, 347, 231., 1030]: g.set_width(w) c = g.channels() assert len(c[0]) == w, \ "expected: %d, got: %d, density: %f, last value: %s " % \ (w, len(c[0]), g.density, str(c[0][-1]))
def test_scroll(): import numpy from gum.lib.mock import Mock, Fake sound = Mock({}) data = numpy.array([1, 2, 3, 4]) sound.frames = data sound.changed = Fake() g = Graph(sound) g.set_width(4) g.scroll_right() length = g.numframes() start, end = g.view() assert length == 4 assert start == 0 assert end == 4
def test_zoom_in(): import numpy from gum.lib.mock import Mock, Fake sound = Mock({"numchan": 1}) sound.changed = Fake() data = numpy.array([1, 2, 3, 4], DTYPE) sound.frames = data g = Graph(sound) g.set_width(2) g.zoom_in() o = g.channels() assert o == [[(2, 2), (3, 3)]] g.zoom_out() g.set_width(4) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]]
def test_fix_selection(): from gum.lib.mock import Fake, Mock from gum.models import Selection import numpy # Undo graph = Mock({}) graph.changed = Fake() selection = Selection(graph, Fake()) sound = Sound() sound.frames = numpy.array(list(range(1000))) editor = Editor(sound, Fake(), Fake(), selection) frames = sound.frames selection.set(0, 999) editor.copy() editor.paste() selection.set(1500, 1500) editor.undo() editor.paste() editor.undo() assert sound.frames.tolist() == frames.tolist() # Redo graph = Mock({}) graph.changed = Fake() selection = Selection(graph, Fake()) sound = Sound() sound.frames = numpy.array(list(range(1000))) editor = Editor(sound, Fake(), Fake(), selection) frames = sound.frames selection.set(10, 999) editor.cut() editor.undo() selection.set(900, 900) editor.redo() frames = sound.frames editor.paste() editor.undo() assert sound.frames.tolist() == frames.tolist()
def test_selection(): from .graph import Graph from gum.lib.mock import Fake class FakeGraph(): density = 10 numframes = (lambda self: 5000) _view_start = 100.1 _view_end = 200.1 changed = Fake() def frmtopxl(self, f): return int(round(f - self._view_start) / self.density) def pxltofrm(self, p): return int(round(self._view_start + p * self.density)) x1, x2 = 10, 100 selection = Selection(FakeGraph(), Fake()) selection.pin(x1) selection.extend(x2) selection._update() assert selection.pixels() == (10, 100) assert selection.get() == (100 + 10 * x1, 100 + 10 * x2) # invert selection order selection.pin(x2) selection.extend(x1) selection._update() assert selection.pixels() == (10, 100) assert selection.get() == (100 + 10 * x1, 100 + 10 * x2) # select all selection.select_all() assert selection.get() == (0, 5000) assert selection.selected() == True # unselect selection.unselect() assert selection.selected() == False
def test_zoom(): from gum.lib.mock import Mock, Fake import numpy sound = Mock({"numchan": 1}) data = numpy.array([1, 2, 3, 4], DTYPE) sound.frames = data sound.changed = Fake() g = Graph(sound) g.set_width(4) g._zoom(1) g.center_on(1.5) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]] g._zoom(factor=1) g.center_on(0) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]] g._zoom(1) g.center_on(6) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]] g._zoom(factor=0.5) g.center_on(1.5) g.set_width(4) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]] g.set_width(2) g._zoom(0.5) g.center_on(0) o = g.channels() assert o == [[(1, 1), (2, 2)]] g.set_width(4) g._zoom(0.25) g.center_on(0) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]] g.set_width(4) g._zoom(4) g.center_on(4) o = g.channels() assert o == [[(1, 1), (2, 2), (3, 3), (4, 4)]], o g.set_width(100) data = numpy.array(list(range(3241))) sound.frames = data g.zoom_out_full() g._zoom(factor=0.5) g._zoom(factor=0.5) start, end = g.view() g.zoom_out_full() g._zoom(factor=0.5 * 0.5) assert (start, end) == g.view()
def __init__(self): self.filename_changed = Fake() self.error = Fake()