Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 18 Apr 2023 21:55:58 GMT
From:      Robert Clausecker <fuz@FreeBSD.org>
To:        ports-committers@FreeBSD.org, dev-commits-ports-all@FreeBSD.org, dev-commits-ports-main@FreeBSD.org
Subject:   git: 6fbe3e61eebe - main - audio/pd: Add MIDI support via PortMIDI and sndio
Message-ID:  <202304182155.33ILtwtB062770@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by fuz:

URL: https://cgit.FreeBSD.org/ports/commit/?id=6fbe3e61eebe76d7cc5a7c4cc8442002e7da6e59

commit 6fbe3e61eebe76d7cc5a7c4cc8442002e7da6e59
Author:     Timothy Beyer <beyert_freebsd@fastmail.net>
AuthorDate: 2022-02-11 08:36:31 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-04-18 21:55:30 +0000

    audio/pd: Add MIDI support via PortMIDI and sndio
    
    Also unbreak port on FreeBSD 13+.
    Submitter becomes maintainer.
    
    PR:             252188
    Approved by:    brittlehaus@gmail.com (maintainer timeout)
---
 audio/pd/Makefile                                  |  58 +++-
 audio/pd/files/extra-patch-configure.ac            |  10 +
 audio/pd/files/extra-patch-portmidi_Makefile.am    |  27 ++
 .../extra-patch-portmidi_pm__common_CMakeLists.txt |  54 +++
 .../files/extra-patch-portmidi_porttime_ptlinux.c  |  46 +++
 audio/pd/files/extra-patch-src_Makefile.am         |  11 +
 audio/pd/files/extra-patch-src_s__midi__oss__pm.c  | 147 ++++++++
 audio/pd/files/portmidi/pm_sndio/pmsndio.c         | 382 +++++++++++++++++++++
 audio/pd/files/portmidi/pm_sndio/pmsndio.h         |   5 +
 audio/pd/pkg-descr                                 |   7 +
 10 files changed, 730 insertions(+), 17 deletions(-)

diff --git a/audio/pd/Makefile b/audio/pd/Makefile
index 4e20018e6ee6..b84e3cb74c16 100644
--- a/audio/pd/Makefile
+++ b/audio/pd/Makefile
@@ -1,46 +1,70 @@
 PORTNAME=	pd
 DISTVERSION=	0.47-1
-PORTREVISION=	1
 DISTVERSIONSUFFIX=	.src
+PORTREVISION=	2
 CATEGORIES=	audio
 MASTER_SITES=	http://msp.ucsd.edu/Software/
 
-MAINTAINER=	brittlehaus@gmail.com
+MAINTAINER=	beyert@cs.ucr.edu
 COMMENT=	MIDI-capable real-time audio processor/synthesizer
 WWW=		http://msp.ucsd.edu/software.html
 
-LICENSE=	BSD3CLAUSE LGPL21
-LICENSE_COMB=	multi
+# license of expr~ changed to BSD3 months prior to this version
+LICENSE=	BSD3CLAUSE
 LICENSE_FILE_BSD3CLAUSE=	${WRKSRC}/LICENSE.txt
 
-BROKEN_FreeBSD_13=	ld: error: duplicate symbol: glist_reloadingabstraction
-BROKEN_FreeBSD_14=	ld: error: duplicate symbol: glist_reloadingabstraction
-
 RUN_DEPENDS=	xdg-open:devel/xdg-utils \
 		dejavu>0:x11-fonts/dejavu
 
+USES=		autoreconf dos2unix gmake libtool pkgconfig shebangfix \
+		tcl:wrapper tk:wrapper
+
 WRKSRC=		${WRKDIR}/${DISTNAME:S,${DISTVERSIONSUFFIX},,}
 
-USES=		autoreconf gettext gmake libtool pkgconfig shebangfix \
-		tcl:wrapper tk:wrapper
+OPTIONS_DEFINE=		NLS PORTMIDI ALSA DOCS JACK
+OPTIONS_SUB=		yes
+OPTIONS_DEFAULT=	NLS JACK PORTMIDI
+
+NLS_USES=	gettext
+
+ALSA_CONFIGURE_ENABLE=	alsa
+ALSA_LIB_DEPENDS=	libasound.so:audio/alsa-lib
+JACK_CONFIGURE_ENABLE=	jack
+JACK_LIB_DEPENDS=	libjack.so:audio/jack
+
+PORTMIDI_DESC=	Midi device support via portmidi and sndio
+PORTMIDI_EXTRA_PATCHES=	${PATCHDIR}/extra-patch-configure.ac \
+	${PATCHDIR}/extra-patch-portmidi_Makefile.am \
+	${PATCHDIR}/extra-patch-portmidi_pm__common_CMakeLists.txt \
+	${PATCHDIR}/extra-patch-portmidi_porttime_ptlinux.c \
+	${PATCHDIR}/extra-patch-src_Makefile.am \
+	${PATCHDIR}/extra-patch-src_s__midi__oss__pm.c
+PORTMIDI_LIB_DEPENDS=	libsndio.so:audio/sndio
+
+.include <bsd.port.options.mk>
+
+.if ${PORT_OPTIONS:MPORTMIDI}
+DOS2UNIX_FILES=	portmidi/porttime/ptlinux.c
+.endif
 SHEBANG_FILES=	tcl/pkg_mkIndex.tcl
 SHEBANG_LANG=	tclsh
 tclsh_CMD=	${LOCALBASE}/bin/tclsh
 GNU_CONFIGURE=	yes
 CONFIGURE_ENV=	ac_cv_lib_pthread_pthread_create=no
 
+CFLAGS+=	-fcommon
 CPPFLAGS+=	-I${LOCALBASE}/include
 LDFLAGS+=	-L${LOCALBASE}/lib -pthread
+.if ${PORT_OPTIONS:MPORTMIDI}
+LDFLAGS+=	-lsndio
+.endif
 
-DESKTOP_ENTRIES="Pd" "" "${PREFIX}/lib/pd/tcl/pd.ico" "pd" "" false
-
-OPTIONS_DEFINE=		ALSA DOCS JACK
-OPTIONS_DEFAULT=	JACK
+DESKTOP_ENTRIES="Pd" "" "${PREFIX}/lib/pd/tcl/pd.ico" "pd" "" ${FALSE}
 
-ALSA_CONFIGURE_ENABLE=	alsa
-ALSA_LIB_DEPENDS=	libasound.so:audio/alsa-lib
-JACK_CONFIGURE_ENABLE=	jack
-JACK_LIB_DEPENDS=	libjack.so:audio/jack
+post-extract:
+.if ${PORT_OPTIONS:MPORTMIDI}
+	${CP} -pr ${FILESDIR}/portmidi/pm_sndio ${WRKSRC}/portmidi/
+.endif
 
 post-patch:
 	${FIND} ${PATCH_WRKSRC} -name "*.[ch]" | ${XARGS} ${REINPLACE_CMD} -e \
diff --git a/audio/pd/files/extra-patch-configure.ac b/audio/pd/files/extra-patch-configure.ac
new file mode 100644
index 000000000000..46ddccf58bf2
--- /dev/null
+++ b/audio/pd/files/extra-patch-configure.ac
@@ -0,0 +1,10 @@
+--- configure.ac.orig	2016-06-18 15:02:47.000000000 -0700
++++ configure.ac	2020-07-06 19:37:37.638911000 -0700
+@@ -38,6 +38,7 @@
+ 	if test "x${ANDROID}" = "xno"; then
+ 	 LINUX=yes
+ 	 portaudio=yes
++	 portmidi=yes
+ 	 CFLAGS="-g -O3 -funroll-loops -fomit-frame-pointer $CFLAGS"
+ 	fi
+ 	EXTERNAL_CFLAGS="-fPIC"
diff --git a/audio/pd/files/extra-patch-portmidi_Makefile.am b/audio/pd/files/extra-patch-portmidi_Makefile.am
new file mode 100644
index 000000000000..7c9884cf4129
--- /dev/null
+++ b/audio/pd/files/extra-patch-portmidi_Makefile.am
@@ -0,0 +1,27 @@
+--- portmidi/Makefile.am.orig	2015-05-13 13:58:54.000000000 -0700
++++ portmidi/Makefile.am	2020-07-07 02:39:30.587648000 -0700
+@@ -9,10 +9,9 @@
+ libportmidi_la_SOURCES = pm_common/pmutil.c pm_common/portmidi.c
+ 
+ if LINUX
+-INCLUDES +=  -Ipm_linux
++INCLUDES +=  -Ipm_sndio
+ libportmidi_la_SOURCES += porttime/ptlinux.c \
+-		pm_linux/pmlinux.c \
+-		pm_linux/pmlinuxalsa.c
++		pm_sndio/pmsndio.c
+ endif
+ 
+ if MACOSX
+@@ -37,10 +36,5 @@
+ 	pm_common/pminternal.h \
+ 	pm_common/pmutil.h \
+ 	pm_common/portmidi.h \
+-	pm_linux/pmlinux.h \
+-	pm_linux/pmlinuxalsa.h \
+-	pm_mac/pmmac.h \
+-	pm_mac/pmmacosxcm.h \
+-	pm_win/pmdll.h \
+-	pm_win/pmwinmm.h \
++	pm_sndio/pmsndio.h \
+ 	porttime/porttime.h
diff --git a/audio/pd/files/extra-patch-portmidi_pm__common_CMakeLists.txt b/audio/pd/files/extra-patch-portmidi_pm__common_CMakeLists.txt
new file mode 100644
index 000000000000..91b41e6e184a
--- /dev/null
+++ b/audio/pd/files/extra-patch-portmidi_pm__common_CMakeLists.txt
@@ -0,0 +1,54 @@
+--- portmidi/pm_common/CMakeLists.txt.orig	2010-09-20 19:57:48 UTC
++++ portmidi/pm_common/CMakeLists.txt
+@@ -66,21 +66,12 @@ if(UNIX)
+     set(JAVA_INCLUDE_PATHS ${JAVAVM_LIB}/Headers)
+     message(STATUS "SYSROOT: " ${CMAKE_OSX_SYSROOT})
+   else(APPLE)
+-    # LINUX settings...
+-    include(FindJNI)
+-    message(STATUS "JAVA_JVM_LIB_PATH is " ${JAVA_JVM_LIB_PATH})
+-    message(STATUS "JAVA_INCLUDE_PATH is " ${JAVA_INCLUDE_PATH})
+-    message(STATUS "JAVA_INCLUDE_PATH2 is " ${JAVA_INCLUDE_PATH2})
+-    message(STATUS "JAVA_JVM_LIBRARY is " ${JAVA_JVM_LIBRARY})
+-    set(JAVA_INCLUDE_PATHS ${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
+-    # libjvm.so is found relative to JAVA_INCLUDE_PATH:
+-    set(JAVAVM_LIB ${JAVA_JVM_LIBRARY}/libjvm.so)
+ 
+-    set(LINUXSRC pmlinuxalsa pmlinux finddefault)
+-    prepend_path(LIBSRC ../pm_linux/ ${LINUXSRC})
++    set(LINUXSRC pmsndio)
++    prepend_path(LIBSRC ../pm_sndio/ ${LINUXSRC})
+     list(APPEND LIBSRC ../porttime/ptlinux)
+ 
+-    set(PM_NEEDED_LIBS pthread asound)
++    set(PM_NEEDED_LIBS pthread sndio)
+   endif(APPLE)
+ else(UNIX)
+   if(WIN32)
+@@ -99,7 +90,6 @@ else(UNIX)
+     set(PM_NEEDED_LIBS winmm.lib)
+   endif(WIN32)
+ endif(UNIX)
+-set(JNI_EXTRA_LIBS ${PM_NEEDED_LIBS} ${JAVA_JVM_LIBRARY})
+ 
+ # this completes the list of library sources by adding shared code
+ list(APPEND LIBSRC pmutil portmidi)
+@@ -109,17 +99,10 @@ add_library(portmidi-static ${LIBSRC})
+ set_target_properties(portmidi-static PROPERTIES OUTPUT_NAME "portmidi_s")
+ target_link_libraries(portmidi-static ${PM_NEEDED_LIBS})
+ 
+-# define the jni library
+-include_directories(${JAVA_INCLUDE_PATHS})
+ 
+-set(JNISRC ${LIBSRC} ../pm_java/pmjni/pmjni.c)
+-add_library(pmjni SHARED ${JNISRC})
+-target_link_libraries(pmjni ${JNI_EXTRA_LIBS})
+-set_target_properties(pmjni PROPERTIES EXECUTABLE_EXTENSION "jnilib")
+-
+ # install the libraries (Linux and Mac OS X command line)
+ if(UNIX)
+-  INSTALL(TARGETS portmidi-static pmjni
++  INSTALL(TARGETS portmidi-static
+     LIBRARY DESTINATION /usr/local/lib
+     ARCHIVE DESTINATION /usr/local/lib)
+ # .h files installed by pm_dylib/CMakeLists.txt, so don't need them here
diff --git a/audio/pd/files/extra-patch-portmidi_porttime_ptlinux.c b/audio/pd/files/extra-patch-portmidi_porttime_ptlinux.c
new file mode 100644
index 000000000000..c3223623f201
--- /dev/null
+++ b/audio/pd/files/extra-patch-portmidi_porttime_ptlinux.c
@@ -0,0 +1,46 @@
+--- portmidi/porttime/ptlinux.c.orig	2020-07-07 04:53:18 UTC
++++ portmidi/porttime/ptlinux.c
+@@ -31,14 +31,13 @@ CHANGE LOG
+ #include "porttime.h"
+ #include "sys/time.h"
+ #include "sys/resource.h"
+-#include "sys/timeb.h"
+ #include "pthread.h"
+ 
+ #define TRUE 1
+ #define FALSE 0
+ 
+ static int time_started_flag = FALSE;
+-static struct timeb time_offset = {0, 0, 0, 0};
++static struct timespec time_offset = {0, 0};
+ static pthread_t pt_thread_pid;
+ static int pt_thread_created = FALSE;
+ 
+@@ -79,7 +78,7 @@ static void *Pt_CallbackProc(void *p)
+ PtError Pt_Start(int resolution, PtCallback *callback, void *userData)
+ {
+     if (time_started_flag) return ptNoError;
+-    ftime(&time_offset); /* need this set before process runs */
++    clock_gettime(CLOCK_MONOTONIC, &time_offset); /* need this set before process runs */
+     if (callback) {
+         int res;
+         pt_callback_parameters *parms = (pt_callback_parameters *) 
+@@ -120,12 +119,12 @@ int Pt_Started()
+ 
+ PtTimestamp Pt_Time()
+ {
+-    long seconds, milliseconds;
+-    struct timeb now;
+-    ftime(&now);
+-    seconds = now.time - time_offset.time;
+-    milliseconds = now.millitm - time_offset.millitm;
+-    return seconds * 1000 + milliseconds;
++    long seconds, nanoseconds;
++    struct timespec now;
++    clock_gettime(CLOCK_MONOTONIC, &now);
++    seconds = now.tv_sec - time_offset.tv_sec;
++    nanoseconds = now.tv_nsec - time_offset.tv_nsec;
++    return seconds * 1000 + nanoseconds / 1000000;
+ }
+ 
+ 
diff --git a/audio/pd/files/extra-patch-src_Makefile.am b/audio/pd/files/extra-patch-src_Makefile.am
new file mode 100644
index 000000000000..f3e3c1a6e5bc
--- /dev/null
+++ b/audio/pd/files/extra-patch-src_Makefile.am
@@ -0,0 +1,11 @@
+--- src/Makefile.am.orig	2016-04-03 04:55:23 UTC
++++ src/Makefile.am
+@@ -81,7 +81,7 @@ endif
+ if OSS
+ if !WINDOWS
+ pd_CFLAGS += -DUSEAPI_OSS
+-pd_SOURCES += s_audio_oss.c s_midi_oss.c
++pd_SOURCES += s_audio_oss.c s_midi_oss_pm.c
+ endif
+ endif
+ 
diff --git a/audio/pd/files/extra-patch-src_s__midi__oss__pm.c b/audio/pd/files/extra-patch-src_s__midi__oss__pm.c
new file mode 100644
index 000000000000..b20485a9364b
--- /dev/null
+++ b/audio/pd/files/extra-patch-src_s__midi__oss__pm.c
@@ -0,0 +1,147 @@
+--- src/s_midi_oss_pm.c.orig	2020-07-07 04:07:26.811553000 -0700
++++ src/s_midi_oss_pm.c	2020-07-07 04:05:55.736126000 -0700
+@@ -0,0 +1,144 @@
++/* Copyright (c) 1997-1999 Guenter Geiger, Miller Puckette, Larry Troxler,
++* Winfried Ritsch, Karl MacMillan, and others.
++* For information on usage and redistribution, and for a DISCLAIMER OF ALL
++* WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */
++
++/* MIDI I/O for Linux using OSS */
++
++#include <stdio.h>
++#ifdef HAVE_UNISTD_H
++#include <unistd.h>
++#endif
++#include <stdlib.h>
++#include <sys/types.h>
++#include <sys/stat.h>
++#include <fcntl.h>
++#include <errno.h>
++#include <string.h>
++#include "m_pd.h"
++#include "s_stuff.h"
++
++#define NSEARCH 10
++static int oss_nmidiindevs, oss_nmidioutdevs;
++static char oss_indevnames[NSEARCH][4], oss_outdevnames[NSEARCH][4];
++static int oss_nmidiin;
++static int oss_midiinfd[MAXMIDIINDEV];
++static int oss_nmidiout;
++static int oss_midioutfd[MAXMIDIOUTDEV];
++
++static void oss_midiout(int fd, int n)
++{
++    char b = n;
++    if ((write(fd, (char *) &b, 1)) != 1)
++        perror("midi write");
++}
++
++#define O_MIDIFLAG O_NDELAY
++
++#define md_msglen(x) (((x)<0xC0)?2:((x)<0xE0)?1:((x)<0xF0)?2:\
++    ((x)==0xF2)?2:((x)<0xF4)?1:0)
++
++
++#if 0   /* this is the "select" version which doesn't work with OSS
++        driver for emu10k1 (it doesn't implement select.) */
++#else
++
++    /* this version uses the asynchronous "read()" ... */
++void sys_poll_midi_oss_pm(void)
++{
++    int i, throttle = 100;
++    struct timeval timout;
++    int did = 1, maxfd = 0;
++    while (did)
++    {
++        fd_set readset, writeset, exceptset;
++        did = 0;
++        if (throttle-- < 0)
++            break;
++        for (i = 0; i < oss_nmidiin; i++)
++        {
++            char c;
++            int ret = read(oss_midiinfd[i], &c, 1);
++            if (ret < 0)
++            {
++                if (errno != EAGAIN)
++                    perror("MIDI");
++            }
++            else if (ret != 0)
++            {
++                sys_midibytein(i, (c & 0xff));
++                did = 1;
++            }
++        }
++    }
++}
++#endif
++
++void midi_oss_init(void)
++{
++    int fd, devno;
++    struct stat statbuf;
++    char namebuf[80];
++         /* we only try to detect devices before trying to open them, because
++         when they're open, they migth not be possible to reopen here */
++    static int initted = 0;
++    if (initted)
++        return;
++    initted = 1;
++    oss_nmidiindevs = oss_nmidioutdevs = 0;
++
++    for (devno = 0; devno < NSEARCH; devno++)
++    {
++        if (devno == 0)
++        {
++                /* try to open the device for reading */
++            fd = open("/dev/midi", O_RDONLY | O_NDELAY);
++            if (fd >= 0)
++            {
++                close(fd);
++                strcpy(oss_indevnames[oss_nmidiindevs++], "");
++            }
++            fd = open("/dev/midi", O_WRONLY | O_NDELAY);
++            if (fd >= 0)
++            {
++                close(fd);
++                strcpy(oss_outdevnames[oss_nmidioutdevs++], "");
++            }
++        }
++        if (oss_nmidiindevs >= NSEARCH || oss_nmidioutdevs >= NSEARCH)
++            break;
++
++        sprintf(namebuf, "/dev/midi%d", devno);
++        fd = open(namebuf, O_RDONLY | O_NDELAY);
++        if (fd >= 0)
++        {
++            close(fd);
++            sprintf(oss_indevnames[oss_nmidiindevs++], "%d", devno);
++        }
++        fd = open(namebuf, O_WRONLY | O_NDELAY);
++        if (fd >= 0)
++        {
++            close(fd);
++            sprintf(oss_outdevnames[oss_nmidioutdevs++], "%d", devno);
++        }
++        if (oss_nmidiindevs >= NSEARCH || oss_nmidioutdevs >= NSEARCH)
++            break;
++
++        sprintf(namebuf, "/dev/midi%2.2d", devno);
++        fd = open(namebuf, O_RDONLY | O_NDELAY);
++        if (fd >= 0)
++        {
++            close(fd);
++            sprintf(oss_indevnames[oss_nmidiindevs++], "%d", devno);
++        }
++        fd = open(namebuf, O_WRONLY | O_NDELAY);
++        if (fd >= 0)
++        {
++            close(fd);
++            sprintf(oss_outdevnames[oss_nmidioutdevs++], "%d", devno);
++        }
++        if (oss_nmidiindevs >= NSEARCH || oss_nmidioutdevs >= NSEARCH)
++            break;
++
++    }
++}
diff --git a/audio/pd/files/portmidi/pm_sndio/pmsndio.c b/audio/pd/files/portmidi/pm_sndio/pmsndio.c
new file mode 100644
index 000000000000..9bfe3d3a16a2
--- /dev/null
+++ b/audio/pd/files/portmidi/pm_sndio/pmsndio.c
@@ -0,0 +1,382 @@
+/* pmsndio.c -- PortMidi os-dependent code */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <sndio.h>
+#include <string.h>
+#include <poll.h>
+#include <errno.h>
+#include <pthread.h>
+#include <glob.h>
+#include "portmidi.h"
+#include "pmutil.h"
+#include "pminternal.h"
+#include "porttime.h"
+
+#define NDEVS 1024
+#define SYSEX_MAXLEN 1024
+
+#define SYSEX_START     0xf0
+#define SYSEX_END       0xf7
+
+PmDeviceID pm_default_input_device_id = -1;
+PmDeviceID pm_default_output_device_id = -1;
+
+extern pm_fns_node pm_sndio_in_dictionary;
+extern pm_fns_node pm_sndio_out_dictionary;
+
+/* length of voice and common messages (status byte included) */
+unsigned int voice_len[] = { 3, 3, 3, 3, 2, 2, 3 };
+unsigned int common_len[] = { 0, 2, 3, 2, 0, 0, 1, 1 };
+
+struct mio_dev {
+    char name[16];
+    struct mio_hdl *hdl;
+    int mode;
+    char errmsg[PM_HOST_ERROR_MSG_LEN];
+    pthread_t thread;
+} devs[NDEVS];
+
+static void set_mode(struct mio_dev *, unsigned int);
+
+void pm_init()
+{
+    int i, j, k = 0;
+    char devices[][16] = {"midithru", "rmidi", "midi", "snd"};
+    glob_t out;
+
+    /* default */
+    strcpy(devs[0].name, MIO_PORTANY);
+    pm_add_device("SNDIO", devs[k].name, TRUE, (void *) &devs[k],
+        &pm_sndio_in_dictionary);
+    pm_add_device("SNDIO", devs[k].name, FALSE, (void *) &devs[k],
+        &pm_sndio_out_dictionary);
+    k++;
+
+    glob("/dev/umidi*.0", GLOB_TILDE, NULL, &out);
+    int umidi_num_major_devs = out.gl_pathc;
+    globfree(&out);
+
+    for (i = 0; i < 4; i++) {
+        for (j = 0; j < umidi_num_major_devs; j++) {
+            sprintf(devs[k].name, "%s/%d", devices[i], j);
+            pm_add_device("SNDIO", devs[k].name, TRUE, (void *) &devs[k],
+              &pm_sndio_in_dictionary);
+            pm_add_device("SNDIO", devs[k].name, FALSE, (void *) &devs[k],
+              &pm_sndio_out_dictionary);
+            k++;
+        }
+    }
+
+    // this is set when we return to Pm_Initialize, but we need it
+    // now in order to (successfully) call Pm_CountDevices()
+    pm_initialized = TRUE;
+    pm_default_input_device_id = 0;
+    pm_default_output_device_id = 1;
+}
+
+void pm_term(void)
+{
+    int i;
+    glob_t out;
+
+    glob("/dev/umidi*.0", GLOB_TILDE, NULL, &out);
+    int umidi_num_major_devs = out.gl_pathc;
+    /* each device has matching midithru, rmidi, midi and snd devices */
+    int ndevs = (umidi_num_major_devs * 4) + 1;
+    globfree(&out);
+
+    for(i = 0; i < ndevs; i++) {
+        if (devs[i].mode != 0) {
+            set_mode(&devs[i], 0);
+            if (devs[i].thread) {
+                pthread_join(devs[i].thread, NULL);
+                devs[i].thread = NULL;
+            }
+        }
+    }
+}
+
+PmDeviceID Pm_GetDefaultInputDeviceID() {
+    Pm_Initialize();
+    return pm_default_input_device_id; 
+}
+
+PmDeviceID Pm_GetDefaultOutputDeviceID() {
+    Pm_Initialize();
+    return pm_default_output_device_id;
+}
+
+void *pm_alloc(size_t s) { return malloc(s); }
+
+void pm_free(void *ptr) { free(ptr); }
+
+/* midi_message_length -- how many bytes in a message? */
+static int midi_message_length(PmMessage message)
+{
+    unsigned char st = message & 0xff;
+    if (st >= 0xf8)
+	return 1;
+    else if (st >= 0xf0)
+        return common_len[st & 7];
+    else if (st >= 0x80)
+        return voice_len[(st >> 4) & 7];
+    else 
+        return 0;
+}
+
+void* input_thread(void *param)
+{
+    PmInternal *midi = (PmInternal*)param;
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+    struct pollfd pfd[1];
+    nfds_t nfds;
+    unsigned char st = 0, c = 0;
+    int rc, revents, idx = 0, len = 0;
+    size_t todo = 0;
+    unsigned char buf[0x200], *p;
+    PmEvent pm_ev, pm_ev_rt;
+    unsigned char sysex_data[SYSEX_MAXLEN];
+
+    while(dev->mode & MIO_IN) {
+        if (todo == 0) {
+            nfds = mio_pollfd(dev->hdl, pfd, POLLIN);
+            rc = poll(pfd, nfds, 100);
+            if (rc < 0) {
+                if (errno == EINTR)
+                    continue;
+                break;
+            }
+            revents = mio_revents(dev->hdl, pfd);
+            if (!(revents & POLLIN))
+                continue;
+
+            todo = mio_read(dev->hdl, buf, sizeof(buf));
+            if (todo == 0)
+                continue;
+            p = buf;
+        }
+        c = *p++;
+        todo--;
+
+        if (c >= 0xf8) {
+            pm_ev_rt.message = c;
+            pm_ev_rt.timestamp = Pt_Time();
+            pm_read_short(midi, &pm_ev_rt);
+        } else if (c == SYSEX_END) {
+            if (st == SYSEX_START) {
+                sysex_data[idx++] = c;
+                pm_read_bytes(midi, sysex_data, idx, Pt_Time());
+            }
+            st = 0;
+            idx = 0;
+        } else if (c == SYSEX_START) {
+            st = c;
+            idx = 0;
+            sysex_data[idx++] = c;
+        } else if (c >= 0xf0) {
+            pm_ev.message = c;
+            len = common_len[c & 7];
+            st = c;
+            idx = 1;
+        } else if (c >= 0x80) {
+            pm_ev.message = c;
+            len = voice_len[(c >> 4) & 7];
+            st = c;
+            idx = 1;
+        } else if (st == SYSEX_START) {
+            if (idx == SYSEX_MAXLEN) {
+                fprintf(stderr, "the message is too long\n");
+                idx = st = 0;
+            } else {
+                sysex_data[idx++] = c;
+            }
+        } else if (st) {
+            if (idx == 0 && st != SYSEX_START)
+                pm_ev.message |= (c << (8 * idx++));
+            pm_ev.message |= (c << (8 * idx++));
+            if (idx == len) {
+                pm_read_short(midi, &pm_ev);
+                if (st >= 0xf0)
+                    st = 0;
+                idx = 0;
+            }
+        }
+    }
+
+    pthread_exit(NULL);
+    return NULL;
+}
+
+static void set_mode(struct mio_dev *dev, unsigned int mode) {
+    if (dev->mode != 0)
+        mio_close(dev->hdl);
+    dev->mode = 0;
+    if (mode != 0)
+        dev->hdl = mio_open(dev->name, mode, 0);
+    if (dev->hdl)
+        dev->mode = mode;
+}
+
+static PmError sndio_out_open(PmInternal *midi, void *driverInfo)
+{
+    descriptor_type desc = &descriptors[midi->device_id];
+    struct mio_dev *dev = (struct mio_dev *) desc->descriptor;
+
+    if (dev->mode & MIO_OUT)
+        return pmNoError;
+
+    set_mode(dev, dev->mode | MIO_OUT);
+    if (!(dev->mode & MIO_OUT)) {
+        snprintf(dev->errmsg, PM_HOST_ERROR_MSG_LEN,
+          "mio_open (output) failed: %s\n", dev->name);
+        return pmHostError;
+    }
+
+    midi->descriptor = (void *)dev;
+    return pmNoError;
+}
+
+static PmError sndio_in_open(PmInternal *midi, void *driverInfo)
+{
+    descriptor_type desc = &descriptors[midi->device_id];
+    struct mio_dev *dev = (struct mio_dev *) desc->descriptor;
+
+    if (dev->mode & MIO_IN)
+        return pmNoError;
+
+    set_mode(dev, dev->mode | MIO_IN);
+    if (!(dev->mode & MIO_IN)) {
+        snprintf(dev->errmsg, PM_HOST_ERROR_MSG_LEN,
+          "mio_open (input) failed: %s\n", dev->name);
+        return pmHostError;
+    }
+    midi->descriptor = (void *)dev;
+    pthread_attr_t attr;
+    pthread_attr_init(&attr);
+    pthread_create(&dev->thread, &attr, input_thread, ( void* )midi);
+    return pmNoError;
+}
+
+static PmError sndio_out_close(PmInternal *midi)
+{
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+
+    if (dev->mode & MIO_OUT)
+        set_mode(dev, dev->mode & ~MIO_OUT);
+    return pmNoError;
+}
+
+static PmError sndio_in_close(PmInternal *midi)
+{
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+
+    if (dev->mode & MIO_IN) {
+        set_mode(dev, dev->mode & ~MIO_IN);
+        pthread_join(dev->thread, NULL);
+        dev->thread = NULL;
+    }
+    return pmNoError;
+}
+
+static PmError sndio_abort(PmInternal *midi)
+{
+    return pmNoError;
+}
+
+static PmTimestamp sndio_synchronize(PmInternal *midi)
+{
+    return 0;
+}
+
+static PmError do_write(struct mio_dev *dev, const void *addr, size_t nbytes)
+{
+    size_t w = mio_write(dev->hdl, addr, nbytes);
+
+    if (w != nbytes) {
+        snprintf(dev->errmsg, PM_HOST_ERROR_MSG_LEN, 
+          "mio_write failed, bytes written:%zu\n", w);
+        return pmHostError;
+    }
+    return pmNoError;
+}
+
+static PmError sndio_write_byte(PmInternal *midi, unsigned char byte,
+                        PmTimestamp timestamp)
+{
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+
+    return do_write(dev, &byte, 1);
+}
+
+static PmError sndio_write_short(PmInternal *midi, PmEvent *event)
+{
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+    int nbytes = midi_message_length(event->message);
+
+    if (midi->latency > 0) {
+        /* XXX the event should be queued for later playback */
+        return do_write(dev, &event->message, nbytes);
+    } else {
+        return do_write(dev, &event->message, nbytes);
+    }
+    return pmNoError;
+}
+
+static PmError sndio_write_flush(PmInternal *midi, PmTimestamp timestamp)
+{
+    return pmNoError;
+}
+
+PmError sndio_sysex(PmInternal *midi, PmTimestamp timestamp)
+{
+    return pmNoError;
+}
+
+static unsigned int sndio_has_host_error(PmInternal *midi)
+{
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+
+    return (dev->errmsg[0] != '\0');
+}
+
+static void sndio_get_host_error(PmInternal *midi, char *msg, unsigned int len)
+{
+    struct mio_dev *dev = (struct mio_dev *) midi->descriptor;
+
+    strlcpy(msg, dev->errmsg, len);
+    dev->errmsg[0] = '\0';
+}
+
+pm_fns_node pm_sndio_in_dictionary = {
+    none_write_short,
+    none_sysex,
+    none_sysex,
+    none_write_byte,
+    none_write_short,
+    none_write_flush,
+    sndio_synchronize,
+    sndio_in_open,
+    sndio_abort,
+    sndio_in_close,
+    success_poll,
+    sndio_has_host_error,
+    sndio_get_host_error
+};
+
+pm_fns_node pm_sndio_out_dictionary = {
+    sndio_write_short,
+    sndio_sysex,
+    sndio_sysex,
+    sndio_write_byte,
+    sndio_write_short,
+    sndio_write_flush,
+    sndio_synchronize,
+    sndio_out_open,
+    sndio_abort,
+    sndio_out_close,
+    none_poll,
+    sndio_has_host_error,
+    sndio_get_host_error
+};
+
diff --git a/audio/pd/files/portmidi/pm_sndio/pmsndio.h b/audio/pd/files/portmidi/pm_sndio/pmsndio.h
new file mode 100644
index 000000000000..4096d9b62857
--- /dev/null
+++ b/audio/pd/files/portmidi/pm_sndio/pmsndio.h
@@ -0,0 +1,5 @@
+/* pmsndio.h */
+
+extern PmDeviceID pm_default_input_device_id;
+extern PmDeviceID pm_default_output_device_id;
+
diff --git a/audio/pd/pkg-descr b/audio/pd/pkg-descr
index 69949e3966dd..0b307420a654 100644
--- a/audio/pd/pkg-descr
+++ b/audio/pd/pkg-descr
@@ -5,4 +5,11 @@ via Mark Dank's GEM package, Pd can be used for simultaneous computer
 animation and computer audio.  Second, an experimental facility is provided
 for defining and accessing data structures.
 
+The MIDI support implemented in this port is derived from Raphael Graf's
+patches for a sndio backend on the OpenBSD audio/portmidi port, applied to an
+embedded PortMIDI library, enhanced to detect a variable number of MIDI (umidi)
+devices:
+http://openbsd-archive.7691.n7.nabble.com/audio-portmidi-input-td363848.html
+https://marc.info/?l=openbsd-ports&m=155221816900336&w=2
+
 Unofficial web site: http://puredata.org/



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202304182155.33ILtwtB062770>