Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

media_audio.py 1.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. import librosa
  2. import os
  3. import hashlib
  4. from utils import *
  5. class BaseAudio:
  6. '''
  7. Base audio abstraction (signal, sampling rate and duration).
  8. This class should not be used directly
  9. '''
  10. def __init__(self, y, sr, duration) -> None:
  11. self.y = y
  12. self.sr = sr
  13. self.duration = duration
  14. def get_signature(self):
  15. ''''
  16. Returns a hash for the given audio
  17. '''
  18. return hashlib.md5(self.y).hexdigest()
  19. def get_descriptor(self):
  20. '''
  21. Returns a dict describing the current object
  22. '''
  23. descriptor = self.__dict__.copy()
  24. descriptor.pop('y')
  25. descriptor['signature'] = self.get_signature()
  26. return descriptor
  27. class PreprocessedAudio(BaseAudio):
  28. '''
  29. Preprocessed audio type. Used after audio is preprocessed
  30. '''
  31. def __init__(self, y, sr, duration, raw_filepath) -> None:
  32. BaseAudio.__init__(self, y, sr, duration)
  33. self.raw_filepath = raw_filepath
  34. self.label = None
  35. def get_signature(self):
  36. ''''
  37. Returns a hash for the given audio
  38. '''
  39. return hashlib.md5(self.y).hexdigest()
  40. def list_media_files(dir, recursively=False):
  41. list_files = []
  42. for root, dirs, files in os.walk(dir):
  43. for f in files:
  44. if '.wav' not in f:
  45. continue
  46. fullpath = os.path.join(root, f)
  47. if not os.path.exists(fullpath.replace('.wav', '.json')):
  48. continue
  49. list_files.append(make_path_relative(fullpath))
  50. if recursively == False:
  51. break
  52. return list_files
  53. class MediaAudio(BaseAudio):
  54. def __init__(self) -> None:
  55. BaseAudio.__init__(self, None, None, None)
  56. self.filepath = ''
  57. def load(self, path):
  58. self.filepath = path.replace(PROJECT_PATH, '')
  59. self.y, self.sr = librosa.load(path, sr=None)
  60. self.duration = librosa.get_duration(filename=path)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...