
'''simple docstring'''
from typing import List, Optional, Union

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


a_     =  logging.get_logger(__name__)






class 				UpperCAmelCase_			(		snake_case__   ):
 UpperCAmelCase_						      =  ["""input_values""", """attention_mask"""]





 def __init__(      self	,    lowercase_=1	,    lowercase_=1_60_00	,    lowercase_=1_28	,    lowercase_=10_24	,    lowercase_=0.0	,    lowercase_=True	,    lowercase_=-4.2_677_393	,    lowercase_=4.5_689_974	,    lowercase_=False	,    **lowercase_	,    ):
     super().__init__(feature_size=lowercase_	,    sampling_rate=lowercase_	,    padding_value=lowercase_	,    **lowercase_)
     snake_case_	:      Optional[int]            =  num_mel_bins
     snake_case_	:      Optional[Any]            =  max_length
     snake_case_	:      str            =  do_normalize
     snake_case_	:      Union[str, Any]            =  mean
     snake_case_	:      Dict            =  std
     snake_case_	:      str            =  return_attention_mask





 def     snake_case__						(      self	,    lowercase_	,    lowercase_	,    ):
     snake_case_	:      int            =  torch.from_numpy(lowercase_).unsqueeze(0)
     snake_case_	:      List[str]            =  ta_kaldi.fbank(
         lowercase_	,    htk_compat=lowercase_	,    sample_frequency=self.sampling_rate	,    use_energy=lowercase_	,    window_type="hanning"	,    num_mel_bins=self.num_mel_bins	,    dither=0.0	,    frame_shift=10	,    )

     snake_case_	:      Optional[Any]            =  fbank.shape[0]
     snake_case_	:      List[Any]            =  max_length - n_frames

     # pad or truncate, depending on difference
     if difference > 0:
         snake_case_	:      Tuple            =  torch.nn.ZeroPadad((0, 0, 0, difference))
         snake_case_	:      List[Any]            =  pad_module(lowercase_)
     elif difference < 0:
         snake_case_	:      Any            =  fbank[0:max_length, :]

     snake_case_	:      Optional[int]            =  fbank.numpy()

     return fbank





 def     snake_case__						(      self	,    lowercase_):
     return (input_values - (self.mean)) / (self.std * 2)





 def __call__(      self	,    lowercase_	,    lowercase_ = None	,    lowercase_ = None	,    **lowercase_	,    ):

     if sampling_rate is not None:
         if sampling_rate != self.sampling_rate:
             raise ValueError(
                 F'The model corresponding to this feature extractor: {self} was trained using a sampling rate of'
                 F' {self.sampling_rate}. Please make sure that the provided `raw_speech` input was sampled with'
                 F' {self.sampling_rate} and not {sampling_rate}.')
     else:
         logger.warning(
             "It is strongly recommended to pass the `sampling_rate` argument to this function. "
             "Failing to do so can result in silent errors that might be hard to debug.")

     snake_case_	:      int            =  isinstance(lowercase_	,    np.ndarray) and len(raw_speech.shape) > 1
     if is_batched_numpy and len(raw_speech.shape) > 2:
         raise ValueError(F'Only mono-channel audio is supported for input to {self}')
     snake_case_	:      List[Any]            =  is_batched_numpy or (
         isinstance(lowercase_	,    (list, tuple)) and (isinstance(raw_speech[0]	,    (np.ndarray, tuple, list)))
     )

     if is_batched:
         snake_case_	:      str            =  [np.asarray(lowercase_	,    dtype=np.floataa) for speech in raw_speech]
     elif not is_batched and not isinstance(lowercase_	,    np.ndarray):
         snake_case_	:      Optional[int]            =  np.asarray(lowercase_	,    dtype=np.floataa)
     elif isinstance(lowercase_	,    np.ndarray) and raw_speech.dtype is np.dtype(np.floataa):
         snake_case_	:      List[Any]            =  raw_speech.astype(np.floataa)

     # always return batch
     if not is_batched:
         snake_case_	:      Union[str, Any]            =  [raw_speech]

     # extract fbank features and pad/truncate to max_length
     snake_case_	:      Optional[int]            =  [self._extract_fbank_features(lowercase_	,    max_length=self.max_length) for waveform in raw_speech]

     # convert into BatchFeature
     snake_case_	:      str            =  BatchFeature({"input_values": features})

     # make sure list is in array format
     snake_case_	:      Union[str, Any]            =  padded_inputs.get("input_values")
     if isinstance(input_values[0]	,    lowercase_):
         snake_case_	:      Union[str, Any]            =  [np.asarray(lowercase_	,    dtype=np.floataa) for feature in input_values]

     # normalization
     if self.do_normalize:
         snake_case_	:      int            =  [self.normalize(lowercase_) for feature in input_values]

     if return_tensors is not None:
         snake_case_	:      str            =  padded_inputs.convert_to_tensors(lowercase_)

     return padded_inputs






'''simple docstring'''
import gc
import unittest

import numpy as np
import torch

from diffusers import DDPMScheduler, MidiProcessor, SpectrogramDiffusionPipeline
from diffusers.pipelines.spectrogram_diffusion import SpectrogramContEncoder, SpectrogramNotesEncoder, TaFilmDecoder
from diffusers.utils import require_torch_gpu, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_note_seq, require_onnxruntime

from ..pipeline_params import TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS, TOKENS_TO_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


a_     =  "./tests/fixtures/elise_format0.mid"






@unittest.skip("""The note-seq package currently throws an error on import"""   )
class 				UpperCAmelCase_			(		snake_case__      ,  unittest.TestCase   ):
 UpperCAmelCase_						      =  SpectrogramDiffusionPipeline
 UpperCAmelCase_						      =  PipelineTesterMixin.required_optional_params - {
     """callback""",
     """latents""",
     """callback_steps""",
     """output_type""",
     """num_images_per_prompt""",
 }
 UpperCAmelCase_						      =  False
 UpperCAmelCase_						      =  False
 UpperCAmelCase_						      =  TOKENS_TO_AUDIO_GENERATION_PARAMS
 UpperCAmelCase_						      =  TOKENS_TO_AUDIO_GENERATION_BATCH_PARAMS





 def     snake_case__						(      self):
     torch.manual_seed(0)
     snake_case_	:      List[str]            =  SpectrogramNotesEncoder(
         max_length=20_48	,    vocab_size=15_36	,    d_model=7_68	,    dropout_rate=0.1	,    num_layers=1	,    num_heads=1	,    d_kv=4	,    d_ff=20_48	,    feed_forward_proj="gated-gelu"	,    )

     snake_case_	:      int            =  SpectrogramContEncoder(
         input_dims=1_28	,    targets_context_length=2_56	,    d_model=7_68	,    dropout_rate=0.1	,    num_layers=1	,    num_heads=1	,    d_kv=4	,    d_ff=20_48	,    feed_forward_proj="gated-gelu"	,    )

     snake_case_	:      Tuple            =  TaFilmDecoder(
         input_dims=1_28	,    targets_length=2_56	,    max_decoder_noise_time=20_000.0	,    d_model=7_68	,    num_layers=1	,    num_heads=1	,    d_kv=4	,    d_ff=20_48	,    dropout_rate=0.1	,    )

     snake_case_	:      Union[str, Any]            =  DDPMScheduler()

     snake_case_	:      List[Any]            =  {
         "notes_encoder": notes_encoder.eval(),
         "continuous_encoder": continuous_encoder.eval(),
         "decoder": decoder.eval(),
         "scheduler": scheduler,
         "melgan": None,
     }
     return components





 def     snake_case__						(      self	,    lowercase_	,    lowercase_=0):
     if str(lowercase_).startswith("mps"):
         snake_case_	:      Dict            =  torch.manual_seed(lowercase_)
     else:
         snake_case_	:      Dict            =  torch.Generator(device=lowercase_).manual_seed(lowercase_)
     snake_case_	:      int            =  {
         "input_tokens": [
             [11_34, 90, 11_35, 11_33, 10_80, 1_12, 11_32, 10_80, 11_33, 10_79, 1_33, 11_32, 10_79, 11_33, 1] + [0] * 20_33
         ],
         "generator": generator,
         "num_inference_steps": 4,
         "output_type": "mel",
     }
     return inputs





 def     snake_case__						(      self):
     snake_case_	:      List[Any]            =  "cpu"  # ensure determinism for the device-dependent torch.Generator
     snake_case_	:      List[Any]            =  self.get_dummy_components()
     snake_case_	:      Tuple            =  SpectrogramDiffusionPipeline(**lowercase_)
     snake_case_	:      Tuple            =  pipe.to(lowercase_)
     pipe.set_progress_bar_config(disable=lowercase_)

     snake_case_	:      List[str]            =  self.get_dummy_inputs(lowercase_)
     snake_case_	:      Any            =  pipe(**lowercase_)
     snake_case_	:      str            =  output.audios

     snake_case_	:      str            =  mel[0, -3:, -3:]

     assert mel_slice.shape == (3, 3)
     snake_case_	:      Any            =  np.array(
         [-11.512_925, -4.788_215, -0.46_172_905, -2.051_715, -10.539_147, -10.970_963, -9.091_634, 4.0, 4.0])
     assert np.abs(mel_slice.flatten() - expected_slice).max() < 1E-2





 @skip_mps
 def     snake_case__						(      self):
     return super().test_save_load_local()





 @skip_mps
 def     snake_case__						(      self):
     return super().test_dict_tuple_outputs_equivalent()





 @skip_mps
 def     snake_case__						(      self):
     return super().test_save_load_optional_components()





 @skip_mps
 def     snake_case__						(      self):
     return super().test_attention_slicing_forward_pass()





 def     snake_case__						(      self):
     pass





 def     snake_case__						(      self):
     pass





 @skip_mps
 def     snake_case__						(      self):
     return super().test_progress_bar()






@slow
@require_torch_gpu
@require_onnxruntime
@require_note_seq
class 				UpperCAmelCase_			(		unittest.TestCase   ):





 def     snake_case__						(      self):
     # clean up the VRAM after each test
     super().tearDown()
     gc.collect()
     torch.cuda.empty_cache()





 def     snake_case__						(      self):
     # TODO - test that pipeline can decode tokens in a callback
     # so that music can be played live
     snake_case_	:      Any            =  torch_device

     snake_case_	:      str            =  SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion")
     snake_case_	:      Tuple            =  pipe.melgan
     snake_case_	:      List[str]            =  None

     snake_case_	:      int            =  pipe.to(lowercase_)
     pipe.set_progress_bar_config(disable=lowercase_)

     def callback(lowercase_	,    lowercase_):
         # decode mel to audio
         snake_case_	:      Dict            =  melgan(input_features=mel_output.astype(np.floataa))[0]
         assert len(audio[0]) == 8_19_20 * (step + 1)
         # simulate that audio is played
         return audio

     snake_case_	:      Tuple            =  MidiProcessor()
     snake_case_	:      int            =  processor(lowercase_)

     snake_case_	:      int            =  input_tokens[:3]
     snake_case_	:      Union[str, Any]            =  torch.manual_seed(0)
     pipe(lowercase_	,    num_inference_steps=5	,    generator=lowercase_	,    callback=lowercase_	,    output_type="mel")





 def     snake_case__						(      self):
     snake_case_	:      Any            =  torch_device

     snake_case_	:      Optional[Any]            =  SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion")
     snake_case_	:      Optional[int]            =  pipe.to(lowercase_)
     pipe.set_progress_bar_config(disable=lowercase_)
     snake_case_	:      Union[str, Any]            =  MidiProcessor()

     snake_case_	:      List[str]            =  processor(lowercase_)
     # just run two denoising loops
     snake_case_	:      int            =  input_tokens[:2]

     snake_case_	:      Optional[Any]            =  torch.manual_seed(0)
     snake_case_	:      Tuple            =  pipe(lowercase_	,    num_inference_steps=2	,    generator=lowercase_)

     snake_case_	:      Optional[Any]            =  output.audios[0]

     assert abs(np.abs(lowercase_).sum() - 3_612.841) < 1E-1





 def     snake_case__						(      self):
     snake_case_	:      Union[str, Any]            =  torch_device

     snake_case_	:      Tuple            =  SpectrogramDiffusionPipeline.from_pretrained("google/music-spectrogram-diffusion")
     snake_case_	:      Optional[Any]            =  pipe.to(lowercase_)
     pipe.set_progress_bar_config(disable=lowercase_)

     snake_case_	:      Tuple            =  MidiProcessor()

     snake_case_	:      int            =  processor(lowercase_)

     # just run 4 denoising loops
     snake_case_	:      Any            =  input_tokens[:4]

     snake_case_	:      Optional[Any]            =  torch.manual_seed(0)
     snake_case_	:      Tuple            =  pipe(lowercase_	,    num_inference_steps=1_00	,    generator=lowercase_)

     snake_case_	:      Tuple            =  output.audios[0]
     assert abs(np.abs(lowercase_).sum() - 9_389.1_111) < 5E-2




