File size: 5,449 Bytes
8fbe43c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
"""Generate one-shot drum samples using DSP synthesis.

Creates WAV files for: kick, snare, hihat, crash, ride, tom_high, tom_low.
Uses scipy filters for realistic frequency shaping and layered synthesis.
"""

import numpy as np
from scipy.io import wavfile
from scipy.signal import butter, lfilter
from pathlib import Path

SR = 44100
OUT_DIR = Path(__file__).parent.parent / "app" / "public" / "drums"

np.random.seed(42)


def _normalize(signal, peak=0.85):
    mx = np.max(np.abs(signal))
    if mx > 0:
        signal = signal * (peak / mx)
    return signal.astype(np.float32)


def _highpass(signal, freq, sr=SR, order=4):
    nyq = 0.5 * sr
    b, a = butter(order, freq / nyq, btype='high')
    return lfilter(b, a, signal)


def _lowpass(signal, freq, sr=SR, order=4):
    nyq = 0.5 * sr
    b, a = butter(order, freq / nyq, btype='low')
    return lfilter(b, a, signal)


def _bandpass(signal, low, high, sr=SR, order=4):
    nyq = 0.5 * sr
    b, a = butter(order, [low / nyq, high / nyq], btype='band')
    return lfilter(b, a, signal)


def make_kick():
    dur = 0.45
    n = int(SR * dur)
    t = np.linspace(0, dur, n, endpoint=False)

    # Pitch sweep: 150Hz -> 45Hz with exponential decay
    freq = 45 + 105 * np.exp(-t * 25)
    phase = 2 * np.pi * np.cumsum(freq) / SR
    body = np.sin(phase)

    # Amplitude envelope: punchy attack, moderate decay
    env = np.exp(-t * 7) * (1 - np.exp(-t * 500))

    # Sub-bass thump (separate low sine for weight)
    sub = np.sin(2 * np.pi * 50 * t) * np.exp(-t * 12) * 0.4

    # Transient click
    click_n = int(SR * 0.004)
    click = np.random.randn(click_n) * 0.25
    click *= np.linspace(1, 0, click_n)

    signal = body * env + sub
    signal[:click_n] += click

    # Lowpass to remove harmonics above 200Hz
    signal = _lowpass(signal, 200)

    return _normalize(signal)


def make_snare():
    dur = 0.3
    n = int(SR * dur)
    t = np.linspace(0, dur, n, endpoint=False)

    # Body: sine at 185Hz with fast decay
    body = np.sin(2 * np.pi * 185 * t) * np.exp(-t * 18) * 0.6

    # Second harmonic for richness
    body2 = np.sin(2 * np.pi * 330 * t) * np.exp(-t * 25) * 0.2

    # Snare wires: bandpassed noise (1kHz-9kHz)
    noise = np.random.randn(n)
    wires = _bandpass(noise, 1000, 9000)
    wire_env = np.exp(-t * 12)
    wires = wires * wire_env * 0.5

    # Sharp transient
    click_n = int(SR * 0.002)
    click = np.random.randn(click_n) * 0.5
    click *= np.linspace(1, 0, click_n)

    signal = body + body2 + wires
    signal[:click_n] += click

    return _normalize(signal)


def make_hihat():
    dur = 0.08
    n = int(SR * dur)
    t = np.linspace(0, dur, n, endpoint=False)

    # High-frequency noise
    noise = np.random.randn(n)
    signal = _highpass(noise, 6000)

    # Very tight envelope
    env = np.exp(-t * 60) * (1 - np.exp(-t * 2000))
    signal = signal * env

    return _normalize(signal, peak=0.7)


def make_crash():
    dur = 1.5
    n = int(SR * dur)
    t = np.linspace(0, dur, n, endpoint=False)

    # Broadband noise with emphasis on 3-8kHz
    noise = np.random.randn(n)
    bright = _bandpass(noise, 3000, 12000) * 0.7
    body = _bandpass(noise, 800, 4000) * 0.3

    signal = bright + body

    # Long decay envelope with sharp attack
    env = np.exp(-t * 2.5) * (1 - np.exp(-t * 1000))
    signal = signal * env

    return _normalize(signal, peak=0.7)


def make_ride():
    dur = 0.6
    n = int(SR * dur)
    t = np.linspace(0, dur, n, endpoint=False)

    # Tighter noise than crash, more "ping" character
    noise = np.random.randn(n)
    bright = _highpass(noise, 5000) * 0.6

    # Add a subtle metallic tone (inharmonic mix of sines)
    ping = (np.sin(2 * np.pi * 4200 * t) * 0.15 +
            np.sin(2 * np.pi * 5800 * t) * 0.10 +
            np.sin(2 * np.pi * 7100 * t) * 0.08)

    signal = bright + ping

    # Medium decay
    env = np.exp(-t * 4) * (1 - np.exp(-t * 1500))
    signal = signal * env

    return _normalize(signal, peak=0.6)


def make_tom(freq_base=120, dur=0.35):
    n = int(SR * dur)
    t = np.linspace(0, dur, n, endpoint=False)

    # Pitched membrane: frequency sweep down
    freq = freq_base * 0.6 + freq_base * 0.4 * np.exp(-t * 15)
    phase = 2 * np.pi * np.cumsum(freq) / SR
    body = np.sin(phase)

    # Amplitude envelope
    env = np.exp(-t * 8) * (1 - np.exp(-t * 800))

    # Slight noise for attack character
    click_n = int(SR * 0.005)
    click = np.random.randn(click_n) * 0.2
    click *= np.linspace(1, 0, click_n)

    signal = body * env
    signal[:click_n] += click

    # Bandpass around the fundamental
    signal = _bandpass(signal, freq_base * 0.4, freq_base * 3)

    return _normalize(signal, peak=0.8)


def write_wav(name, data):
    path = OUT_DIR / f"{name}.wav"
    # Convert float32 [-1, 1] to int16
    int_data = (data * 32767).astype(np.int16)
    wavfile.write(str(path), SR, int_data)
    size_kb = path.stat().st_size / 1024
    print(f"  {name}.wav: {len(data)/SR:.3f}s, {size_kb:.1f}KB")


if __name__ == "__main__":
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    print("Generating drum samples...")

    write_wav("kick", make_kick())
    write_wav("snare", make_snare())
    write_wav("hihat", make_hihat())
    write_wav("crash", make_crash())
    write_wav("ride", make_ride())
    write_wav("tom_high", make_tom(freq_base=200, dur=0.25))
    write_wav("tom_low", make_tom(freq_base=100, dur=0.4))

    print("Done!")