Advertisement
yclee126

Python synth noise generator

Sep 23rd, 2021
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. import scipy.io.wavfile
  2. import numpy as np
  3. import random
  4.  
  5. sr = 44100
  6. duration = 10 # secs
  7. y = np.zeros((sr*duration), dtype='float')
  8.  
  9. func_names = ('saw', 'square', 'tri', 'sin')
  10. funcs = [
  11. lambda i, l: i/(l-1) * 2 - 1, # saw
  12. lambda i, l: round(i / (l-1)) * 2 - 1, # square
  13. lambda i, l: min(i/l,-(i/l-0.5))*4 if i/l < 0.5 else max(-(i/l-0.5), i/l-1)*4, # tri
  14. lambda i, l: np.sin(np.pi*2 * i/l) # sin
  15. ]
  16.  
  17. for func in range(len(func_names)):
  18. print('processing', func_names[func])
  19. try:
  20. index = 0
  21. while True:
  22. length = random.randint(10, 50)
  23. mul = random.uniform(0.2, 1)
  24. for i in range(length):
  25. y[index] = funcs[func](i, length) * mul
  26. if index % sr == 0:
  27. print(index/sr, 'sec')
  28. index += 1
  29. except:
  30. pass
  31.  
  32. scipy.io.wavfile.write(func_names[func] + '_noise.wav', sr, y)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement