Commit a725063c authored by Robozman's avatar Robozman

Refactored to not be shit

parent 719e8676
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
......@@ -8,22 +8,37 @@ import time
import math
import msaf
import librosa
import sys
import generate_waves
volume = 0.5 # range [0.0, 1.0]
fs = 44100 # sampling rate, Hz, must be integer
duration = 1 # in seconds, may be float
f = 1440.0 # sine frequency, Hz, may be float
load_from_file = False
file_name = None
wave_type = 0
fs=44100
# generate samples, note conversion to float32 array
samples = np.zeros(10 * fs)
# for i in range(0, 10):
# curr_freq = 440 + (i * 1000)
# samples[(i * fs):((i + 1) * fs)] = np.sin(2*np.pi*np.arange(fs*duration)*curr_freq/fs).astype(np.float32)
if len(sys.argv) == 1:
print("Provide a file name or an integer as an argument")
exit()
else:
try:
wave_type = int(sys.argv[1])
except:
load_from_file = True
file_name = sys.argv[1]
for i in range(0, 10):
curr_freq = 440 + (i * 1000)
samples[(i * fs):((i + 1) * fs)] = signal.square(2*np.pi*np.arange(fs*duration)*curr_freq/fs).astype(np.float32)
if load_from_file == False:
wav_data, rate = generate_waves.generate_waves(wave_type, 52)
elif load_from_file == True:
rate, wav_data = wavfile.read(sys.argv[1], mmap=True)
# wav_data = samples
boundaries, labels = msaf.process(sys.argv[1])
audio = librosa.load(sys.argv[1], sr=None)[0]
audio_harmonic, audio_percussive = librosa.effects.hpss(audio)
tempo, frames = librosa.beat.beat_track(y=audio_harmonic,
sr=fs, hop_length=1024)
beat_times = librosa.frames_to_time(frames, sr=fs,
hop_length=1024)
plt.ion()
fig = plt.figure()
......@@ -34,16 +49,6 @@ def plot(data):
plt.plot(data, color='steelblue')
rate, wav_data = wavfile.read("violin.wav", mmap=True)
# wav_data = samples
boundaries, labels = msaf.process("violin.wav")
audio = librosa.load("violin.wav", sr=fs)[0]
audio_harmonic, audio_percussive = librosa.effects.hpss(audio)
tempo, frames = librosa.beat.beat_track(y=audio_harmonic,
sr=fs, hop_length=1024)
beat_times = librosa.frames_to_time(frames, sr=fs,
hop_length=1024)
print(frames)
sounddevice.play(wav_data, rate)
timestep = 1/24
......@@ -51,9 +56,10 @@ t = time.time()
origin = time.time()
nextBeat = 0
while t - origin < len(wav_data) / rate:
if time.time() - origin >= beat_times[nextBeat]:
nextBeat += 1
print("Beat")
if load_from_file == True:
if time.time() - origin >= beat_times[nextBeat]:
nextBeat += 1
print("Beat")
while (time.time() - t < timestep):
pass
......@@ -65,6 +71,9 @@ while t - origin < len(wav_data) / rate:
# print(second)
fft_plot.clear()
fft_plot.plot(np.abs(np.fft.fftshift(np.fft.fft(second[:,0]))))
if load_from_file == True:
fft_plot.plot(np.abs(np.fft.fftshift(np.fft.fft(second[:,0]))))
else:
fft_plot.plot(np.abs(np.fft.fftshift(np.fft.fft(second[:]))))
fig.canvas.draw()
fig.canvas.flush_events()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment