By Jacob Joaquin
jacobjoaquin@gmail.com
twitter @jacobjoaquin
May 9th, 2013
This is just a collection of graphs that can potentially be in future Csound IPython Notebook based papers. Much of everything I did here I learned from studying matplotlib - 2D and 3D plotting in Python by J.R. Johansson. Though this is a Csound notebook, no actual sound is generated as this notebook focuses solely on graph design.
The following cell loads boots the Csound 6 API and defines a few time saving functions.
import csnd6
cs = csnd6.Csound()
csPerf = csnd6.CsoundPerformanceThread(cs)
cs.SetOption('-odac')
cs.CompileOrc('''
sr = 44100
kr = 4410
ksmps = 10
nchnls = 1
0dbfs = 1.0
''')
cs.Start()
csPerf.Play()
def copy_table_data(t):
'''Copy Csound table data into a Python List'''
length = cs.TableLength(t)
array = csnd6.doubleArray(length)
cs.TableCopyOut(t, array)
table = []
for i in xrange(length):
table.append(array[i])
return table
def gen_table(*args):
'''Generate a Csound Table'''
table_index = str(args[0])
data = ', '.join(map(str, list(args)))
data = 'gitable_' + str(table_index) + ' ftgen ' + data
cs.CompileOrc(data)
The graph is based on the following linseg line of code:
fig, ax = subplots(figsize=(8, 4))
ax.vlines(1, 0, 1, 'black', 'dashed')
ax.set_title('linseg Envelope', fontsize=22)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.set_xticks([0, 1, 2])
ax.set_xticklabels(['0.0', 'idur / 2', 'idur'])
ax.set_xlabel('Time', fontsize=18)
ax.set_xlim([0, 2])
ax.set_yticks([0, 1])
ax.set_yticklabels([0.0, 'iamp'])
ax.set_ylabel('Amplitude', fontsize=18)
ax.set_ylim([0.0, 1.1])
ax.plot([1, 1, 0], lw=4, color='black')
[<matplotlib.lines.Line2D at 0x42b2050>]
Plotting a Csound table.
gen_table(1, 0, 8192, 10, *(map(lambda x: 1.0 / (x + 1), range(16 + 1))))
table_data = copy_table_data(1)
table_length = cs.TableLength(1)
fig, ax = subplots(figsize=(10, 6))
xlim(0, table_length)
ax.hlines(0, 0, table_length)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.set_xticks(xrange(0, table_length + 1, table_length / 4))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.plot(table_data, color='black', lw=2)
[<matplotlib.lines.Line2D at 0x2b0b050>]
This shows a 4x4 grid of graphs, showcasing the evolution of a band-limited saw wave.
for h in range(1, 17):
gen_table(h, 0, 128, 10, *(map(lambda x: 1.0 / (x + 1), range(h))))
fig, ax = subplots(4, 4, figsize=(12, 12))
table = 1
for x in xrange(4):
for y in xrange(4):
table_length = cs.TableLength(table)
ax[x][y].hlines(0, 0, table_length)
ax[x][y].spines['top'].set_visible(False)
ax[x][y].spines['right'].set_visible(False)
ax[x][y].spines['bottom'].set_visible(False)
ax[x][y].xaxis.set_ticks_position('none')
ax[x][y].yaxis.set_ticks_position('left')
ax[x][y].plot(copy_table_data(table) + [0], lw=2, color='black')
ax[x][y].set_xticks([])
ax[x][y].set_title('harmonics = ' + str(table), fontsize=10)
table += 1
This example showcases a way to display data from a Csound table as discrete points.
gen_table(1, 0, 32, 10, 1)
sine_table = copy_table_data(1)
table_length = cs.TableLength(1)
fig, ax = subplots(figsize=(12, 6))
ax.hlines(0.0, 0, table_length, 'black')
ax.set_title(str(table_length) + ' Point Sine Wave Table', fontsize=22)
ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('left')
ax.set_xlabel('Index', fontsize=18)
ax.set_xticks(xrange(table_length))
ax.set_xlim([0, table_length])
ax.set_ylabel('Value', fontsize=18)
ax.set_ylim([-1.1, 1.1])
for i, v in enumerate(sine_table):
ax.vlines(i, 0.0, v, linewidth=0.5)
ax.plot(sine_table, lw=4, color='black', ls='*', marker='s',
markersize=9, markeredgewidth=2, markeredgecolor="white")
[<matplotlib.lines.Line2D at 0x67abcf0>]
An example for showing harmonic strength.
n_harmonics = 32
fig, ax = subplots(figsize=(12,8))
ax.set_title('Harmonic Strength', fontsize=22)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.xaxis.set_ticks_position('none')
ax.yaxis.set_ticks_position('left')
ax.set_xticks(xrange(1, n_harmonics + 1))
ax.set_xlim([0, n_harmonics + 1])
ax.set_ylim([0, 1.0])
ax.set_xlabel('Harmonic', fontsize=18)
ax.set_ylabel('Amplitude', fontsize=18)
for i in xrange(1, n_harmonics + 1):
ax.vlines(i, 0, 1.0 / i, lw=4)
csPerf.Stop()