Advertisement
Guest User

Untitled

a guest
Nov 27th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. """
  2. Credits go to: https://github.com/Zulko/pianoputer
  3. """
  4.  
  5. from scipy.io import wavfile
  6. import numpy as np
  7. import pygame
  8. import smtplib
  9.  
  10. def speedx(snd_array, factor):
  11. """ Speeds up / slows down a sound, by some factor. """
  12. indices = np.round(np.arange(0, len(snd_array), factor))
  13. indices = indices[indices < len(snd_array)].astype(int)
  14. return snd_array[indices]
  15.  
  16.  
  17. def stretch(snd_array, factor, window_size, h):
  18. """ Stretches/shortens a sound, by some factor. """
  19. phase = np.zeros(window_size)
  20. hanning_window = np.hanning(window_size)
  21. result = np.zeros(int(len(snd_array) / factor + window_size))
  22.  
  23. for i in np.arange(0, len(snd_array) - (window_size + h), h*factor):
  24. i = int(i)
  25. # Two potentially overlapping subarrays
  26. a1 = snd_array[i: i + window_size]
  27. a2 = snd_array[i + h: i + window_size + h]
  28.  
  29. # The spectra of these arrays
  30. s1 = np.fft.fft(hanning_window * a1)
  31. s2 = np.fft.fft(hanning_window * a2)
  32.  
  33. # Rephase all frequencies
  34. phase = (phase + np.angle(s2/s1)) % 2*np.pi
  35.  
  36. a2_rephased = np.fft.ifft(np.abs(s2)*np.exp(1j*phase))
  37. i2 = int(i/factor)
  38. result[i2: i2 + window_size] += hanning_window*a2_rephased.real
  39.  
  40. # normalize (16bit)
  41. result = ((2**(16-4)) * result/result.max())
  42.  
  43. return result.astype('int16')
  44.  
  45.  
  46. def pitchshift(snd_array, n, window_size=2**13, h=2**11):
  47. """ Changes the pitch of a sound by ``n`` semitones. """
  48. factor = 2**(1.0 * n / 12.0)
  49. stretched = stretch(snd_array, 1.0/factor, window_size, h)
  50. return speedx(stretched[window_size:], factor)
  51.  
  52.  
  53. def init_sounds():
  54. fps, sound = wavfile.read('bowl.wav')
  55.  
  56. tones = range(-25, 25)
  57. print('initializing the sounds')
  58. transposed_sounds = [pitchshift(sound, n) for n in tones]
  59. print('done')
  60.  
  61. return transposed_sounds
  62.  
  63.  
  64. from flask import Flask, request
  65.  
  66. # Preprocess the keys and notes
  67. app = Flask(__name__)
  68. fps, _ = wavfile.read('bowl.wav')
  69. transposed_sounds = init_sounds()
  70. pygame.mixer.init(fps, -16, 1, 2048)
  71. sounds = map(pygame.sndarray.make_sound, transposed_sounds)
  72. key_sound = list(sounds)
  73. print('There are {} keys to be played from 0 to {}'.format(
  74. len(key_sound), len(key_sound) - 1))
  75.  
  76.  
  77. @app.route("/")
  78. def play():
  79. """
  80. index route, playing the note.
  81.  
  82. accepts: /?key={key}
  83.  
  84. e.g. /?key=10 to play note number 10
  85. """
  86. key = int(request.args.get('key'))
  87. print('Playing key {}'.format(key))
  88. key_sound[key].play(fade_ms=50)
  89. return 'Playing key {}'.format(key)
  90.  
  91. @app.route("/sendMove")
  92. def send():
  93. gmail_user = ''
  94. gmail_password = ''
  95.  
  96. sent_from = gmail_user
  97. to = ['']
  98. subject = 'PiggyBank(Moved)'
  99. body = 'Someone moved your PiggyBank!!'
  100.  
  101. message = 'Subject: {}\n\n{}'.format(subject, body)
  102.  
  103. try:
  104. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  105. server.ehlo()
  106. server.login(gmail_user, gmail_password)
  107. server.sendmail(sent_from, to, message)
  108. server.close()
  109.  
  110. return('Email sent!')
  111. except:
  112. return('Something went wrong...')
  113.  
  114. @app.route("/sendClose")
  115. def sendd():
  116. gmail_user = ''
  117. gmail_password = ''
  118.  
  119. sent_from = gmail_user
  120. to = ['']
  121. subject = 'PiggyBank(Close)'
  122. body = 'Someone get close to your PiggyBank!!'
  123.  
  124. message = 'Subject: {}\n\n{}'.format(subject, body)
  125.  
  126. try:
  127. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  128. server.ehlo()
  129. server.login(gmail_user, gmail_password)
  130. server.sendmail(sent_from, to, message)
  131. server.close()
  132.  
  133. return('Email sent!')
  134. except:
  135. return('Something went wrong...')
  136.  
  137.  
  138. # start the server at the port 5000
  139. app.run(host = '0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement