|
10 | 10 | from scipy import signal |
11 | 11 | from tqdm import tqdm |
12 | 12 |
|
| 13 | +from lib.pickling import * |
| 14 | +from os.path import exists |
| 15 | + |
13 | 16 | def load_wav(fpath): |
14 | 17 | """Loads a .wav file and returns the data and sample rate. |
15 | 18 |
|
@@ -214,21 +217,36 @@ def plot_series_ax(ax, series, label=None): |
214 | 217 | ax.plot(t, series, label=label) |
215 | 218 |
|
216 | 219 |
|
| 220 | +def wav_to_enf(filename, nominal_freq, freq_band_size, harmonic_n=1): |
| 221 | + """ |
| 222 | + This code attempts to load a pickle file that contains the enf samples. |
| 223 | + If the pickle file does not exist, the code loads the corresponding wav file, |
| 224 | + processes it, and saves the enf samples in a new pickle file. |
| 225 | + This approach prevents unnecessary loading and computing. |
| 226 | + """ |
| 227 | + pklfilename = "." + filename + ".pkl" |
| 228 | + if exists(pklfilename): |
| 229 | + return decompress_pickle(pklfilename) |
| 230 | + else: |
| 231 | + ref_data, ref_fs = load_wav(filename) |
| 232 | + enf = enf_series(ref_data, ref_fs, nominal_freq, freq_band_size, harmonic_n) |
| 233 | + compress_pickle(pklfilename, enf) |
| 234 | + return enf |
| 235 | + |
217 | 236 | if __name__ == "__main__": |
218 | 237 | nominal_freq = 50 |
219 | 238 | freq_band_size = 0.2 |
220 | 239 |
|
221 | | - # !!!: make sure to run ./bin/download-example-files first |
222 | | - ref_data, ref_fs = load_wav("./001_ref.wav") |
| 240 | + refwav = './001_ref.wav' |
| 241 | + wav = "./001.wav" |
223 | 242 |
|
224 | | - ref_enf_output = enf_series(ref_data, ref_fs, nominal_freq, freq_band_size) |
| 243 | + # !!!: make sure to run ./bin/download-example-files first |
| 244 | + ref_enf_output = wav_to_enf(refwav, nominal_freq, freq_band_size, harmonic_n=1) |
225 | 245 | ref_enf = ref_enf_output['enf'] |
226 | 246 |
|
227 | 247 | # !!!: make sure to run ./bin/download-example-files first |
228 | | - data, fs = load_wav("./001.wav") |
229 | | - |
230 | 248 | harmonic_n = 1 |
231 | | - enf_output = enf_series(data, fs, nominal_freq, freq_band_size, harmonic_n=2) |
| 249 | + enf_output = wav_to_enf(refwav, nominal_freq, freq_band_size, harmonic_n=2) |
232 | 250 | target_enf = enf_output['enf'] |
233 | 251 |
|
234 | 252 | stft = enf_output['stft'] |
|
0 commit comments