rawoutputstream.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "rawoutputstream.hh"
  18. #include <assert.h>
  19. #include <string.h>
  20. #include <errno.h>
  21. using std::string;
  22. using std::vector;
  23. RawOutputStream::~RawOutputStream()
  24. {
  25. close();
  26. }
  27. Error
  28. RawOutputStream::open (const string& filename, const RawFormat& format)
  29. {
  30. assert (m_state == State::NEW);
  31. if (!format.n_channels())
  32. return Error ("RawOutputStream: output format: missing number of channels");
  33. if (!format.bit_depth())
  34. return Error ("RawOutputStream: output format: missing bit depth");
  35. if (!format.sample_rate())
  36. return Error ("RawOutputStream: output format: missing sample rate");
  37. Error err = Error::Code::NONE;
  38. m_raw_converter.reset (RawConverter::create (format, err));
  39. if (err)
  40. return err;
  41. if (filename == "-")
  42. {
  43. m_output_file = stdout;
  44. m_close_file = false;
  45. }
  46. else
  47. {
  48. m_output_file = fopen (filename.c_str(), "w");
  49. if (!m_output_file)
  50. return Error (strerror (errno));
  51. m_close_file = true;
  52. }
  53. m_format = format;
  54. m_state = State::OPEN;
  55. return Error::Code::NONE;
  56. }
  57. int
  58. RawOutputStream::sample_rate() const
  59. {
  60. return m_format.sample_rate();
  61. }
  62. int
  63. RawOutputStream::bit_depth() const
  64. {
  65. return m_format.bit_depth();
  66. }
  67. int
  68. RawOutputStream::n_channels() const
  69. {
  70. return m_format.n_channels();
  71. }
  72. Error
  73. RawOutputStream::write_frames (const vector<float>& samples)
  74. {
  75. assert (m_state == State::OPEN);
  76. vector<unsigned char> bytes;
  77. m_raw_converter->to_raw (samples, bytes);
  78. fwrite (&bytes[0], 1, bytes.size(), m_output_file);
  79. if (ferror (m_output_file))
  80. return Error ("write sample data failed");
  81. return Error::Code::NONE;
  82. }
  83. Error
  84. RawOutputStream::close()
  85. {
  86. if (m_state == State::OPEN)
  87. {
  88. if (m_output_file)
  89. {
  90. fflush (m_output_file);
  91. if (ferror (m_output_file))
  92. return Error ("error during flush");
  93. }
  94. if (m_close_file && m_output_file)
  95. {
  96. if (fclose (m_output_file) != 0)
  97. return Error ("error during close");
  98. m_output_file = nullptr;
  99. m_close_file = false;
  100. }
  101. m_state = State::CLOSED;
  102. }
  103. return Error::Code::NONE;
  104. }