6
6
except ImportError :
7
7
pass
8
8
from sqlalchemy import text
9
- import yaml
10
9
import os
11
- # import time
12
10
import tempfile
13
11
import subprocess
14
12
@@ -335,30 +333,24 @@ def __init__(self, path):
335
333
self .frames = None
336
334
337
335
@staticmethod
338
- def writeProfileFile (fh ):
336
+ def analyze (path ):
337
+ analysis = SongAnalysis (path )
338
+
339
339
dir = '/usr/share/essentia-extractor-svm_models/'
340
340
history_files = [os .path .join (dir , x )
341
341
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 ()
345
342
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 )
362
354
363
355
return analysis
364
356
@@ -381,24 +373,10 @@ def import_keys_with_stats(self):
381
373
self .connection .execute (t .insert ().values (** vars ))
382
374
383
375
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)))
388
376
for key in keys_with_lists_stats :
389
377
if key in keys_to_ignore :
390
378
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))]
400
379
401
- # self.connection.execute(sql, vars)
402
380
for pos in range (len (self .stats [key + '.var' ])):
403
381
t = table ('analysis.' + key .replace ('.' , '__' ).lower () +
404
382
'_stats' )
@@ -421,19 +399,13 @@ def import_fkeys_with_lists(self):
421
399
for key in fkeys_with_lists :
422
400
if key in keys_to_ignore :
423
401
continue
424
- # t = table('analysis.' + key.replace('.', '__').lower())
425
402
tablename = 'analysis.' + key .replace ('.' , '__' ).lower ()
426
403
sql = text (f'INSERT INTO { tablename } (song_id, pos, value) '
427
404
f'VALUES ({ self .song_id } , :pos, :value)' )
428
405
vars = [{'pos' : pos , 'value' : val .item ()}
429
406
for pos , val in enumerate (self .frames [key ])]
430
407
431
408
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))
437
409
438
410
def import_fkeys_with_lists_of_lists (self ):
439
411
for key in fkeys_with_lists_of_lists :
@@ -446,13 +418,6 @@ def import_fkeys_with_lists_of_lists(self):
446
418
f'VALUES ({ self .song_id } , :pos, :values)' )
447
419
448
420
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))
456
421
457
422
def import_conversion_dict (self ):
458
423
tables = set (x [0 ] for x in conversion_dict .values ())
@@ -464,7 +429,6 @@ def import_conversion_dict(self):
464
429
self .connection .execute (t .insert ().values (** vars ))
465
430
466
431
def import_analysis (self , * , connection = None ):
467
- # time1 = time.time()
468
432
if not self .stats or not self .frames :
469
433
raise ValueError ("There's no analysis to import" )
470
434
@@ -486,8 +450,6 @@ def import_analysis(self, *, connection=None):
486
450
if not connection :
487
451
self .connection .commit ()
488
452
self .connection = None
489
- # time2 = time.time()
490
- # print('function took {:.3f} ms'.format((time2 - time1) * 1000.0))
491
453
492
454
def has_analysis_for_song_id (self , song_id , * , connection = None ):
493
455
if not connection :
0 commit comments