Skip to content

Instantly share code, notes, and snippets.

@kafecho
Created August 16, 2013 08:57
Show Gist options
  • Save kafecho/6248352 to your computer and use it in GitHub Desktop.
Save kafecho/6248352 to your computer and use it in GitHub Desktop.
Random MIDI tune with Scala and the Java MIDI API.
package org.kafecho.learning.midi
import javax.sound.midi.MidiSystem
import javax.sound.midi.ShortMessage
import scala.util.Random
object MidiTest extends App {
val rcvr = MidiSystem.getReceiver
while (true) {
val myMsg = new ShortMessage
myMsg.setMessage(ShortMessage.NOTE_ON, 8, Random.nextInt.abs % 127, 93)
rcvr.send(myMsg, -1)
Thread.sleep(100)
}
}
@WarriorXXL
Copy link

THANK YOU SO MUCH FOR POSTING THIS CODE! I'VE BEEN TRYING TO FIGURE OUT HOW TO GENERATE SOUND, IN A SIMPLE WAY, USING SCALA (USING OS X) FOR 3 WEEKS. WHEN YOU HAVE TIME, CAN YOU PLEASE CREATE AN EXAMPLE OF HOW TO CHANGE THE TYPE OF SOUND /INSTRUMENT THAT'S PLAYED?

THANK YOU
WARRIOR XXL

@kafecho
Copy link
Author

kafecho commented Mar 12, 2014

I did quite a lot of MIDI work but that was ages ago, however I can't remember from the top of my head. Have a look at: http://docs.oracle.com/javase/tutorial/sound/ for inspiration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment