hum 1.0.5__tar.gz → 1.0.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {hum-1.0.5 → hum-1.0.7}/PKG-INFO +4 -2
- {hum-1.0.5 → hum-1.0.7}/README.md +3 -1
- {hum-1.0.5 → hum-1.0.7}/hum/pyo_util.py +170 -5
- hum-1.0.7/hum/synth_funcs.py +97 -0
- hum-1.0.7/hum/util.py +295 -0
- {hum-1.0.5 → hum-1.0.7}/hum.egg-info/PKG-INFO +4 -2
- {hum-1.0.5 → hum-1.0.7}/hum.egg-info/SOURCES.txt +1 -0
- {hum-1.0.5 → hum-1.0.7}/setup.cfg +1 -1
- hum-1.0.5/hum/util.py +0 -131
- {hum-1.0.5 → hum-1.0.7}/LICENSE +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/__init__.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/examples/__init__.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/examples/keyboard_control.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/examples/pyo_util_examples.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/extra_util.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/pyo_synths.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/pyo_util_examples.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/scrap/__init__.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/scrap/belangeo_Degradation.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/utils/__init__.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/utils/date_ticks.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum/utils/plotting.py +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum.egg-info/dependency_links.txt +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum.egg-info/not-zip-safe +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum.egg-info/requires.txt +0 -0
- {hum-1.0.5 → hum-1.0.7}/hum.egg-info/top_level.txt +0 -0
- {hum-1.0.5 → hum-1.0.7}/setup.py +0 -0
{hum-1.0.5 → hum-1.0.7}/PKG-INFO
RENAMED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hum
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.7
|
|
4
4
|
Summary: A python synthesizer for creating and manipulating audio signals.
|
|
5
5
|
Home-page: https://github.com/thorwhalen/hum
|
|
6
6
|
License: mit
|
|
@@ -417,8 +417,10 @@ def complex_synth(freq=440, cutoff=2000, delay_time=0.25, num_voices=3):
|
|
|
417
417
|
|
|
418
418
|
# Add a filter
|
|
419
419
|
filt = MoogLP(harm, freq=cutoff, res=0.3)
|
|
420
|
+
|
|
421
|
+
# etc...
|
|
420
422
|
|
|
421
|
-
|
|
423
|
+
```
|
|
422
424
|
|
|
423
425
|
## Hooking a Synth to Event Streams
|
|
424
426
|
|
|
@@ -16,6 +16,7 @@ from typing import (
|
|
|
16
16
|
Set,
|
|
17
17
|
Tuple,
|
|
18
18
|
TypeVar,
|
|
19
|
+
Mapping,
|
|
19
20
|
runtime_checkable,
|
|
20
21
|
)
|
|
21
22
|
from collections.abc import MutableMapping
|
|
@@ -81,6 +82,112 @@ class PyoServer(Server):
|
|
|
81
82
|
self.shutdown()
|
|
82
83
|
|
|
83
84
|
|
|
85
|
+
_PLAY_REQUIRED = set()
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _build_play_required():
|
|
89
|
+
import pyo
|
|
90
|
+
|
|
91
|
+
global _PLAY_REQUIRED
|
|
92
|
+
|
|
93
|
+
for name, cls in inspect.getmembers(pyo, inspect.isclass):
|
|
94
|
+
if issubclass(cls, PyoObject) and cls.__module__.startswith("pyo"):
|
|
95
|
+
if "play" in cls.__dict__:
|
|
96
|
+
_PLAY_REQUIRED.add(cls.__name__)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
# You can call this manually if dynamic loading is desired
|
|
100
|
+
def refresh_play_required_list():
|
|
101
|
+
_build_play_required()
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# Fallback manual list
|
|
105
|
+
_MANUAL_FALLBACK = {
|
|
106
|
+
"Sine",
|
|
107
|
+
"Osc",
|
|
108
|
+
"Phasor",
|
|
109
|
+
"LFO",
|
|
110
|
+
"Adsr",
|
|
111
|
+
"Fader",
|
|
112
|
+
"Metro",
|
|
113
|
+
"Pattern",
|
|
114
|
+
"TrigEnv",
|
|
115
|
+
"Randi",
|
|
116
|
+
"Noise",
|
|
117
|
+
"Choice",
|
|
118
|
+
"Seq",
|
|
119
|
+
"TrigFunc",
|
|
120
|
+
"Counter",
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def must_be_played(obj_or_cls) -> bool:
|
|
125
|
+
"""
|
|
126
|
+
Returns True if the given pyo object or class needs `.play()` to produce sound.
|
|
127
|
+
|
|
128
|
+
>>> from pyo import Sine, Freeverb, Adsr
|
|
129
|
+
>>> must_be_played(Sine)
|
|
130
|
+
True
|
|
131
|
+
>>> must_be_played(Freeverb)
|
|
132
|
+
False
|
|
133
|
+
>>> must_be_played(Adsr)
|
|
134
|
+
True
|
|
135
|
+
"""
|
|
136
|
+
cls = obj_or_cls if isinstance(obj_or_cls, type) else obj_or_cls.__class__
|
|
137
|
+
name = cls.__name__
|
|
138
|
+
if not _PLAY_REQUIRED:
|
|
139
|
+
_build_play_required()
|
|
140
|
+
return name in _PLAY_REQUIRED or name in _MANUAL_FALLBACK
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
import ast
|
|
144
|
+
import inspect
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class PyoPlayAnalyzer(ast.NodeVisitor):
|
|
148
|
+
def __init__(self):
|
|
149
|
+
self.objs_created = {}
|
|
150
|
+
self.play_calls = set()
|
|
151
|
+
|
|
152
|
+
def visit_Assign(self, node):
|
|
153
|
+
# Detect PyoObject instance creation: foo = Sine(...) or foo = Adsr(...)
|
|
154
|
+
if isinstance(node.value, ast.Call) and isinstance(node.value.func, ast.Name):
|
|
155
|
+
class_name = node.value.func.id
|
|
156
|
+
if class_name in _MANUAL_FALLBACK:
|
|
157
|
+
for target in node.targets:
|
|
158
|
+
if isinstance(target, ast.Name):
|
|
159
|
+
self.objs_created[target.id] = class_name
|
|
160
|
+
self.generic_visit(node)
|
|
161
|
+
|
|
162
|
+
def visit_Expr(self, node):
|
|
163
|
+
# Detect .play() calls like: foo.play()
|
|
164
|
+
if isinstance(node.value, ast.Call) and isinstance(
|
|
165
|
+
node.value.func, ast.Attribute
|
|
166
|
+
):
|
|
167
|
+
if node.value.func.attr == "play":
|
|
168
|
+
if isinstance(node.value.func.value, ast.Name):
|
|
169
|
+
self.play_calls.add(node.value.func.value.id)
|
|
170
|
+
self.generic_visit(node)
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def analyze_synth_func_play(func):
|
|
174
|
+
src = inspect.getsource(func)
|
|
175
|
+
tree = ast.parse(src)
|
|
176
|
+
analyzer = PyoPlayAnalyzer()
|
|
177
|
+
analyzer.visit(tree)
|
|
178
|
+
|
|
179
|
+
# Build the report
|
|
180
|
+
report = {}
|
|
181
|
+
for var, cls in analyzer.objs_created.items():
|
|
182
|
+
report[var] = {
|
|
183
|
+
"class": cls,
|
|
184
|
+
"must_be_played": True,
|
|
185
|
+
"play_called": var in analyzer.play_calls,
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return report
|
|
189
|
+
|
|
190
|
+
|
|
84
191
|
def round_event_times(events, round_to=0.001):
|
|
85
192
|
"""
|
|
86
193
|
Round the times (d[0]) of events ([d, ...]) to the nearest multiple of round_to.
|
|
@@ -255,6 +362,37 @@ class Knobs(MutableMapping):
|
|
|
255
362
|
# -------------------------------------------------------------------------------------
|
|
256
363
|
|
|
257
364
|
|
|
365
|
+
def add_default_dials(dials: Union[str, List[str]]):
|
|
366
|
+
def decorator(func):
|
|
367
|
+
func._default_dials = set(ensure_identifier_list(dials))
|
|
368
|
+
return func
|
|
369
|
+
|
|
370
|
+
return decorator
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def add_default_settings(settings: Union[str, List[str]]):
|
|
374
|
+
def decorator(func):
|
|
375
|
+
func._default_settings = set(ensure_identifier_list(settings))
|
|
376
|
+
return func
|
|
377
|
+
|
|
378
|
+
return decorator
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def add_synth_defaults(dials=None, settings=None):
|
|
382
|
+
"""
|
|
383
|
+
Decorator to add default dials and settings to a synth function.
|
|
384
|
+
"""
|
|
385
|
+
|
|
386
|
+
def decorator(func):
|
|
387
|
+
if dials is not None:
|
|
388
|
+
func._default_dials = set(ensure_identifier_list(dials))
|
|
389
|
+
if settings is not None:
|
|
390
|
+
func._default_settings = set(ensure_identifier_list(settings))
|
|
391
|
+
return func
|
|
392
|
+
|
|
393
|
+
return decorator
|
|
394
|
+
|
|
395
|
+
|
|
258
396
|
# TODO: Deprecate if params_dict deprecates
|
|
259
397
|
def is_valid_sigto_value(value):
|
|
260
398
|
"""
|
|
@@ -538,10 +676,12 @@ class Synth(MutableMapping):
|
|
|
538
676
|
settings: Optional[Set[str]] = None,
|
|
539
677
|
sr=DFLT_PYO_SR,
|
|
540
678
|
nchnls=DFLT_PYO_NCHNLS,
|
|
679
|
+
value_trans: Optional[Dict[str, Callable]] = None,
|
|
541
680
|
record_on_start: bool = True,
|
|
542
681
|
event_log_factory: RecordFactory = list, # No argument factory that makes an Appendable
|
|
543
682
|
audio="portaudio",
|
|
544
683
|
verbosity=DFLT_PYO_VERBOSITY,
|
|
684
|
+
synth_func_kwargs=None,
|
|
545
685
|
**server_kwargs,
|
|
546
686
|
):
|
|
547
687
|
"""
|
|
@@ -574,11 +714,27 @@ class Synth(MutableMapping):
|
|
|
574
714
|
self._server_kwargs = dict(
|
|
575
715
|
server_kwargs, sr=sr, nchnls=nchnls, verbosity=verbosity, audio=audio
|
|
576
716
|
)
|
|
717
|
+
if synth_func_kwargs:
|
|
718
|
+
synth_func_attributes = synth_func.__dict__.copy()
|
|
719
|
+
synth_func = partial(synth_func, **synth_func_kwargs)
|
|
720
|
+
synth_func.__dict__.update(synth_func_attributes)
|
|
721
|
+
self._synth_func_kwargs = synth_func_kwargs
|
|
577
722
|
self._synth_func = synth_func
|
|
578
723
|
self._server = None
|
|
579
724
|
self.output = None
|
|
580
725
|
self._synth_func_params = synth_func_defaults(synth_func)
|
|
581
726
|
|
|
727
|
+
assert value_trans is None or (
|
|
728
|
+
isinstance(value_trans, Mapping)
|
|
729
|
+
and all(map(callable, value_trans.values()))
|
|
730
|
+
), f"value_trans must be a dict of callables. Was: {value_trans}"
|
|
731
|
+
self._has_value_trans = value_trans is not None
|
|
732
|
+
self._value_trans = value_trans
|
|
733
|
+
|
|
734
|
+
# if function has some default dials or settings, use them if not specified in Synth arguments
|
|
735
|
+
dials = dials or getattr(synth_func, "_default_dials", None)
|
|
736
|
+
settings = settings or getattr(synth_func, "_default_settings", None)
|
|
737
|
+
|
|
582
738
|
self._dials, self._settings = _resolve_dials_and_settings(
|
|
583
739
|
dials, settings, self._synth_func_params
|
|
584
740
|
)
|
|
@@ -613,6 +769,8 @@ class Synth(MutableMapping):
|
|
|
613
769
|
rebuild_updates = {}
|
|
614
770
|
|
|
615
771
|
for k, v in updates.items():
|
|
772
|
+
if self._has_value_trans and k in self._value_trans:
|
|
773
|
+
v = self._value_trans[k](v)
|
|
616
774
|
if k in self._dials:
|
|
617
775
|
live_updates[k] = v
|
|
618
776
|
elif k in self._settings:
|
|
@@ -704,7 +862,14 @@ class Synth(MutableMapping):
|
|
|
704
862
|
_initial_knob_params = {}
|
|
705
863
|
for name, spec in self._synth_func_params.items():
|
|
706
864
|
if name in self._dials:
|
|
707
|
-
|
|
865
|
+
try:
|
|
866
|
+
_initial_knob_params[name] = dict_to_sigto(spec)
|
|
867
|
+
except Exception as e:
|
|
868
|
+
raise type(e)(
|
|
869
|
+
f"Invalid initial value for knob '{name}': {spec}. "
|
|
870
|
+
"Expected a number or a dictionary with 'value', 'time', 'mul', and/or 'add' keys. "
|
|
871
|
+
f"Original error: {e}"
|
|
872
|
+
)
|
|
708
873
|
else:
|
|
709
874
|
_initial_knob_params[name] = spec
|
|
710
875
|
|
|
@@ -791,11 +956,11 @@ class Synth(MutableMapping):
|
|
|
791
956
|
f"Failed to initialize synth function '{self._synth_func.__name__}'.\n"
|
|
792
957
|
f"Perhaps some arguments were wrongly wrapped into SigTo.\n"
|
|
793
958
|
f"Consider :\n"
|
|
794
|
-
f" * using the
|
|
795
|
-
f" * using the
|
|
959
|
+
f" * using the dials argument to control which parameters are live.\n"
|
|
960
|
+
f" * using the settings argument to control which parameters are not live.\n"
|
|
796
961
|
f"Alternatively, at function definition time, you can control this by:\n"
|
|
797
|
-
f" * using the @
|
|
798
|
-
f" * using the @
|
|
962
|
+
f" * using the @add_default_dials decorator to control which parameters are live.\n"
|
|
963
|
+
f" * using the @add_default_settings decorator to control which parameters are not live.\n"
|
|
799
964
|
f"Original error: {e}"
|
|
800
965
|
) from e
|
|
801
966
|
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"""Synth funcs"""
|
|
2
|
+
|
|
3
|
+
from pyo import *
|
|
4
|
+
from hum.pyo_util import add_synth_defaults, add_default_dials, add_default_settings
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def sine(freq=440, volume=0.3):
|
|
8
|
+
return Sine(freq=freq, mul=volume)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def pulse(freq=440, duty=0.5, volume=0.3):
|
|
12
|
+
return LFO(freq=freq, sharp=duty, type=2, mul=volume)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@add_synth_defaults(settings="type")
|
|
16
|
+
def noise(cutoff=1000, q=1, volume=0.2, type=0):
|
|
17
|
+
noise = Noise(mul=volume)
|
|
18
|
+
return Biquadx(noise, freq=cutoff, q=q, type=type, stages=2)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@add_synth_defaults(dials="freq")
|
|
22
|
+
def simple_waveforms(freq=440, attack=0.01, waveform="sine"):
|
|
23
|
+
env = Adsr(
|
|
24
|
+
attack=attack, decay=0.1, sustain=0.8, release=0.1, dur=0, mul=0.5
|
|
25
|
+
).play()
|
|
26
|
+
wave = {
|
|
27
|
+
"sine": Sine,
|
|
28
|
+
"triangle": lambda freq, mul: LFO(freq=freq, type=3, mul=mul),
|
|
29
|
+
"square": lambda freq, mul: LFO(freq=freq, type=1, mul=mul),
|
|
30
|
+
}.get(waveform, Sine)
|
|
31
|
+
return wave(freq=freq, mul=env)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@add_default_dials(["freq", "cutoff", "reso", "decay", "mult"])
|
|
35
|
+
@add_default_settings(["shape"])
|
|
36
|
+
def ankg_fm303(freq=110, cutoff=2000, reso=5, decay=0.5, mult=0.1, shape=1):
|
|
37
|
+
decay_val = decay.value if isinstance(decay, SigTo) else decay
|
|
38
|
+
mult_val = mult.value if isinstance(mult, SigTo) else mult
|
|
39
|
+
freq_val = freq.value if isinstance(freq, SigTo) else freq
|
|
40
|
+
|
|
41
|
+
env = Adsr(
|
|
42
|
+
attack=0.05, decay=decay_val, sustain=0.1, release=0.05, dur=0.2 + decay_val
|
|
43
|
+
)
|
|
44
|
+
env.play() # This line is crucial
|
|
45
|
+
|
|
46
|
+
env2 = env * mult_val
|
|
47
|
+
env2.play()
|
|
48
|
+
wave1 = Phasor(freq=freq_val, phase=0, mul=1)
|
|
49
|
+
wave2 = Phasor(freq=-freq_val, phase=0.5, mul=1)
|
|
50
|
+
|
|
51
|
+
if shape == 0:
|
|
52
|
+
wave = Phasor(freq=freq_val, phase=0, mul=env2)
|
|
53
|
+
else:
|
|
54
|
+
wave = ((wave1 + wave2) - 1) * env2
|
|
55
|
+
|
|
56
|
+
filt = Biquadx([wave, wave], freq=cutoff, q=reso, type=0, stages=2)
|
|
57
|
+
return filt
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
@add_synth_defaults(dials="IpulseHz")
|
|
61
|
+
def intro_crunch_pulsar(IpulseHz=110):
|
|
62
|
+
wavPas = DataTable(size=256) # default dummy waveform
|
|
63
|
+
envPulse = LinTable([(0, 1), (8191, 1)])
|
|
64
|
+
pas = Pulsar(
|
|
65
|
+
table=wavPas,
|
|
66
|
+
env=envPulse,
|
|
67
|
+
freq=IpulseHz,
|
|
68
|
+
frac=[IpulseHz * 0.5, IpulseHz * 0.48],
|
|
69
|
+
interp=4,
|
|
70
|
+
mul=1.25,
|
|
71
|
+
)
|
|
72
|
+
rezons = Waveguide(pas, freq=[32.7032, 65.4064, 98.1, 81.758], dur=30, mul=0.1)
|
|
73
|
+
rezfilt = Tone(rezons, freq=1500)
|
|
74
|
+
rev = WGVerb(pas, feedback=0.99, cutoff=10000, bal=0.25, mul=0.75)
|
|
75
|
+
return rezfilt + rev
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@add_synth_defaults(dials=["base_pitch", "spread"])
|
|
79
|
+
def intro_buzz_pulsar(base_pitch=60, spread=0.007):
|
|
80
|
+
wav = HarmTable([0.1, 0, 0.2, 0, 0.1, 0, 0, 0, 0.04, 0, 0, 0, 0.02])
|
|
81
|
+
env = HannTable()
|
|
82
|
+
fade = Fader(fadein=4, fadeout=60, mul=1)
|
|
83
|
+
pitches = [midiToHz(base_pitch + i * spread) for i in range(6)]
|
|
84
|
+
buzz = Pulsar(table=wav, env=env, freq=pitches, frac=0.7, mul=fade * 0.05)
|
|
85
|
+
mix = Mix(buzz, voices=2)
|
|
86
|
+
rev = Freeverb(mix, size=1.0, damp=0.25, bal=0.75)
|
|
87
|
+
return rev
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def intro_high_sines(base_freq=4000, mod_freq=0.4, mod_mul=0.005):
|
|
91
|
+
wav = HarmTable([0.1, 0, 0.2, 0, 0.1, 0, 0, 0, 0.04, 0, 0, 0, 0.02])
|
|
92
|
+
fade = Fader(fadein=3, fadeout=20, mul=mod_mul)
|
|
93
|
+
fade.play()
|
|
94
|
+
mod = Osc(table=wav, freq=mod_freq)
|
|
95
|
+
car = Sine(freq=[base_freq, base_freq + 40, base_freq - 10], mul=mod)
|
|
96
|
+
pan = SPan(car, pan=[0, 0.5, 1], mul=fade)
|
|
97
|
+
return pan
|
hum-1.0.7/hum/util.py
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
"""
|
|
2
|
+
A few general utils
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from warnings import warn
|
|
6
|
+
from inspect import getmodule
|
|
7
|
+
from typing import Iterable, Iterator, Tuple, TypeVar, Callable, Iterable, Union
|
|
8
|
+
from itertools import zip_longest
|
|
9
|
+
from functools import partial
|
|
10
|
+
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
T = TypeVar("T")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def simple_chunker(
|
|
18
|
+
a: Iterable[T], chk_size: int, *, include_tail: bool = True
|
|
19
|
+
) -> Iterator[Tuple[T, ...]]:
|
|
20
|
+
"""
|
|
21
|
+
Chunks an iterable into non-overlapping chunks of size `chk_size`.
|
|
22
|
+
|
|
23
|
+
Note: This chunker is simpler, but also less efficient than `chunk_iterable`.
|
|
24
|
+
It does have the extra `include_tail` argument, though.
|
|
25
|
+
Though note that you can get the effect of `include_tail=False` in `chunk_iterable`
|
|
26
|
+
by using `filter(lambda x: len(x) == chk_size, chunk_iterable(...))`.
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
a: The iterable to be chunked.
|
|
30
|
+
chk_size: The size of each chunk.
|
|
31
|
+
include_tail: If True, includes the remaining elements as the last chunk
|
|
32
|
+
even if they are fewer than `chk_size`. Defaults to True.
|
|
33
|
+
|
|
34
|
+
Returns:
|
|
35
|
+
An iterator of tuples, where each tuple is a chunk of size `chk_size`
|
|
36
|
+
(or fewer elements if `include_tail` is True).
|
|
37
|
+
|
|
38
|
+
Examples:
|
|
39
|
+
>>> list(simple_chunker(range(8), 3))
|
|
40
|
+
[(0, 1, 2), (3, 4, 5), (6, 7)]
|
|
41
|
+
>>> list(simple_chunker(range(8), 3, include_tail=False))
|
|
42
|
+
[(0, 1, 2), (3, 4, 5)]
|
|
43
|
+
"""
|
|
44
|
+
it = iter(a)
|
|
45
|
+
if include_tail:
|
|
46
|
+
sentinel = object()
|
|
47
|
+
for chunk in zip_longest(*([it] * chk_size), fillvalue=sentinel):
|
|
48
|
+
yield tuple(item for item in chunk if item is not sentinel)
|
|
49
|
+
else:
|
|
50
|
+
yield from zip(*([it] * chk_size))
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def round_number(number, round_to=0.001):
|
|
54
|
+
"""
|
|
55
|
+
Round a number to the nearest multiple of round_to.
|
|
56
|
+
|
|
57
|
+
Parameters:
|
|
58
|
+
- number: float, the number to round.
|
|
59
|
+
- round_to: float, the value to round to.
|
|
60
|
+
|
|
61
|
+
Returns:
|
|
62
|
+
- float, the rounded number.
|
|
63
|
+
|
|
64
|
+
>>> round_number(1.234567, 0.01)
|
|
65
|
+
1.23
|
|
66
|
+
|
|
67
|
+
Note, the rounder rounds to nearest multiple of round_to, not to the nearest decimal place.
|
|
68
|
+
|
|
69
|
+
>>> round_number(1.234567, 0.001)
|
|
70
|
+
1.235
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
"""
|
|
74
|
+
digits = len(str(round_to).split(".")[-1])
|
|
75
|
+
return round(round(number / round_to) * round_to, digits)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def round_numbers(items, round_to=0.001, *, index_of_item_number=None, egress=None):
|
|
79
|
+
"""
|
|
80
|
+
Round numbers in an iterable, optionally extracting the number to round from an index.
|
|
81
|
+
|
|
82
|
+
Parameters:
|
|
83
|
+
- items: iterable of numbers or iterable of tuples/lists containing numbers.
|
|
84
|
+
- round_to: float, round to the nearest multiple of this value.
|
|
85
|
+
- index_of_item_number: int or None. If None, round the item directly;
|
|
86
|
+
otherwise, round the item at this index in each iterable element.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
- generator yielding items with rounded numbers.
|
|
90
|
+
|
|
91
|
+
Examples:
|
|
92
|
+
>>> list(round_numbers([1.23, 3.14159], round_to=0.1))
|
|
93
|
+
[1.2, 3.1]
|
|
94
|
+
|
|
95
|
+
>>> items = [(1.234, 'one'), (3.14159, 'three')]
|
|
96
|
+
>>> list(round_numbers(items, round_to=0.1, index_of_item_number=0))
|
|
97
|
+
[[1.2, 'one'], [3.1, 'three']]
|
|
98
|
+
"""
|
|
99
|
+
digits = len(str(round_to).split(".")[-1])
|
|
100
|
+
|
|
101
|
+
for item in items:
|
|
102
|
+
if index_of_item_number is None:
|
|
103
|
+
yield round(round(item / round_to) * round_to, digits)
|
|
104
|
+
else:
|
|
105
|
+
val = item[index_of_item_number]
|
|
106
|
+
rounded_val = round(round(val / round_to) * round_to, digits)
|
|
107
|
+
item = list(item)
|
|
108
|
+
item[index_of_item_number] = rounded_val
|
|
109
|
+
|
|
110
|
+
if egress is None:
|
|
111
|
+
yield item
|
|
112
|
+
else:
|
|
113
|
+
yield egress(item)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
from bisect import bisect_left
|
|
117
|
+
from math import log2
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class ListSnapper:
|
|
121
|
+
"""
|
|
122
|
+
Snap to the nearest value in a list.
|
|
123
|
+
|
|
124
|
+
>>> snapper = ListSnapper([0, 1, 2, 3, 4, 5])
|
|
125
|
+
>>> snapper(2.3)
|
|
126
|
+
2
|
|
127
|
+
>>> snapper(2.7)
|
|
128
|
+
3
|
|
129
|
+
>>> snapper(2.5) # exactly the middle of 2 and 3; snaps to 2
|
|
130
|
+
2
|
|
131
|
+
|
|
132
|
+
"""
|
|
133
|
+
|
|
134
|
+
def __init__(self, snap_values):
|
|
135
|
+
self.snap_values = sorted(snap_values)
|
|
136
|
+
|
|
137
|
+
def __call__(self, x):
|
|
138
|
+
pos = bisect_left(self.snap_values, x)
|
|
139
|
+
if pos == 0:
|
|
140
|
+
return self.snap_values[0]
|
|
141
|
+
if pos == len(self.snap_values):
|
|
142
|
+
return self.snap_values[-1]
|
|
143
|
+
before = self.snap_values[pos - 1]
|
|
144
|
+
after = self.snap_values[pos]
|
|
145
|
+
return before if abs(x - before) <= abs(after - x) else after
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
class SnapToChromatic:
|
|
149
|
+
"""
|
|
150
|
+
Snap to the nearest chromatic note in a tempered scale.
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
def __init__(self, base_tuning=440):
|
|
154
|
+
self.base_tuning = base_tuning
|
|
155
|
+
|
|
156
|
+
def __call__(self, x):
|
|
157
|
+
if x <= 0:
|
|
158
|
+
raise ValueError("Frequency must be positive")
|
|
159
|
+
midi_note = round(69 + 12 * log2(x / 440))
|
|
160
|
+
return 440 * 2 ** ((midi_note - 69) / 12)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
DFLT_OCTAVE_RANGE = (0, 10)
|
|
164
|
+
DFLT_TUNING = 440.0 # A4 tuning frequency
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def tempered_semitone_frequencies(
|
|
168
|
+
tuning=DFLT_TUNING, octave_range=DFLT_OCTAVE_RANGE, digits=5
|
|
169
|
+
):
|
|
170
|
+
"""
|
|
171
|
+
Get a sorted list of frequencies for a given tuning and octave range.
|
|
172
|
+
|
|
173
|
+
:param tuning: The tuning frequency (default is 440 Hz).
|
|
174
|
+
It corresponds to A4 (that is, the 69th MIDI note).
|
|
175
|
+
:param octave_range: A tuple of two integers representing the range of octaves.
|
|
176
|
+
|
|
177
|
+
The first integer is the starting octave (inclusive), and the second integer is the ending octave (exclusive).
|
|
178
|
+
For example, (0, 10) will generate frequencies from C0 to B9.
|
|
179
|
+
The default is (0, 10), which covers a wide range of frequencies.
|
|
180
|
+
:return: A sorted list of frequencies in the specified octave range. The first (i.e. .[0]) frequency will correspond to a C.
|
|
181
|
+
|
|
182
|
+
>>> frequencies = tempered_semitone_frequencies()
|
|
183
|
+
>>> len(frequencies)
|
|
184
|
+
120
|
|
185
|
+
>>> frequencies # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
|
|
186
|
+
[8.1758, 8.66196, 9.17702, 9.72272, ..., 7040.0, 7458.62018, 7902.13282]
|
|
187
|
+
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
assert len(octave_range) == 2, "octave_range must be a tuple of two integers"
|
|
191
|
+
n_octave_divisions = 12
|
|
192
|
+
|
|
193
|
+
def gen_freqs():
|
|
194
|
+
for octave in range(*octave_range):
|
|
195
|
+
base_midi = n_octave_divisions * octave
|
|
196
|
+
for st in range(n_octave_divisions):
|
|
197
|
+
midi_note = base_midi + st
|
|
198
|
+
freq = tuning * 2 ** ((midi_note - 69) / n_octave_divisions)
|
|
199
|
+
yield round(freq, digits)
|
|
200
|
+
|
|
201
|
+
return sorted(gen_freqs())
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
# TODO: Add string_to_scale_map argument to convert string to midi note array scale
|
|
205
|
+
def scale_snapper(
|
|
206
|
+
scale: Iterable[Union[float, int]] = (0, 2, 4, 5, 7, 9, 11),
|
|
207
|
+
*,
|
|
208
|
+
tuning=DFLT_TUNING,
|
|
209
|
+
octave_range=DFLT_OCTAVE_RANGE,
|
|
210
|
+
):
|
|
211
|
+
"""
|
|
212
|
+
Snap frequency to the nearest midi note of the midi_nodes list, modulo 12.
|
|
213
|
+
|
|
214
|
+
:param scale: A list of integers representing the scale.
|
|
215
|
+
For example, (0, 2, 4, 5, 7, 9, 11) (the default) corresponds to the C major scale.
|
|
216
|
+
:param tuning: The tuning frequency (default is 440 Hz).
|
|
217
|
+
It corresponds to A4 (that is, the 69th MIDI note).
|
|
218
|
+
:param octave_range: A tuple of two integers representing the range of octaves.
|
|
219
|
+
The first integer is the starting octave (inclusive), and the second integer is the ending octave (exclusive).
|
|
220
|
+
For example, (0, 10) will generate frequencies from C0 to B9.
|
|
221
|
+
The default is (0, 10), which covers a wide range of frequencies.
|
|
222
|
+
|
|
223
|
+
:return: A ListSnapper object that can be used to snap frequencies to the nearest note in the scale.
|
|
224
|
+
|
|
225
|
+
>>> snapper = scale_snapper()
|
|
226
|
+
>>> snapper(440.0) # A4
|
|
227
|
+
440.0
|
|
228
|
+
|
|
229
|
+
If you're near that A4, `snapper` will snap to it:
|
|
230
|
+
|
|
231
|
+
>>> snapper(441.0)
|
|
232
|
+
440.0
|
|
233
|
+
>>> snapper(439.0)
|
|
234
|
+
440.0
|
|
235
|
+
|
|
236
|
+
But even if you're, say at A#, you'll still snap to A4 (since it's the closest note
|
|
237
|
+
of C major scale):
|
|
238
|
+
|
|
239
|
+
>>> snapper(466.16) # A#4
|
|
240
|
+
440.0
|
|
241
|
+
|
|
242
|
+
If you are slightly higher than A#, you'll snap to B4
|
|
243
|
+
|
|
244
|
+
>>> snapper(467) # doctest: +ELLIPSIS
|
|
245
|
+
493.8833...
|
|
246
|
+
|
|
247
|
+
"""
|
|
248
|
+
chromatic_scale = tempered_semitone_frequencies(
|
|
249
|
+
tuning=tuning, octave_range=octave_range
|
|
250
|
+
)
|
|
251
|
+
# keep all elements of chromatic_scale that have index in midi_notes module 12
|
|
252
|
+
scale_frequencies = [
|
|
253
|
+
chromatic_scale[i] for i in range(len(chromatic_scale)) if i % 12 in scale
|
|
254
|
+
]
|
|
255
|
+
# create a ListSnapper object
|
|
256
|
+
return ListSnapper(scale_frequencies)
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def getmodulename(obj, default=""):
|
|
260
|
+
"""Get name of module of object"""
|
|
261
|
+
return getattr(getmodule(obj), "__name__", default)
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
class ModuleNotFoundErrorNiceMessage:
|
|
265
|
+
def __init__(self, msg=None):
|
|
266
|
+
self.msg = msg
|
|
267
|
+
|
|
268
|
+
def __enter__(self):
|
|
269
|
+
pass
|
|
270
|
+
|
|
271
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
272
|
+
if exc_type is ModuleNotFoundError:
|
|
273
|
+
msg = (
|
|
274
|
+
self.msg
|
|
275
|
+
or f"""
|
|
276
|
+
It seems you don't have required `{exc_val.name}` package for this Store.
|
|
277
|
+
Try installing it by running:
|
|
278
|
+
|
|
279
|
+
pip install {exc_val.name}
|
|
280
|
+
|
|
281
|
+
in your terminal.
|
|
282
|
+
For more information: https://pypi.org/project/{exc_val.name}
|
|
283
|
+
"""
|
|
284
|
+
)
|
|
285
|
+
warn(msg)
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
class ModuleNotFoundIgnore:
|
|
289
|
+
def __enter__(self):
|
|
290
|
+
pass
|
|
291
|
+
|
|
292
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
293
|
+
if exc_type is ModuleNotFoundError:
|
|
294
|
+
pass
|
|
295
|
+
return True
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hum
|
|
3
|
-
Version: 1.0.
|
|
3
|
+
Version: 1.0.7
|
|
4
4
|
Summary: A python synthesizer for creating and manipulating audio signals.
|
|
5
5
|
Home-page: https://github.com/thorwhalen/hum
|
|
6
6
|
License: mit
|
|
@@ -417,8 +417,10 @@ def complex_synth(freq=440, cutoff=2000, delay_time=0.25, num_voices=3):
|
|
|
417
417
|
|
|
418
418
|
# Add a filter
|
|
419
419
|
filt = MoogLP(harm, freq=cutoff, res=0.3)
|
|
420
|
+
|
|
421
|
+
# etc...
|
|
420
422
|
|
|
421
|
-
|
|
423
|
+
```
|
|
422
424
|
|
|
423
425
|
## Hooking a Synth to Event Streams
|
|
424
426
|
|
hum-1.0.5/hum/util.py
DELETED
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
A few general utils
|
|
3
|
-
"""
|
|
4
|
-
|
|
5
|
-
from warnings import warn
|
|
6
|
-
from inspect import getmodule
|
|
7
|
-
from typing import Iterable, Iterator, Tuple, TypeVar, Callable
|
|
8
|
-
from itertools import zip_longest
|
|
9
|
-
from functools import partial
|
|
10
|
-
|
|
11
|
-
import numpy as np
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
T = TypeVar("T")
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
def simple_chunker(
|
|
18
|
-
a: Iterable[T], chk_size: int, *, include_tail: bool = True
|
|
19
|
-
) -> Iterator[Tuple[T, ...]]:
|
|
20
|
-
"""
|
|
21
|
-
Chunks an iterable into non-overlapping chunks of size `chk_size`.
|
|
22
|
-
|
|
23
|
-
Note: This chunker is simpler, but also less efficient than `chunk_iterable`.
|
|
24
|
-
It does have the extra `include_tail` argument, though.
|
|
25
|
-
Though note that you can get the effect of `include_tail=False` in `chunk_iterable`
|
|
26
|
-
by using `filter(lambda x: len(x) == chk_size, chunk_iterable(...))`.
|
|
27
|
-
|
|
28
|
-
Args:
|
|
29
|
-
a: The iterable to be chunked.
|
|
30
|
-
chk_size: The size of each chunk.
|
|
31
|
-
include_tail: If True, includes the remaining elements as the last chunk
|
|
32
|
-
even if they are fewer than `chk_size`. Defaults to True.
|
|
33
|
-
|
|
34
|
-
Returns:
|
|
35
|
-
An iterator of tuples, where each tuple is a chunk of size `chk_size`
|
|
36
|
-
(or fewer elements if `include_tail` is True).
|
|
37
|
-
|
|
38
|
-
Examples:
|
|
39
|
-
>>> list(simple_chunker(range(8), 3))
|
|
40
|
-
[(0, 1, 2), (3, 4, 5), (6, 7)]
|
|
41
|
-
>>> list(simple_chunker(range(8), 3, include_tail=False))
|
|
42
|
-
[(0, 1, 2), (3, 4, 5)]
|
|
43
|
-
"""
|
|
44
|
-
it = iter(a)
|
|
45
|
-
if include_tail:
|
|
46
|
-
sentinel = object()
|
|
47
|
-
for chunk in zip_longest(*([it] * chk_size), fillvalue=sentinel):
|
|
48
|
-
yield tuple(item for item in chunk if item is not sentinel)
|
|
49
|
-
else:
|
|
50
|
-
yield from zip(*([it] * chk_size))
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
from functools import partial
|
|
54
|
-
from collections.abc import Iterable
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
def round_numbers(items, round_to=0.001, *, index_of_item_number=None, egress=None):
|
|
58
|
-
"""
|
|
59
|
-
Round numbers in an iterable, optionally extracting the number to round from an index.
|
|
60
|
-
|
|
61
|
-
Parameters:
|
|
62
|
-
- items: iterable of numbers or iterable of tuples/lists containing numbers.
|
|
63
|
-
- round_to: float, round to the nearest multiple of this value.
|
|
64
|
-
- index_of_item_number: int or None. If None, round the item directly;
|
|
65
|
-
otherwise, round the item at this index in each iterable element.
|
|
66
|
-
|
|
67
|
-
Returns:
|
|
68
|
-
- generator yielding items with rounded numbers.
|
|
69
|
-
|
|
70
|
-
Examples:
|
|
71
|
-
>>> list(round_numbers([1.23, 3.14159], round_to=0.1))
|
|
72
|
-
[1.2, 3.1]
|
|
73
|
-
|
|
74
|
-
>>> items = [(1.234, 'one'), (3.14159, 'three')]
|
|
75
|
-
>>> list(round_numbers(items, round_to=0.1, index_of_item_number=0))
|
|
76
|
-
[[1.2, 'one'], [3.1, 'three']]
|
|
77
|
-
"""
|
|
78
|
-
digits = len(str(round_to).split(".")[-1])
|
|
79
|
-
|
|
80
|
-
for item in items:
|
|
81
|
-
if index_of_item_number is None:
|
|
82
|
-
yield round(round(item / round_to) * round_to, digits)
|
|
83
|
-
else:
|
|
84
|
-
val = item[index_of_item_number]
|
|
85
|
-
rounded_val = round(round(val / round_to) * round_to, digits)
|
|
86
|
-
item = list(item)
|
|
87
|
-
item[index_of_item_number] = rounded_val
|
|
88
|
-
|
|
89
|
-
if egress is None:
|
|
90
|
-
yield item
|
|
91
|
-
else:
|
|
92
|
-
yield egress(item)
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
def getmodulename(obj, default=""):
|
|
96
|
-
"""Get name of module of object"""
|
|
97
|
-
return getattr(getmodule(obj), "__name__", default)
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
class ModuleNotFoundErrorNiceMessage:
|
|
101
|
-
def __init__(self, msg=None):
|
|
102
|
-
self.msg = msg
|
|
103
|
-
|
|
104
|
-
def __enter__(self):
|
|
105
|
-
pass
|
|
106
|
-
|
|
107
|
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
108
|
-
if exc_type is ModuleNotFoundError:
|
|
109
|
-
msg = (
|
|
110
|
-
self.msg
|
|
111
|
-
or f"""
|
|
112
|
-
It seems you don't have required `{exc_val.name}` package for this Store.
|
|
113
|
-
Try installing it by running:
|
|
114
|
-
|
|
115
|
-
pip install {exc_val.name}
|
|
116
|
-
|
|
117
|
-
in your terminal.
|
|
118
|
-
For more information: https://pypi.org/project/{exc_val.name}
|
|
119
|
-
"""
|
|
120
|
-
)
|
|
121
|
-
warn(msg)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
class ModuleNotFoundIgnore:
|
|
125
|
-
def __enter__(self):
|
|
126
|
-
pass
|
|
127
|
-
|
|
128
|
-
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
129
|
-
if exc_type is ModuleNotFoundError:
|
|
130
|
-
pass
|
|
131
|
-
return True
|
{hum-1.0.5 → hum-1.0.7}/LICENSE
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{hum-1.0.5 → hum-1.0.7}/setup.py
RENAMED
|
File without changes
|