mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 10:54:51 +00:00
Merge remote-tracking branch 'origin/UI' into UI
# Conflicts: # app/src/main/java/org/toop/app/layer/layers/MultiplayerLayer.java # app/src/main/java/org/toop/app/layer/layers/game/TicTacToeLayer.java
This commit is contained in:
@@ -5,9 +5,11 @@ import org.toop.framework.asset.types.LoadableResource;
|
||||
|
||||
import javax.sound.sampled.*;
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
|
||||
@FileExtension({"wav"})
|
||||
public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
||||
private byte[] rawData;
|
||||
|
||||
public SoundEffectAsset(final File audioFile) {
|
||||
super(audioFile);
|
||||
@@ -15,10 +17,6 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
||||
|
||||
// Gets a new clip to play
|
||||
public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
|
||||
if(!this.isLoaded()){
|
||||
this.load();
|
||||
}
|
||||
|
||||
// Get a new clip from audio system
|
||||
Clip clip = AudioSystem.getClip();
|
||||
|
||||
@@ -32,7 +30,13 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
||||
|
||||
// Generates a new audio stream from byte array
|
||||
private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
|
||||
return AudioSystem.getAudioInputStream(this.file);
|
||||
// Check if raw data is loaded into memory
|
||||
if(!this.isLoaded()){
|
||||
this.load();
|
||||
}
|
||||
|
||||
// Turn rawData into an input stream and turn that into an audio input stream;
|
||||
return AudioSystem.getAudioInputStream(new ByteArrayInputStream(this.rawData));
|
||||
}
|
||||
|
||||
private AudioInputStream downSampleAudio(AudioInputStream audioInputStream, AudioFormat baseFormat) {
|
||||
@@ -52,16 +56,17 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
||||
@Override
|
||||
public void load() {
|
||||
try {
|
||||
this.getAudioStream();
|
||||
this.rawData = Files.readAllBytes(file.toPath());
|
||||
this.isLoaded = true;
|
||||
} catch (UnsupportedAudioFileException | IOException e) {
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unload() {
|
||||
this.isLoaded = false; // TODO?
|
||||
this.rawData = null;
|
||||
this.isLoaded = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -75,6 +75,20 @@ public class SoundManager {
|
||||
for (MediaPlayer mediaPlayer : this.activeMusic) {
|
||||
mediaPlayer.setVolume(this.volume);
|
||||
}
|
||||
for (Clip clip : this.activeSoundEffects.values()){
|
||||
updateClipVolume(clip);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateClipVolume(Clip clip){
|
||||
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)){
|
||||
FloatControl volumeControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
|
||||
float min = volumeControl.getMinimum();
|
||||
float max = volumeControl.getMaximum();
|
||||
float dB = (float) (Math.log10(Math.max(volume, 0.0001)) * 20.0); // convert linear to dB
|
||||
dB = Math.max(min, Math.min(max, dB));
|
||||
volumeControl.setValue(dB);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleGetCurrentVolume(AudioEvents.GetCurrentVolume event) {
|
||||
@@ -144,6 +158,9 @@ public class SoundManager {
|
||||
// Get a new clip from resource
|
||||
Clip clip = asset.getNewClip();
|
||||
|
||||
// Set volume of clip
|
||||
updateClipVolume(clip);
|
||||
|
||||
// If supposed to loop make it loop, else just start it once
|
||||
if (loop) {
|
||||
clip.loop(Clip.LOOP_CONTINUOUSLY);
|
||||
|
||||
Reference in New Issue
Block a user