Advertisement
here2share

# Tk_audio_generator.py ZZZ

Feb 14th, 2021
1,022
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. # Tk_audio_generator.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. 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.  
  42. amplitude = 0.1
  43. frequency = 20
  44. # for frequency in range(20,20000,20):
  45. graph = []
  46. sound = []
  47. for bit in range(SAMPLE_LEN*2):
  48.     data = math.sin(bit**amplitude*frequency)
  49.     graph += [(bit,data*(hh-10)/2+hh/2)]
  50.     sound += [struct.pack('h', data*for_amp)]
  51. draw_wave(graph)
  52. save_wave(sound)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement