Slick Forums

Discuss the Slick 2D Library
It is currently Mon Sep 24, 2018 11:20 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Play midi files easily?
PostPosted: Thu Aug 09, 2012 2:35 pm 
Offline

Joined: Tue Jul 31, 2012 3:08 pm
Posts: 16
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?

_________________
"I reject your reality and substitute my own" - Mythbusters


Top
 Profile  
 
PostPosted: Thu Aug 09, 2012 5:35 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1482
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! :)


Top
 Profile  
 
PostPosted: Fri Aug 10, 2012 4:48 pm 
Offline

Joined: Tue Jul 31, 2012 3:08 pm
Posts: 16
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);
   }
}

_________________
"I reject your reality and substitute my own" - Mythbusters


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 1:52 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1482
Looks easy. :) Unfortunately since Slick uses OpenAL for audio, this wouldn't really fit in well with the rest of the library.


Top
 Profile  
 
PostPosted: Tue Aug 14, 2012 9:42 pm 
Offline
Regular

Joined: Mon Dec 08, 2008 2:17 pm
Posts: 160
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.


Top
 Profile  
 
PostPosted: Sat Feb 10, 2018 1:40 am 
Offline

Joined: Fri Feb 09, 2018 7:49 am
Posts: 1
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group