sequencer
util.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <sequencer/assert.hpp>
5 
6 #include <array>
7 #include <cstddef>
8 #include <cstdint>
9 #include <utility>
10 
11 namespace sequencer::midi
12 {
13  static constexpr auto invalid_string = "invalid";
14  static constexpr auto max_14bit = 16384;
15 
16  constexpr std::byte status_byte_for( std::byte status_byte, std::uint8_t channel ) noexcept
17  {
18  return status_byte | std::byte{channel};
19  }
20 
21  constexpr bool check_status_byte( std::byte status_byte, std::byte message_byte )
22  {
23  return ( message_byte & status_byte ) == status_byte;
24  }
25 
26  constexpr std::array< std::uint8_t, 3 >
28  {
29  return {static_cast< std::uint8_t >( message[ 0 ] & std::byte{0x0F} ),
30  static_cast< std::uint8_t >( message[ 1 ] ),
31  static_cast< std::uint8_t >( message[ 2 ] )};
32  }
33 
34  constexpr std::pair< std::byte, std::byte > uint16_to_lsb_msb( std::uint16_t value ) noexcept
35  {
36  SEQUENCER_ASSERT( value < max_14bit );
37  return value < 128
38  ? std::pair( std::byte{static_cast< std::uint8_t >( value )}, std::byte{0x00} )
39  : std::make_pair( std::byte{static_cast< std::uint8_t >( value % 128 )},
40  std::byte{static_cast< std::uint8_t >( value / 128 )} );
41  }
42 
43  constexpr std::uint16_t
44  lsb_msb_to_uint16( const std::pair< std::byte, std::byte >& two_bytes ) noexcept
45  {
46  return static_cast< std::uint8_t >( two_bytes.second ) * std::uint16_t( 128 ) +
47  static_cast< std::uint8_t >( two_bytes.first );
48  }
49 } // namespace sequencer::midi
constexpr bool check_status_byte(std::byte status_byte, std::byte message_byte)
Definition: util.hpp:21
std::conditional_t< greater_than< number_of_bytes, 0 >::value, std::array< std::byte, number_of_bytes >, std::vector< std::byte > > message_t
Definition: message_type.hpp:18
#define SEQUENCER_ASSERT(cond)
Definition: assert.hpp:8
constexpr std::array< std::uint8_t, 3 > read_channel_with_two_7bit_values(const message_t< 3 > &message) noexcept
Definition: util.hpp:27
constexpr std::uint16_t lsb_msb_to_uint16(const std::pair< std::byte, std::byte > &two_bytes) noexcept
Definition: util.hpp:44
Definition: clock.hpp:13
constexpr std::byte status_byte_for(std::byte status_byte, std::uint8_t channel) noexcept
Definition: util.hpp:16
constexpr std::pair< std::byte, std::byte > uint16_to_lsb_msb(std::uint16_t value) noexcept
Definition: util.hpp:34