code: def auto_tune(
      f0_midi, tuning_factor, mask_on, amount=0.0, chromatic=False
  ):
      """Reduce variance of f0 from the chromatic or scale intervals."""
      if chromatic:
          midi_diff = (f0_midi - tuning_factor) % 1.0
          midi_diff[midi_diff > 0.5] -= 1.0
      else:
          major_scale = np.ravel(
              [
                  np.array([0, 2, 4, 5, 7, 9, 11]) + 12 * i
                  for i in range(10)
              ]
          )
          all_scales = np.stack([major_scale + i for i in range(12)])
  
          f0_on = f0_midi[mask_on]
          # [time, scale, note]
          f0_diff_tsn = (
              f0_on[:, np.newaxis, np.newaxis]
              - all_scales[np.newaxis, :, :]
          )
          # [time, scale]
          f0_diff_ts = np.min(np.abs(f0_diff_tsn), axis=-1)
          # [scale]
          f0_diff_s = np.mean(f0_diff_ts, axis=0)
          scale_idx = np.argmin(f0_diff_s)
          scale = [
              "C",
              "Db",
              "D",
              "Eb",
              "E",
              "F",
              "Gb",
              "G",
              "Ab",
              "A",
              "Bb",
              "B",
              "C",
          ][scale_idx]
  
          # [time]
          f0_diff_tn = (
              f0_midi[:, np.newaxis]
              - all_scales[scale_idx][np.newaxis, :]
          )
          note_idx = np.argmin(np.abs(f0_diff_tn), axis=-1)
          midi_diff = np.take_along_axis(
              f0_diff_tn, note_idx[:, np.newaxis], axis=-1
          )[:, 0]
          print(
              "Autotuning... \nInferred key: {}  "
              "\nTuning offset: {} cents".format(
                  scale, int(tuning_factor * 100)
              )
          )
  
      # Adjust the midi signal.
      return f0_midi - amount * midi_diff
  
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 7 for calling a global function with example usage: auto_tune(f0_midi, tuning_factor, mask_on, amount, chromatic) and returns: unknown