Skip to content

Instantly share code, notes, and snippets.

@mfansler
Created July 1, 2013 20:01
Show Gist options
  • Save mfansler/5904041 to your computer and use it in GitHub Desktop.
Save mfansler/5904041 to your computer and use it in GitHub Desktop.
This demos how to use an arbitrary .wav file as the instrument in java.sound.midi Synthesizer.
import javax.sound.midi._
import com.sun.media.sound.AudioFileSoundbankReader
object SampleInstrument extends App {
// AudioFileSoundbankReader allows an individual .wav file to be interpreted as an instrument
val sbr = new AudioFileSoundbankReader();
val sb =
sbr.getSoundbank(new java.io.File("./resources/bassoon-g4.wav"))
//sbr.getSoundbank(new java.io.File("./resources/sin256.wav"))
val synth: Synthesizer = MidiSystem.getSynthesizer()
synth.open()
synth.loadInstrument(sb.getInstruments()(0)) //> res1: Boolean = true
val channels: Array[MidiChannel] = synth.getChannels()
channels(0).noteOn(50,60) // idk what these numbers do :/
Thread.sleep(1000) // hold the not for a sec
channels(0).noteOff(50)
synth.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment