Skip to content

Commit

Permalink
[core] code cleanups for C++20
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jul 11, 2023
1 parent b2bf415 commit 0f645cb
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 47 deletions.
43 changes: 26 additions & 17 deletions include/libremidi/detail/alsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,29 @@
// time stamps and other assorted fixes!!!

// If you don't need timestamping for incoming MIDI events, define the
// preprocessor definition AVOID_TIMESTAMPING to save resources
// preprocessor definition LIBREMIDI_ALSA_AVOID_TIMESTAMPING to save resources
// associated with the ALSA sequencer queues.

namespace libremidi
{
namespace
{
// This function is used to count or get the pinfo structure for a given port
// number.
inline unsigned int
portInfo(snd_seq_t* seq, snd_seq_port_info_t* pinfo, unsigned int type, int portNumber)
{
snd_seq_client_info_t* cinfo{};
int client;
int count = 0;
snd_seq_client_info_alloca(&cinfo);

snd_seq_client_info_set_client(cinfo, -1);
while (snd_seq_query_next_client(seq, cinfo) >= 0)
{
client = snd_seq_client_info_get_client(cinfo);
int client = snd_seq_client_info_get_client(cinfo);
if (client == 0)
continue;

// Reset query info
snd_seq_port_info_set_client(pinfo, client);
snd_seq_port_info_set_port(pinfo, -1);
Expand Down Expand Up @@ -295,7 +297,7 @@ class midi_in_alsa final : public midi_in_api
}

// Create the input queue
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
data.queue_id = snd_seq_alloc_named_queue(seq, "libremidi queue");
// Set arbitrary tempo (mm=100) and resolution (240)
snd_seq_queue_tempo_t* qtempo;
Expand Down Expand Up @@ -327,13 +329,16 @@ class midi_in_alsa final : public midi_in_api
close(data.trigger_fds[1]);
if (data.vport >= 0)
snd_seq_delete_port(data.seq, data.vport);
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
snd_seq_free_queue(data.seq, data.queue_id);
#endif
snd_seq_close(data.seq);
}

libremidi::API get_current_api() const noexcept override { return libremidi::API::LINUX_ALSA; }
libremidi::API get_current_api() const noexcept override
{
return libremidi::API::LINUX_ALSA_SEQ;
}

void open_port(unsigned int portNumber, std::string_view portName) override
{
Expand All @@ -350,7 +355,7 @@ class midi_in_alsa final : public midi_in_api
return;
}

snd_seq_port_info_t* src_pinfo;
snd_seq_port_info_t* src_pinfo{};
snd_seq_port_info_alloca(&src_pinfo);
if (portInfo(
data.seq, src_pinfo, SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ,
Expand All @@ -364,12 +369,12 @@ class midi_in_alsa final : public midi_in_api
return;
}

snd_seq_addr_t sender, receiver;
snd_seq_addr_t sender{}, receiver{};
sender.client = snd_seq_port_info_get_client(src_pinfo);
sender.port = snd_seq_port_info_get_port(src_pinfo);
receiver.client = snd_seq_client_id(data.seq);

snd_seq_port_info_t* pinfo;
snd_seq_port_info_t* pinfo{};
snd_seq_port_info_alloca(&pinfo);
if (data.vport < 0)
{
Expand All @@ -380,7 +385,7 @@ class midi_in_alsa final : public midi_in_api
snd_seq_port_info_set_type(
pinfo, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
snd_seq_port_info_set_midi_channels(pinfo, 16);
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
snd_seq_port_info_set_timestamping(pinfo, 1);
snd_seq_port_info_set_timestamp_real(pinfo, 1);
snd_seq_port_info_set_timestamp_queue(pinfo, data.queue_id);
Expand Down Expand Up @@ -420,7 +425,7 @@ class midi_in_alsa final : public midi_in_api
if (inputData_.doInput == false)
{
// Start the input queue
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
snd_seq_start_queue(data.seq, data.queue_id, nullptr);
snd_seq_drain_output(data.seq);
#endif
Expand Down Expand Up @@ -451,14 +456,14 @@ class midi_in_alsa final : public midi_in_api
{
if (data.vport < 0)
{
snd_seq_port_info_t* pinfo;
snd_seq_port_info_t* pinfo{};
snd_seq_port_info_alloca(&pinfo);
snd_seq_port_info_set_capability(
pinfo, SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE);
snd_seq_port_info_set_type(
pinfo, SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
snd_seq_port_info_set_midi_channels(pinfo, 16);
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
snd_seq_port_info_set_timestamping(pinfo, 1);
snd_seq_port_info_set_timestamp_real(pinfo, 1);
snd_seq_port_info_set_timestamp_queue(pinfo, data.queue_id);
Expand All @@ -481,7 +486,7 @@ class midi_in_alsa final : public midi_in_api
pthread_join(data.thread, nullptr);

// Start the input queue
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
snd_seq_start_queue(data.seq, data.queue_id, nullptr);
snd_seq_drain_output(data.seq);
#endif
Expand All @@ -508,6 +513,7 @@ class midi_in_alsa final : public midi_in_api
}
}
}

void close_port() override
{
if (connected_)
Expand All @@ -519,7 +525,7 @@ class midi_in_alsa final : public midi_in_api
data.subscription = nullptr;
}
// Stop the input queue
#ifndef AVOID_TIMESTAMPING
#ifndef LIBREMIDI_ALSA_AVOID_TIMESTAMPING
snd_seq_stop_queue(data.seq, data.queue_id, nullptr);
snd_seq_drain_output(data.seq);
#endif
Expand All @@ -537,10 +543,12 @@ class midi_in_alsa final : public midi_in_api
pthread_join(data.thread, nullptr);
}
}

void set_client_name(std::string_view clientName) override
{
snd_seq_set_client_name(data.seq, clientName.data());
}

void set_port_name(std::string_view portName) override
{
snd_seq_port_info_t* pinfo;
Expand All @@ -549,6 +557,7 @@ class midi_in_alsa final : public midi_in_api
snd_seq_port_info_set_name(pinfo, portName.data());
snd_seq_set_port_info(data.seq, data.vport, pinfo);
}

unsigned int get_port_count() override
{
snd_seq_port_info_t* pinfo;
Expand Down Expand Up @@ -601,7 +610,7 @@ class midi_in_alsa final : public midi_in_api
int poll_fd_count{};
pollfd* poll_fds{};

snd_seq_event_t* ev;
snd_seq_event_t* ev{};

apidata.bufferSize = 32;
int result = snd_midi_event_new(0, &apidata.coder);
Expand Down Expand Up @@ -739,7 +748,7 @@ class midi_in_alsa final : public midi_in_api
{

// Calculate the time stamp:
message.timestamp = 0.0;
message.timestamp = 0;

// Method 1: Use the system time.
// gettimeofday(&tv, (struct timezone *)nullptr);
Expand Down
4 changes: 2 additions & 2 deletions include/libremidi/detail/raw_alsa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ namespace libremidi
{
struct midi_stream_decoder
{
uint8_t runningStatusType_{};
midi_in_api::in_data& data;
midi_bytes bytes;
message msg;

midi_stream_decoder(midi_in_api::in_data& data)
explicit midi_stream_decoder(midi_in_api::in_data& data)
: data{data}
{
bytes.reserve(16);
Expand All @@ -49,7 +50,6 @@ struct midi_stream_decoder
bytes.erase(bytes.begin(), bytes.begin() + (begin - bytes.data()));
}

uint8_t runningStatusType_{};
int parse(unsigned char* bytes, unsigned char* end)
{
int sz = end - bytes;
Expand Down
10 changes: 1 addition & 9 deletions include/libremidi/libremidi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,26 +353,18 @@ std::string midi_out::get_port_name(unsigned int portNumber)
return rtapi_->get_port_name(portNumber);
}

LIBREMIDI_INLINE
void midi_out::send_message(const std::vector<unsigned char>& message)
{
send_message(message.data(), message.size());
}

LIBREMIDI_INLINE
void midi_out::send_message(const libremidi::message& message)
{
send_message(message.bytes.data(), message.bytes.size());
}

#if LIBREMIDI_HAS_SPAN
LIBREMIDI_INLINE
void midi_out::send_message(std::span<unsigned char> message)
void midi_out::send_message(std::span<const unsigned char> message)
{
send_message(message.data(), message.size());
}

#endif
LIBREMIDI_INLINE
void midi_out::send_message(const unsigned char* message, size_t size)
{
Expand Down
12 changes: 2 additions & 10 deletions include/libremidi/libremidi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,12 @@
#include <iostream>
#include <memory>
#include <optional>
#include <span>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>

#if defined(__cpp_lib_span) && __cpp_lib_span >= 202002
#define LIBREMIDI_HAS_SPAN 1
#include <span>
#endif

namespace libremidi
{
//! Defines various error types.
Expand Down Expand Up @@ -464,8 +460,6 @@ class LIBREMIDI_EXPORT midi_out
An exception is thrown if an error occurs during output or an
output connection was not previously established.
*/
void send_message(const std::vector<unsigned char>& message);

void send_message(const libremidi::message& message);

//! Immediately send a single message out an open MIDI output port.
Expand All @@ -478,9 +472,7 @@ class LIBREMIDI_EXPORT midi_out
*/
void send_message(const unsigned char* message, size_t size);

#if LIBREMIDI_HAS_SPAN
void send_message(std::span<unsigned char>);
#endif
void send_message(std::span<const unsigned char>);

//! Set an error callback function to be invoked when an error has occured.
/*!
Expand Down
20 changes: 11 additions & 9 deletions include/libremidi/message.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <libremidi/config.hpp>

#include <span>

namespace libremidi
{
enum class message_type : uint8_t
Expand Down Expand Up @@ -59,18 +61,15 @@ enum class meta_event_type : uint8_t
UNKNOWN = 0xFF
};

constexpr inline uint8_t clamp(uint8_t val, uint8_t min, uint8_t max)
{
return std::max(std::min(val, max), min);
}

struct message
{
midi_bytes bytes;
double timestamp{};
int64_t timestamp{};

operator std::span<const unsigned char>() const noexcept { return bytes; }

message() noexcept = default;
message(const midi_bytes& src_bytes, double src_timestamp)
message(const midi_bytes& src_bytes, int64_t src_timestamp)
: bytes(src_bytes)
, timestamp(src_timestamp)
{
Expand All @@ -81,7 +80,7 @@ struct message
}
static uint8_t make_command(const message_type type, const int channel) noexcept
{
return (uint8_t)((uint8_t)type | clamp(channel, 0, channel - 1));
return (uint8_t)((uint8_t)type | std::clamp(channel, 0, channel - 1));
}

static message note_on(uint8_t channel, uint8_t note, uint8_t velocity) noexcept
Expand Down Expand Up @@ -206,7 +205,10 @@ struct meta_events
{
static message end_of_track() { return {0xFF, 0x2F, 0}; }

static message channel(int channel) { return {0xff, 0x20, 0x01, clamp(0, 0xff, channel - 1)}; }
static message channel(int channel)
{
return {0xff, 0x20, 0x01, (uint8_t)std::clamp(0, 0xff, channel - 1)};
}

static message tempo(int mpqn)
{
Expand Down

0 comments on commit 0f645cb

Please sign in to comment.