-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHumPiano.py
More file actions
37 lines (34 loc) · 1.41 KB
/
Copy pathHumPiano.py
File metadata and controls
37 lines (34 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import matplotlib.pyplot as plt
import matplotlib.patches as pat
import numpy as np
import pygame
from scipy.fftpack import fft
samplerate = 48000 # Get the sampling rate right: http://www.audiomountain.com/tech/audio-file-size.html
pygame.mixer.init(samplerate)
music = pygame.mixer.Sound(r"D:\Users\Charles Turvey\Music\SFX\Rising.wav")
musicarraystereo = pygame.sndarray.samples(music)
pygame.mixer.find_channel().play(pygame.sndarray.make_sound(musicarraystereo))
# Fast Fourier Transform!
T = 1 / samplerate
N = musicarraystereo.shape[0]
subN = 1000
print(N, subN, T)
x = np.float32([]) # Time
y = np.float32([]) # Frequency
z = np.float32([]) # Amplitude?
for i in range(0, N - subN):
musicarraystereoportion = musicarraystereo[i:i + subN]
subN = min(subN, musicarraystereoportion.shape[0])
musicfourier = fft(musicarraystereoportion)
freqout = np.linspace(0, 1 / (2 * T), subN // 2)
ampout = (2 / subN) * np.abs(musicfourier[0:subN // 2])
if ampout.shape[0] == 0:
break
print("%ss - %ss: Max at %s Hz (amp = %s)" % (i * T,
(i + subN) * T,
max(freqout[np.argmax(ampout, axis=0)]),
np.max(ampout)))
x = np.append(x, (i + (subN/2)) * T)
y = np.append(y, max(freqout[np.argmax(ampout, axis=0)]))
plt.scatter(x, y, color="#000000")
plt.show()