Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# from numpy.fft import fft
# from scipy import signal
# from scipy.fft import fftshift
# import matplotlib.pyplot as plt
# import numpy as np
import json
# from scipy.io import wavfile
# from os import path
# from pydub import AudioSegment
# from matplotlib.colors import Normalize
# import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

import threading
from pydub.playback import play
from pydub import AudioSegment
import time

import pandas as pd

data_path = 'ch_wine.txt'
data = {}
with open(data_path, 'r') as infile:
data = json.load(infile)
pxx_data = data['Pxx']
db_data = data['dB']
freqs = data['freqs']

db_df = pd.DataFrame(np.asarray(pxx_data), index=freqs)
db_amp_slice = (db_df + min(db_df))[100:300].sum()#.rolling(window=1, min_periods=0).mean().fillna(0)


# plt.plot(db_amp_slice)
# plt.show()


# Plot Spec
# plt.imshow(db_df, aspect='auto', origin='lower')
# plt.show()
# exit()

# Getting a single frequency's dB data over the course of audio
desired_frequency = 1000
target_index = int((desired_frequency / max(freqs)) * len(freqs))
# print(len(db_data[target_index]))

# Animation Vars
size = 10

# Plot Stuff
fig = plt.figure(figsize=(7, 7))
ax = fig.add_axes([0, 0, 1, 1], frameon=False)
ax.set_xlim(0, 1), ax.set_xticks([])
ax.set_ylim(0, 1), ax.set_yticks([])
scat = ax.scatter([0.5], [0.5], s=[size], lw=0.5, edgecolors=(0, 0, 0, 1), facecolors='blue')#, facecolors='none')

ax.set_xlim(0, 1), ax.set_xticks([])
ax.set_ylim(0, 1), ax.set_yticks([])

# Music stuff
sound = AudioSegment.from_wav("assets/Hozier - Cherry Wine - Cover (Fingerstyle Guitar).wav")
music_thread = threading.Thread(target=play, args=(sound,))
song_length = sound.duration_seconds

global music_start
music_start = 0
def update(frame_number):
global music_start

if frame_number == 0:
music_thread.start()
music_start = time.perf_counter()

print('music_start', music_start)
i = (time.perf_counter() - music_start)/song_length
print('i', i)
print(len(db_amp_slice))
i = round(i * len(db_amp_slice))
print(frame_number, i)

size = db_amp_slice[i]
# size = db_data[target_index][frame_number]
# if size > 1000:
# size = 1000
# if size < 0:
# size = 0
print('size', size)
scat.set_sizes([size])

animation = FuncAnimation(fig, update, interval=5)
plt.show()


print("closing program...")














Binary file added assets/100hz.wav
Binary file not shown.
Binary file not shown.
Binary file added assets/Shepard Tone.wav
Binary file not shown.
136 changes: 65 additions & 71 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,82 @@
from scipy.io import wavfile
from os import path
from pydub import AudioSegment
from matplotlib.colors import Normalize
# import seaborn as sns

# import files
src = "assets/piano2.wav"
# dst = "test.wav"
dst = "assets/piano3.wav"
piano_3 = "assets/piano3.wav"
audio_tone = "assets/100hz.wav"
bread = "assets/bread.mp3"
shepard = "assets/Shepard Tone.wav"
wine = "assets/Hozier - Cherry Wine - Cover (Fingerstyle Guitar).wav"

print("converting to wav file...")
# sound = AudioSegment.from_mp3(src)

# Code for converting Mp3 to Wav file. Need to add other conversions and type checking in the future.
# sound = AudioSegment.from_mp3(shepard)
# sound = sound.set_channels(1)
# sound = sound.set_frame_rate(5000) # sample rate in kHz, higher sample rate, longer spectrogram computation takes
# sound.export(dst, format="wav")

sound = AudioSegment.from_wav(src)
print(sound.duration_seconds)
sound = sound.set_channels(1)
sound = sound.set_frame_rate(5000) # sample rate in kHz
sound.export(dst, format="wav")

print("importing wav file...")
sample_rate, samples = wavfile.read(dst)
print(f"sample_rate:\n{sample_rate}")
print(f"samples:\n{samples}")
print(len(samples))
# sample_rate = len(samples)
# print(samples.tolist())



# !
rng = np.random.default_rng()
# # Generate a test signal, a 2 Vrms sine wave whose frequency is slowly modulated around 3kHz, corrupted by white noise of exponentially decreasing magnitude sampled at 10 kHz.
fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
noise_power = 0.01 * fs / 2
time = np.arange(N) / float(fs)
mod = 500*np.cos(2*np.pi*0.25*time)
carrier = amp * np.sin(2*np.pi*3e3*time + mod)
noise = rng.normal(scale=np.sqrt(noise_power), size=time.shape)
noise *= np.exp(-time/5)
x = carrier + noise

# Compute and plot the spectrogram.
f, t, Sxx = signal.spectrogram(x, fs)

plt.pcolormesh(t, f, Sxx, shading='gouraud')
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.show()
# !

Pxx, freqs, bins, im = plt.specgram(samples, NFFT=1024, Fs=44100, noverlap=900)
# plt.yscale('log')
# AudioSegment.frame_rate
# sound = AudioSegment.from_mp3(bread)
# sound = sound.set_channels(1)
# sound = sound.set_frame_rate(5000)
# sample_rate = AudioSegment.frame_rate
# samples = sound.raw_data

# Reading Wav file. Must be Mono (single channel)
sample_rate, samples = wavfile.read(wine)
print("sample_rate:", sample_rate)

if len(samples[0]) == 2:
print("moving from duo to mono...")
samples = np.asarray(samples)
samples = samples[:,0]
print(samples)

Pxx, freqs, bins, im = plt.specgram(samples, NFFT=1024, Fs=sample_rate, noverlap=128, scale="dB")
# plt.ylim((0, 20000))
# plt.yscale('symlog')
# plt.xscale('log')
# plt.show()

print(np.shape(Pxx))
print(np.shape(freqs))
print(np.shape(bins))

Pxx_dB = 10*np.log(Pxx) # Convert to dB scale
plt.imshow(Pxx_dB, aspect='auto', origin='lower')
plt.show()
# print("exporing data...")

data = {}
data['Pxx'] = Pxx.tolist()
data['dB'] = Pxx_dB.tolist()
data['freqs'] = freqs.tolist()
data['bins'] = bins.tolist()

with open('ch_wine.txt', 'w') as outfile:
json.dump(data, outfile)







print("closing program...")









print("making spectrogram...")
f, t, Sxx = signal.spectrogram(samples, sample_rate, mode='magnitude')
print(f"f:\n{f}")
print(f"t:\n{t}")
print(f"Sxx:\n{Sxx}")

print("making graph...")
# plt.pcolormesh(t, f, Sxx, shading='gouraud')
# plt.ylabel('Frequency [Hz]')
# plt.xlabel('Time [sec]')
# plt.show()

plt.pcolormesh(t, f, np.log10(Sxx), shading='gouraud')
# plt.pcolormesh(t, f, Sxx, shading='gouraud')
plt.imshow(Sxx)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
# plt.yscale('log')
# plt.xscale('log')
plt.show()

print("exporing data...")

# data = {}
# data['f'] = f.tolist()
# data['t'] = t.tolist()
# data['Sxx'] = Sxx.tolist()

# with open('data.txt', 'w') as outfile:
# json.dump(data, outfile)
1 change: 1 addition & 0 deletions shepard.txt

Large diffs are not rendered by default.