franta-hg@2: /**
franta-hg@2: * DJM-Fix
franta-hg@2: * Copyright © 2020 František Kučera (Frantovo.cz, GlobalCode.info)
franta-hg@2: *
franta-hg@2: * This program is free software: you can redistribute it and/or modify
franta-hg@2: * it under the terms of the GNU General Public License as published by
franta-hg@2: * the Free Software Foundation, version 3 of the License.
franta-hg@2: *
franta-hg@2: * This program is distributed in the hope that it will be useful,
franta-hg@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of
franta-hg@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
franta-hg@2: * GNU General Public License for more details.
franta-hg@2: *
franta-hg@2: * You should have received a copy of the GNU General Public License
franta-hg@2: * along with this program. If not, see .
franta-hg@2: */
franta-hg@2: #include
franta-hg@5: #include
franta-hg@5: #include
franta-hg@5: #include
franta-hg@2:
franta-hg@3: #include
franta-hg@3:
franta-hg@2: #include "AlsaBridge.h"
franta-hg@2:
franta-hg@2: namespace djmfix {
franta-hg@2: namespace alsa {
franta-hg@2:
franta-hg@2: class AlsaBridgeImpl : public AlsaBridge, private djmfix::MidiSender {
franta-hg@2: private:
franta-hg@2: djmfix::DJMFix* djmFix;
franta-hg@5: snd_rawmidi_t* input;
franta-hg@5: snd_rawmidi_t* output;
franta-hg@5: std::thread receivingThread;
franta-hg@5: std::atomic stopped{false};
franta-hg@5:
franta-hg@5: void run() {
franta-hg@5: while (!stopped) {
franta-hg@5: // TODO: poll
franta-hg@5: uint8_t buffer[256];
franta-hg@5: ssize_t length = snd_rawmidi_read(input, buffer, sizeof (buffer));
franta-hg@5: if (length > 0 && length <= sizeof (buffer)) {
franta-hg@5: // TODO: multiple messages combined together?
franta-hg@5: djmFix->receive(MidiMessage(buffer, buffer + length));
franta-hg@5: }
franta-hg@5:
franta-hg@5: std::this_thread::sleep_for(std::chrono::milliseconds(100));
franta-hg@5: }
franta-hg@5: }
franta-hg@2: public:
franta-hg@2:
franta-hg@5: AlsaBridgeImpl(djmfix::DJMFix* djmFix, const std::string& deviceName) : djmFix(djmFix) {
franta-hg@5: if (djmFix == nullptr) throw std::invalid_argument("need a djmFix for AlsaBridge");
franta-hg@5:
franta-hg@5: int error = snd_rawmidi_open(&input, &output, deviceName.c_str(), SND_RAWMIDI_NONBLOCK);
franta-hg@5: if (error) throw std::invalid_argument("unable to open ALSA device");
franta-hg@5:
franta-hg@5:
franta-hg@2: djmFix->setMidiSender(this);
franta-hg@2: }
franta-hg@2:
franta-hg@2: virtual ~AlsaBridgeImpl() {
franta-hg@5: // TODO: do not use raw/exclusive access to the device
franta-hg@5: snd_rawmidi_close(input);
franta-hg@5: snd_rawmidi_close(output);
franta-hg@2: std::cerr << "~AlsaBridgeImpl()" << std::endl; // TODO: do not mess STDIO
franta-hg@2: }
franta-hg@2:
franta-hg@2: virtual void start() override {
franta-hg@2: djmFix->start();
franta-hg@5: receivingThread = std::thread(&AlsaBridgeImpl::run, this);
franta-hg@2: }
franta-hg@2:
franta-hg@2: virtual void stop() override {
franta-hg@5: stopped = true;
franta-hg@5: receivingThread.join();
franta-hg@2: djmFix->stop();
franta-hg@2: }
franta-hg@2:
franta-hg@2: virtual void send(MidiMessage midiMessage) override {
franta-hg@5: ssize_t length = snd_rawmidi_write(output, midiMessage.data(), midiMessage.size());
franta-hg@5: std::cerr << "AlsaBridgeImpl::send(): length = " << length << std::endl; // TODO: do not mess STDIO
franta-hg@2: }
franta-hg@2:
franta-hg@2: };
franta-hg@2:
franta-hg@5: AlsaBridge* create(djmfix::DJMFix* djmFix, const std::string& deviceName) {
franta-hg@5: return new AlsaBridgeImpl(djmFix, deviceName);
franta-hg@2: }
franta-hg@2:
franta-hg@2: }
franta-hg@2: }