Hotfix for loading clip volume issue (#174)

This commit is contained in:
Stef
2025-10-12 00:04:59 +02:00
committed by GitHub
parent 73a2fe3da2
commit 34ccddaea5
2 changed files with 5 additions and 1 deletions

View File

@@ -44,7 +44,7 @@ public class SoundEffectManager implements org.toop.framework.audio.interfaces.S
} }
asset.play(); asset.play();
// TODO: Volume of Sound Effect isn't set when loading. When loading an effect it will be full volume.
logger.debug("Playing sound: {}", asset.getFile().getName()); logger.debug("Playing sound: {}", asset.getFile().getName());
} }

View File

@@ -15,6 +15,8 @@ import static javax.sound.sampled.LineEvent.Type.STOP;
public class SoundEffectAsset extends BaseResource implements LoadableResource, AudioResource { public class SoundEffectAsset extends BaseResource implements LoadableResource, AudioResource {
private final Clip clip = AudioSystem.getClip(); private final Clip clip = AudioSystem.getClip();
private double volume = 100; // TODO: Find a better way to set volume on clip load
public SoundEffectAsset(final File audioFile) throws LineUnavailableException { public SoundEffectAsset(final File audioFile) throws LineUnavailableException {
super(audioFile); super(audioFile);
} }
@@ -54,6 +56,7 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource,
if (baseFormat.getSampleSizeInBits() > 16) if (baseFormat.getSampleSizeInBits() > 16)
inputStream = downSampleAudio(inputStream, baseFormat); inputStream = downSampleAudio(inputStream, baseFormat);
this.clip.open(inputStream); // ^ Clip can only run 16 bit and lower, thus downsampling necessary. this.clip.open(inputStream); // ^ Clip can only run 16 bit and lower, thus downsampling necessary.
this.updateVolume(this.volume);
this.isLoaded = true; this.isLoaded = true;
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) { } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@@ -79,6 +82,7 @@ public class SoundEffectAsset extends BaseResource implements LoadableResource,
@Override @Override
public void updateVolume(double volume) { public void updateVolume(double volume) {
{ {
this.volume = volume;
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) { if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
FloatControl volumeControl = FloatControl volumeControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);