############################################################
## Based on code from Python online documention for os.walk
## and the Essentia 'Python tutorial'
############################################################
import os
import essentia
import essentia.standard
import essentia.streaming
from os.path import join, getsize
extensions = dict()
durations = []
with open('durations.txt', 'wb') as f:
for root, dirs, files in os.walk('/Users/ogc/Music/iTunes'):
if 'CVS' in dirs:
dirs.remove('CVS') # don't visit CVS directories
#print sum(getsize(join(root, name)) for name in files),
print 'album: {0}\n'.format(root)
for name in files:
#used this code to get a complete list of different audio formats in my music library
#extension = os.path.splitext(name)[1]
#if extension not in extensions:
# extensions[extension] = 1
#else:
# extensions[extension] += 1
if name.endswith(('.wav', '.aif', 'aiff', '.flac', '.mp3', '.m4a')):
#print root
# and then we actually perform the loading:
try:
loader = essentia.standard.MonoLoader(filename = join(root, name))
#print loader.paramValue('sr') # How can I check for the sampling rate of the file?
audio = loader()
#durations.append(len(audio)/44100.) # Using hard-coded 44100 sr for now
duration_in_seconds = len(audio)/44100.
f.write('{0}, '.format(duration_in_seconds))
print '{0},'.format(duration_in_seconds),
del loader
del audio
gc.collect()
except (RuntimeError, TypeError, NameError):
#print 'could not parse file $s\n' % name
continue
print
#print extensions.keys()
#print durations