1
0

openmpt123_raw.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * openmpt123_raw.hpp
  3. * ------------------
  4. * Purpose: libopenmpt command line player
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #ifndef OPENMPT123_RAW_HPP
  10. #define OPENMPT123_RAW_HPP
  11. #include "openmpt123_config.hpp"
  12. #include "openmpt123.hpp"
  13. #include <fstream>
  14. namespace openmpt123 {
  15. class raw_stream_raii : public file_audio_stream_base {
  16. private:
  17. commandlineflags flags;
  18. std::ofstream file;
  19. std::vector<float> interleaved_float_buffer;
  20. std::vector<std::int16_t> interleaved_int_buffer;
  21. public:
  22. raw_stream_raii( const std::string & filename, const commandlineflags & flags_, std::ostream & /*log*/ ) : flags(flags_), file(filename.c_str(), std::ios::binary) {
  23. return;
  24. }
  25. ~raw_stream_raii() {
  26. return;
  27. }
  28. void write_metadata( std::map<std::string,std::string> /* metadata */ ) override {
  29. return;
  30. }
  31. void write( const std::vector<float*> buffers, std::size_t frames ) override {
  32. interleaved_float_buffer.clear();
  33. for ( std::size_t frame = 0; frame < frames; frame++ ) {
  34. for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
  35. interleaved_float_buffer.push_back( buffers[channel][frame] );
  36. }
  37. }
  38. file.write( reinterpret_cast<const char *>( interleaved_float_buffer.data() ), frames * buffers.size() * sizeof( float ) );
  39. }
  40. void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
  41. interleaved_int_buffer.clear();
  42. for ( std::size_t frame = 0; frame < frames; frame++ ) {
  43. for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
  44. interleaved_int_buffer.push_back( buffers[channel][frame] );
  45. }
  46. }
  47. file.write( reinterpret_cast<const char *>( interleaved_int_buffer.data() ), frames * buffers.size() * sizeof( std::int16_t ) );
  48. }
  49. };
  50. } // namespace openmpt123
  51. #endif // OPENMPT123_RAW_HPP