"""Experimental string-specific guitar with finite finger contact and internal loss. Procedural coupled string/body model, no recorded audio. Parameters provisional. """ import numpy as np from numba import njit from scipy.signal import resample_poly from scipy.io.wavfile import write import json from pathlib import Path @njit def simulate(pitch=58, amplitude=.0008, contact=False, seconds=3., release_seconds=.0015, string_index=-1): sr=88200; dt=1/sr; f=440*2**((pitch-69)/12) # Six distinct open-string wave speeds; fretting changes speaking length. opens=np.array([40.,45.,50.,55.,59.,64.]) tensions=np.array([68.,64.,61.,55.,53.,57.]) idx=0 for j in range(6): if pitch>=opens[j]:idx=j if 0<=string_index<6 and opens[string_index]<=pitch:idx=string_index tension=tensions[idx]; speed=2*.65*440*2**((opens[idx]-69)/12) density=tension/speed**2 length=speed/(2*f); n=min(160,int(sr*.36/(2*f))) h=length/(n+1); mass=density*h; k=tension/h # Modal masses and frequencies are provisional, not measured responses. freq=np.array([102.,195.,340.,520.,700.,1050.,1700.,2600.]) bm=np.array([.4,.15,.1,.07,.06,.05,.04,.03]) weight=np.array([.8,1.,.42,.22,.4,.1,.06,.03]) damping=np.array([32.,42.,55.,75.,95.,135.,190.,270.]) q=np.zeros(8);vbody=np.zeros(8);y=np.zeros(n);v=np.zeros(n) # Keep hand position in physical units instead of moving with every fret. pluck=1.-min(.46,max(.16,.14/length)) pluck_index=max(0,min(n-1,int(pluck*(n+1))-1)) # A finite finger pad pulls several adjacent points, rather than a sharp kink. pull_weights=np.zeros(n) for j in range(n): distance=((j+1)/(n+1)-pluck)*length pull_weights[j]=np.exp(-.5*(distance/.004)**2) pull_weights/=np.sum(pull_weights) for j in range(n): x=(j+1)/(n+1) for a in range(n): pos=(a+1)/(n+1) y[j]+=pull_weights[a]*min(x,pos)*(1.-max(x,pos)) # Match the old string's stored pluck energy; avoid a louder-is-better change. old_speed=2*.65*(195.997718 if pitch>=55 else 146.832384) old_length=old_speed/(2*f) old_n=n; old_pluck=int(.74*(old_n+1))/(old_n+1) target_energy=55.*amplitude**2/(2*old_length*old_pluck*(1-old_pluck)) elastic=0.;prev=0. for j in range(n): elastic+=.5*k*(y[j]-prev)**2;prev=y[j] elastic+=.5*k*y[-1]**2 shape_scale=np.sqrt(target_energy/elastic) y*=shape_scale held_force=tension*shape_scale/length # Passive losses preferentially damp very short-wavelength string motion. viscous_k=mass*.0006/(h*h) # Equilibrate body and string under the held pluck before releasing it. compliance=np.sum(weight*weight/(bm*(2*np.pi*freq)**2)) held_bridge_force=k*y[-1]/(1+k*compliance/(n+1)) for j in range(8):q[j]=weight[j]*held_bridge_force/(bm[j]*(2*np.pi*freq[j])**2) bridge=np.sum(q*weight) for j in range(n):y[j]+=(j+1)/(n+1)*bridge out=np.zeros(int(seconds*sr)); force=np.zeros(n);bf=np.zeros(8) # String pulled away from the board; collisions arise on its return. contacts=0; peak_penetration=0.; energies=np.zeros(1+len(out)//882) for frame in range(len(out)): for half in range(2): bridge=np.sum(q*weight) total_contact=0. bridge_velocity=np.sum(vbody*weight) for j in range(n): left=0. if j==0 else y[j-1] right=bridge if j==n-1 else y[j+1] left_velocity=0. if j==0 else v[j-1] right_velocity=bridge_velocity if j==n-1 else v[j+1] force[j]=k*(left-2*y[j]+right)+viscous_k*(left_velocity-2*v[j]+right_velocity) x=(j+1)/(n+1) clearance=.0004+.0014*x penetration=bridge-clearance-y[j] if contact and x<.72 and penetration>0: # Elastic distributed penalty spring; no random noise. cf=1800.*penetration force[j]+=cf;total_contact+=cf if half==0:contacts+=1 peak_penetration=max(peak_penetration,penetration) # Finite release of the force holding the string, not an audio envelope. time=(frame+half)*dt if time0];assert max(e)