-
Notifications
You must be signed in to change notification settings - Fork 0
/
audiodatabase.py
89 lines (72 loc) · 2.43 KB
/
audiodatabase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Author Giampaolo Ferraro
Date December 2023
Handles reading and writing tracks (or segments) to a large database. Uses HDF5 as a backing store.
"""
import h5py
import os
import logging
import filelock
import numpy as np
from dateutil.parser import parse as parse_date
import json
import numpy as np
special_datasets = [
"tag_frames",
"original_frames",
"background_frame",
"predictions",
"overlay",
]
class HDF5Manager:
"""Class to handle locking of HDF5 files."""
LOCK_FILE = "/var/lock/classifier-hdf5.lock"
READ_ONLY = False
def __init__(self, db, mode="r"):
self.mode = mode
self.f = None
self.db = db
self.lock = filelock.FileLock(HDF5Manager.LOCK_FILE, timeout=60 * 3)
filelock.logger().setLevel(logging.ERROR)
def __enter__(self):
# note: we might not have to lock when in read only mode?
# this could improve performance
if HDF5Manager.READ_ONLY and self.mode != "r":
raise ValueError("Only read can be done in readonly mode")
if not HDF5Manager.READ_ONLY:
self.lock.acquire()
self.f = h5py.File(self.db, self.mode)
return self.f
def __exit__(self, exc_type, exc_val, exc_tb):
try:
self.f.close()
finally:
if not HDF5Manager.READ_ONLY:
self.lock.release()
class AudioDatabase:
def __init__(self, database_filename, read_only=False):
"""
Initialises given database. If database does not exist an empty one is created.
:param database_filename: filename of database
"""
self.database = database_filename
if not os.path.exists(database_filename):
logging.info("Creating new database %s", database_filename)
f = h5py.File(database_filename, "w")
f.create_group("recs")
f.close()
HDF5Manager.READ_ONLY = read_only
def set_read_only(self, read_only):
HDF5Manager.READ_ONLY = read_only
def has_rec(self, rec_id):
"""
Returns if database contains track information for given clip
:param clip_id: name of clip
:return: If the database contains given clip
"""
with HDF5Manager(self.database) as f:
clips = f["recs"]
has_record = rec_id in clips and "finished" in clips[rec_id].attrs
if has_record:
return True
return False