code: def extract_energy(
      waveform: torch.Tensor,
      output_path: Optional[Path] = None,
      hop_length: int = 256,
      n_fft: int = 1024,
      log_scale: bool = True,
      phoneme_durations: Optional[List[int]] = None,
  ):
      if output_path is not None and output_path.is_file():
          return
  
      assert len(waveform.shape) == 2 and waveform.shape[0] == 1
      waveform = waveform.view(1, 1, waveform.shape[1])
      waveform = F.pad(
          waveform.unsqueeze(1),
          [n_fft // 2, n_fft // 2, 0, 0],
          mode="reflect",
      )
      waveform = waveform.squeeze(1)
  
      fourier_basis = np.fft.fft(np.eye(n_fft))
      cutoff = int((n_fft / 2 + 1))
      fourier_basis = np.vstack(
          [
              np.real(fourier_basis[:cutoff, :]),
              np.imag(fourier_basis[:cutoff, :]),
          ]
      )
  
      forward_basis = torch.FloatTensor(fourier_basis[:, None, :])
      forward_transform = F.conv1d(
          waveform, forward_basis, stride=hop_length, padding=0
      )
  
      real_part = forward_transform[:, :cutoff, :]
      imag_part = forward_transform[:, cutoff:, :]
      magnitude = torch.sqrt(real_part**2 + imag_part**2)
      energy = torch.norm(magnitude, dim=1).squeeze(0).numpy()
  
      if phoneme_durations is not None:
          energy = trim_or_pad_to_target_length(
              energy, sum(phoneme_durations)
          )
          d_cumsum = np.cumsum(
              np.concatenate([np.array([0]), phoneme_durations])
          )
          energy = np.array(
              [
                  np.mean(energy[d_cumsum[i - 1] : d_cumsum[i]])
                  for i in range(1, len(d_cumsum))
              ]
          )
          assert len(energy) == len(phoneme_durations)
  
      if log_scale:
          energy = np.log(energy + 1)
  
      if output_path is not None:
          np.save(output_path.as_posix(), energy)
      else:
          return energy
  
imports: import io
  import os
  from pathlib import Path
  from typing import Optional, List, Dict
  import zipfile
  import tempfile
  from dataclasses import dataclass
  from itertools import groupby
  import torch
  import torch.nn.functional
  import numpy
  from tqdm import tqdm
  from examples.speech_to_text.data_utils import load_tsv_to_dicts
  from fairseq.data.audio.audio_utils import TTSSpectrogram, TTSMelScale, parse_path, read_from_stored_zip, is_npy_data
label_desc: Use image node_id 4 for calling a global function with example usage: extract_energy(waveform, output_path, hop_length, n_fft, log_scale, phoneme_durations) and returns: energy