Include basic structure for music control and looping

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2, Sig, Sigo 2 years ago committed by GitHub
parent 0d5ccae00b
commit 79caec1fdf
  1. 2
      src/sig/DrawLoop.java
  2. 4
      src/sig/RabiClone.java
  3. 29
      src/sig/engine/MusicPlayer.java
  4. 22
      src/sig/engine/Song.java
  5. 51
      src/sig/engine/SongThread.java
  6. 39
      src/sig/engine/Sound.java
  7. 1
      src/sig/objects/BunnyGirls.java

@ -11,8 +11,6 @@ import sig.engine.Rectangle;
import sig.engine.Sprite; import sig.engine.Sprite;
import sig.engine.String; import sig.engine.String;
import sig.engine.Transform; import sig.engine.Transform;
import sig.map.Map;
import sig.objects.LevelRenderer;
public class DrawLoop { public class DrawLoop {
public static Panel panel; public static Panel panel;

@ -12,7 +12,6 @@ import java.util.List;
import sig.engine.Panel; import sig.engine.Panel;
import sig.engine.Point; import sig.engine.Point;
import sig.engine.Sound;
import sig.engine.Transform; import sig.engine.Transform;
import sig.engine.objects.AnimatedObject; import sig.engine.objects.AnimatedObject;
import sig.engine.objects.Object; import sig.engine.objects.Object;
@ -26,6 +25,7 @@ import sig.objects.actor.RenderedObject;
import sig.engine.Action; import sig.engine.Action;
import sig.engine.Key; import sig.engine.Key;
import sig.engine.KeyBind; import sig.engine.KeyBind;
import sig.engine.MusicPlayer;
import sig.engine.PaletteColor; import sig.engine.PaletteColor;
import java.awt.Toolkit; import java.awt.Toolkit;
@ -82,6 +82,8 @@ public class RabiClone{
public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF);
public static MusicPlayer MUSICPLAYER;
public static void main(String[] args) { public static void main(String[] args) {
//Sound s = new Sound(); //Sound s = new Sound();
System.setProperty("sun.java2d.transaccel", "True"); System.setProperty("sun.java2d.transaccel", "True");

@ -0,0 +1,29 @@
package sig.engine;
import java.util.List;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineEvent.Type;
public class MusicPlayer implements LineListener{
public static SongThread SongThread;
public static List<Clip> SoundEffects;
public static boolean songChangeRequested=false;
public static void init() {
SongThread = new SongThread();
}
public static void changeMusic(Song newSong) {
SongThread.currentSong=newSong;
MusicPlayer.songChangeRequested=true;
SongThread.run();
}
@Override
public void update(LineEvent event) {
if (event.getType()==Type.CLOSE||event.getType()==Type.STOP) {
SongThread.run(); //Cause a loop so the music continues playing.
}
}
}

@ -0,0 +1,22 @@
package sig.engine;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public enum Song{
WOOLOOKOLOGIE(new File("..","Woolookologie.wav"));
File songFile;
public Song(File f) {
this.songFile=songFile;
}
}

@ -0,0 +1,51 @@
package sig.engine;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import sig.RabiClone;
public class SongThread extends Thread{
Song currentSong;
AudioInputStream audioStream;
AudioFormat format;
DataLine.Info info;
SourceDataLine audioLine;
public void run() {
try {
if (audioStream!=null) {
audioLine.close();
audioStream.close();
audioStream=null;
}
audioStream = AudioSystem.getAudioInputStream(audioStream);
format = audioStream.getFormat();
info = new DataLine.Info(SourceDataLine.class,format);
audioLine = (SourceDataLine)AudioSystem.getLine(info);
audioLine.addLineListener(RabiClone.MUSICPLAYER);
audioLine.open(format);
audioLine.start();
final int BUFFER_SIZE=4096;
byte[] bytesBuffer = new byte[BUFFER_SIZE];
int bytesRead=-1;
while ((bytesRead=audioStream.read(bytesBuffer))!=-1&&!MusicPlayer.songChangeRequested) {
audioLine.write(bytesBuffer,0,bytesRead);
}
if (!MusicPlayer.songChangeRequested) {
audioLine.drain();
} else {
audioLine.stop();
audioLine.flush();
}
} catch (IOException | UnsupportedAudioFileException | LineUnavailableException e) {
e.printStackTrace();
}
}
}

@ -1,39 +0,0 @@
package sig.engine;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Sound{
public Sound() {
try {
File audioFile = new File("..","Woolookologie.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = audioStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
SourceDataLine audioLine = (SourceDataLine)AudioSystem.getLine(info);
audioLine.open(format);
audioLine.start();
final int BUFFER_SIZE=4096;
byte[] bytesBuffer = new byte[BUFFER_SIZE];
int bytesRead=-1;
while ((bytesRead=audioStream.read(bytesBuffer))!=-1) {
audioLine.write(bytesBuffer,0,bytesRead);
}
audioLine.drain();
audioLine.close();
audioStream.close();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
e.printStackTrace();
}
}
}

@ -1,6 +1,5 @@
package sig.objects; package sig.objects;
import sig.engine.Alpha;
import sig.engine.AnimatedSprite; import sig.engine.AnimatedSprite;
import sig.engine.Panel; import sig.engine.Panel;
import sig.engine.Rectangle; import sig.engine.Rectangle;

Loading…
Cancel
Save