def post(self, filename=None): filename = secure_filename(filename) if not re.search(r'\.sf[23]$', filename, re.I): abort( 400, message='Invalid file extension, please use .sf2 or .sf3 files' ) filepath = os.path.join(settings.sound_dir, filename) overwrite = os.path.isfile(filepath) tmp = tempfile.NamedTemporaryFile(delete=False, dir=settings.upload_dir) try: while True: chunk = request.stream.read(16 * 1024 * 1024) if len(chunk) == 0: break tmp.write(chunk) tmp.seek(0) try: SoundFont().parse_file(tmp) except Exception as e: raise UploadError( 'Invalid file format, is this really a SoundFont?') tmp.close() if os.path.isfile(filepath): os.unlink(filepath) shutil.move(tmp.name, filepath) except UploadError as e: try: os.unlink(tmp.name) except Exception: pass abort(400, message=str(e)) if overwrite: with self.state.lock('Loading...', goto_home=True): signals.emit('sound:changed', {'id': filename}) else: signals.emit('sound:added', {'id': filename}) return SoundFont(filepath).as_dict()
def show_soundfonts(self): from mg.sf2 import SoundFont soundfonts = SoundFont.load_all() items = [(-1, None, None)] # only append midigurdy soundfonts if they contain at least one # sound of the current voice type for sf in [obj for obj in soundfonts if obj.mode != 'generic']: found = False for snd in sf.sounds: if snd.type == self.voice.type: found = True break if found: items.append((0, sf, None)) if not self.limit_to_type: for sf in [obj for obj in soundfonts if obj.mode == 'generic']: items.append((0, sf, None)) self.set_items(items) if self.sf_cursor == -1: cursor_pos = 0 for i, (_inum, sf, _sound) in enumerate(items): if (sf and sf.id == self.voice.soundfont_id): cursor_pos = i break else: cursor_pos = self.sf_cursor self.set_cursor(cursor_pos) self.sf_cursor = cursor_pos
def get(self, id): filepath = self.id_to_filepath(id) if not os.path.isfile(filepath): return abort(404) try: return SoundFont(filepath).as_dict() except Exception: abort(500, message='Unable to load sound')
def has_midigurdy_soundfont(self): from mg.sf2 import SoundFont if not self.soundfont_id: return True sf = SoundFont.by_id(self.soundfont_id) if not sf: return True return sf.mode == 'midigurdy'
def test_set_sound_on_voice(db, menu): sf = SoundFont.by_id('mg.sf2') sound = sf.get_sound(0, 1) voice = VoiceState('melody') voice.set_sound(sound) assert voice.soundfont_id == 'mg.sf2' assert voice.type == 'melody' assert voice.bank == 0 assert voice.program == 1 assert voice.get_sound() is not None
def from_dict(self, data, partial=False): if 'soundfont' in data and 'bank' in data and 'program' in data: sf = SoundFont.by_id(data['soundfont']) if data['soundfont'] else None sound = sf.get_sound(data['bank'], data['program']) if sf else None if sound: self.set_sound(sound) if self.type == 'keynoise': self.muted = False else: _set(self, 'muted', data, 'muted', True, partial) else: self.clear_sound() elif not partial: self.clear_sound() _set(self, 'volume', data, 'volume', 100, partial) _set(self, 'panning', data, 'panning', 64, partial) _set(self, 'base_note', data, 'note', 60, partial) _set(self, 'capo', data, 'capo', 0, partial) _set(self, 'polyphonic', data, 'polyphonic', False, partial) _set(self, 'mode', data, 'mode', 'midigurdy', partial) _set(self, 'finetune', data, 'finetune', 0, partial) _set(self, 'chien_threshold', data, 'chien_threshold', 50, partial)
def show(self, render=True, **kwargs): from mg.sf2 import SoundFont self.font_size = 3 self.win_len = 3 soundfonts = SoundFont.load_all() items = [(-1, None, None)] for sf in [obj for obj in soundfonts if obj.mode != 'generic']: sounds = [] for i, sound in enumerate(sf.sounds): if sound.type == self.voice.type: sounds.append((i + 1, sf, sound)) if sounds: items.append((0, sf, None)) items.extend(sounds) if not self.limit_to_type: for sf in [obj for obj in soundfonts if obj.mode == 'generic']: sounds = [] for i, sound in enumerate(sf.sounds): sounds.append((i + 1, sf, sound)) if sounds: items.append((0, sf, None)) items.extend(sounds) pos = 0 voice = self.voice for i, (_inum, sf, sound) in enumerate(items): if sound is None: continue if (sf.id == voice.soundfont_id and sound.bank == voice.bank and sound.program == voice.program): pos = i break self.set_items(items) self.set_cursor(pos) if render: self.render()
def get(self): return [sf2.as_dict() for sf2 in SoundFont.load_all()]
def get_sound(self): from mg.sf2 import SoundFont if self.soundfont_id: sf = SoundFont.by_id(self.soundfont_id) if sf: return sf.get_sound(self.bank, self.program)