def test_delete_preset(client): Preset.create(name='p1') assert Preset.select().count() == 1 rv = client.delete('/api/presets/1') assert rv.status_code == 204 assert Preset.select().count() == 0
def load_prev_preset(self, evt, wrap=False): number = self.state.last_preset_number - 1 try: preset = Preset.get(Preset.number == number) except Preset.DoesNotExist: if not wrap: return try: preset = Preset.select().order_by(Preset.number.desc()).get() except Preset.DoesNotExist: return with self.menu.lock_state(f'Loading preset {preset.number}...'): self.state.load_preset(preset.id)
def test_get_preset(client): Preset.create(name='p1', data='{}') rv = client.get('/api/presets/1') expected = { 'id': 1, 'name': 'p1', 'number': 1, } assert rjson(rv) == expected assert rv.status_code == 200
def test_create_preset(client): assert len(Preset.select()) == 0 data = { 'name': 'p1', 'voices': { 'melody': [{ 'soundfont': 'test', 'bank': 0, 'program': 0, 'note': 60, 'polyphonic': False, 'muted': False, 'capo': 1, 'volume': 2, 'panning': 0, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }, { 'soundfont': 'test', 'bank': 0, 'program': 1, 'note': 61, 'polyphonic': False, 'muted': False, 'capo': 2, 'volume': 3, 'panning': 100, 'mode': 'keyboard', 'finetune': 0, 'chien_threshold': 50, }], 'drone': [], 'trompette': [], }, } rv = client.post('/api/presets', data=json.dumps(data), content_type='application/json') expected = dict(data) expected['id'] = 1 expected['number'] = 1 assert rv.status_code == 201 assert rjson(rv) == expected assert len(Preset.select(Preset.id == 1)) == 1
def load_preset(self, evt): preset_number = int(evt.value) try: preset = Preset.get(Preset.number == preset_number) except Preset.DoesNotExist: return with self.menu.lock_state(f'Loading preset {preset.number}...'): self.state.load_preset(preset.id)
def load_presets(self): presets = list(Preset.select()) cursor = None for idx, preset in enumerate(presets): if preset.id == self.preset.id: cursor = idx self.set_items(presets) self.cursor = cursor self.highlight = cursor self.update_window()
def test_load_preset(db, handler): p = Preset.create(name='P1') # load preset shows a loading page, so we need a home to return # back to... handler.menu.register_page('home', Page) assert handler.state.last_preset_number == 0 handler.load_preset(_evt('', '', p.number)) assert handler.state.last_preset_number == p.number
def test_order_presets(client): Preset.create(name='p1') Preset.create(name='p2') Preset.create(name='p3') order = [3, 1, 2] rv = client.post('/api/presets/order', data=json.dumps({'order': order}), content_type='application/json') assert rv.status_code == 200 assert rjson(rv) == {'order': order} assert Preset.get(Preset.id == 3).number == 1 assert Preset.get(Preset.id == 1).number == 2 assert Preset.get(Preset.id == 2).number == 3
def preload_presets(self): mgcore.halt_outputs() try: current_id = self.preset.id for preset in Preset.select().order_by(Preset.number): self.load_preset(preset.id, preload=True) self.load_preset(current_id) self.presets_preloaded = True except RuntimeError: raise finally: mgcore.resume_outputs()
def test_save_and_load_state_as_preset(db, menu): assert Preset.select().count() == 0 menu.state.clear() menu.state.preset.melody[0].soundfont_id = 'mg.sf2' menu.state.preset.melody[0].bank = 1 menu.state.preset.melody[0].prog = 2 menu.state.save_preset() assert Preset.select().count() == 1 preset = Preset.get() assert preset.name == 'Unnamed' assert preset.number == 1 menu.state.clear() menu.state.load_preset(preset.id) assert menu.state.preset.melody[0].soundfont_id == 'mg.sf2' assert menu.state.preset.melody[0].bank == 1 assert menu.state.preset.melody[0].prog == 2
def test_list_presets(client): p1 = Preset(name='p1') p1.set_data({ 'voices': { 'melody': [{ 'soundfont': 'test', }], 'drone': [], 'trompette': [], } }) p1.save() Preset.create(name='p2', data='{}') rv = client.get('/api/presets') assert rjson(rv) == [ { 'id': 1, 'name': 'p1', 'number': 1, 'voices': { 'melody': [{ 'soundfont': 'test', 'bank': 0, 'program': 0, 'note': 60, 'polyphonic': False, 'muted': True, 'capo': 0, 'volume': 127, 'panning': 64, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }], 'trompette': [], 'drone': [], } }, { 'id': 2, 'name': 'p2', 'number': 2, }, ] assert rv.status_code == 200
def test_delete_reorders_presets(client): p1 = Preset.create(name='p1') Preset.create(name='p2') p3 = Preset.create(name='p3') rv = client.delete('/api/presets/2') assert rv.status_code == 204 assert Preset.select().count() == 2 assert Preset.get(Preset.id == p1.id).number == 1 assert Preset.get(Preset.id == p3.id).number == 2
def test_load_preset(client): Preset.create(name='p1') Preset.create(name='p2') Preset.create(name='p3') rv = client.post('/api/presets/3/load') assert rv.status_code == 204
def post(self): data = request.get_json() if not data: abort(400, message='Please supply a preset!') errors = Preset.data_schema.validate(data) if errors: abort(400, errors=errors) preset = Preset(name=data.get('name')) preset.set_data(data) preset.save(force_insert=True) signals.emit('preset:added', {'id': preset.id}) return preset.to_dict(), 201
def load_preset(self, preset_id, preload=False): try: preset = Preset.get(Preset.id == int(preset_id)) except Preset.DoesNotExist: log.error('Preset {} not found!'.format(preset_id)) return with self.lock(): with signals.suppress(): self.from_preset_dict(preset.get_data()) self.preset.id = preset_id if preload: signals.emit('active:preset:preload') signals.emit('active:preset:changed') self.last_preset_number = preset.number
def save_preset(self, name=None, preset_id=None): if preset_id is not None: try: preset = Preset.get(Preset.id == int(preset_id)) except Preset.DoesNotExist: log.error('Preset {} not found!'.format(preset_id)) return if name is not None: preset.name = name else: preset = Preset() preset.name = name or 'Unnamed' preset.set_data(self.to_preset_dict()) preset.save() signals.emit('preset:changed', {'id': preset.id})
def test_create_preset_checks_max_voice_len(client): voice = { 'soundfont': 'test.sf2', 'bank': 0, 'program': 1, 'note': 60, 'polyphonic': False, 'muted': False, 'capo': 1, 'volume': 2, 'panning': 0, 'finetune': 0, 'chien_threshold': 50, } data = { 'name': 'p1', 'voices': { 'melody': [voice, voice, voice, voice], 'drone': [voice, voice, voice, voice], 'trompette': [voice, voice, voice, voice], }, } rv = client.post('/api/presets', data=json.dumps(data), content_type='application/json') expected = { 'errors': { 'voices': { 'trompette': ['Longer than maximum length 3.'], 'drone': ['Longer than maximum length 3.'], 'melody': ['Longer than maximum length 3.'] } } } assert rv.status_code == 400 assert rjson(rv) == expected assert Preset.select().count() == 0
def load_presets(self): presets = list(Preset.select()) presets.append(Preset(name='New Preset' + chr(127))) # placeholder for 'new preset' self.set_items(presets)
def select_item(self, item): order = [item.id for item in self.items] self.state.preset.last_preset_number = 0 Preset.reorder(order) signals.emit('preset:reordered', {'order': order}) self.menu.message('Preset moved', popup=True, timeout=1)
def test_update_preset(client): p = Preset(name='p1') p.set_data({ 'voices': { 'melody': [{ 'soundfont': 'test', }], 'drone': [{ 'soundfont': 'test', 'program': 1, 'note': 61, }], } }) p.save() assert Preset.select().count() == 1 data = { 'id': 1, 'name': 'p1-updated', 'voices': { 'melody': [{ 'soundfont': 'test', 'bank': 1, 'program': 0, 'note': 12, 'polyphonic': True, 'muted': True, 'capo': 12, 'volume': 20, 'panning': 0, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }, { 'soundfont': 'test', 'bank': 1, 'program': 1, 'note': 1, 'polyphonic': False, 'muted': True, 'capo': 6, 'volume': 100, 'panning': 0, 'mode': 'midigurdy', 'finetune': 0, 'chien_threshold': 50, }], 'drone': [], 'trompette': [], }, } rv = client.put('/api/presets/1', data=json.dumps(data), content_type='application/json') expected = data expected['number'] = 1 assert rjson(rv) == expected assert rv.status_code == 200 assert Preset.select().count() == 1
def get(self): return [preset.to_dict() for preset in Preset.select()]
def post(self): order = request.get_json()['order'] Preset.reorder(order) signals.emit('preset:reordered', {'order': order}) return {'order': [p.id for p in Preset.select(Preset.id)]}