audio_reader.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.deps import class_requires_deps
  15. from ...utils.io import AudioReader
  16. @class_requires_deps("paddlepaddle")
  17. class ReadAudio:
  18. """Load audio from the file."""
  19. def __init__(self):
  20. """
  21. Initialize the instance.
  22. """
  23. super().__init__()
  24. self._audio_reader = AudioReader(backend="wav")
  25. def read(self, input):
  26. import paddle
  27. if isinstance(input, str):
  28. audio, sample_rate = self._audio_reader.read(input)
  29. if sample_rate != 16000:
  30. raise ValueError(
  31. f"ReadAudio only supports 16k pcm or wav file.\n"
  32. f"However, got: {sample_rate}."
  33. )
  34. audio = audio[:, 0]
  35. audio = paddle.to_tensor(audio)
  36. return audio, sample_rate
  37. else:
  38. raise TypeError(
  39. f"ReadAudio only supports str, indicating an audio file path.\n"
  40. f"However, got type: {type(input).__name__}."
  41. )