Last active
June 5, 2018 19:42
-
-
Save dchabot/26f625cb1d49ee716110ae2097279663 to your computer and use it in GitHub Desktop.
This file contains 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
import os | |
import soundfile as sd | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import re | |
flacs = list() | |
for root, dir, files in os.walk('/Users/chabot/Dropbox/Music'): | |
for file in files: | |
if file.endswith('.flac'): | |
flacs.append(os.path.join(root, file)) | |
nplots = 5 | |
for i in range(nplots): | |
track = np.random.choice(flacs) | |
print('Using track ', track) | |
data, samplerate = sd.read(track) | |
window = samplerate*3 | |
rms_t = list() | |
peaks_t = list() | |
for ch in range(data.shape[1]): | |
raw_chunks = np.array_split(data[:, ch], data.shape[0]/window, axis=0) | |
rms = [20*np.log10(np.sqrt(np.sum(chunk**2)/chunk.shape[0])) for chunk in raw_chunks] | |
peaks = [20*np.log10(np.max(np.abs(chunk))) for chunk in raw_chunks] | |
print('ch{} avg rms = {} avg peak = {}'.format(ch, np.mean(rms), np.mean(peaks))) | |
rms_t.append(rms) | |
peaks_t.append(peaks) | |
# unpack ch0/ch1 and average | |
rms_t = [(a + b)/2 for a, b in zip(rms_t[0], rms_t[1])] | |
peaks_t = [(a + b)/2 for a, b in zip(peaks_t[0], peaks_t[1])] | |
bins = np.linspace(-100, 0, 101) | |
plt.subplot(nplots, 1, i+1) | |
plt.hist(rms_t, bins, color='red', alpha=0.5, label='rms') | |
plt.hist(peaks_t, bins, color='blue', alpha=0.5, label='peak') | |
plt.ylim([0.5, 200]) | |
plt.yscale('log') | |
ts = track.split('/') | |
artist = ts[-3] | |
album = ts[-2] | |
song = re.findall(r"[\w\s']+", ts[-1])[-2] | |
plt.title('{}, {}, {}'.format(artist, album, song), y=0.75, fontsize=10) | |
plt.legend(loc='center left') | |
plt.xlabel('dB') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment