Advertisement
here2share

# Tk_audio_generator2.py

Feb 14th, 2021 (edited)
921
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # Tk_audio_generator2.py ZZZ not near what I hope for, but... will most likely do from data training
  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. sample_rate = 44100
  19. duration = 1 # 1 second of audio
  20. SAMPLE_LEN = sample_rate * duration
  21.  
  22. # -32767 to 32767
  23. for_amp = 32767
  24.    
  25. root = Tk()
  26. canvas = Canvas(root, width=ww, height=hh, bg='white')
  27. canvas.pack()
  28.  
  29. def save_wave(packed_value):
  30.     sss = str(frequency)+'_'+str(0)
  31.     noise_output = wave.open(dir+sss+'.wav', 'w')
  32.     noise_output.setparams((2, 2, sample_rate, 0, 'NONE', 'not compressed'))
  33.     noise_output.writeframes('\n'.join(packed_value))
  34.     noise_output.close()
  35.  
  36. def draw_wave(values):
  37.     canvas.delete(ALL)
  38.     canvas.create_line(values, fill='red')
  39.     canvas.update()
  40.  
  41. amplitude = 0.12
  42. for frequency in range(1,20000):
  43.     graph = []
  44.     sound = []
  45.     for bit in range(SAMPLE_LEN*2):
  46.         amp = math.sin((bit+16)**amplitude*frequency)
  47.         b = bit*0.1
  48.         data = (amp+math.sin(b*frequency)+math.sin(b*(frequency+2)))/3
  49.         graph += [(bit,data*(hh-10/2-bit)+hh/2)]
  50.         sound += [struct.pack('h', data*100)]
  51.     draw_wave(graph)
  52.     save_wave(sound)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement