code: def get_tuning_factor(f0_midi, f0_confidence, mask_on):
      """Get an offset in cents, to most consistent set of chromatic intervals."""
      # Difference from midi offset by different tuning_factors.
      tuning_factors = np.linspace(-0.5, 0.5, 101)  # 1 cent divisions.
      midi_diffs = (
          f0_midi[mask_on][:, np.newaxis]
          - tuning_factors[np.newaxis, :]
      ) % 1.0
      midi_diffs[midi_diffs > 0.5] -= 1.0
      weights = f0_confidence[mask_on][:, np.newaxis]
  
      ## Computes mininmum adjustment distance.
      cost_diffs = np.abs(midi_diffs)
      cost_diffs = np.mean(weights * cost_diffs, axis=0)
  
      ## Computes mininmum "note" transitions.
      f0_at = f0_midi[mask_on][:, np.newaxis] - midi_diffs
      f0_at_diffs = np.diff(f0_at, axis=0)
      deltas = (f0_at_diffs != 0.0).astype(float)
      cost_deltas = np.mean(weights[:-1] * deltas, axis=0)
  
      # Tuning factor is minimum cost.
      norm = lambda x: (x - np.mean(x)) / np.std(x)
      cost = norm(cost_deltas) + norm(cost_diffs)
      return tuning_factors[np.argmin(cost)]
  
file_path: ddsp/ddsp/colab/colab_utils.py
imports: import base64
  import io
  import pickle
  import ddsp
  import ddsp.training
  from google.colab import files
  from google.colab import output
  from IPython import display
  import note_seq
  import numpy
  from scipy.io import wavfile
  import tensorflow.compat.v2
label_desc: Use image node_id 6 for calling a global function with example usage: get_tuning_factor(f0_midi, f0_confidence, mask_on) and returns: tuning_factors