In [80]:
import os
import subprocess
import numpy as np
import pandas as pd
from supriya.tools import nonrealtimetools
from supriya.tools import soundfiletools
from supriya.tools import synthdeftools
from supriya.tools import ugentools
import hashlib
import math
import matplotlib.pyplot as plt
import scipy.special as sps
from scipy import stats
In [3]:
def mtof(d):
return 2.**((d-69.)/12.) * 440.
def ftom(f):
return 69. + (12. * math.log2(f/440.))
In [172]:
def min_max_freq(size=1, lambda_=0.7):
def min_gen(a):
"""Generate a random int between 0 & a (inclusive) of a 1-D array"""
return np.random.randint(0, a+1, size=1)
# Vectorize function so it can be applied to array
min_func = np.vectorize(min_gen)
octave_range = stats.planck.rvs(lambda_, size=size)
note_range = np.random.randint(1, 13, size=size)
total_range = np.clip(octave_range, 0, 8)*12 + note_range
base_note = min_func(110-total_range)
return np.append(mtof(base_note), mtof((base_note+total_range))).reshape(2, size).T
In [87]:
def gen_params(rows=1, lambda_=0.7, M=100, n=12, N=65):
return np.concatenate([np.random.uniform(low=0.0001, high=1.0, size=4*rows).reshape(rows, 4),
np.random.randint(6, size=2*rows).reshape(rows, 2),
((stats.hypergeom.rvs(M=M, n=n, N=N, size=1*rows))+4).reshape(rows, 1),
min_max_freq(size=rows, lambda_=lambda_),
(np.zeros(rows)+16).reshape(rows, 1)],
axis=1)
In [160]:
def format_params(param_mtx):
cols = ('adparam', 'ampscale', 'ddparam', 'durscale', 'ampdist', 'durdist',
'knum', 'minfrequency', 'maxfrequency', 'init_cps')
_df = pd.DataFrame(param_mtx, columns=cols)
_df["hash"] = np.array([hashlib.md5(series.tostring()).hexdigest() for series in param_mtx])
return _df
In [171]:
def make_builder(row):
return synthdeftools.SynthDefBuilder(adparam=row["adparam"],
ampdist=row["ampdist"],
ampscale=row["ampscale"],
ddparam=row["ddparam"],
durdist=row["durdist"],
durscale=row["durscale"],
init_cps=row["init_cps"],
knum=row["knum"],
maxfrequency=row["maxfrequency"],
minfrequency=row["minfrequency"],
)
In [7]:
def make_out_dirs(path):
if os.path.exists(path):
of = os.path.join(path, "osc_files")
af = os.path.join(path, "aif_files")
os.makedirs(of, exist_ok=True)
os.makedirs(af, exist_ok=True)
return of, af
else:
return "{} does not exist".format(path)
In [8]:
def join_path_to_file(osc_p, aif_p, f):
osc_fp = os.path.join(osc_p, f) + ".osc"
aif_fp = os.path.join(aif_p, f) + ".aiff"
return osc_fp, aif_fp
In [9]:
def make_osc_file(session, fpath):
datagram = session.to_datagram()
with open(fpath, 'wb') as file_pointer:
file_pointer.write(datagram)
In [10]:
def make_cmd(osc_fp, aif_fp):
return "scsynth -N {0} _ {1} 44100 AIFF int16 -o 1".format(osc_fp, aif_fp)
In [11]:
def render_session(session, out_dir, fname):
osc_path, aif_path = make_out_dirs(out_dir)
osc_fpath, aif_fpath = join_path_to_file(osc_path, aif_path, fname)
make_osc_file(session, osc_fpath)
bashCommand = make_cmd(osc_fpath, aif_fpath)
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
return output, error
In [176]:
def build_out(builder):
with builder:
return ugentools.Out.ar(
source=ugentools.Gendy1.ar(
adparam=builder['adparam'],
ampdist=builder['ampdist'],
ampscale=builder['ampscale'],
ddparam=builder['ddparam'],
durdist=builder['durdist'],
durscale=builder['durscale'],
init_cps=builder['init_cps'],
knum=builder['knum'],
maxfrequency=builder['maxfrequency'],
minfrequency=builder['minfrequency'],
)
)
In [173]:
pmtx = gen_params(rows=20)
In [174]:
df = format_params(pmtx)
In [186]:
df.sort_values(["hash"])
Out[186]:
In [192]:
def cp_to_sc(r):
print("{}, {}, {}, {}, {}, {}, {}, {}, {}, {}".format(r["ampdist"],
r["durdist"],
r["adparam"],
r["ddparam"],
r["minfrequency"],
r["maxfrequency"],
r["ampscale"],
r["durscale"],
r["init_cps"],
r["knum"]))
In [193]:
cp_to_sc(df.iloc[1])
In [187]:
this_dir = os.getcwd()
for i, row in df.iterrows():
session = nonrealtimetools.Session()
builder = make_builder(row)
out = build_out(builder)
synthdef = builder.build()
with session.at(0):
synth_a = session.add_synth(duration=10, synthdef=synthdef)
render_session(session, this_dir, row["hash"])
In [ ]: