Skip to content

Commit

Permalink
signalflow_visualisation: Add plot_buffer()
Browse files Browse the repository at this point in the history
  • Loading branch information
ideoforms committed Aug 13, 2024
1 parent 5f4afe0 commit d119f96
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
3 changes: 2 additions & 1 deletion auxiliary/libs/signalflow_visualisation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .patch_structure import visualise_patch_structure
from .node import plot_node_output
from .node import plot_node_output
from .buffer import plot_buffer
48 changes: 48 additions & 0 deletions auxiliary/libs/signalflow_visualisation/buffer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from signalflow import Buffer, WavetableBuffer, WaveShaperBuffer, EnvelopeBuffer
import matplotlib
import matplotlib.pyplot as plt
import numpy as np

def plot_buffer(buffer: Buffer,
ax: matplotlib.axis.Axis = None,
title: str = None):
axis_passed = (ax is not None)

#--------------------------------------------------------------------------------
# In the case that no matplotlib axis is given, create a new high-resolution
# plot and axis
#--------------------------------------------------------------------------------
if not axis_passed:
_, ax = plt.subplots(figsize=(16, 4), dpi=300)

ax.grid(linestyle=":")

if isinstance(buffer, WavetableBuffer) or isinstance(buffer, WaveShaperBuffer) or isinstance(buffer, EnvelopeBuffer):
#--------------------------------------------------------------------------------
# Plot envelope and wavetable/shaper buffers by sample
#--------------------------------------------------------------------------------
ax.plot(np.arange(len(buffer)), buffer.data[0])
else:
#--------------------------------------------------------------------------------
# Plot all other buffers by time, in seconds
#--------------------------------------------------------------------------------
ax.plot(np.arange(len(buffer)) / buffer.sample_rate, buffer.data[0])

#--------------------------------------------------------------------------------
# For wavetable/shaper buffers, highlight the y=0 line.
#--------------------------------------------------------------------------------
if isinstance(buffer, WavetableBuffer) or isinstance(buffer, WaveShaperBuffer):
plt.axhline(y=0, color="red", linestyle=":", linewidth=0.5)

#--------------------------------------------------------------------------------
# Plot title
#--------------------------------------------------------------------------------
if title is not None:
ax.set_title(title)

#--------------------------------------------------------------------------------
# If this is a new axis, render it to screen
#--------------------------------------------------------------------------------
if not axis_passed:
plt.plot()
plt.show()

0 comments on commit d119f96

Please sign in to comment.