sfoutputstream.hh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright (C) 2018-2020 Stefan Westerfeld
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef AUDIOWMARK_SF_OUTPUT_STREAM_HH
  18. #define AUDIOWMARK_SF_OUTPUT_STREAM_HH
  19. #include <string>
  20. #include <sndfile.h>
  21. #include "audiostream.hh"
  22. #include "sfinputstream.hh"
  23. class SFOutputStream : public AudioOutputStream
  24. {
  25. public:
  26. enum class OutFormat { WAV, FLAC };
  27. private:
  28. SFVirtualData m_virtual_data;
  29. SNDFILE *m_sndfile = nullptr;
  30. int m_bit_depth = 0;
  31. int m_sample_rate = 0;
  32. int m_n_channels = 0;
  33. enum class State {
  34. NEW,
  35. OPEN,
  36. CLOSED
  37. };
  38. State m_state = State::NEW;
  39. Error open (std::function<SNDFILE* (SF_INFO *)> open_func, int n_channels, int sample_rate, int bit_depth, OutFormat out_format);
  40. public:
  41. ~SFOutputStream();
  42. Error open (const std::string& filename, int n_channels, int sample_rate, int bit_depth, OutFormat out_format = OutFormat::WAV);
  43. Error open (std::vector<unsigned char> *data, int n_channels, int sample_rate, int bit_depth, OutFormat out_format = OutFormat::WAV);
  44. Error write_frames (const std::vector<float>& frames) override;
  45. Error close() override;
  46. int bit_depth() const override;
  47. int sample_rate() const override;
  48. int n_channels() const override;
  49. };
  50. #endif /* AUDIOWMARK_SF_OUTPUT_STREAM_HH */