File size: 3,644 Bytes
df5dab1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from . import main, utils
BM_SAMPLES = {'cowbell' : 'beat_manipulator/samples/cowbell.flac',
              }

presets = {}

def presets_load(path, mode = 'add'):
    global presets
    import yaml
    with open(path, 'r') as f:
        yaml_presets = yaml.safe_load(f.read())
    
    # if mode.lower() == 'add':
    #     presets = presets | yaml_presets
    # elif mode.lower() == 'replace':
        presets = yaml_presets

presets_load('beat_manipulator/presets.yaml')

def _beatswap(song, pattern, pattern_name, scale = 1, shift = 0, output = '', modify = False):
    if isinstance(scale, str):
        if ',' in scale: scale = scale.replace(' ', '').split(',')
    elif not isinstance(scale, list): scale = [scale]
    if modify is False:
        for i in scale:
            main.beatswap(song, pattern = pattern, scale = i, shift = shift, output=output, suffix = f' ({pattern_name}{(" x"+str(round(utils._safer_eval(i), 4))) * (len(scale)>1)})', copy = True)
    else:
        assert isinstance(song, main.song), f"In order to modify a song, it needs to be of a main.song type, but it is {type(song)}"
        song.beatswap(pattern, scale = scale[0], shift = shift)
        return song

def get(preset):
    """returns (pattern, scale, shift)"""
    global presets
    assert preset in presets, f"{preset} not found in presets."
    preset = presets[preset]
    return preset['pattern'], preset['scale'] if 'scale' in preset else 1, preset['shift'] if 'shift' in preset else 0

def use(song, preset, output = '', scale = 1, shift = 0):
    global presets
    assert preset in presets, f"{preset} not found in presets."
    preset_name = preset
    preset = presets[preset]
    if not isinstance(song, main.song): song = main.song(song)
    if isinstance(list(preset.values())[0], dict):
        for i in preset.values():
            if 'sample' in i:
                pass
            elif 'sidechain' in i:
                pass
            else:
                song = _beatswap(song, pattern = i['pattern'], scale = scale*(i['scale'] if 'scale' in i else 1), shift = shift*(i['shift'] if 'shift' in i else 0), output = output, modify = True, pattern_name = preset_name)
        song.write(output, suffix = f' ({preset})')
    else:
        if 'sample' in preset:
            pass
        elif 'sidechain' in preset:
            pass
        else:
            _beatswap(song, pattern = preset['pattern'], scale = scale*(preset['scale'] if 'scale' in preset else 1), shift = shift*(preset['shift'] if 'shift' in preset else 0), output = output, modify = False, pattern_name = preset_name)

def use_all(song, output = ''):
    if not isinstance(song, main.song): song = main.song(song)
    for key in presets.keys():
        print(f'__ {key} __')
        use(song, key, output = output)
        print()

def test(song, scale = 1, shift = 0, adjust = 0, output = '', load_settings = False):
    song = main.song(song)
    song.beatmap_generate(load_settings = load_settings)
    song.beatswap('test', scale = scale, shift = shift, adjust = 500+adjust)
    song.write(output = output, suffix = ' (test)')

def save(song, scale = 1, shift = 0, adjust = 0):
    song = main.song(song)
    song.beatmap_save_settings(scale = scale, shift = shift, adjust = adjust)

def savetest(song, scale = 1, shift = 0, adjust = 0, output = '', load_settings = False):
    song = main.song(song)
    song.beatmap_generate(load_settings = load_settings)
    song.beatswap('test', scale = scale, shift = shift, adjust = 500+adjust)
    song.write(output = output, suffix = ' (test)')
    song.beatmap_save_settings(scale = scale, shift = shift, adjust = adjust)