here2share

# Tk_audio_generator3.py

Mar 1st, 2021 (edited)
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.78 KB | None | 0 0
  1. # Tk_audio_generator3.py ZZZ an attempt to create beautiful samples of sound
  2.  
  3. dir='C://py//audio//samples//'
  4.  
  5. ww = 600
  6. hh = 600
  7.  
  8. try:
  9.     from Tkinter import *
  10. except:
  11.     from tkinter import *
  12. import random
  13.  
  14. import wave
  15. import math
  16. import struct
  17.  
  18. import itertools
  19.  
  20. sample_rate = 44100
  21. duration = 1 # seconds of audio
  22. SAMPLE_LEN = sample_rate * duration
  23.  
  24. # -32767 to 32767
  25. for_amp = 32767
  26.  
  27. def ttt():
  28.     AMPS[amp,dur] += [int(t)]
  29.  
  30. freq = 16
  31. AMPS = {}
  32. for amp in range(10,for_amp,10):
  33.     print for_amp,'/',amp
  34.     for dur in range(freq,freq+32,freq):
  35.         t = 0
  36.         AMPS[amp,dur] = []
  37.         bit = (amp*1.0)/(dur*4)
  38.         for _ in range(dur/4): # up
  39.             ttt()
  40.             t += bit
  41.         for _ in range(dur/2): # down
  42.             ttt()
  43.             t -= bit
  44.         for _ in range(dur/4): # up
  45.             ttt()
  46.             t += bit
  47.  
  48. root = Tk()
  49. canvas = Canvas(root, width=ww, height=hh, bg='white')
  50. canvas.pack()
  51.  
  52. def save_wave(packed_value):
  53.     sss = str(tag)
  54.     noise_output = wave.open(dir+sss+'.wav', 'w')
  55.     noise_output.setparams((2, 2, sample_rate, 0, 'NONE', 'not compressed'))
  56.     noise_output.writeframes('\n'.join(packed_value))
  57.     noise_output.close()
  58.  
  59. def draw_wave(values):
  60.     canvas.delete(ALL)
  61.     canvas.create_line(values, fill='red')
  62.     canvas.update()
  63.  
  64. amplitude = -0.1
  65. frequency = 20
  66.  
  67. '''
  68. t = 100
  69. while 1: # 9282
  70.     for p in itertools.permutations(AMPS, t):
  71.         print t
  72.         t += 1
  73.         break
  74. '''
  75.  
  76. tag = 0
  77. for p in itertools.permutations(AMPS, 3680): # note: keep permutations near lowest
  78.     p = list(p)
  79.     graph = []
  80.     sound = []
  81.     x = 0
  82.     for bit in range(2,int(SAMPLE_LEN*2)+2):
  83.         while 1:
  84.             try:
  85.                 data = segment.pop()
  86.                 break
  87.             except:
  88.                 amp,freq = p.pop()
  89.                 segment = AMPS[amp,freq][:]
  90.         graph += [(x,data*0.12+hh/2)]
  91.         sound += [struct.pack('h', data)]
  92.         x += 1
  93.     draw_wave(graph)
  94.     save_wave(sound)
  95.     tag += 1
Add Comment
Please, Sign In to add comment