User Tools

Site Tools


fluidcanvas_r2pi:implementing_auto_midi_routing_with_fluidsynth

This is an old revision of the document!


FluidSynth 자동 실행 및 MIDI 오토 라우팅 구현(ALSA-based)

이름을 찾아서 자동 연결

MIDI 기기나 FluidSynth 포트 번호는 매번 연결 때마다 달라진다. 따라서 번호 대신 이름으로 찾아서 자동 연결을 시도하는 것이 바람직하다.

fluidsynth ... &

# fluidsynth 뜰 때까지 대기
until aconnect -l | grep -q "FLUID"; do sleep 0.5; done

# 키보드 연결
aconnect \
$(aconnect -l | grep -A1 "USB" | tail -n1 | awk '{print $1}') \
$(aconnect -l | grep -A1 "FLUID" | tail -n1 | awk '{print $1}')

스트립트 활용

다음을

start_fluid.sh

라는 이름으로 저장해 두고 실행한다.

#!/bin/bash

SF2=~/sf2/GeneralUser-GS.sf2
AUDIO_DEV=plughw:2,0

echo "[INFO] Starting FluidSynth..."

fluidsynth -a alsa -m alsa_seq -i -s \
-o audio.alsa.device=$AUDIO_DEV \
"$SF2" > /dev/null 2>&1 &

# fluidsynth 포트 생성 대기
echo "[INFO] Waiting for FluidSynth..."
until aconnect -l | grep -q "FLUID"; do sleep 0.5; done

echo "[INFO] FluidSynth ready"

# 키보드 연결 함수
connect_midi() {
  SRC=$(aconnect -l | grep -A1 "USB" | tail -n1 | awk '{print $1}')
  DST=$(aconnect -l | grep -A1 "FLUID" | tail -n1 | awk '{print $1}')

  if [ -n "$SRC" ] && [ -n "$DST" ]; then
    aconnect $SRC $DST 2>/dev/null
    echo "[INFO] Connected $SRC → $DST"
  fi
}

# 최초 연결
connect_midi

# 🎯 지속 감시 (핫플러그 대응)
echo "[INFO] Monitoring MIDI devices..."

while true; do
  sleep 2
  connect_midi
done

systemd 자동 실행

/etc/systemd/system/fluid.service

[Unit]
Description=FluidSynth Auto MIDI
After=sound.target

[Service]
ExecStart=/home/pi/start_fluid.sh
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

활성화

sudo systemctl daemon-reexec
sudo systemctl enable fluid
sudo systemctl start fluid
fluidcanvas_r2pi/implementing_auto_midi_routing_with_fluidsynth.1773876239.txt.gz · Last modified: by hyjeong