| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- """Original deterministic 116 BPM instrumental, authored for Diamond Arrow.
- No samples or third-party composition. Requires numpy; exports stereo PCM WAV.
- All notes, room tails and delays wrap into the loop instead of a fade-to-silence.
- """
- from pathlib import Path
- import json, wave
- import numpy as np
- SR = 44100
- BPM = 116
- BEAT = 60 / BPM
- FRAMES = round(64 * BEAT * SR)
- mix = np.zeros((FRAMES, 2), dtype=np.float64)
- rng = np.random.default_rng(314159)
- def add(signal, beat, gain=1, pan=0):
- idx = (np.arange(len(signal)) + round(beat * BEAT * SR)) % FRAMES
- np.add.at(mix[:, 0], idx, signal * gain * np.sqrt((1-pan)/2))
- np.add.at(mix[:, 1], idx, signal * gain * np.sqrt((1+pan)/2))
- def note(midi, beats, voice):
- duration = beats * BEAT
- t = np.arange(round((duration + .28) * SR)) / SR
- f = 440 * 2 ** ((midi - 69) / 12)
- attack = 1 - np.exp(-t * 350)
- release = np.exp(-np.maximum(t-duration, 0) * 26)
- if voice == 'bass':
- y = np.sin(2*np.pi*f*t) + .22*np.sin(4*np.pi*f*t)
- return y * attack * np.exp(-t*3) * release
- if voice == 'bell':
- y = np.sin(2*np.pi*f*t + 1.1*np.exp(-t*9)*np.sin(2*np.pi*f*2*t))
- y += .14*np.sin(2*np.pi*f*3*t)*np.exp(-t*7)
- return y * attack * np.exp(-t*4.8) * release
- y = np.sin(2*np.pi*f*t) + .26*np.sin(2*np.pi*f*2*t) + .08*np.sin(2*np.pi*f*3*t)
- return y * attack * np.exp(-t*2.8) * release
- def drum(kind):
- duration = {'kick':.28,'clap':.15,'hat':.055,'open':.16}[kind]
- t = np.arange(round(duration*SR)) / SR
- noise = rng.normal(0,1,len(t)); high = np.r_[0,np.diff(noise)]*.35
- if kind == 'kick':
- phase = 2*np.pi*(48*t + 63*.018*(1-np.exp(-t/.018)))
- y = np.sin(phase)*np.exp(-t*16) + high*np.exp(-t*170)*.13
- elif kind == 'clap':
- y = high*np.exp(-t*34)*.7 + np.sin(2*np.pi*185*t)*np.exp(-t*36)*.16
- else:
- y = high*np.exp(-t*(80 if kind=='hat' else 30))*.5
- return y*np.minimum(1,t*2000)
- # D major, B minor, G major, A major; second phrase adds a gentle lift.
- chords = [[62,66,69,73],[59,62,66,69],[55,59,62,66],[57,61,64,69],
- [55,59,62,66],[57,61,64,69],[54,57,62,66],[57,61,64,69]]*2
- roots = [38,35,31,33,31,33,38,33]*2
- melodies = [
- [(0,78,.5),(1,81,.5),(1.75,78,.5),(2.5,76,.5),(3.25,74,.65)],
- [(.5,78,.5),(1.25,76,.5),(2,74,.75),(3.25,73,.5)],
- [(0,71,.5),(.75,74,.5),(1.5,78,.75),(3,76,.75)],
- [(.25,76,.5),(1,73,.5),(2,69,1)],
- [(0,74,.75),(1.25,78,.5),(2,81,.5),(3,83,.65)],
- [(.5,81,.5),(1.25,78,.5),(2,76,.75),(3.25,73,.5)],
- [(0,78,.5),(1,76,.5),(2,74,1)],
- [(.5,73,.5),(1.25,76,.75),(2.5,73,.5),(3.25,69,.5)]]
- for bar in range(16):
- base = bar*4
- for b in (0,1,2,3): add(drum('kick'),base+b,.26 if b%2==0 else .20)
- for b in (1,3): add(drum('clap'),base+b,.12,.12)
- for i in range(8): add(drum('open' if i==7 and bar%4==3 else 'hat'),base+i*.5+(0.035 if i%2 else 0),.07 if i%2 else .045,(-1)**i*.35)
- for offset, pitch in [(0,roots[bar]),(1.5,roots[bar]),(2.5,roots[bar]+12),(3.5,roots[bar]+7)]:
- add(note(pitch,.4,'bass'),base+offset,.17)
- for offset in (.5,2.5):
- for j,pitch in enumerate(chords[bar]): add(note(pitch,.7,'keys'),base+offset+j*.012,.047,(j-1.5)*.22)
- for offset,pitch,length in melodies[bar%8]:
- signal=note(pitch+(12 if bar==12 and offset==3 else 0),length,'bell')
- pan=.12*np.sin(bar)
- add(signal,base+offset,.105,pan);add(signal,base+offset+.75,.019,-pan-.25)
- if bar>=8:
- for i,pitch in enumerate(chords[bar]): add(note(pitch+12,.25,'bell'),base+3+i*.25,.018,(-1)**i*.5)
- # A light circular room return and gentle saturation, no hard clip or silence gap.
- dry=mix.copy()
- for seconds,gain in [(.071,.09),(.113,.065),(.173,.04)]: mix += np.roll(dry[:,::-1],round(seconds*SR),axis=0)*gain
- mix=np.tanh(mix*1.12)
- mix *= min(.88/np.max(np.abs(mix)), .13/np.sqrt(np.mean(mix**2)))
- pcm=np.round(mix*32767).astype('<i2')
- root=Path(__file__).resolve().parents[2]
- out=root/'Art/Audio/NeonDaydream.wav'
- with wave.open(str(out),'wb') as wav:
- wav.setnchannels(2);wav.setsampwidth(2);wav.setframerate(SR);wav.writeframes(pcm.tobytes())
- report={'title':'Neon Daydream','bpm':BPM,'key':'D major','bars':16,'seconds':FRAMES/SR,'sampleRate':SR,'channels':2,'peakDbFS':float(20*np.log10(np.max(np.abs(mix)))),'rmsDbFS':float(20*np.log10(np.sqrt(np.mean(mix**2)))),'loopBoundaryDelta':float(np.max(np.abs(mix[0]-mix[-1]))),'clippedSamples':int(np.sum(np.abs(pcm)>=32767)),'source':'Original procedural composition; no third-party samples'}
- (root/'Art/Audio/NeonDaydream.json').write_text(json.dumps(report,indent=2)+'\n',encoding='utf-8')
- print(json.dumps(report))
|