sequencer
util.hpp
Go to the documentation of this file.
1 #pragma once
2 
9 
10 #include <RtMidi.h>
11 #include <ios>
12 #include <iostream>
13 #include <memory>
14 #include <vector>
15 
17 {
18  inline void cout_callback( double time_delta, std::vector< unsigned char >* message,
19  void* /*userData*/ )
20  {
21  const auto number_of_bytes = message->size();
22  for ( decltype( message->size() ) i = 0; i < number_of_bytes; ++i )
23  std::cout << "Byte " << i << " = " << std::hex << static_cast< int >( message->at( i ) )
24  << ", ";
25  if ( number_of_bytes > 0 )
26  std::cout << "expired since last message = " << time_delta << "s" << std::endl;
27  }
28 
29  template < class RtMidi >
30  std::unique_ptr< RtMidi > make_midi_port( unsigned port_number = 0 )
31  {
32  auto rtmidi = std::make_unique< RtMidi >();
33  if ( rtmidi->getPortCount() < port_number + 1u )
34  {
35  std::cout << "Requested port: " << port_number
36  << ". Available number of ports: " << rtmidi->getPortCount() << std::endl;
37  return {};
38  }
39  std::cout << "Opening port " << rtmidi->getPortName( port_number ) << std::endl;
40  rtmidi->openPort( port_number );
41  return rtmidi;
42  }
43 
44  inline void wait_for_press_enter( const std::string& message )
45  {
46  std::cout << message << std::endl;
47  char input;
48  std::cin.get( input );
49  }
50 
52  {
53  public:
54  explicit message_sender( RtMidiOut& rtmidiout ) : rtmidiout_( rtmidiout )
55  {
56  }
57 
58  template < class Message >
59  void operator()( const Message& msg ) const
60  {
61  rtmidiout_.sendMessage(
62  static_cast< const unsigned char* >( static_cast< const void* >( msg.data() ) ),
63  msg.size() );
64  }
65 
66  private:
67  RtMidiOut& rtmidiout_;
68  };
69 } // namespace sequencer::rtmidi
Definition: util.hpp:16
std::unique_ptr< RtMidi > make_midi_port(unsigned port_number=0)
Definition: util.hpp:30
void wait_for_press_enter(const std::string &message)
Definition: util.hpp:44
Definition: util.hpp:51
void cout_callback(double time_delta, std::vector< unsigned char > *message, void *)
Definition: util.hpp:18
void operator()(const Message &msg) const
Definition: util.hpp:59
message_sender(RtMidiOut &rtmidiout)
Definition: util.hpp:54