Slick Forums
http://slick.ninjacave.com/forum/

Play midi files easily?
http://slick.ninjacave.com/forum/viewtopic.php?f=3&t=5368
Page 1 of 1

Author:  Trek [ Thu Aug 09, 2012 2:35 pm ]
Post subject:  Play midi files easily?

This is just a suggestion but after searching the web I see that a lot of people have trouble figuring out how to play midi and mp3 files easily

Maybe Slick should add a way to easily play midi and mp3 files as easily as a .wav and have them continue to play after the project is exported as a jar?

Author:  davedes [ Thu Aug 09, 2012 5:35 pm ]
Post subject:  Re: Play midi files easily?

There is no MIDI solution for OpenAL that I'm aware of. If you need MIDI (through JavaSound), you can use Paul's Sound System or another MIDI-capable library.

Slick uses MOD files for "tracker" audio, instead of MIDI.

MP3 is not a viable choice because of its commercial license.

I don't know of any decoders in Java that support MP3, let alone something that will be easy to implement in OpenAL. Besides, MP3 is a horrible codec for games -- poor quality, high filesize, gap of silence at the beginning makes looping more difficult, etc.

Best to just stick with WAV, OGG and MOD. Cheers! :)

Author:  Trek [ Fri Aug 10, 2012 4:48 pm ]
Post subject:  Re: Play midi files easily?

So I looked into it and created this code

I hope it could be at least a small improvement on slick and I know some people would appreciate being able to play Midi files easier

Code:
package org.newdawn.slick;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
* @author Trek
*/
public class MIDI {
   Clip buhClip;

   public MIDI() {

   }

   public void play(String filename) {
      URL soundURL = getClass().getClassLoader().getResource(filename);
      Line.Info linfo = new Line.Info(Clip.class);
      try {
         Line line = AudioSystem.getLine(linfo);
         buhClip = (Clip) line;
         AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL);
         buhClip.open(ais);
         buhClip.loop(30);
      } catch (MalformedURLException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      } catch (LineUnavailableException e) {
         e.printStackTrace();
      } catch (UnsupportedAudioFileException e) {
         e.printStackTrace();
      }

      if (buhClip != null) {
         buhClip.setFramePosition(0);
         buhClip.start();
      } else
         System.out.println("Couldn't load sound");
   }

   public void stop() {
      buhClip.close();
   }
}


And a sample

Code:
import org.newdawn.slick.MIDI;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

public class Game extends StateBasedGame {
   public static final String gamename = "Space Adventures";
   public static final int menu = 0;
   public static final int onep = 1;
   MIDI m = new MIDI();

   public static void main(String[] args) {
      AppGameContainer app;
      try {
         app = new AppGameContainer(new Game(gamename));
         app.setDisplayMode(800, 600, true);
         app.setShowFPS(false);
         app.setTargetFrameRate(50);
         app.setVSync(true);
         app.setIcon("res/icon.png");
         app.start();
      } catch (SlickException e) {
         e.printStackTrace();
      }
   }

   public Game(String gamename) {
      super(gamename);
      this.addState(new Menu1(menu));
      this.addState(new OnePlayer(onep));

      m.play("Music.mid");
   }

   public void initStatesList(GameContainer gc) throws SlickException {
      this.getState(menu).init(gc, this);
      this.getState(onep).init(gc, this);
      this.enterState(menu);
   }
}

Author:  davedes [ Tue Aug 14, 2012 1:52 pm ]
Post subject:  Re: Play midi files easily?

Looks easy. :) Unfortunately since Slick uses OpenAL for audio, this wouldn't really fit in well with the rest of the library.

Author:  Stef [ Tue Aug 14, 2012 9:42 pm ]
Post subject:  Re: Play midi files easily?

Moved from Development to here

midi is nice to have, but as davedes said,

Quote:
Looks easy. Unfortunately since Slick uses OpenAL for audio, this wouldn't really fit in well with the rest of the library.

Author:  Marbles [ Sat Feb 10, 2018 1:40 am ]
Post subject:  Re: Play midi files easily?

I think http://www.midipro.org/Editor.php is really good.
Step 1: Like OnlineSequencer.net, firstly, you need to load your MIDI files into the MIDI file player.
Step 2: Set different instruments for tracks.
Step 3: Click “Play” button to play MIDI file.
I copied some words from http://www.videoconverterfactory.com/tips/midi-player.html you can check here, good luck

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/