2019-08-30 00:49:58 +02:00
|
|
|
// Copyright (c) 2019, Paul Ferrand
|
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
// list of conditions and the following disclaimer.
|
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
|
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
2020-01-23 22:17:06 +01:00
|
|
|
#include "sfizz/Synth.h"
|
2020-03-11 00:18:18 +01:00
|
|
|
#include "sfizz/Macros.h"
|
2019-08-22 21:11:59 +02:00
|
|
|
#include <absl/flags/parse.h>
|
2019-12-01 21:40:27 +01:00
|
|
|
#include <absl/flags/flag.h>
|
2019-08-22 21:11:59 +02:00
|
|
|
#include <absl/types/span.h>
|
2019-08-24 02:46:40 +02:00
|
|
|
#include <atomic>
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
#include <ios>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <jack/jack.h>
|
|
|
|
|
#include <jack/midiport.h>
|
|
|
|
|
#include <jack/types.h>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <string_view>
|
2019-09-16 14:51:17 +02:00
|
|
|
#include <chrono>
|
2019-08-24 02:46:40 +02:00
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
|
|
static jack_port_t* midiInputPort;
|
|
|
|
|
static jack_port_t* outputPort1;
|
|
|
|
|
static jack_port_t* outputPort2;
|
|
|
|
|
static jack_client_t* client;
|
|
|
|
|
|
|
|
|
|
namespace midi {
|
|
|
|
|
constexpr uint8_t statusMask { 0b11110000 };
|
|
|
|
|
constexpr uint8_t channelMask { 0b00001111 };
|
|
|
|
|
constexpr uint8_t noteOff { 0x80 };
|
|
|
|
|
constexpr uint8_t noteOn { 0x90 };
|
|
|
|
|
constexpr uint8_t polyphonicPressure { 0xA0 };
|
|
|
|
|
constexpr uint8_t controlChange { 0xB0 };
|
|
|
|
|
constexpr uint8_t programChange { 0xC0 };
|
|
|
|
|
constexpr uint8_t channelPressure { 0xD0 };
|
|
|
|
|
constexpr uint8_t pitchBend { 0xE0 };
|
|
|
|
|
constexpr uint8_t systemMessage { 0xF0 };
|
|
|
|
|
|
|
|
|
|
constexpr uint8_t status(uint8_t midiStatusByte)
|
|
|
|
|
{
|
|
|
|
|
return midiStatusByte & statusMask;
|
|
|
|
|
}
|
|
|
|
|
constexpr uint8_t channel(uint8_t midiStatusByte)
|
|
|
|
|
{
|
|
|
|
|
return midiStatusByte & channelMask;
|
|
|
|
|
}
|
2019-12-17 19:19:10 +01:00
|
|
|
|
|
|
|
|
constexpr int buildAndCenterPitch(uint8_t firstByte, uint8_t secondByte)
|
|
|
|
|
{
|
|
|
|
|
return (int)(((unsigned int)secondByte << 7) + (unsigned int)firstByte) - 8192;
|
|
|
|
|
}
|
2019-08-24 02:46:40 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
static std::atomic<bool> keepRunning { true };
|
2019-08-24 02:46:40 +02:00
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
int process(jack_nframes_t numFrames, void* arg)
|
2019-08-24 02:46:40 +02:00
|
|
|
{
|
|
|
|
|
auto synth = reinterpret_cast<sfz::Synth*>(arg);
|
|
|
|
|
|
|
|
|
|
auto buffer = jack_port_get_buffer(midiInputPort, numFrames);
|
|
|
|
|
assert(buffer);
|
|
|
|
|
|
|
|
|
|
auto numMidiEvents = jack_midi_get_event_count(buffer);
|
|
|
|
|
jack_midi_event_t event;
|
|
|
|
|
|
2019-08-30 12:07:57 +02:00
|
|
|
// Midi dispatching
|
2019-08-24 02:46:40 +02:00
|
|
|
for (uint32_t i = 0; i < numMidiEvents; ++i) {
|
|
|
|
|
if (jack_midi_event_get(&event, buffer, i) != 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (event.size == 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
switch (midi::status(event.buffer[0])) {
|
|
|
|
|
case midi::noteOff:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] Note " << +event.buffer[1] << " OFF at time " << event.time);
|
2019-12-23 01:25:20 +01:00
|
|
|
synth->noteOff(event.time, event.buffer[1], event.buffer[2]);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::noteOn:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] Note " << +event.buffer[1] << " ON at time " << event.time);
|
2019-12-23 01:25:20 +01:00
|
|
|
synth->noteOn(event.time, event.buffer[1], event.buffer[2]);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::polyphonicPressure:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] Polyphonic pressure on at time " << event.time);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::controlChange:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] CC " << +event.buffer[1] << " at time " << event.time);
|
2019-12-23 01:25:20 +01:00
|
|
|
synth->cc(event.time, event.buffer[1], event.buffer[2]);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::programChange:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] Program change at time " << event.time);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::channelPressure:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] Channel pressure at time " << event.time);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::pitchBend:
|
2019-12-23 01:25:20 +01:00
|
|
|
synth->pitchWheel(event.time, midi::buildAndCenterPitch(event.buffer[1], event.buffer[2]));
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] Pitch bend at time " << event.time);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
case midi::systemMessage:
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("[MIDI] System message at time " << event.time);
|
2019-08-24 02:46:40 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-30 12:07:57 +02:00
|
|
|
|
2019-09-07 01:12:20 +02:00
|
|
|
auto leftOutput = reinterpret_cast<float*>(jack_port_get_buffer(outputPort1, numFrames));
|
|
|
|
|
auto rightOutput = reinterpret_cast<float*>(jack_port_get_buffer(outputPort2, numFrames));
|
|
|
|
|
synth->renderBlock({ { leftOutput, rightOutput }, numFrames });
|
2019-08-29 02:23:33 +02:00
|
|
|
|
2019-08-24 02:46:40 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
int sampleBlockChanged(jack_nframes_t nframes, void* arg)
|
2019-08-24 02:46:40 +02:00
|
|
|
{
|
|
|
|
|
if (arg == nullptr)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
auto synth = reinterpret_cast<sfz::Synth*>(arg);
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("Sample per block changed to " << nframes);
|
2019-08-24 02:46:40 +02:00
|
|
|
synth->setSamplesPerBlock(nframes);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
int sampleRateChanged(jack_nframes_t nframes, void* arg)
|
2019-08-24 02:46:40 +02:00
|
|
|
{
|
|
|
|
|
if (arg == nullptr)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
auto synth = reinterpret_cast<sfz::Synth*>(arg);
|
2019-12-08 17:10:46 +01:00
|
|
|
// DBG("Sample rate changed to " << nframes);
|
2019-08-24 02:46:40 +02:00
|
|
|
synth->setSampleRate(nframes);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-25 00:00:50 +02:00
|
|
|
static bool shouldClose { false };
|
|
|
|
|
|
2020-03-11 00:18:18 +01:00
|
|
|
static void done(int sig)
|
2019-08-24 02:46:40 +02:00
|
|
|
{
|
2019-08-25 00:00:50 +02:00
|
|
|
std::cout << "Signal received" << '\n';
|
|
|
|
|
shouldClose = true;
|
2020-03-11 00:18:18 +01:00
|
|
|
UNUSED(sig);
|
2019-08-25 00:00:50 +02:00
|
|
|
// if (client != nullptr)
|
2019-09-07 01:12:20 +02:00
|
|
|
|
2019-08-25 00:00:50 +02:00
|
|
|
// exit(0);
|
2019-08-24 02:46:40 +02:00
|
|
|
}
|
2019-07-29 02:13:03 +02:00
|
|
|
|
2020-02-23 17:35:22 +01:00
|
|
|
ABSL_FLAG(std::string, client_name, "sfizz", "Jack client name");
|
2019-12-23 01:25:20 +01:00
|
|
|
ABSL_FLAG(std::string, oversampling, "1x", "Internal oversampling factor (value values are x1, x2, x4, x8)");
|
2019-12-01 21:40:27 +01:00
|
|
|
ABSL_FLAG(uint32_t, preload_size, 8192, "Preloaded value");
|
|
|
|
|
|
2019-07-29 02:13:03 +02:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2019-08-24 02:46:40 +02:00
|
|
|
// std::ios::sync_with_stdio(false);
|
2019-08-22 21:11:59 +02:00
|
|
|
auto arguments = absl::ParseCommandLine(argc, argv);
|
2020-01-19 19:01:55 +01:00
|
|
|
if (arguments.size() < 2) {
|
|
|
|
|
std::cout << "You need to specify an SFZ file to load." << '\n';
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 21:11:59 +02:00
|
|
|
auto filesToParse = absl::MakeConstSpan(arguments).subspan(1);
|
2020-02-23 17:35:22 +01:00
|
|
|
const std::string clientName = absl::GetFlag(FLAGS_client_name);
|
2019-12-01 21:40:27 +01:00
|
|
|
const std::string oversampling = absl::GetFlag(FLAGS_oversampling);
|
|
|
|
|
const uint32_t preload_size = absl::GetFlag(FLAGS_preload_size);
|
|
|
|
|
|
|
|
|
|
std::cout << "Flags" << '\n';
|
2020-02-23 17:35:22 +01:00
|
|
|
std::cout << "- Client name: " << clientName << '\n';
|
2019-12-01 21:40:27 +01:00
|
|
|
std::cout << "- Oversampling: " << oversampling << '\n';
|
|
|
|
|
std::cout << "- Preloaded Size: " << preload_size << '\n';
|
|
|
|
|
const auto factor = [&]() {
|
|
|
|
|
if (oversampling == "x1") return sfz::Oversampling::x1;
|
|
|
|
|
if (oversampling == "x2") return sfz::Oversampling::x2;
|
|
|
|
|
if (oversampling == "x4") return sfz::Oversampling::x4;
|
|
|
|
|
if (oversampling == "x8") return sfz::Oversampling::x8;
|
|
|
|
|
return sfz::Oversampling::x1;
|
|
|
|
|
}();
|
|
|
|
|
|
2019-08-22 21:11:59 +02:00
|
|
|
std::cout << "Positional arguments:";
|
2019-08-24 02:46:40 +02:00
|
|
|
for (auto& file : filesToParse)
|
2019-08-22 21:11:59 +02:00
|
|
|
std::cout << " " << file << ',';
|
|
|
|
|
std::cout << '\n';
|
2019-07-29 02:13:03 +02:00
|
|
|
|
|
|
|
|
sfz::Synth synth;
|
2019-12-01 21:40:27 +01:00
|
|
|
synth.setOversamplingFactor(factor);
|
|
|
|
|
synth.setPreloadSize(preload_size);
|
2019-09-17 14:52:27 +02:00
|
|
|
synth.loadSfzFile(filesToParse[0]);
|
2019-07-29 02:13:03 +02:00
|
|
|
std::cout << "==========" << '\n';
|
|
|
|
|
std::cout << "Total:" << '\n';
|
|
|
|
|
std::cout << "\tMasters: " << synth.getNumMasters() << '\n';
|
|
|
|
|
std::cout << "\tGroups: " << synth.getNumGroups() << '\n';
|
|
|
|
|
std::cout << "\tRegions: " << synth.getNumRegions() << '\n';
|
|
|
|
|
std::cout << "\tCurves: " << synth.getNumCurves() << '\n';
|
2019-08-04 16:07:09 +02:00
|
|
|
std::cout << "\tPreloadedSamples: " << synth.getNumPreloadedSamples() << '\n';
|
2019-07-29 02:13:03 +02:00
|
|
|
std::cout << "==========" << '\n';
|
|
|
|
|
std::cout << "Included files:" << '\n';
|
2019-08-24 02:46:40 +02:00
|
|
|
for (auto& file : synth.getIncludedFiles())
|
2019-08-04 16:07:09 +02:00
|
|
|
std::cout << '\t' << file.string() << '\n';
|
2019-07-29 02:13:03 +02:00
|
|
|
std::cout << "==========" << '\n';
|
|
|
|
|
std::cout << "Defines:" << '\n';
|
2019-08-24 02:46:40 +02:00
|
|
|
for (auto& define : synth.getDefines())
|
2019-07-29 02:13:03 +02:00
|
|
|
std::cout << '\t' << define.first << '=' << define.second << '\n';
|
2019-07-30 00:29:23 +02:00
|
|
|
std::cout << "==========" << '\n';
|
|
|
|
|
std::cout << "Unknown opcodes:";
|
2019-08-24 02:46:40 +02:00
|
|
|
for (auto& opcode : synth.getUnknownOpcodes())
|
2019-07-30 00:29:23 +02:00
|
|
|
std::cout << opcode << ',';
|
|
|
|
|
std::cout << '\n';
|
2019-08-24 02:46:40 +02:00
|
|
|
// std::cout << std::flush;
|
|
|
|
|
|
|
|
|
|
jack_status_t status;
|
2020-02-23 17:35:22 +01:00
|
|
|
client = jack_client_open(clientName.c_str(), JackNullOption, &status);
|
2019-08-24 02:46:40 +02:00
|
|
|
if (client == nullptr) {
|
|
|
|
|
std::cerr << "Could not open JACK client" << '\n';
|
|
|
|
|
// if (status & JackFailure)
|
|
|
|
|
// std::cerr << "JackFailure: Overall operation failed" << '\n';
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status & JackNameNotUnique) {
|
|
|
|
|
std::cout << "Name was taken: assigned " << jack_get_client_name(client) << "instead" << '\n';
|
|
|
|
|
}
|
|
|
|
|
if (status & JackServerStarted) {
|
|
|
|
|
std::cout << "Connected to JACK" << '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
synth.setSamplesPerBlock(jack_get_buffer_size(client));
|
|
|
|
|
synth.setSampleRate(jack_get_sample_rate(client));
|
|
|
|
|
|
|
|
|
|
jack_set_sample_rate_callback(client, sampleRateChanged, &synth);
|
|
|
|
|
jack_set_buffer_size_callback(client, sampleBlockChanged, &synth);
|
|
|
|
|
jack_set_process_callback(client, process, &synth);
|
|
|
|
|
|
|
|
|
|
midiInputPort = jack_port_register(client, "input", JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, 0);
|
|
|
|
|
if (midiInputPort == nullptr) {
|
|
|
|
|
std::cerr << "Could not open MIDI input port" << '\n';
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
outputPort1 = jack_port_register(client, "output_1", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
|
|
|
|
|
outputPort2 = jack_port_register(client, "output_2", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
|
|
|
|
|
if (outputPort1 == nullptr || outputPort2 == nullptr) {
|
|
|
|
|
std::cerr << "Could not open output ports" << '\n';
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (jack_activate(client) != 0) {
|
|
|
|
|
std::cerr << "Could not activate client" << '\n';
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto systemPorts = jack_get_ports(client, nullptr, nullptr, JackPortIsPhysical | JackPortIsInput);
|
|
|
|
|
if (systemPorts == nullptr) {
|
|
|
|
|
std::cerr << "No physical output ports found" << '\n';
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (jack_connect(client, jack_port_name(outputPort1), systemPorts[0])) {
|
|
|
|
|
std::cerr << "Cannot connect to physical output ports (0)" << '\n';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (jack_connect(client, jack_port_name(outputPort2), systemPorts[1])) {
|
|
|
|
|
std::cerr << "Cannot connect to physical output ports (1)" << '\n';
|
|
|
|
|
}
|
|
|
|
|
jack_free(systemPorts);
|
|
|
|
|
|
|
|
|
|
signal(SIGHUP, done);
|
|
|
|
|
signal(SIGINT, done);
|
|
|
|
|
signal(SIGTERM, done);
|
|
|
|
|
signal(SIGQUIT, done);
|
2019-08-25 00:00:50 +02:00
|
|
|
|
2019-09-16 14:51:17 +02:00
|
|
|
while (!shouldClose){
|
2019-12-02 12:35:14 +01:00
|
|
|
#ifndef NDEBUG
|
2019-12-02 12:24:54 +01:00
|
|
|
std::cout << "Allocated buffers: " << synth.getAllocatedBuffers() << '\n';
|
|
|
|
|
std::cout << "Total size: " << synth.getAllocatedBytes() << '\n';
|
|
|
|
|
#endif
|
2020-03-10 18:50:31 +01:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
2019-09-16 14:51:17 +02:00
|
|
|
}
|
2019-09-07 01:12:20 +02:00
|
|
|
|
2019-08-25 00:00:50 +02:00
|
|
|
std::cout << "Closing..." << '\n';
|
|
|
|
|
jack_client_close(client);
|
2019-07-29 02:13:03 +02:00
|
|
|
return 0;
|
2019-12-01 21:40:27 +01:00
|
|
|
}
|