import csv
import string
rcParams['figure.figsize'] = (20, 5)
with open('rms_and_metadata.txt', 'rb') as csvfile:
#determine csv format and read the data
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)
#create container for data
data = dict()
for row in reader:
if row[2] not in data: #add a new genre
data[row[2]] = []
data[row[2]].append(row[0]) #append an rms value
output = []
for genre in data:
rms_sum = 0
rms_count = len(data[genre])
for rms in data[genre]:
rms_sum += float(rms)
data[genre] = rms_sum / rms_count
output.append((genre, data[genre]))
output.sort(key = lambda x: x[1], reverse=True)
#print output
highest = output[:10]
#print highest
#print '~~~~~~~~~~~~~~'
lowest = output[-11:-1]
#print lowest
pyplot.figure(1)
#pyplot.subplot(211)
pyplot.bar(range(len(highest)), [pair[1] for pair in highest], align='center')
pyplot.xticks(range(len(highest)), [pair[0] for pair in highest])
pyplot.title('Highest average RMS, by genre')
pyplot.ylim(ymax=15000)
pyplot.xticks(rotation='vertical')
pyplot.figure(2)
pyplot.bar(range(len(lowest)), [pair[1] for pair in lowest], align='center')
pyplot.xticks(range(len(lowest)), [pair[0] for pair in lowest])
pyplot.title('Lowest average RMS, by genre')
pyplot.ylim(ymax=15000)
pyplot.xticks(rotation='vertical')
pyplot.show()
import os
from os.path import join, getsize
from pydub import AudioSegment
rcParams['figure.figsize'] = (20, 10)
window_size = 500
for root, dirs, files in os.walk('/Users/ogc/Documents/ucsb/mat/240e/media'):
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
#print sum(getsize(join(root, name)) for name in files),
for name in files:
if name.endswith(('.wav', '.mp3', '.m4a')):
try:
#load the file
filename = join(root, name)
songname, filetype = name.split('.')
audio = AudioSegment.from_file(filename, filetype)
rms = []
#get windowed RMS
duration_ms = len(audio)
begin = 0
end = window_size
hop_factor = 1
hop_size = window_size / hop_factor
while end < duration_ms:
window = audio[begin:end]
rms.append(window.rms)
begin += hop_size
end += hop_size
pyplot.plot(rms, label=songname)
except:
continue
#pyplot.xlim(xmax=1500)
pyplot.ylim(ymax=25000)
pyplot.title('RMS - Window Size: {0} ms'.format(window_size))
pyplot.legend()
pyplot.show()
window_size = 100
for root, dirs, files in os.walk('/Users/ogc/Documents/ucsb/mat/240e/media'):
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
#print sum(getsize(join(root, name)) for name in files),
for name in files:
if name.endswith(('.wav', '.mp3', '.m4a')):
try:
#load the file
filename = join(root, name)
songname, filetype = name.split('.')
audio = AudioSegment.from_file(filename, filetype)
rms = []
#get windowed RMS
duration_ms = len(audio)
begin = 0
end = window_size
hop_factor = 1
hop_size = window_size / hop_factor
while end < duration_ms:
window = audio[begin:end]
rms.append(window.rms)
begin += hop_size
end += hop_size
pyplot.plot(rms, label=songname)
except:
continue
#pyplot.xlim(xmax=1500)
pyplot.ylim(ymax=25000)
pyplot.title('RMS - Window Size: {0}'.format(window_size))
pyplot.legend()
pyplot.show()
window_size = 10
for root, dirs, files in os.walk('/Users/ogc/Documents/ucsb/mat/240e/media'):
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
#print sum(getsize(join(root, name)) for name in files),
for name in files:
if name.endswith(('.wav', '.mp3', '.m4a')):
try:
#load the file
filename = join(root, name)
songname, filetype = name.split('.')
audio = AudioSegment.from_file(filename, filetype)
rms = []
#get windowed RMS
duration_ms = len(audio)
begin = 0
end = window_size
hop_factor = 1
hop_size = window_size / hop_factor
while end < duration_ms:
window = audio[begin:end]
rms.append(window.rms)
begin += hop_size
end += hop_size
pyplot.plot(rms, label=songname)
except:
continue
#pyplot.xlim(xmax=1500)
pyplot.ylim(ymax=25000)
pyplot.title('RMS - Window Size: {0}'.format(window_size))
pyplot.legend()
pyplot.show()