kjysmu commited on
Commit
eb1ddbb
·
verified ·
1 Parent(s): 945aa1e

Update utils/mir_eval_modules.py

Browse files
Files changed (1) hide show
  1. utils/mir_eval_modules.py +48 -48
utils/mir_eval_modules.py CHANGED
@@ -26,64 +26,64 @@ def idx2voca_chord():
26
  idx2voca_chord[i] = chord
27
  return idx2voca_chord
28
 
29
- def audio_file_to_features(audio_file, config):
30
- original_wav, sr = librosa.load(audio_file, sr=config.mp3['song_hz'], mono=True)
31
-
32
- # Total duration in seconds
33
- total_duration = librosa.get_duration(y=original_wav, sr=sr)
34
-
35
- # Define segment length in seconds (you can still use config.mp3['inst_len'] for the segment size)
36
- segment_len_sec = config.mp3.get('inst_len', 10) # default to 10s if not defined
37
-
38
- currunt_sec_hz = 0
39
- feature = None
40
-
41
- while currunt_sec_hz + sr * segment_len_sec < len(original_wav):
42
- start_idx = int(currunt_sec_hz)
43
- end_idx = int(currunt_sec_hz + sr * segment_len_sec)
44
- tmp = librosa.cqt(
45
- original_wav[start_idx:end_idx], sr=sr,
46
- n_bins=config.feature['n_bins'],
47
- bins_per_octave=config.feature['bins_per_octave'],
48
- hop_length=config.feature['hop_length']
49
- )
50
- feature = tmp if feature is None else np.concatenate((feature, tmp), axis=1)
51
- currunt_sec_hz = end_idx
52
-
53
- # Handle the final chunk (remaining audio)
54
- tmp = librosa.cqt(
55
- original_wav[currunt_sec_hz:], sr=sr,
56
- n_bins=config.feature['n_bins'],
57
- bins_per_octave=config.feature['bins_per_octave'],
58
- hop_length=config.feature['hop_length']
59
- )
60
- feature = np.concatenate((feature, tmp), axis=1)
61
- feature = np.log(np.abs(feature) + 1e-6)
62
 
63
- feature_per_second = segment_len_sec / config.model['timestep']
64
- song_length_second = len(original_wav) / sr
65
 
66
- return feature, feature_per_second, song_length_second
 
67
 
68
- # def audio_file_to_features(audio_file, config):
69
- # original_wav, sr = librosa.load(audio_file, sr=config.mp3['song_hz'], mono=True)
70
  # currunt_sec_hz = 0
71
- # while len(original_wav) > currunt_sec_hz + config.mp3['song_hz'] * config.mp3['inst_len']:
 
 
72
  # start_idx = int(currunt_sec_hz)
73
- # end_idx = int(currunt_sec_hz + config.mp3['song_hz'] * config.mp3['inst_len'])
74
- # tmp = librosa.cqt(original_wav[start_idx:end_idx], sr=sr, n_bins=config.feature['n_bins'], bins_per_octave=config.feature['bins_per_octave'], hop_length=config.feature['hop_length'])
75
- # if start_idx == 0:
76
- # feature = tmp
77
- # else:
78
- # feature = np.concatenate((feature, tmp), axis=1)
 
 
79
  # currunt_sec_hz = end_idx
80
- # tmp = librosa.cqt(original_wav[currunt_sec_hz:], sr=sr, n_bins=config.feature['n_bins'], bins_per_octave=config.feature['bins_per_octave'], hop_length=config.feature['hop_length'])
 
 
 
 
 
 
 
81
  # feature = np.concatenate((feature, tmp), axis=1)
82
  # feature = np.log(np.abs(feature) + 1e-6)
83
- # feature_per_second = config.mp3['inst_len'] / config.model['timestep']
84
- # song_length_second = len(original_wav)/config.mp3['song_hz']
 
 
85
  # return feature, feature_per_second, song_length_second
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  # Audio files with format of wav and mp3
88
  def get_audio_paths(audio_dir):
89
  return [os.path.join(root, fname) for (root, dir_names, file_names) in os.walk(audio_dir, followlinks=True)
 
26
  idx2voca_chord[i] = chord
27
  return idx2voca_chord
28
 
29
+ # def audio_file_to_features(audio_file, config):
30
+ # original_wav, sr = librosa.load(audio_file, sr=config.mp3['song_hz'], mono=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
+ # # Total duration in seconds
33
+ # total_duration = librosa.get_duration(y=original_wav, sr=sr)
34
 
35
+ # # Define segment length in seconds (you can still use config.mp3['inst_len'] for the segment size)
36
+ # segment_len_sec = config.mp3.get('inst_len', 10) # default to 10s if not defined
37
 
 
 
38
  # currunt_sec_hz = 0
39
+ # feature = None
40
+
41
+ # while currunt_sec_hz + sr * segment_len_sec < len(original_wav):
42
  # start_idx = int(currunt_sec_hz)
43
+ # end_idx = int(currunt_sec_hz + sr * segment_len_sec)
44
+ # tmp = librosa.cqt(
45
+ # original_wav[start_idx:end_idx], sr=sr,
46
+ # n_bins=config.feature['n_bins'],
47
+ # bins_per_octave=config.feature['bins_per_octave'],
48
+ # hop_length=config.feature['hop_length']
49
+ # )
50
+ # feature = tmp if feature is None else np.concatenate((feature, tmp), axis=1)
51
  # currunt_sec_hz = end_idx
52
+
53
+ # # Handle the final chunk (remaining audio)
54
+ # tmp = librosa.cqt(
55
+ # original_wav[currunt_sec_hz:], sr=sr,
56
+ # n_bins=config.feature['n_bins'],
57
+ # bins_per_octave=config.feature['bins_per_octave'],
58
+ # hop_length=config.feature['hop_length']
59
+ # )
60
  # feature = np.concatenate((feature, tmp), axis=1)
61
  # feature = np.log(np.abs(feature) + 1e-6)
62
+
63
+ # feature_per_second = segment_len_sec / config.model['timestep']
64
+ # song_length_second = len(original_wav) / sr
65
+
66
  # return feature, feature_per_second, song_length_second
67
 
68
+ def audio_file_to_features(audio_file, config):
69
+ original_wav, sr = librosa.load(audio_file, sr=config.mp3['song_hz'], mono=True)
70
+ currunt_sec_hz = 0
71
+ while len(original_wav) > currunt_sec_hz + config.mp3['song_hz'] * config.mp3['inst_len']:
72
+ start_idx = int(currunt_sec_hz)
73
+ end_idx = int(currunt_sec_hz + config.mp3['song_hz'] * config.mp3['inst_len'])
74
+ tmp = librosa.cqt(original_wav[start_idx:end_idx], sr=sr, n_bins=config.feature['n_bins'], bins_per_octave=config.feature['bins_per_octave'], hop_length=config.feature['hop_length'])
75
+ if start_idx == 0:
76
+ feature = tmp
77
+ else:
78
+ feature = np.concatenate((feature, tmp), axis=1)
79
+ currunt_sec_hz = end_idx
80
+ tmp = librosa.cqt(original_wav[currunt_sec_hz:], sr=sr, n_bins=config.feature['n_bins'], bins_per_octave=config.feature['bins_per_octave'], hop_length=config.feature['hop_length'])
81
+ feature = np.concatenate((feature, tmp), axis=1)
82
+ feature = np.log(np.abs(feature) + 1e-6)
83
+ feature_per_second = config.mp3['inst_len'] / config.model['timestep']
84
+ song_length_second = len(original_wav)/config.mp3['song_hz']
85
+ return feature, feature_per_second, song_length_second
86
+
87
  # Audio files with format of wav and mp3
88
  def get_audio_paths(audio_dir):
89
  return [os.path.join(root, fname) for (root, dir_names, file_names) in os.walk(audio_dir, followlinks=True)