Skip to content
Snippets Groups Projects
Commit 42653af1 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Add output to jupyter script

parent 3b28d50a
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:52c8bec3-db2d-4522-b692-b035a71410de tags:
``` python
from matplotlib import pyplot as plt
%matplotlib inline
import math
!{sys.executable} -m pip install clipboard
%matplotlib inline
import clipboard
def draw(r, g, b):
x = [x for x in range(256)]
fig = plt.figure(figsize=(8, 4))
ax = fig.add_axes([0, 0, 1, 1])
ax.axhline(y=0.5, color='black', linestyle='--')
ax.set_xticks(range(0, 256, 64))
ax.set_yticks(range(-256*2, 256*2+1, 128))
ax.grid()
ax.plot(x, r, 'r')
ax.plot(x, g, 'g')
ax.plot(x, b, 'b')
def r():
start = 100
start2 = 220
last_a = 0
for i in range(256):
if i < start:
yield 0
elif i < start2:
span = 255-start
d = (i-start)/span
last_a = int(d*30.0)
yield min(255, last_a)
else:
span = 255-start2
d = (i-start2)/span
d = d*d*d
yield min(255, last_a + int(d*350.0))
def g():
start = 0
end = 180-80
scale = 0.25
for i in range(256):
if i < start:
yield 0
elif i > end:
d = 1.0 - ((i-end)/(295-end))
# d = (d*d)/4 + d/2
yield max(0, min(255, int(d*175*scale)))
else:
d = ((i-start)/(255-start))
d = (d*d*d)
yield min(255, int(d*2800*scale))
def b():
start = 4
end = 40
scale = 0.2
for i in range(256):
if i < start:
yield 0
elif i > end:
d = (i-end)/(60)
d = d*d
d = 1.0 - d
# d = math.sqrt(d)
yield max(0, min(255, int(d*32*scale)))
else:
d = (i-start)/(255-start)
d = math.sqrt(d)/2 + d/2
yield max(0, min(255, int(d*107*scale)))
r, g, b = list(r()), list(g()), list(b())
draw(r, g, b)
text = ""
text += "// Lookup Table for Red LED Channel\n"
text += f"int red_lookup[] = {{{', '.join(str(v) for v in r)}}};\n\n"
text += "// Lookup Table for Green LED Channel\n"
text += f"int green_lookup[] = {{{', '.join(str(v) for v in g)}}};\n\n"
text += "// Lookup Table for Blue LED Channel\n"
text += f"int blue_lookup[] = {{{', '.join(str(v) for v in b)}}};\n\n"
import ipywidgets as widgets
from IPython.display import display, HTML, Javascript
mybtn = widgets.Button(description='copy C++ to clipboard', button_style='success')
def mybtn_event_handler(b):
print("copied")
clipboard.copy(text)
mybtn.on_click(mybtn_event_handler)
display(mybtn)
```
%% Output
zsh:1: parse error near `-m'
%% Cell type:code id:41562cc6-9911-4fb1-87ec-f2b3c8bfb3c2 tags:
``` python
from matplotlib import pyplot as plt
%matplotlib inline
import math
def draw(r):
l = len(r)
x = [x for x in range(l)]
fig = plt.figure(figsize=(8, 4))
ax = fig.add_axes([0, 0, 1, 1])
ax.axhline(y=0.5, color='black', linestyle='--')
ax.axvline(x=l/2, color='black', linestyle='--')
ax.set_xticks(range(0, l, 64))
ax.set_yticks(range(-l*2, l*2+1, 128))
ax.grid()
ax.plot(x, r)
def deadband(length, deadband=0.04):
readings = []
for i in range(length):
current_reading = i/(length-1)
scaler = (1.0) / (1.0 - deadband)
scaler += 0.1
if current_reading < 0.5:
current_reading += deadband
current_reading = min(0.5, current_reading)
current_reading = 0.5 - current_reading
current_reading *= scaler
current_reading = 0.5 - current_reading
# current_reading =
else:
current_reading -= deadband
current_reading = max(0.5, current_reading)
current_reading = 0.5 - current_reading
current_reading *= scaler
current_reading = 0.5 - current_reading
val = min(length, max(0, current_reading))
readings.append(val)
return readings
bip = deadband(16, deadband = 0.08)
draw(bip)
text = ""
text += "// Lookup Table for Bipolar Curve with deadband\n"
text += f"float bip_lookup[] = {{{', '.join(str(v) for v in bip)}}};\n\n"
import ipywidgets as widgets
from IPython.display import display, HTML, Javascript
mybtn = widgets.Button(description='copy C++ to clipboard', button_style='success')
def mybtn_event_handler(b):
print("copied")
clipboard.copy(text)
mybtn.on_click(mybtn_event_handler)
display(mybtn)
len(bip)
```
%% Output
16
%% Cell type:code id:ecc666b0-8195-4276-a576-39d41753b540 tags:
``` python
from matplotlib import pyplot as plt
%matplotlib inline
import math
import sys
def draw(r):
l = len(r)
x = [x for x in range(l)]
fig = plt.figure(figsize=(8, 4))
ax = fig.add_axes([0, 0, 1, 1])
ax.axhline(y=0.5, color='black', linestyle='--')
ax.set_xticks(range(0, l, 64))
ax.set_yticks(range(-l*2, l*2+1, 128))
ax.grid()
ax.plot(x, r)
def lin_to_log(length, strength=1.0):
# Limit to range 0.0 and 1.0
strength = min(1.0, max(0.0, strength))
readings = []
linear_readings = []
for i in range(length):
current_reading = i/length
linear_readings.append(current_reading)
# Log of 0 is error, so handle it explicitly
if i == 0:
current_reading = 0.0
else:
current_reading = math.log10(i)
readings.append(current_reading)
# Normalize to scale 0.1 to one
maxima = max(readings)
scaler = 1.0 / maxima
readings = [r*scaler for r in readings]
output = []
for i, r in enumerate(readings):
val = r*strength + linear_readings[i] * (1.0 - strength)
output.append(val)
# Convert to integer value range
output = [o for o in output]
return output
# lilo = lin_to_log(4096, strength=1.0)
lilo = lin_to_log(32, strength=1.0)
# lilo = [l/256.0 for l in lilo]
draw(lilo)
text = ""
text += "// Lookup Table for Logarithmic Curve\n"
text += f"float log_lookup[] = {{{', '.join(str(v) for v in lilo)}}};\n\n"
# print(text)
import ipywidgets as widgets
from IPython.display import display, HTML, Javascript
mybtn = widgets.Button(description='copy C++ to clipboard', button_style='success')
def mybtn_event_handler(b):
print("copied")
clipboard.copy(text)
mybtn.on_click(mybtn_event_handler)
display(mybtn)
min(lilo)
```
%% Output
0.0
%% Cell type:code id:2ecfda42-4a3c-489a-bc90-8576648c339c tags:
``` python
from matplotlib import pyplot as plt
%matplotlib inline
import math
import sys
def draw(r):
l = len(r)
x = [x for x in range(l)]
fig = plt.figure(figsize=(8, 4))
ax = fig.add_axes([0, 0, 1, 1])
ax.axhline(y=0.5, color='black', linestyle='--')
ax.set_xticks(range(0, l, 64))
ax.set_yticks(range(-l*2, l*2+1, 128))
ax.grid()
ax.plot(x, r)
def exp_lookup(length, strength=1.0):
# Limit to range 0.0 and 1.0
strength = min(1.0, max(0.0, strength))
readings = []
linear_readings = []
for i in range(length):
current_reading = i/length
linear_readings.append(current_reading)
# Log of 0 is error, so handle it explicitly
if i == 0:
current_reading = 0.0
else:
current_reading = i*i
readings.append(current_reading)
# Normalize to scale 0.1 to one
maxima = max(readings)
scaler = 1.0 / maxima
readings = [r*scaler for r in readings]
output = []
for i, r in enumerate(readings):
val = r*strength + linear_readings[i] * (1.0 - strength)
output.append(val)
# Convert to integer value range
output = [o for o in output]
return output
# lilo = lin_to_log(4096, strength=1.0)
lilo = exp_lookup(8, strength=1.0)
draw(lilo)
text = ""
text += "// Lookup Table for Exponential Curve\n"
text += f"float exp_lookup[] = {{{', '.join(str(v) for v in lilo)}}};\n\n"
# print(text)
import ipywidgets as widgets
from IPython.display import display, HTML, Javascript
mybtn = widgets.Button(description='copy C++ to clipboard', button_style='success')
def mybtn_event_handler(b):
print("copied")
clipboard.copy(text)
mybtn.on_click(mybtn_event_handler)
display(mybtn)
max(lilo)
```
%% Output
0.9999999999999999
%% Cell type:code id:e51416f3-f34d-4513-9f0c-fa52c468274e tags:
``` python
from matplotlib import pyplot as plt
%matplotlib inline
from ipywidgets import interact, FloatSlider
import matplotlib.transforms as transforms
import math
import clipboard
def draw(f):
fig = plt.figure(figsize=(8, 4))
ax = fig.add_axes([0, 0, 1, 1])
b = scan2d(x, y, f)
ax.axhline(y=b, color='red', linestyle='--')
ax.axvline(x=f, color='red', linestyle='--')
trans = transforms.blended_transform_factory(
ax.get_yticklabels()[0].get_transform(), ax.transData)
ax.text(0.95, b, "{:.02f}".format(b), color="red", transform=trans, ha="right", va="bottom")
ax.grid()
ax.plot(x, y)
def lerp(a, b, f=0.5) -> float:
f = min(1.0, max(0.0, f))
if f == 0.0:
return a
elif f == 1.0:
return b
else:
return a * (1.0-f) + b * f
def lerp2d(x1, y1, x2, y2, f=0.5):
if f == 0.0:
return [x1, x2]
elif f == 1.0:
return [x1, x2]
else:
x = lerp(x1, x2, f)
y = lerp(y1, y2, f)
return [x, y]
# A function that scans through two lists representing x/y values using a
# third value called f and returns the linear interpolation between those points
def scan2d(x, y, f):
# f = min(1.0, max(0.0, f))
assert len(x) == len(y)
# Find ax and bx for given factor
xa = None
last_value = None
idx = None
for i, v in enumerate(x):
# this = abs(f-v)
this = f-v
if xa is None or this > 0:
xa = this
idx = i
idx2 = min(idx+1, len(x)-1)
if idx == idx2:
return y[idx]
xa = x[idx]
xb = x[idx2]
ya = y[idx]
yb = y[idx2]
xspan = xb-xa
xscaler = 1/xspan
new_f = (f-xa)*xscaler
return lerp(ya, yb, new_f)
lines_orig = [
[0.0, 0.0, -0.5],
[0.45, 0.5, 0.0],
[0.55, 0.5, 1.0],
[1.0, 1.0, 0.0],
]
half_deadband = 0.1
lines_orig = [
[-10.0, -10.0, 0.0],
[-10.0+half_deadband, -10.0, 0.0],
]
steps = list(range(-9, 11))
for i in steps:
f = float(i)
lines_orig.append([f-half_deadband, f, 0.0])
lines_orig.append([f+half_deadband, f, 0.0])
# Calculate curves for points of curvature
lines = []
for i, l in enumerate(lines_orig):
i2 = min(len(lines_orig)-1, i+1)
if l[2] == 0.0:
lines.append(l)
else:
xa = lines_orig[i][0]
xb = lines_orig[i2][0]
ya = lines_orig[i][1]
yb = lines_orig[i2][1]
x_span = xb-xa
y_span = yb-ya
x_step = 1/20
y_step = 1/20
for j in range(20):
x = x_step * j
y = y_step * j
y_curve = 0
if l[2] > 0.0:
y_curve = y*y*y
else:
y_curve = y*y
y = (1.0-l[2]) * y + l[2] * y_curve
lines.append([xa+x*x_span, ya+y*y_span, 0.0])
x = [a[0] for a in lines]
y = [a[1] for a in lines]
c = [a[2] for a in lines]
# draw(x, y, 0.45/2)
interact(draw, f=FloatSlider(min=min(x), max=max(x), step=0.001, value=0.2))
text = ""
text += "// Lookup Table for Pitch Knob\n"
text += f"float pitch_knob_x[] = {{{', '.join(str(xv) for xv in x)}}};\n\n"
text += f"float pitch_knob_y[] = {{{', '.join(str(yv) for yv in y)}}};\n\n"
import ipywidgets as widgets
from IPython.display import display, HTML, Javascript
mybtn = widgets.Button(description='copy C++ to clipboard', button_style='success')
def mybtn_event_handler(b):
print("copied")
clipboard.copy(text)
mybtn.on_click(mybtn_event_handler)
display(mybtn)
len(x)
```
%% Output
<function __main__.draw(f)>
42
%% Cell type:code id:f35f1609-3a10-4dce-b7dd-201d79f2c39c tags:
``` python
# Saturation curve
# X / Y / Curvature
lines_orig = [
[-1.5, -1.0, 1.0],
[-0.7, -0.7, 0.0],
[0.0, 0.0, 0.0],
[0.7, 0.7, -1.4],
[1.5, 1.0, 0.0],
]
lines = []
for i, l in enumerate(lines_orig):
i2 = min(len(lines_orig)-1, i+1)
if l[2] == 0.0:
lines.append(l)
else:
xa = lines_orig[i][0]
xb = lines_orig[i2][0]
ya = lines_orig[i][1]
yb = lines_orig[i2][1]
x_span = xb-xa
y_span = yb-ya
x_step = 1/20
y_step = 1/20
for j in range(20):
x = x_step * j
y = y_step * j
y_curve = 0
if l[2] > 0.0:
y_curve = y*y*y
else:
y_curve = y*y
y = (1.0-l[2]) * y + l[2] * y_curve
lines.append([xa+x*x_span, ya+y*y_span, 0.0])
x = [a[0] for a in lines]
y = [a[1] for a in lines]
c = [a[2] for a in lines]
# draw(x, y, 0.45/2)
interact(draw, f=FloatSlider(min=min(x), max=max(x), step=0.001, value=0.2))
```
%% Output
<function __main__.draw(f)>
%% Cell type:code id:f0cd9d06-a15a-46b4-a3ef-4aee3d4f7cd0 tags:
``` python
# Midi to pitch
notes = [36, 48, 60]
for note in notes:
print((note-36)/12.0)
```
%% Output
0.0
1.0
2.0
%% Cell type:code id:59b56dbc-6852-4989-bc19-3525ee7caf8b tags:
``` python
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment