audio.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #pragma once
  2. #include <vector>
  3. #include <string>
  4. #include <stdexcept>
  5. /**
  6. * Audio buffer structure for storing audio data.
  7. * Data is stored in interleaved format (L, R, L, R, ...) for stereo.
  8. */
  9. struct AudioBuffer {
  10. std::vector<float> data; // Interleaved samples
  11. unsigned int channels;
  12. unsigned int sampleRate;
  13. size_t samples; // Total samples (frames * channels)
  14. };
  15. /**
  16. * Audio file I/O utilities.
  17. * Supports WAV format (via dr_wav).
  18. */
  19. class AudioFile {
  20. public:
  21. /**
  22. * Load audio from a WAV file.
  23. * @param path Path to the WAV file
  24. * @return AudioBuffer containing the loaded audio data
  25. * @throws std::runtime_error if the file cannot be opened
  26. */
  27. static AudioBuffer Load(const std::string& path);
  28. /**
  29. * Save audio to a WAV file.
  30. * @param path Path to save the WAV file
  31. * @param buffer AudioBuffer containing audio data to save
  32. * @throws std::runtime_error if the file cannot be written
  33. */
  34. static void Save(const std::string& path, const AudioBuffer& buffer);
  35. };