rawinputstream.hh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_RAW_INPUT_STREAM_HH
  18. #define AUDIOWMARK_RAW_INPUT_STREAM_HH
  19. #include <string>
  20. #include <memory>
  21. #include <sndfile.h>
  22. #include "audiostream.hh"
  23. class RawFormat
  24. {
  25. public:
  26. enum Endian {
  27. LITTLE,
  28. BIG
  29. };
  30. enum Encoding {
  31. SIGNED,
  32. UNSIGNED
  33. };
  34. private:
  35. int m_n_channels = 2;
  36. int m_sample_rate = 0;
  37. int m_bit_depth = 16;
  38. Endian m_endian = LITTLE;
  39. Encoding m_encoding = SIGNED;
  40. public:
  41. RawFormat();
  42. RawFormat (int n_channels, int sample_rate, int bit_depth);
  43. int n_channels() const { return m_n_channels; }
  44. int sample_rate() const { return m_sample_rate; }
  45. int bit_depth() const { return m_bit_depth; }
  46. Endian endian() const { return m_endian; }
  47. Encoding encoding() const { return m_encoding; }
  48. void set_channels (int channels);
  49. void set_sample_rate (int rate);
  50. void set_bit_depth (int bits);
  51. void set_endian (Endian endian);
  52. void set_encoding (Encoding encoding);
  53. };
  54. class RawConverter;
  55. class RawInputStream : public AudioInputStream
  56. {
  57. enum class State {
  58. NEW,
  59. OPEN,
  60. CLOSED
  61. };
  62. State m_state = State::NEW;
  63. RawFormat m_format;
  64. FILE *m_input_file = nullptr;
  65. bool m_close_file = false;
  66. std::unique_ptr<RawConverter> m_raw_converter;
  67. public:
  68. ~RawInputStream();
  69. Error open (const std::string& filename, const RawFormat& format);
  70. Error read_frames (std::vector<float>& samples, size_t count) override;
  71. void close();
  72. int bit_depth() const override;
  73. int sample_rate() const override;
  74. size_t n_frames() const override;
  75. int n_channels() const override;
  76. };
  77. #endif /* AUDIOWMARK_RAW_INPUT_STREAM_HH */