"""Music: looping pieces for a game's background. from libstormgames import sfx from libstormgames.sfx import music sg.play_bgm(sfx.make_music(music.battle_music())) The pieces: - battle_music: original heavy metal in D minor in the manner of a Bach keyboard concerto, a 24-second loop. - moonlight_metal: Beethoven's Moonlight Sonata sped up and made heavy, in C sharp minor, a 27-second loop. Each piece takes a seed like the other generators, which changes only the noise in the drums and the small differences between the two rhythm guitars, never the notes, and a tempo in beats a minute. Each piece lives in its own module in music.pieces, named after its function, and is loaded the first time it is asked for. To add a piece, add a module there with a function of the same name. The instruments the pieces share are in music.instruments. """ import importlib def __getattr__(name): """Load a piece the first time it is asked for, from the module of the same name in pieces.""" if name.startswith("_"): raise AttributeError(f"module {__name__!r} has no attribute {name!r}") moduleName = f"{__name__}.pieces.{name}" try: module = importlib.import_module(moduleName) except ModuleNotFoundError as error: if error.name != moduleName: raise raise AttributeError(f"module {__name__!r} has no attribute or piece {name!r}") from None piece = getattr(module, name) globals()[name] = piece return piece