Skip to content

Commit 5735824

Browse files
committed
Use MusicExtractor highlevel argument and remove commented code
This requires essentia to be patched with MTG/essentia#1052 in order to fix MTG/essentia#1051
1 parent 69318d3 commit 5735824

File tree

1 file changed

+14
-52
lines changed

1 file changed

+14
-52
lines changed

bard/analysis_database.py

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
except ImportError:
77
pass
88
from sqlalchemy import text
9-
import yaml
109
import os
11-
# import time
1210
import tempfile
1311
import subprocess
1412

@@ -335,30 +333,24 @@ def __init__(self, path):
335333
self.frames = None
336334

337335
@staticmethod
338-
def writeProfileFile(fh):
336+
def analyze(path):
337+
analysis = SongAnalysis(path)
338+
339339
dir = '/usr/share/essentia-extractor-svm_models/'
340340
history_files = [os.path.join(dir, x)
341341
for x in os.listdir(dir) if x.endswith('.history')]
342-
profile = {'highlevel': {'compute': 1, 'svm_models': history_files}}
343-
fh.write(yaml.dump(profile))
344-
fh.flush()
345342

346-
@staticmethod
347-
def analyze(path):
348-
analysis = SongAnalysis(path)
349-
350-
with (tempfile.NamedTemporaryFile(mode='w',
351-
prefix='bard_essentia_profile')) as profile_file:
352-
SongAnalysis.writeProfileFile(profile_file)
353-
extractor = MusicExtractor(profile=profile_file.name)
354-
if any(path.lower().endswith(x) for x in essentia_allowed_exts):
355-
analysis.stats, analysis.frames = extractor(path)
356-
else:
357-
with (tempfile.NamedTemporaryFile(mode='w',
358-
prefix='bard_raw_audio', suffix='.wav')) as raw:
359-
args = ['ffmpeg', '-y', '-i', path, '-ac', '2', raw.name]
360-
subprocess.run(args, capture_output=True)
361-
analysis.stats, analysis.frames = extractor(raw.name)
343+
extractor = MusicExtractor(highlevel=history_files,
344+
lowlevelSilentFrames='keep',
345+
tonalSilentFrames='keep')
346+
if any(path.lower().endswith(x) for x in essentia_allowed_exts):
347+
analysis.stats, analysis.frames = extractor(path)
348+
else:
349+
with (tempfile.NamedTemporaryFile(mode='w',
350+
prefix='bard_raw_audio', suffix='.wav')) as raw:
351+
args = ['ffmpeg', '-y', '-i', path, '-ac', '2', raw.name]
352+
subprocess.run(args, capture_output=True)
353+
analysis.stats, analysis.frames = extractor(raw.name)
362354

363355
return analysis
364356

@@ -381,24 +373,10 @@ def import_keys_with_stats(self):
381373
self.connection.execute(t.insert().values(**vars))
382374

383375
def import_keys_with_lists_stats(self):
384-
# st_keys = [k for k in stat_variables.keys()]
385-
# sql_pat = ('INSERT INTO {0} (song_id, pos, %s) VALUES (%d, :pos, %s)' %
386-
# (','.join(stat_variables[k] for k in st_keys), self.song_id,
387-
# ','.join(':' + k for k in st_keys)))
388376
for key in keys_with_lists_stats:
389377
if key in keys_to_ignore:
390378
continue
391-
# tablename = 'analysis.' + key.replace('.', '__').lower() + '_stats'
392-
# sql = text(sql_pat.format(tablename))
393-
# lists = [self.stats[key + '.' + st_key] for st_key in st_keys]
394-
395-
# vars = [{'pos': pos, **{st_key: val.item() for st_key, val in zip(st_keys, values)}}
396-
# for pos, values in enumerate(zip(*lists))]
397-
398-
# vars = [{'pos': pos, **{st_key: val.item() for st_key, val in zip(st_keys, values)}}
399-
# for pos, values in enumerate(zip(self.stats[key + '.' + st_key] for st_key in st_keys))]
400379

401-
# self.connection.execute(sql, vars)
402380
for pos in range(len(self.stats[key + '.var'])):
403381
t = table('analysis.' + key.replace('.', '__').lower() +
404382
'_stats')
@@ -421,19 +399,13 @@ def import_fkeys_with_lists(self):
421399
for key in fkeys_with_lists:
422400
if key in keys_to_ignore:
423401
continue
424-
# t = table('analysis.' + key.replace('.', '__').lower())
425402
tablename = 'analysis.' + key.replace('.', '__').lower()
426403
sql = text(f'INSERT INTO {tablename} (song_id, pos, value) '
427404
f'VALUES ({self.song_id}, :pos, :value)')
428405
vars = [{'pos': pos, 'value': val.item()}
429406
for pos, val in enumerate(self.frames[key])]
430407

431408
self.connection.execute(sql, vars)
432-
# for pos in range(len(self.frames[key])):
433-
# vars = {'value': self.frames[key][pos].item(),
434-
# 'song_id': self.song_id,
435-
# 'pos': pos}
436-
# self.connection.execute(t.insert(), **vars))
437409

438410
def import_fkeys_with_lists_of_lists(self):
439411
for key in fkeys_with_lists_of_lists:
@@ -446,13 +418,6 @@ def import_fkeys_with_lists_of_lists(self):
446418
f'VALUES ({self.song_id}, :pos, :values)')
447419

448420
self.connection.execute(sql, vars)
449-
# for pos in range(len(self.frames[key[0]])):
450-
# t = table('analysis.' + key[0].replace('.', '__').lower())
451-
# vars = {'values': list(x.item()
452-
# for x in self.frames[key[0]][pos]),
453-
# 'song_id': self.song_id,
454-
# 'pos': pos}
455-
# self.connection.execute(t.insert().values(**vars))
456421

457422
def import_conversion_dict(self):
458423
tables = set(x[0] for x in conversion_dict.values())
@@ -464,7 +429,6 @@ def import_conversion_dict(self):
464429
self.connection.execute(t.insert().values(**vars))
465430

466431
def import_analysis(self, *, connection=None):
467-
# time1 = time.time()
468432
if not self.stats or not self.frames:
469433
raise ValueError("There's no analysis to import")
470434

@@ -486,8 +450,6 @@ def import_analysis(self, *, connection=None):
486450
if not connection:
487451
self.connection.commit()
488452
self.connection = None
489-
# time2 = time.time()
490-
# print('function took {:.3f} ms'.format((time2 - time1) * 1000.0))
491453

492454
def has_analysis_for_song_id(self, song_id, *, connection=None):
493455
if not connection:

0 commit comments

Comments
 (0)