AlsaBridge.cpp
branchv_0
changeset 16 63154f9d24a2
parent 13 334b727f7516
child 18 358a601bfe81
     1.1 --- a/AlsaBridge.cpp	Tue Apr 15 22:44:31 2025 +0200
     1.2 +++ b/AlsaBridge.cpp	Tue Apr 15 22:45:25 2025 +0200
     1.3 @@ -1,6 +1,6 @@
     1.4  /**
     1.5   * DJM-Fix
     1.6 - * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
     1.7 + * Copyright © 2025 František Kučera (Frantovo.cz, GlobalCode.info)
     1.8   *
     1.9   * This program is free software: you can redistribute it and/or modify
    1.10   * it under the terms of the GNU General Public License as published by
    1.11 @@ -67,18 +67,24 @@
    1.12  		}
    1.13  
    1.14  		if (cardNumbers.size() == 1) {
    1.15 -			logger->log(L::INFO, "Going to fix card #" + std::to_string(cardNumbers[0]));
    1.16 -			return "hw:" + std::to_string(cardNumbers[0]);
    1.17 +			const auto n = std::to_string(cardNumbers[0]);
    1.18 +			logger->log(L::INFO, "Going to fix card #" + n);
    1.19 +			return "hw:" + n;
    1.20  		} else if (cardNumbers.empty()) {
    1.21 -			throw std::invalid_argument("No card with matching name found. Is the card connected? Maybe try to provide different name pattern.");
    1.22 +			throw std::invalid_argument(
    1.23 +					"No card with matching name found. Is the card connected? "
    1.24 +					"Maybe try to provide different name pattern.");
    1.25  		} else {
    1.26 -			throw std::invalid_argument("Multiple cards with matching name found. Please provide a name pattern that matches only one card");
    1.27 +			throw std::invalid_argument(
    1.28 +					"Multiple cards with matching name found. "
    1.29 +					"Please provide a name pattern that matches only one card");
    1.30  		}
    1.31  	}
    1.32  
    1.33  	std::string toString(const MidiMessage& midiMessage) {
    1.34  		std::stringstream result;
    1.35 -		for (uint8_t b : midiMessage) result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
    1.36 +		for (uint8_t b : midiMessage)
    1.37 +			result << std::hex << std::setw(2) << std::setfill('0') << (int) b;
    1.38  		return result.str();
    1.39  	}
    1.40  
    1.41 @@ -87,11 +93,11 @@
    1.42  			{
    1.43  				std::lock_guard<std::recursive_mutex> lock(midiMutex);
    1.44  				// TODO: poll
    1.45 -				uint8_t buffer[256];
    1.46 -				ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer));
    1.47 -				if (length > 0 && length <= sizeof (buffer)) {
    1.48 +				uint8_t buf[256];
    1.49 +				ssize_t length = snd_rawmidi_read(input, buf, sizeof (buf));
    1.50 +				if (length > 0 && length <= sizeof (buf)) {
    1.51  					// TODO: multiple messages combined together?
    1.52 -					djmFix->receive(MidiMessage(buffer, buffer + length));
    1.53 +					djmFix->receive(MidiMessage(buf, buf + length));
    1.54  				}
    1.55  			}
    1.56  			std::this_thread::sleep_for(std::chrono::milliseconds(100));
    1.57 @@ -99,15 +105,21 @@
    1.58  	}
    1.59  public:
    1.60  
    1.61 -	AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& cardNamePattern, djmfix::logging::Logger* logger) : djmFix(djmFix), logger(logger ? logger : djmfix::logging::blackhole()) {
    1.62 -		if (djmFix == nullptr) throw std::invalid_argument("Need a djmFix for AlsaBridge.");
    1.63 +	AlsaBridgeImpl(
    1.64 +			djmfix::DJMFix* djmFix,
    1.65 +			const std::string& cardNamePattern,
    1.66 +			djmfix::logging::Logger* logger)
    1.67 +	: djmFix(djmFix), logger(logger ? logger : djmfix::logging::blackhole()) //
    1.68 +	{
    1.69 +		if (djmFix == nullptr)
    1.70 +			throw std::invalid_argument("Need a djmFix for AlsaBridge.");
    1.71  
    1.72  		std::string deviceName = findDeviceName(std::regex(cardNamePattern));
    1.73  
    1.74 -		int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK);
    1.75 +		int mode = SND_RAWMIDI_NONBLOCK;
    1.76 +		int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), mode);
    1.77  		if (error) throw std::invalid_argument("Unable to open ALSA device.");
    1.78  
    1.79 -
    1.80  		djmFix->setMidiSender(this);
    1.81  	}
    1.82  
    1.83 @@ -129,15 +141,21 @@
    1.84  		djmFix->stop();
    1.85  	}
    1.86  
    1.87 -	virtual void send(MidiMessage midiMessage) override {
    1.88 +	virtual void send(MidiMessage msg) override {
    1.89  		std::lock_guard<std::recursive_mutex> lock(midiMutex);
    1.90 -		ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size());
    1.91 -		logger->log(L::FINE, "Sent message: length = " + std::to_string(length) + " data = " + toString(midiMessage));
    1.92 +		ssize_t length = snd_rawmidi_write(output, msg.data(), msg.size());
    1.93 +		logger->log(L::FINE, "Sent message:"
    1.94 +				" length = " + std::to_string(length)
    1.95 +				+ " data = " + toString(msg));
    1.96  	}
    1.97  
    1.98  };
    1.99  
   1.100 -AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName, djmfix::logging::Logger* logger) {
   1.101 +AlsaBridge* create(
   1.102 +		djmfix::DJMFix* djmFix,
   1.103 +		const std::string& deviceName,
   1.104 +		djmfix::logging::Logger* logger) //
   1.105 +{
   1.106  	return new AlsaBridgeImpl(djmFix, deviceName, logger);
   1.107  }
   1.108