openmpt123_stdout.hpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. * openmpt123_stdout.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_STDOUT_HPP
  10. #define OPENMPT123_STDOUT_HPP
  11. #include "openmpt123_config.hpp"
  12. #include "openmpt123.hpp"
  13. namespace openmpt123 {
  14. class stdout_stream_raii : public write_buffers_interface {
  15. private:
  16. std::vector<float> interleaved_float_buffer;
  17. std::vector<std::int16_t> interleaved_int_buffer;
  18. public:
  19. stdout_stream_raii() {
  20. return;
  21. }
  22. public:
  23. void write( const std::vector<float*> buffers, std::size_t frames ) override {
  24. interleaved_float_buffer.clear();
  25. for ( std::size_t frame = 0; frame < frames; frame++ ) {
  26. for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
  27. interleaved_float_buffer.push_back( buffers[channel][frame] );
  28. }
  29. }
  30. std::cout.write( reinterpret_cast<const char *>( interleaved_float_buffer.data() ), interleaved_float_buffer.size() * sizeof( float ) );
  31. }
  32. void write( const std::vector<std::int16_t*> buffers, std::size_t frames ) override {
  33. interleaved_int_buffer.clear();
  34. for ( std::size_t frame = 0; frame < frames; frame++ ) {
  35. for ( std::size_t channel = 0; channel < buffers.size(); channel++ ) {
  36. interleaved_int_buffer.push_back( buffers[channel][frame] );
  37. }
  38. }
  39. std::cout.write( reinterpret_cast<const char *>( interleaved_int_buffer.data() ), interleaved_int_buffer.size() * sizeof( std::int16_t ) );
  40. }
  41. };
  42. } // namespace openmpt123
  43. #endif // OPENMPT123_STDOUT_HPP