From 7b83933c6f7822783b490eebeabeb6eebd691fa8 Mon Sep 17 00:00:00 2001 From: Nicole Date: Thu, 22 Jan 2026 21:36:33 +0100 Subject: [PATCH] Added support for Kawai CA49 BLE Midi with added virtual midi compatible with any DAW --- etc/nixos/configuration.nix | 14 +++++- etc/nixos/modules/KawaiCA49.nix | 81 +++++++++++++++++++++++++++++++++ etc/nixos/modules/bluetooth.nix | 1 + etc/nixos/modules/programs.nix | 2 + 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 etc/nixos/modules/KawaiCA49.nix diff --git a/etc/nixos/configuration.nix b/etc/nixos/configuration.nix index 049854c..13ad6ed 100644 --- a/etc/nixos/configuration.nix +++ b/etc/nixos/configuration.nix @@ -26,6 +26,7 @@ in ./modules/ld-fix.nix ./modules/programs.nix ./modules/bluetooth.nix + ./modules/KawaiCA49.nix ./cachix.nix ]; @@ -33,7 +34,7 @@ in device = "/dev/disk/by-uuid/fc0150d8-4f57-4d95-abe9-4b979336b3ce"; fsType = "ext4"; }; - + #░█▀▄░█▀█░█▀█░▀█▀░█░░░█▀█░█▀█░█▀▄░█▀▀░█▀▄ #░█▀▄░█░█░█░█░░█░░█░░░█░█░█▀█░█░█░█▀▀░█▀▄ @@ -71,6 +72,12 @@ in loader.timeout = 0; loader.systemd-boot.consoleMode = "auto"; + + # Add a midi bridge for BLE MIDI devices + kernelModules = [ "snd-virmidi" ]; + extraModprobeConfig = '' + options snd-virmidi midi_devs=1 + ''; }; @@ -182,6 +189,11 @@ in jack.enable = true; }; + hardware.kawaiCA49 = { + enable = true; + user = "nicole"; + }; + # ISO mounting utils # programs.cdemu.enable = true; diff --git a/etc/nixos/modules/KawaiCA49.nix b/etc/nixos/modules/KawaiCA49.nix new file mode 100644 index 0000000..a635c55 --- /dev/null +++ b/etc/nixos/modules/KawaiCA49.nix @@ -0,0 +1,81 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.hardware.kawaiCA49; +in +{ + options.hardware.kawaiCA49 = { + enable = mkEnableOption "Kawai CA49 Bluetooth MIDI support"; + + user = mkOption { + type = types.str; + description = "User to run the MIDI connection service as"; + }; + + virtualMidiDevice = mkOption { + type = types.str; + default = "Virtual Raw MIDI"; + description = "Virtual MIDI device name to connect to"; + }; + }; + + config = mkIf cfg.enable { + # Load virtual MIDI kernel module + boot.kernelModules = [ "snd-virmidi" ]; + boot.extraModprobeConfig = '' + options snd-virmidi midi_devs=1 + ''; + + # Create a system service that runs as the user + systemd.services.midi-connect-ca49 = { + description = "Monitor and connect CA49 Piano to Virtual MIDI"; + wantedBy = [ "multi-user.target" ]; + after = [ "sound.target" "bluetooth.target" ]; + serviceConfig = { + Type = "simple"; + User = cfg.user; + Restart = "always"; + RestartSec = "5s"; + ExecStart = pkgs.writeShellScript "monitor-ca49-midi" '' + set -euo pipefail + + connect_ca49() { + # Find CA49 client number (not port number) + CA49_CLIENT=$(${pkgs.alsa-utils}/bin/aconnect -l | grep -oP "client \K[0-9]+(?=:.*CA49)" || true) + + # Find Virtual MIDI client number + VIRMIDI_CLIENT=$(${pkgs.alsa-utils}/bin/aconnect -l | grep -oP "client \K[0-9]+(?=:.*${cfg.virtualMidiDevice})" || true) + + if [ -z "$CA49_CLIENT" ] || [ -z "$VIRMIDI_CLIENT" ]; then + return 0 + fi + + # Check if already connected + if ${pkgs.alsa-utils}/bin/aconnect -l | grep -q "$CA49_CLIENT:0.*Connecting To:.*$VIRMIDI_CLIENT:0"; then + return 0 + fi + + # Try to connect + if ${pkgs.alsa-utils}/bin/aconnect "$CA49_CLIENT:0" "$VIRMIDI_CLIENT:0" 2>/dev/null; then + echo "Connected CA49 (client $CA49_CLIENT) to Virtual MIDI (client $VIRMIDI_CLIENT)" + fi + } + + # Initial connection attempt + sleep 2 + connect_ca49 + + # Monitor for new ALSA sequencer events + ${pkgs.alsa-utils}/bin/aseqdump -p 0:1 | while read -r line; do + if echo "$line" | grep -q "Port subscribed\|Port start"; then + sleep 1 + connect_ca49 + fi + done + ''; + }; + }; + }; +} diff --git a/etc/nixos/modules/bluetooth.nix b/etc/nixos/modules/bluetooth.nix index 6bad02b..f8e1672 100644 --- a/etc/nixos/modules/bluetooth.nix +++ b/etc/nixos/modules/bluetooth.nix @@ -4,6 +4,7 @@ hardware.bluetooth = { enable = true; powerOnBoot = true; + package = pkgs.bluez; settings.General = { experimental = true; # show battery Privacy = "device"; diff --git a/etc/nixos/modules/programs.nix b/etc/nixos/modules/programs.nix index 314590c..20dad53 100644 --- a/etc/nixos/modules/programs.nix +++ b/etc/nixos/modules/programs.nix @@ -104,6 +104,7 @@ gcc uv nodejs_24 + dbgate # GAMING # mangohud @@ -153,6 +154,7 @@ yabridgectl alsa-scarlett-gui qjackctl + alsa-utils # WINE # wineWowPackages.stable