<jupyter_start><jupyter_text>http://supriya.mbrsi.org/api/tools/nonrealtimetools/Session.htmlsupriya.tools.nonrealtimetools.Session<jupyter_code>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<jupyter_output><empty_output><jupyter_text>1. Param builder2. Data structure mapping osc/aiff file name to parameters3.<jupyter_code>def mtof(d):
    return 2.**((d-69.)/12.) * 440.

def ftom(f):
    return 69. + (12. * math.log2(f/440.))
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
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)
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
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"],
                                        )
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)
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
def make_osc_file(session, fpath):
    datagram = session.to_datagram()
    with open(fpath, 'wb') as file_pointer:
        file_pointer.write(datagram)
def make_cmd(osc_fp, aif_fp):
    return "scsynth -N {0} _ {1} 44100 AIFF int16 -o 1".format(osc_fp, aif_fp)
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
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'],
            )
            )
pmtx = gen_params(rows=20)
df = format_params(pmtx)
df.sort_values(["hash"])
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"]))
cp_to_sc(df.iloc[1])
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"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1228936170212766, 'fraction_numerical': 0.01446808510638298, 'mean_word_length': 1.8318072289156626, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 39, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '218006', 'license': 'Apache-2.0', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1785, 'n_words': 322, 'path': 'Notebooks/nrt_generator.ipynb', 'repo_name': 'spacecoffin/GravelKicker', 'size': '5875'}