audio.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import sys
  13. from soundfile import SoundFile, LibsndfileError # pyright: ignore
  14. from PyQt5.QtWidgets import ( # pylint: disable=no-name-in-module
  15. QMessageBox,
  16. )
  17. def get_wave_duration_in_frames(file_name: str) -> int:
  18. try:
  19. wav = SoundFile(file_name)
  20. return int(wav.frames / wav.samplerate * 75)
  21. except LibsndfileError:
  22. QMessageBox.critical(
  23. None,
  24. "Error",
  25. f"Error: Could not get duration of {file_name}",
  26. )
  27. sys.exit(1)
  28. def get_wave_duration_in_secs(file_name: str) -> int:
  29. return int(get_wave_duration_in_frames(file_name) / 75)