Xpressive Python Expression Random Generator

Write guides and how-tos about LMMS for other members.

Simple Xpressive function Generator in Python.

Not an expert in Xpressive but I like to generate random stuff. So I have wrote a small python script where a formula can be set and outputs a function that can be used in Xpressive as expression on O1.

Code: Select all

import numpy as np
import cmath
import math 
import random

print(f"{'Freq (Hz)':<42} | {'Cosine Component Formula':<45} | {'Sine Component Formula':<15}")
print("-" * 90)
t = 0.5
x = ""
for k in range(1, 10): 
    if(k>random.randint(2, 5)):
        break

eval = ['sin','sinew', 'trianglew', 'squarew', 'saww'] 
#'abs', 'sin', 'cos', 'tan', 'cot', 'asin', 'acos', 'atan', 'atan2', 'sinh',
#'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'sinc', 'hypot',
#'exp', 'log', 'log2', 'log10', 'logn', 'pow', 'sqrt', 'min', 'max', 'floor', 'ceil',
#'round', 'trunc', 'frac', 'avg', 'sgn', 'mod']

# Filter out negligible ambient noise or silence
if k:
    randnum = random.randint(1, 9);
    randomunicos = random.uniform(0.5, 7.5)
    randomunisin = random.uniform(0.5, 7.5)
    randomfreq = random.randint(41, 227)
    #round(random.uniform(33.33, 66.66), 2)
    #x = x + f"{eval[random.randint(0, 5)]}(t * {randomfreq}  * exp(-t * .1)) * exp(-t * .{randnum})"
    x = x + f"{eval[random.randint(0, 4)]} (t * {randomfreq} *  exp(-t * .{randnum}) ) "
    print(f"{randomfreq:<42.1f} | {randomunicos:<45} | {randomunisin:<15}")
print("-" * 90)
print(x)

Output Example

Code: Select all

Freq (Hz)                                  | Cosine Component Formula                      | Sine Component Formula
------------------------------------------------------------------------------------------
223.0                                      | 1.6458303668121976                            | 3.3729510793230104
86.0                                       | 5.191355095525813                             | 0.5670091219740144
172.0                                      | 5.934424313930378                             | 2.9379080980547427
------------------------------------------------------------------------------------------
sin (t * 223 *  exp(-t * .7) ) sinew (t * 86 *  exp(-t * .7) ) saww (t * 172 *  exp(-t * .7) )

Random Generated Expressions Examples

Code: Select all

Example 1:
sin (t * 223 *  exp(-t * .7) ) sinew (t * 86 *  exp(-t * .7) ) saww (t * 172 *  exp(-t * .7) ) 

Example 2:
sinew (t * 194 *  exp(-t * .6) ) sinew (t * 108 *  exp(-t * .8) ) sinew (t * 42 *  exp(-t * .2) ) squarew (t * 1630 *  exp(-t * .2) ) 

Example 3:
sin (t * 1785 *  exp(-t * .9) ) sin (t * 556 *  exp(-t * .7) ) sin (t * 200 *  exp(-t * .5) ) sin (t * 201 *  exp(-t * .2) ) sin (t * 60 *  exp(-t * .8) ) 

Copilot gave me a better idea on how to reduce the code after I asked how to define more variables in single line, code was scale down to few lines. Here is the output:

Code: Select all

import numpy as np, math, random
print(f"{'Freq (Hz)':<42} | {'Cosine Component Formula':<45} | {'Sine Component Formula':<15}") , print("-" * 90)
t, x, evals = 0.5, "", ['sin','sinew','trianglew','squarew','saww']

for k in range(1, random.randint(3,7)):
    f, rc, rs = random.randint(41,227), random.uniform(0.5,7.5), random.uniform(0.5,7.5)
    x += f"{evals[random.randint(0,4)]}(t*{f}*exp(-t*.{random.randint(1,9)})) "
    print(f"{f:<42.1f} | {rc:<45} | {rs:<15}")

print("-" * 90), print(x)

The evals list can be updated by wish with extra functions available in Xpressive like:

Code: Select all

abs, sin, cos, tan, cot, asin, acos, atan, atan2, sinh, cosh, tanh, asinh, acosh, atanh, sinc, hypot, exp, log, log2, log10, logn, pow, sqrt, min, max, floor, ceil, round, trunc, frac, avg, sgn, mod

The x output can be changed too by needs, I just used a simplified version of it.

Code can be runned on docker python containers, online python compilers ( more convenient because involves no installation ) or any Linux machine.

Here are examples of random generated Xpressive sounds using python:
https://lmms.io/lsp/?action=show&file=24448
https://lmms.io/lsp/?action=show&file=24451