audio_reader.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from ...utils.io import AudioReader
  15. class ReadAudio:
  16. """Load audio from the file."""
  17. def __init__(self):
  18. """
  19. Initialize the instance.
  20. """
  21. super().__init__()
  22. self._audio_reader = AudioReader(backend="wav")
  23. def read(self, input):
  24. import paddle
  25. if isinstance(input, str):
  26. audio, sample_rate = self._audio_reader.read(input)
  27. if sample_rate != 16000:
  28. raise ValueError(
  29. f"ReadAudio only supports 16k pcm or wav file.\n"
  30. f"However, got: {sample_rate}."
  31. )
  32. audio = audio[:, 0]
  33. audio = paddle.to_tensor(audio)
  34. return audio, sample_rate
  35. else:
  36. raise TypeError(
  37. f"ReadAudio only supports str, indicating an audio file path.\n"
  38. f"However, got type: {type(input).__name__}."
  39. )