2026年10月10日 星期六

Patchbox OS × Roland SC-8850 × Akai MPK mini Plus 37:開機自動連線設定


前言:讓 MIDI 鍵盤開機就能使用

如果沒有 Raspberry Pi,也沒有使用 Patchbox OS,最簡單的組合方式其實不複雜。

只要將 Roland SC-8850 的開關切換到 MIDI DIN-5 輸入模式,再將 Akai MPK mini Plus 37 透過 MIDI DIN-5 連接至 SC-8850,就能讓鍵盤直接控制音源器。

MPK mini Plus 37 不需要透過電腦 USB 供電,只要使用合適的獨立供電方式,並確保兩台設備都已開機,就能直接透過 MIDI 傳送演奏訊息。實際供電方式仍須符合鍵盤的規格。

但是,如果系統中還有 Raspberry Pi 與 Patchbox OS,就可以有另一種玩法。

sudo aconnect 'MPK mini Plus'  'SC-8850'

指令派的作法是這樣,但Patchbox OS 本身支援 MIDI 周邊自動連線,不一定需要自己寫程式。我想要的效果很單純:開機之後,不用再手動連接 MIDI 埠,直接按下琴鍵就能演奏。

以下整理三種做法。


方法一:使用 Patchbox OS 原生 MIDI 規則(推薦)

Patchbox OS 提供 amidiminder,可以監控 ALSA MIDI 裝置,並依照規則自動建立連線。

這是最符合需求的做法,不必額外建立 Python 程式,也不需要另外撰寫 systemd 服務。

步驟 1:開啟規則檔

在 Patchbox OS 終端機輸入:

sudo nano /etc/default/amidiminder.rules

步驟 2:加入 MIDI 連線規則

在檔案中加入:

"MPK mini Plus":0 --> "SC-8850":4

這條規則的意思是:

  • 來源:Akai MPK mini Plus 的第 0 號 MIDI 埠。

  • 目的地:Roland SC-8850 的第 4 號 MIDI 埠。

  • 方向:由 MPK mini Plus 輸出 MIDI 訊息至 SC-8850。

這裡的 0 與 4 是 amidiminder 規則使用的埠索引,不是 USB 接頭編號。

實際使用前,應先透過 aconnect -l 確認裝置名稱與埠索引符合目前系統。

步驟 3:儲存並重新啟動服務

在 Nano 中按下 Ctrl + O,再按 Enter 儲存,最後按 Ctrl + X 離開。

接著執行:

sudo systemctl restart amidiminder

確認服務狀態:

systemctl status amidiminder --no-pager

再檢查 MIDI 連線:

aconnect -l

如果連線成功,MPK mini Plus 的 MIDI 輸出埠就會顯示已連接至 SC-8850 的輸入埠。

完成後,amidiminder 會依照規則處理裝置連線,不必每次開機都手動輸入指令。


方法二:使用 systemd 建立開機自動執行服務

如果沒有使用 amidiminder,或者有特殊的啟動需求,也可以透過 systemd 在開機時執行 MIDI 連線指令。

不過,這種方法需要正確處理裝置尚未就緒、MIDI 埠編號改變等情況,因此不如方法一簡潔。

步驟 1:建立 Service 檔案

sudo nano /etc/systemd/system/midi-connect.service

步驟 2:填入服務設定

[Unit]
Description=Auto connect MIDI ports (MPK mini Plus to SC-8850)
After=sound.target

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStartPre=/bin/sleep 3
ExecStart=/usr/local/bin/midi-connect.sh

[Install]
WantedBy=multi-user.target

這份設定會在啟動時先等待三秒,再執行連線腳本。

這裡使用獨立的 midi-connect.sh,是為了避免將裝置名稱直接當成 aconnect 的埠編號。腳本應先透過 aconnect -l 找到實際的 ALSA Client/Port,再執行連線。

步驟 3:啟用服務

sudo systemctl daemon-reload
sudo systemctl enable midi-connect.service
sudo systemctl start midi-connect.service

檢查服務狀態:

systemctl status midi-connect.service --no-pager

注意:這個方法需要另外建立並測試 /usr/local/bin/midi-connect.sh。若只是要讓 Patchbox OS 自動連接兩台 MIDI 設備,建議優先使用方法一。


方法三:整合到既有的 KMIDI.py

之前分享的 KMIDI.py 網頁控制程式,可以將 MIDI 裝置連線功能整合進原本的硬體偵測流程。

這個方法適合希望透過平板操作網頁,並在偵測到音源器後自動處理 MIDI 連線的使用情境。

以下是原本的偵測流程範例,示範如何尋找 SC-8850 的 MIDI 埠:

def detect_midi_port():
    """偵測 SC-8850 / Roland MIDI 埠並更新 MIDI_PORT"""

    global MIDI_PORT

    try:
        out = subprocess.check_output(
            ["aplaymidi", "-l"],
            text=True,
            stderr=subprocess.DEVNULL
        )
    except Exception as e:
        return False, m("aplay_fail", e=e)

    priority, others = [], []

    for line in out.splitlines():
        line_lower = line.lower()

        if "client" in line_lower and "port" in line_lower:
            continue

        parts = line.split()

        if not parts or not re.match(r"^\d+:\d+$", parts[0]):
            continue

        port = parts[0]

        if "through" in line_lower or "system" in line_lower:
            continue

        if any(k in line_lower for k in [
            "8850", "sc-8850", "sc8850", "roland", "edirol"
        ]):
            priority.append(port)
        else:
            others.append(port)

    candidates = priority + others

    if not candidates:
        return False, m("no_midi")

    port = candidates[0]
    MIDI_PORT = port

    try:
        (Path.home() / "sc8850_port.txt").write_text(
            port + "\n"
        )
    except Exception:
        pass

    return True, m("port_found", port=port)

這段程式只負責偵測音源器的 MIDI 埠,並將結果保存至 sc8850_port.txt。

若要在這個流程中加入 MPK mini Plus 的自動連線,應另外偵測 MPK 的輸出埠,並使用正確的 ALSA Client/Port 編號建立連線,而不是直接假設 aconnect 可以接受裝置名稱。

此外,如果 KMIDI.py 需要在開機後持續執行,還需要確認它本身的服務啟動與裝置重新插拔處理方式。


結語

我的需求其實很簡單:不想每次開機後還要打開終端機,手動輸入 MIDI 連線指令,最後三個辦法都一起用上了,開機還是要等一下,沒有MIDI直連來的快,但透過PatchOS變化比較多元。


沒有留言:

張貼留言

Patchbox OS × Roland SC-8850 × Akai MPK mini Plus 37:開機自動連線設定

前言:讓 MIDI 鍵盤開機就能使用 如果沒有 Raspberry Pi,也沒有使用 Patchbox OS,最簡單的組合方式其實不複雜。 只要將 Roland SC-8850 的開關切換到 MIDI DIN-5 輸入模式,再將 Akai MPK mini Plus 37 透過 ...