Files
op-pedal/src/JackServerSettings.cpp
T
2021-08-29 14:04:59 -04:00

252 lines
7.7 KiB
C++

// Copyright (c) 2021 Robin Davies
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "pch.h"
#include "Lv2Log.hpp"
#include "JackServerSettings.hpp"
#include <fstream>
#include "PiPedalException.hpp"
#include <string>
#include "unistd.h"
#include <filesystem>
#define DRC_FILENAME "/etc/jackdrc"
using namespace std;
using namespace pipedal;
JackServerSettings::JackServerSettings()
{
}
static std::vector<std::string> SplitArgs(const char *szBuff)
{
std::vector<std::string> result;
while (*szBuff != '\0')
{
while (*szBuff == ' ' || *szBuff == '\t' || *szBuff == '\r' || *szBuff == '\n')
++szBuff;
if (*szBuff == 0)
break;
std::stringstream s;
while (*szBuff != ' ' && *szBuff != '\t' && *szBuff != '\0' && *szBuff != '\r' && *szBuff != '\n')
{
s.put(*szBuff++);
}
result.push_back(s.str());
}
return result;
}
static std::string GetJackStringArg(const std::vector<std::string> &args, const std::string&shortOption, const std::string&longOption)
{
std::string strVal;
for (size_t i = 1; i < args.size(); ++i)
{
auto pos = args[i].rfind(longOption,0);
if (pos != std::string::npos) {
if (args[i].length() == longOption.length())
{
if (i == args.size()-1) {
throw PiPedalException("Can't read Jack configuration.");
}
strVal = args[i+1];
} else {
strVal = args[i].substr(longOption.length());
}
break;
}
pos = args[i].rfind(shortOption,0);
if (pos != std::string::npos) {
if (args[i].length() == shortOption.length())
{
if (i == args.size()-1) {
throw PiPedalException("Can't read Jack configuration.");
}
strVal = args[i+1];
} else {
strVal = args[i].substr(shortOption.length());
}
break;
}
}
return strVal;
}
static std::int32_t GetJackArg(const std::vector<std::string> &args, const std::string&shortOption, const std::string&longOption)
{
std::string strVal = GetJackStringArg(args,shortOption,longOption);
try
{
unsigned long value = std::stoul(strVal);
return value;
}
catch (const std::exception &)
{
throw PiPedalException("Can't read Jack configuration.");
}
}
void JackServerSettings::ReadJackConfiguration()
{
this->valid_ = false;
std::string lastLine;
{
ifstream input(DRC_FILENAME);
if (!input.is_open())
{
Lv2Log::error("Can't read " DRC_FILENAME);
return;
}
while (true)
{
std::string line;
std::getline(input,line);
if (line.length() != 0) {
lastLine = line;
}
if (input.eof()) {
break;
}
}
try
{
std::vector<std::string> argv = SplitArgs(lastLine.c_str());
for (auto i = argv.begin(); i != argv.end(); ++i)
{
if ((*i) == "-dalsa") {
argv.erase(i);
break;
}
}
this->bufferSize_ = GetJackArg(argv, "-p", "--period");
this->numberOfBuffers_ = (uint32_t)GetJackArg(argv, "-n", "--nperiods");
this->sampleRate_ = (uint32_t)GetJackArg(argv, "-r", "--rate");
this->alsaDevice_ = GetJackStringArg(argv,"-d", "--device");
valid_ = true;
}
catch (std::exception &)
{
Lv2Log::error("Can't parse " DRC_FILENAME);
}
}
}
void JackServerSettings::Write()
{
this->valid_ = false;
std::vector<std::string> precedingLines;
std::vector<std::string> argv;
std::string lastLine;
if (std::filesystem::exists(DRC_FILENAME)) {
ifstream input(DRC_FILENAME);
if (!input.is_open())
{
Lv2Log::error("Can't read " DRC_FILENAME);
return;
}
while (true)
{
if (input.eof())
{
Lv2Log::error("Premature end of file in " DRC_FILENAME);
break;
}
std::getline(input,lastLine);
precedingLines.push_back(lastLine);
if (input.eof())
{
break;
}
}
// erase blank lines at the end.
while (precedingLines.size() != 0 && precedingLines[precedingLines.size()-1] == "")
{
precedingLines.erase(precedingLines.begin()+precedingLines.size()-1);
}
// erase the last line, which should contain the command invocation.
if (precedingLines.size() != 0)
{
precedingLines.erase(precedingLines.begin()+precedingLines.size()-1);
}
}
// write to the output.
try
{
ofstream output(DRC_FILENAME);
if (!output.is_open())
{
throw PiPedalException("Can't write " DRC_FILENAME);
}
if (precedingLines.size() == 0)
{
// jack1 incantation for promiscuous servers.
output << "#!/bin/sh" <<endl;
output << "export JACK_PROMISCUOUS_SERVER=" << endl;
output << "export JACK_NO_AUDIO_RESERVATION=" << endl;
output << "umask 0" << endl;
}
for (auto line : precedingLines)
{
output << line << endl;
}
// the style used by qjackctl. :-/
output << "/usr/bin/jackd "
<< "-R -P90"
<< " -driver alsa -d" << this->alsaDevice_
<< " -r" << this->sampleRate_
<< " -p" << this->bufferSize_
<< " -n" << this->numberOfBuffers_ << " -Xseq"
<< endl;
system("/usr/bin/chmod 755 " DRC_FILENAME);
system("pulseaudio --kill");
system("/usr/bin/systemctl enable jack");
if (system("/usr/bin/systemctl restart jack") != 0)
{
throw PiPedalException("Failed to start jack audio service.");
}
}
catch (const std::exception &e)
{
stringstream s;
s << "jack - " << e.what();
Lv2Log::error(s.str());
}
}
JSON_MAP_BEGIN(JackServerSettings)
JSON_MAP_REFERENCE(JackServerSettings, valid)
JSON_MAP_REFERENCE(JackServerSettings, rebootRequired)
JSON_MAP_REFERENCE(JackServerSettings, alsaDevice)
JSON_MAP_REFERENCE(JackServerSettings, sampleRate)
JSON_MAP_REFERENCE(JackServerSettings, bufferSize)
JSON_MAP_REFERENCE(JackServerSettings, numberOfBuffers)
JSON_MAP_END()