Created
September 23, 2023 17:17
-
-
Save gwbischof/ae63bb171f3017343d2ed33814d27edd to your computer and use it in GitHub Desktop.
Skully
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.io import wavfile | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pyfirmata | |
import pyaudio | |
import wave | |
import time | |
vocals = '/Users/gbischof/Downloads/Bush-Machinehead-Isolated-Vocals.wav' | |
full = '/Users/gbischof/Downloads/Bush-Machinehead.wav' | |
def plot_wav(wav_path='/Users/gbischof/Downloads/Bush-Machinehead-Isolated-Vocals.wav'): | |
rate, data = wavfile.read(wav_path) | |
length = data.shape[0] / rate | |
time = np.linspace(0., length, data.shape[0]) | |
normalized_data = np.absolute(data_np) | |
plt.plot(time, normalized_data, label="Audio") | |
plt.legend() | |
plt.xlabel("Time [s]") | |
plt.ylabel("Amplitude") | |
plt.show() | |
def plot_data(data): | |
length = data.shape[0] | |
time = np.linspace(0., length, data.shape[0]) | |
plt.plot(time, data, label="Audio") | |
plt.legend() | |
plt.xlabel("Time [s]") | |
plt.ylabel("Amplitude") | |
plt.show() | |
def normalize(data): | |
# Use only second collumn of wav data. | |
data = data[:,1] | |
data = np.absolute(data) | |
mean = np.mean(data[data>0]) | |
std = np.std(data[data>0]) | |
max_val = mean + (std * 5) | |
trunc = np.frompyfunc(lambda x: max_val if x >= max_val else x, 1, 1) | |
data = trunc(data) | |
# Convert to float. | |
data = data.astype(float) | |
return data / max_val | |
def load_np(wav_path): | |
rate, data = wavfile.read(wav_path) | |
return data | |
def sing(vocals_path=vocals, wav_path=full, offset=575): | |
data_np = load_np(vocals_path) | |
data_np = normalize(data_np) | |
# Setup audrino. | |
board=pyfirmata.Arduino('/dev/cu.usbmodem14101') | |
it=pyfirmata.util.Iterator(board) | |
it.start() | |
# Assign servo to variable. | |
masseter = board.get_pin('d:9:s') | |
#open a wav format music | |
f = wave.open(wav_path,"rb") | |
#instantiate PyAudio | |
p = pyaudio.PyAudio() | |
#open stream | |
stream = p.open(format = p.get_format_from_width(f.getsampwidth()), | |
channels = f.getnchannels(), | |
rate = f.getframerate(), | |
output = True) | |
#read data | |
chunk = 1024 | |
for i in range(offset): | |
data = f.readframes(chunk) | |
print("START") | |
#play stream | |
i = 0 | |
while data: | |
amplitude = max(data_np[i*chunk:(i+1)*chunk]) * 50 | |
# Move masseter. | |
masseter.write(amplitude) | |
stream.write(data) | |
print(i) | |
data = f.readframes(chunk) | |
i += 1 | |
#stop stream | |
stream.stop_stream() | |
stream.close() | |
#close PyAudio | |
p.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment