Giter Site home page Giter Site logo

scala-midi's Introduction

scala-midi

Automatically exported from code.google.com/p/scala-midi

This project provides a Scala API layer over javax.sound.midi and javax.sound.sampled.

##Play an audio file

import javax.sound.sampled.AudioSystem
import java.io.File
import com.googlecode.scala.sound.sampled._

object Player {

  def main(args: Array[String]) {
    val file = new File("some audio file")
    val stream = AudioSystem.getAudioInputStream(file)
    using(SourceDataLine(stream)) {
      line => {
        line.open(stream)
        line.start()
        var myData = new Array[Byte](1024)
        val numBytesToRead = 1024
        var numBytesRead = stream.read(myData, 0, numBytesToRead)
        while (numBytesRead > 0) {
          line.write(myData, 0, numBytesRead);
          numBytesRead = stream.read(myData, 0, numBytesToRead)
        }
        line.drain()
        line.stop()
      }
    }
  }
}

##Simpler file player using default buffer sizes and audio formats

import java.io.File
import com.googlecode.scala.sound.sampled._
import javax.sound.sampled.{LineEvent, AudioSystem}

object Player {

  def main(args: Array[String]) {
    val file = new File("some audio file")
    val stream = AudioSystem.getAudioInputStream(file)
    using(SourceDataLine(stream)) {
      line => {
        line.addLineListener((e: LineEvent) => {println(e)})
        stream >> line
      }
    }
  }
}

##Play audio from a URL

import com.googlecode.scala.sound.sampled._
import javax.sound.sampled.{LineEvent, AudioSystem}
import java.net.URL

object Player {

  def main(args: Array[String]) {
    val url = new URL("http://www.mediacollege.com/audio/tone/files/1kHz_44100Hz_16bit_05sec.wav")
    val stream = AudioSystem.getAudioInputStream(url)
    using(SourceDataLine(stream)) {
      line => {
        line.addLineListener((e: LineEvent) => {println(e)})
        stream >> line
      }
    }
  }
}

##Writing a MIDI file

There is a good example of writing a MIDI file at www.automatic-pilot.com/midifile.html

Here is that code ported to use this project:

package com.googlecode.scala.sound.midi.examples

import javax.sound.midi.Sequence
import com.googlecode.scala.sound.midi._
import com.googlecode.scala.sound.midi.Notes._
import com.googlecode.scala.sound.midi.MidiEvent
import com.googlecode.scala.sound.midi.message._
import java.io.File

// scala port of http://www.automatic-pilot.com/midifile.html
object MidiFile {

  def main(args: Array[String]) {
    val sequence = new Sequence(Sequence.PPQ, 24)
    val track = sequence.createTrack()

    MidiEvent(GeneralMIDIOn(), 0) -> track
    MidiEvent(SetTempo(457), 0) -> track  //457.76 in example code
    MidiEvent(TrackName("midifile track"), 0) -> track
    MidiEvent(OmniOn(), 0) -> track
    MidiEvent(PolyOn(), 0) -> track
    MidiEvent(ProgramChange(0, 0), 0) -> track
    MidiEvent(NoteOn(0, C4, 96), 1) -> track
    MidiEvent(NoteOff(0, C4, 64), 121) -> track
    MidiEvent(EndOfTrack(), 140) -> track

    sequence >> new File("midifile.mid")
  }
}

##Examples

Comparing code examples from docs.oracle.com/javase/tutorial/sound/MIDI-messages.html

#Sending a message Java API:

ShortMessage myMsg = new ShortMessage();
// Start playing the note Middle C (60), 
// moderately loud (velocity = 93).
myMsg.setMessage(ShortMessage.NOTE_ON, 0, 60, 93);
long timeStamp = -1;
Receiver rcvr = MidiSystem.getReceiver();
rcvr.send(myMsg, timeStamp);

Scala API:

// Start playing the note Middle C (60),
// moderately loud (velocity = 93).
val timeStamp: Long = -1
val rcvr = MidiSystem.getReceiver
rcvr.send(NoteOn(0, C4, 93), timeStamp)
or

rcvr ! (NoteOn(0, C4, 93), timeStamp)

##Connecting devices ###Java API:

Sequencer        seq;
Transmitter      seqTrans;
Synthesizer      synth;
Receiver         synthRcvr;
try {
      seq     = MidiSystem.getSequencer();
      seqTrans = seq.getTransmitter();
      synth   = MidiSystem.getSynthesizer();
      synthRcvr = synth.getReceiver(); 
      seqTrans.setReceiver(synthRcvr);      
} catch (MidiUnavailableException e) {
      // handle or throw exception
}

###Scala API:

val seq: Sequencer
val synth: Synthesizer
try {
      seq   = MidiSystem.getSequencer
      synth = MidiSystem.getSynthesizer
      seq -> synth
} catch {
    case e:MidiUnavailableException =>
      // handle or throw exception
}

##Playing a MIDI file

import com.googlecode.scala.sound.midi._
import java.io.File
import javax.sound.midi.MidiSystem

object SequencePlayer {

  def main(args: Array[String]) {
    val file = new File("some midi file")
    using(MidiSystem.getSequencer) {
      seq => {
        seq.open
        (file -> seq).start
        // hack - there should be a better way to do this.  Without
        // this the code exits before the sequence is played 
        while (seq.isRunning) {
          Thread.sleep(2000)
        }
      }
    }
  }
}

##Resources

scala-midi's People

Stargazers

 avatar Zó avatar Jan Christopher Vogt avatar

Watchers

James Cloos avatar Beth Anderson avatar

Forkers

khitiara yasuba

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.