Skip to content

Commit

Permalink
Import jack1 alsa_midi driver as internal client
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed Oct 26, 2023
1 parent 0620b1b commit 7cf014d
Show file tree
Hide file tree
Showing 16 changed files with 2,973 additions and 1 deletion.
10 changes: 9 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ os = build_machine.system()
cc = meson.get_compiler('c')

alsa_required = false
if get_option('alsa_in_out').enabled() or get_option('zalsa').enabled()
if get_option('alsa_in_out').enabled() or get_option('alsa_midi').enabled() or get_option('zalsa').enabled()
alsa_required = true
endif

Expand Down Expand Up @@ -144,6 +144,13 @@ if get_option('alsa_in_out').enabled() or (
build_alsa_in_out = true
endif

build_alsa_midi = false
if get_option('alsa_midi').enabled() or (
get_option('alsa_midi').auto() and dep_alsa.found() and has_jack2_internal_client
)
build_alsa_midi = true
endif

build_jack_net = false
if get_option('jack_net').enabled() or (get_option('jack_net').auto() and lib_jacknet_dep.found())
build_jack_net = true
Expand Down Expand Up @@ -178,6 +185,7 @@ endif


message('Build alsa_in and alsa_out executables: ' + build_alsa_in_out.to_string())
message('Build alsa_midi internal client: ' + build_alsa_midi.to_string())
message('Build jack_net_master and jack_net_slave executables: ' + build_jack_net.to_string())
message('Build jack_netsource executable: ' + build_jack_netsource.to_string())
if build_jack_netsource
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
option('alsa_in_out', type: 'feature', value: 'auto', description: 'Build the alsa_in and alsa_out executables (default: auto)')
option('alsa_midi', type: 'feature', value: 'auto', description: 'Build the alsa_midi internal client (default: auto)')
option('jack_net', type: 'feature', value: 'auto', description: 'Build the jack_net_master and jack_net_slave executables (default: auto)')
option('jack_netsource', type: 'feature', value: 'auto', description: 'Build the jack_netsource executable (default: auto)')
option('jack_rec', type: 'feature', value: 'auto', description: 'Build the jack_rec executable (default: auto)')
Expand Down
139 changes: 139 additions & 0 deletions tools/alsa_midi/a2j.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
* ALSA SEQ < - > JACK MIDI bridge
*
* Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
* Copyright (c) 2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#ifndef __jack_alsa_midi_h__
#define __jack_alsa_midi_h__

#include <stdbool.h>
#include <semaphore.h>
#include <jack/midiport.h>
#include <jack/ringbuffer.h>

#ifdef ALSA_MIDI_INTERNAL_CLIENT
#define JACK_DRIVER_DECL
typedef struct jack_engine_t jack_engine_t;
#else
#include "driver.h"
#endif

#include "list.h"

#define JACK_INVALID_PORT NULL

#define MAX_PORTS 2048
#define MAX_EVENT_SIZE 1024

#define PORT_HASH_BITS 4
#define PORT_HASH_SIZE (1 << PORT_HASH_BITS)

/* Beside enum use, these are indeces for (struct a2j).stream array */
#define A2J_PORT_CAPTURE 0 // ALSA playback port -> JACK capture port
#define A2J_PORT_PLAYBACK 1 // JACK playback port -> ALSA capture port

typedef struct a2j_port * a2j_port_hash_t[PORT_HASH_SIZE];

struct alsa_midi_driver;

struct a2j_port {
struct a2j_port * next; /* hash - jack */
struct list_head siblings; /* list - main loop */
struct alsa_midi_driver * driver_ptr;
bool is_dead;
char name[64];
snd_seq_addr_t remote;
jack_port_t * jack_port;

jack_ringbuffer_t * inbound_events; // alsa_midi_event_t + data
int64_t last_out_time;

void * jack_buf;
};

struct a2j_stream {
snd_midi_event_t *codec;

jack_ringbuffer_t *new_ports;

a2j_port_hash_t port_hash;
struct list_head list;
};

typedef struct alsa_midi_driver {
JACK_DRIVER_DECL;

jack_client_t * jack_client;

snd_seq_t *seq;
pthread_t alsa_input_thread;
pthread_t alsa_output_thread;
int client_id;
int port_id;
int queue;
bool freewheeling;
bool running;
bool finishing;

jack_ringbuffer_t* port_del; // struct a2j_port*
jack_ringbuffer_t* outbound_events; // struct a2j_delivery_event
jack_nframes_t cycle_start;

sem_t output_semaphore;

struct a2j_stream stream[2];

} alsa_midi_driver_t;

#define NSEC_PER_SEC ((int64_t)1000 * 1000 * 1000)

struct a2j_alsa_midi_event {
int64_t time;
int size;
};

#define MAX_JACKMIDI_EV_SIZE 64

struct a2j_delivery_event {
struct list_head siblings;

/* a jack MIDI event, plus the port its destined for: everything
the ALSA output thread needs to deliver the event. time is
part of the jack_event.
*/
jack_midi_event_t jack_event;
jack_nframes_t time; /* realtime, not offset time */
struct a2j_port* port;
char midistring[MAX_JACKMIDI_EV_SIZE];
};

void a2j_error(const char* fmt, ...);

#define A2J_DEBUG
/*#undef A2J_DEBUG*/

#ifdef A2J_DEBUG
extern bool a2j_do_debug;
extern void _a2j_debug(const char* fmt, ...);
#define a2j_debug(fmt, ...) if (a2j_do_debug) { _a2j_debug ((fmt), ## __VA_ARGS__); }
#else
#define a2j_debug(fmt, ...)
#endif

#endif /* __jack_alsa_midi_h__ */
Loading

0 comments on commit 7cf014d

Please sign in to comment.