1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

FL StudioをGeminiで動かしたい!MCPで挫折した私がPython 1ファイルで「AI作曲アシスタント」を構築した話

Last updated at Posted at 2026-01-20

1. はじめに:なぜMCPを諦めたのか

最近バイブコーディングにはまりすぎてAIとAPI連携させる楽しさを覚えて以降狂ったようにAntigravityと手をつないでいるのですが、その中で「あれ、これDAWにも応用できるのでは?」と思い、最新の MCP (Model Context Protocol) を使ってAIとDAWを連携させようとしましたが、Windows環境特有の npm エラーや、サーバー設定の複雑さに直面し、挫折しました。

「もっとシンプルに、PythonだけでGeminiとFL Studioを繋げないか?」
そう考えた結果、MCPサーバーを立てるのではなく、Geminiの「Function Calling」機能を使って、Pythonから直接MIDI信号を叩くという最短ルートを開拓しました。

2. システム構成

「知能」はクラウド(Gemini)、「命令」はローカル(Python)、「発声」はDAW(FL Studio)という役割分担です。

  • Gemini API: 自然言語を解釈し、適切なMIDIノート番号を「関数」として選択
  • Python (mido): Geminiの判断を実行し、MIDI信号として送信
  • loopMIDI: Windows内の仮想MIDIケーブル(PythonとFL Studioの橋渡し)
  • FL Studio: MIDI信号を受け取り、音源を鳴らす

3. 準備:インフラを整える

① 仮想MIDIポートの作成

  1. loopMIDI を起動し、+ ボタンで loopMIDI Port 1 を作成します。
  2. FL StudioOptions > MIDI Settings を開き、Input 欄の loopMIDI Port 1Enable(緑点灯)にします。

② ライブラリのインストール

PowerShellで以下のコマンドを実行し、MIDI操作とGemini接続用のライブラリを導入します。

pip install mido python-rtmidi google-genai

4. 実装:AIアシスタントの作成

フォルダ内に test_gemini.py という名前でファイルを作成し、以下のコードを貼り付けます。Geminiに play_note という「ツール(関数)」を認識させるのがポイントです。
※こちらのコードはAntigravityで生成しました。

import mido
import time
from google import genai
from google.genai import types

# --- 設定 ---
API_KEY = "あなたのAPIキー"  # Google AI Studioで取得したキー
client = genai.Client(api_key=API_KEY)
MODEL_ID = 'gemini-1.5-flash' # 無料枠が安定しやすいモデル

# --- AIが使える「関数(ツール)」の定義 ---
def play_note(note: int, duration: float = 0.5):
    """FL Studioで指定されたMIDIノート番号を鳴らします。"""
    port_name = [n for n in mido.get_output_names() if "loopMIDI" in n]
    if not port_name: return "MIDIポートが見つかりません"
    
    with mido.open_output(port_name[0]) as outport:
        # ノートON
        outport.send(mido.Message('note_on', note=note, velocity=90))
        time.sleep(duration)
        # ノートOFF
        outport.send(mido.Message('note_off', note=note, velocity=0))
    return f"Note {note} played"

# --- メインループ ---
print("--- FL Studio AI Assistant Active ---")
while True:
    user_msg = input("指示(例: 悲しい音を鳴らして / quitで終了): ")
    if user_msg.lower() == 'quit': break

    # Geminiにツールを教えて回答を生成
    response = client.models.generate_content(
        model=MODEL_ID,
        contents=user_msg,
        config=types.GenerateContentConfig(tools=[play_note])
    )

    # AIが関数呼び出し(ツール使用)を選択した場合の処理
    if response.candidates[0].content.parts and \
       response.candidates[0].content.parts[0].function_call:
        fc = response.candidates[0].content.parts[0].function_call
        note_val = int(fc.args['note'])
        print(f"AIが判断したノート: {note_val}")
        play_note(note_val)
    else:
        print("AI:", response.text)

5. 実行:AIと対話して音を出す

PowerShellでスクリプトのあるディレクトリに移動し、実行します。

cd ~\Desktop\fl-mcp-server
python test_gemini.py

実行後、プロンプトに「高いドを鳴らして」や「カノン進行を生成して」と入力すると、FL Studioから音が流れます!

6. おわりに

MCPの導入に苦戦している方は、一度このように「Python直通」の仕組みを試してみてください。 ここに関数を追加していけば、和音を奏でたり、ミキサーを操作したりといった拡張も今後やっていけそうだな・・・と。

ちなみに、インストールするPythonのバージョンや環境にも依拠するのでいろいろGeminiやChatGPTなどと対話しながらトラブルシュートしてみてください。

知見ある方いたらどこかで情報交換させてください!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?