never-gonna-give-beep-up

Never Gonna Give You Up but it's played on a PC speaker using the beep command

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
import numpy as np
import pyaudio
from requests import get

audio = pyaudio.PyAudio()
stream = audio.open(
    format = pyaudio.paInt16,
    channels = 1,
    rate=200000,
    input=True
)

# https://stackoverflow.com/questions/45908268/how-to-know-the-frequency-of-audio-from-microphone
# https://stackoverflow.com/questions/3694918/how-to-extract-frequency-associated-with-fft-values-in-python
playingfreq = 0
lastfreq = 0
while True:
    chunk = stream.read(10000)
    w = np.fft.fft(list(chunk))[10:200]
    f = np.fft.fftfreq(20000)
    i = 10+np.argmax(np.abs(w))
    mag = abs(w[i-10])
    freq = abs(2 * f[i] * 200000)

    if mag <= 200000:
        freq = 0
    
    print(freq)
    
    if playingfreq != 0 and abs(freq-playingfreq) > 20 and abs(lastfreq-playingfreq) > 20:
        # Stop playing 
        print('Stopping', playingfreq)
        get('http://10.242.6.228:5000/stopfreq/' + str(playingfreq))
        playingfreq = 0
    if freq != 0 and playingfreq == 0 and abs(freq-lastfreq) <= 20:
        # Nothing playing and freq matches last freq
        print('Starting', freq)
        get('http://10.242.6.228:5000/startfreq/' + str(freq))
        playingfreq = freq
    lastfreq = freq