17
Nov 12

Sampled piano over WIFI

Recently, after playing a bit with the Overtone project I had this idea to make a small toy application that enables to play the piano on phone and make the actual sound come from proper computer speakers. Here is the incredibly small Clojure code for the server:

(ns remotetone.core
  (:use compojure.core
        [ring.adapter.jetty :only (run-jetty)])
  (:require [compojure.route :as route]
            [compojure.handler :as handler]))

(use 'overtone.core)
(connect-external-server 8090)
(use 'overtone.inst.sampled-piano)

(defroutes rest-routes
  (GET "/play" [n] (do (sampled-piano (Integer/parseInt n)) "ok"))
  (route/resources "/")
  (route/not-found "Page not found"))

(def app
  (-> (handler/site rest-routes)))

(defn run-server [] (run-jetty (var app) {:port 8080 :join? false}))

It uses Compojure and Overtone. One REST method is defined to play different musical notes of the piano. On the Android side I set up a quick and dirty app with buttons to play the full tones of the piano (white keys):

final int[] fullTones = new int[]{/*b2*/47, 48, 50, 52, 53, 55, 57, 59, /*c3*/60};

Numbers correspond to MIDI codes for the notes. Android app calls the REST method on the server (over WIFI) every time a button is pressed. As a result a note is played by Overtone. You can watch the result in the video (please ignore my cracked phone screen :)

After I made this toy app I encountered an information saying it is possible to send MIDI data over WIFI. The main difference is that my solution’s latency is terrible off course :)

Leave a Reply

Your email address will not be published.