From 7d009f8f495c2e6eda254c633da989fc08d8a148 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 2 Dec 2022 15:08:11 -0600 Subject: [PATCH] Include volume controls for sound engine. Co-authored-by: sigonasr2 --- README.md | 5 +++++ src/sig/JavaProjectTemplate.java | 2 ++ src/sig/engine/Sound.java | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/README.md b/README.md index 276f6fa..3068320 100644 --- a/README.md +++ b/README.md @@ -20,5 +20,10 @@ Sprites can be placed in the `sprites` folder and referenced accordingly. Instan Mouse input is handled from the `Mouse` static instance. Can poll for `Held`,`Pressed`, and `Released` status of the mouse. Keyboard input is handled much the same way with the `Key` static instance. Also has `Held`,`Pressed`, and `Released` statuses per key. Keycodes come from the Java Swing `KeyEvent` class (ex.`KeyEvent.VK_UP`). +# Sounds +Sounds can be loaded up and played by creating new instances of the `Sound` class. There is also a loop variable which determines whether or not a sound loops. You can set it in the constructor using the `new Sound(filename,loop)` constructor or set it using the `setCanLoop()` function. You can control the volume of a suond using `setVolume(val)` where val is a number between 0-100 (percent). By default sounds play at 100% their original volume. + +Each sound consumes an active line on your sound device. To clear the line, use the `unload()` function, which is useful when your game no longer needs to use that sound or when trying to load a different sound to replace it (say background music). + # Building I include some compile scripts, but you are free to use your own. `./sig` lists all possible commands, `./sig build` builds and tries to run the compiled Java classes. `./sig jar` will output a jar. `./sig zip` will piece together the sprites folder, bin folder, and a run script (for Linux) to run a finished java jar, which provides an easy way to "export" the game. \ No newline at end of file diff --git a/src/sig/JavaProjectTemplate.java b/src/sig/JavaProjectTemplate.java index 509a1ce..d0976f3 100644 --- a/src/sig/JavaProjectTemplate.java +++ b/src/sig/JavaProjectTemplate.java @@ -41,6 +41,8 @@ public class JavaProjectTemplate { game.SetBorderColor(Color.BRIGHT_BLACK); game.Cursor_SetCursor(new Sprite("cursor.png"),0,0); + laserShootSound.setVolume(60); //60% its original volume. + backgroundMusic.play(); } diff --git a/src/sig/engine/Sound.java b/src/sig/engine/Sound.java index 06f483a..173d189 100644 --- a/src/sig/engine/Sound.java +++ b/src/sig/engine/Sound.java @@ -6,25 +6,43 @@ import java.io.IOException; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; +import javax.sound.sampled.Control; +import javax.sound.sampled.FloatControl; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.UnsupportedAudioFileException; +import javax.sound.sampled.FloatControl.Type; public class Sound { public final static File SOUNDS_FOLDER = new File("..","sounds"); AudioInputStream data; Clip c; + FloatControl control; java.lang.String filename; + int volume=100; boolean loop; public Sound(java.lang.String filename){ this(filename,false); } public Sound(java.lang.String filename,boolean loop) { + this(filename,false,100); + } + public Sound(java.lang.String filename,boolean loop,int volume) { this.loop=loop; this.filename=filename; + this.volume=Math.max(Math.min(volume,100),0); try { data=AudioSystem.getAudioInputStream(new File(SOUNDS_FOLDER.getAbsolutePath(),filename)); c = AudioSystem.getClip(); c.open(data); + if (c.getControls().length>0) { + for (Control cc:c.getControls()) { + if (cc instanceof FloatControl && cc.getType()==Type.MASTER_GAIN) { + control = (FloatControl)cc; + break; + } + } + } + setVolume(volume); System.out.println("Loaded sound for "+filename+"."); } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) { e.printStackTrace(); @@ -38,6 +56,12 @@ public class Sound { public boolean isPlaying(){ return c.isRunning(); } + //value is between 0-100. 0 means muted. 100 means full blast. + public void setVolume(int value) { + if (control!=null) { + control.setValue((float)((control.getMaximum()-control.getMinimum())*Math.pow(value/100f,0.25)+control.getMinimum())); + } + } public void unload(){ try { c.close();