|
26 | 26 |
|
27 | 27 | import scipy.signal |
28 | 28 |
|
| 29 | +DEFAULT_BAND_CENTER = 0.5 |
| 30 | +DEFAULT_KERNEL_LENGTH = 256 |
| 31 | +DEFAULT_TRANSITION_BANDWIDTH = 0.03 |
| 32 | +DEFAULT_DTYPE = tf.float32 |
| 33 | +DEFAULT_WINDOW_BANDWIDTH = 1.5 |
| 34 | +DEFAULT_CQT_HOP_LENGTH = 512 |
| 35 | +DEFAULT_CQT_FMIN = 32.70 |
| 36 | +DEFAULT_CQT_N_BINS = 84 |
| 37 | +DEFAULT_CQT_BINS_PER_OCTAVE = 12 |
| 38 | +DEFAULT_CQT_BASIS_NORM = 1 |
| 39 | +DEFAULT_CQT_WINDOW = "hann" |
| 40 | +DEFAULT_CQT_PAD_MODE = "reflect" |
| 41 | +DEFAULT_CQT_OUTPUT_FORMAT = "Magnitude" |
| 42 | +DEFAULT_LOW_PASS_TRANSITION_BANDWIDTH = 0.001 |
29 | 43 |
|
30 | 44 | def create_lowpass_filter( |
31 | | - band_center: float = 0.5, |
32 | | - kernel_length: int = 256, |
33 | | - transition_bandwidth: float = 0.03, |
34 | | - dtype: tf.dtypes.DType = tf.float32, |
| 45 | + band_center: float = DEFAULT_BAND_CENTER, |
| 46 | + kernel_length: int = DEFAULT_KERNEL_LENGTH, |
| 47 | + transition_bandwidth: float = DEFAULT_TRANSITION_BANDWIDTH, |
| 48 | + dtype: tf.dtypes.DType = DEFAULT_DTYPE, |
35 | 49 | ) -> np.ndarray: |
36 | 50 | """ |
37 | 51 | Calculate the highest frequency we need to preserve and the lowest frequency we allow |
@@ -106,15 +120,15 @@ def get_early_downsample_params( |
106 | 120 | ) -> Tuple[Union[float, int], int, float, np.array, bool]: |
107 | 121 | """Compute downsampling parameters used for early downsampling""" |
108 | 122 |
|
109 | | - window_bandwidth = 1.5 # for hann window |
| 123 | + window_bandwidth = DEFAULT_WINDOW_BANDWIDTH # for hann window |
110 | 124 | filter_cutoff = fmax_t * (1 + 0.5 * window_bandwidth / Q) |
111 | 125 | sr, hop_length, downsample_factor = early_downsample(sr, hop_length, n_octaves, sr // 2, filter_cutoff) |
112 | 126 | if downsample_factor != 1: |
113 | 127 | earlydownsample = True |
114 | 128 | early_downsample_filter = create_lowpass_filter( |
115 | 129 | band_center=1 / downsample_factor, |
116 | | - kernel_length=256, |
117 | | - transition_bandwidth=0.03, |
| 130 | + kernel_length=DEFAULT_KERNEL_LENGTH, |
| 131 | + transition_bandwidth=DEFAULT_TRANSITION_BANDWIDTH, |
118 | 132 | dtype=dtype, |
119 | 133 | ) |
120 | 134 | else: |
@@ -455,19 +469,19 @@ class CQT2010v2(tf.keras.layers.Layer): |
455 | 469 | def __init__( |
456 | 470 | self, |
457 | 471 | sr: int = 22050, |
458 | | - hop_length: int = 512, |
459 | | - fmin: float = 32.70, |
| 472 | + hop_length: int = DEFAULT_CQT_HOP_LENGTH, |
| 473 | + fmin: float = DEFAULT_CQT_FMIN, |
460 | 474 | fmax: Optional[float] = None, |
461 | | - n_bins: int = 84, |
| 475 | + n_bins: int = DEFAULT_CQT_N_BINS, |
462 | 476 | filter_scale: int = 1, |
463 | | - bins_per_octave: int = 12, |
| 477 | + bins_per_octave: int = DEFAULT_CQT_BINS_PER_OCTAVE, |
464 | 478 | norm: bool = True, |
465 | | - basis_norm: int = 1, |
466 | | - window: str = "hann", |
467 | | - pad_mode: str = "reflect", |
| 479 | + basis_norm: int = DEFAULT_CQT_BASIS_NORM, |
| 480 | + window: str = DEFAULT_CQT_WINDOW, |
| 481 | + pad_mode: str = DEFAULT_CQT_PAD_MODE, |
468 | 482 | earlydownsample: bool = True, |
469 | 483 | trainable: bool = False, |
470 | | - output_format: str = "Magnitude", |
| 484 | + output_format: str = DEFAULT_CQT_OUTPUT_FORMAT, |
471 | 485 | match_torch_exactly: bool = True, |
472 | 486 | ): |
473 | 487 | super().__init__() |
@@ -516,7 +530,11 @@ def build(self, input_shape: tf.TensorShape) -> None: |
516 | 530 | # This will be used to calculate filter_cutoff and creating CQT kernels |
517 | 531 | Q = float(self.filter_scale) / (2 ** (1 / self.bins_per_octave) - 1) |
518 | 532 |
|
519 | | - self.lowpass_filter = create_lowpass_filter(band_center=0.5, kernel_length=256, transition_bandwidth=0.001) |
| 533 | + self.lowpass_filter = create_lowpass_filter( |
| 534 | + band_center=DEFAULT_BAND_CENTER, |
| 535 | + kernel_length=DEFAULT_KERNEL_LENGTH, |
| 536 | + transition_bandwidth=DEFAULT_LOW_PASS_TRANSITION_BANDWIDTH, |
| 537 | + ) |
520 | 538 |
|
521 | 539 | # Calculate num of filter requires for the kernel |
522 | 540 | # n_octaves determines how many resampling requires for the CQT |
|
0 commit comments