mirror of
https://github.com/2OOP/pism.git
synced 2026-02-04 02:44:50 +00:00
Merge pull request #129
* Sound data is now stored in memory as a byte[]
This commit is contained in:
@@ -5,9 +5,11 @@ import org.toop.framework.asset.types.LoadableResource;
|
|||||||
|
|
||||||
import javax.sound.sampled.*;
|
import javax.sound.sampled.*;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
|
||||||
@FileExtension({"wav"})
|
@FileExtension({"wav"})
|
||||||
public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
||||||
|
private byte[] rawData;
|
||||||
|
|
||||||
public SoundEffectAsset(final File audioFile) {
|
public SoundEffectAsset(final File audioFile) {
|
||||||
super(audioFile);
|
super(audioFile);
|
||||||
@@ -15,10 +17,6 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
|||||||
|
|
||||||
// Gets a new clip to play
|
// Gets a new clip to play
|
||||||
public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
|
public Clip getNewClip() throws LineUnavailableException, UnsupportedAudioFileException, IOException {
|
||||||
if(!this.isLoaded()){
|
|
||||||
this.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get a new clip from audio system
|
// Get a new clip from audio system
|
||||||
Clip clip = AudioSystem.getClip();
|
Clip clip = AudioSystem.getClip();
|
||||||
|
|
||||||
@@ -32,7 +30,13 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
|||||||
|
|
||||||
// Generates a new audio stream from byte array
|
// Generates a new audio stream from byte array
|
||||||
private AudioInputStream getAudioStream() throws UnsupportedAudioFileException, IOException {
|
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) {
|
private AudioInputStream downSampleAudio(AudioInputStream audioInputStream, AudioFormat baseFormat) {
|
||||||
@@ -52,16 +56,17 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource {
|
|||||||
@Override
|
@Override
|
||||||
public void load() {
|
public void load() {
|
||||||
try {
|
try {
|
||||||
this.getAudioStream();
|
this.rawData = Files.readAllBytes(file.toPath());
|
||||||
this.isLoaded = true;
|
this.isLoaded = true;
|
||||||
} catch (UnsupportedAudioFileException | IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void unload() {
|
public void unload() {
|
||||||
this.isLoaded = false; // TODO?
|
this.rawData = null;
|
||||||
|
this.isLoaded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user