rawinputstream.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "rawinputstream.hh"
  18. #include "rawconverter.hh"
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. using std::string;
  23. using std::vector;
  24. RawFormat::RawFormat()
  25. {
  26. }
  27. RawFormat::RawFormat (int n_channels, int sample_rate, int bit_depth) :
  28. m_n_channels (n_channels),
  29. m_sample_rate (sample_rate),
  30. m_bit_depth (bit_depth)
  31. {
  32. }
  33. void
  34. RawFormat::set_channels (int channels)
  35. {
  36. m_n_channels = channels;
  37. }
  38. void
  39. RawFormat::set_sample_rate (int rate)
  40. {
  41. m_sample_rate = rate;
  42. }
  43. void
  44. RawFormat::set_bit_depth (int bits)
  45. {
  46. m_bit_depth = bits;
  47. }
  48. void
  49. RawFormat::set_endian (Endian endian)
  50. {
  51. m_endian = endian;
  52. }
  53. void
  54. RawFormat::set_encoding (Encoding encoding)
  55. {
  56. m_encoding = encoding;
  57. }
  58. RawInputStream::~RawInputStream()
  59. {
  60. close();
  61. }
  62. Error
  63. RawInputStream::open (const string& filename, const RawFormat& format)
  64. {
  65. assert (m_state == State::NEW);
  66. if (!format.n_channels())
  67. return Error ("RawInputStream: input format: missing number of channels");
  68. if (!format.bit_depth())
  69. return Error ("RawInputStream: input format: missing bit depth");
  70. if (!format.sample_rate())
  71. return Error ("RawInputStream: input format: missing sample rate");
  72. Error err = Error::Code::NONE;
  73. m_raw_converter.reset (RawConverter::create (format, err));
  74. if (err)
  75. return err;
  76. if (filename == "-")
  77. {
  78. m_input_file = stdin;
  79. m_close_file = false;
  80. }
  81. else
  82. {
  83. m_input_file = fopen (filename.c_str(), "r");
  84. if (!m_input_file)
  85. return Error (strerror (errno));
  86. m_close_file = true;
  87. }
  88. m_format = format;
  89. m_state = State::OPEN;
  90. return Error::Code::NONE;
  91. }
  92. int
  93. RawInputStream::sample_rate() const
  94. {
  95. return m_format.sample_rate();
  96. }
  97. int
  98. RawInputStream::bit_depth() const
  99. {
  100. return m_format.bit_depth();
  101. }
  102. size_t
  103. RawInputStream::n_frames() const
  104. {
  105. return N_FRAMES_UNKNOWN;
  106. }
  107. int
  108. RawInputStream::n_channels() const
  109. {
  110. return m_format.n_channels();
  111. }
  112. Error
  113. RawInputStream::read_frames (vector<float>& samples, size_t count)
  114. {
  115. assert (m_state == State::OPEN);
  116. const int n_channels = m_format.n_channels();
  117. const int sample_width = m_format.bit_depth() / 8;
  118. vector<unsigned char> input_bytes (count * n_channels * sample_width);
  119. size_t r_count = fread (input_bytes.data(), n_channels * sample_width, count, m_input_file);
  120. if (ferror (m_input_file))
  121. return Error ("error reading sample data");
  122. input_bytes.resize (r_count * n_channels * sample_width);
  123. m_raw_converter->from_raw (input_bytes, samples);
  124. return Error::Code::NONE;
  125. }
  126. void
  127. RawInputStream::close()
  128. {
  129. if (m_state == State::OPEN)
  130. {
  131. if (m_close_file && m_input_file)
  132. {
  133. fclose (m_input_file);
  134. m_input_file = nullptr;
  135. m_close_file = false;
  136. }
  137. m_state = State::CLOSED;
  138. }
  139. }