@@ -71,9 +71,11 @@ public synchronized void playSound(final String sound, final float pitch, final
7171 @ Override
7272 public synchronized void writeSamples () {
7373 final long [] samples = new long [this .samplesPerTick ];
74+ final int [] outputBuffer = new int [this .samplesPerTick ];
75+ final int [] mutationBuffer = new int [this .samplesPerTick * 2 ];
7476 for (SoundInstance playingSound : this .playingSounds ) {
75- playingSound .render ();
76- playingSound .write (samples );
77+ playingSound .render (mutationBuffer );
78+ playingSound .write (samples , outputBuffer );
7779 }
7880 this .dataLine .write (this .writeNormalized (samples ), 0 , samples .length * 2 );
7981 this .playingSounds .removeIf (SoundInstance ::isFinished );
@@ -130,8 +132,6 @@ protected class SoundInstance {
130132 private final float volume ;
131133 private final float panning ;
132134 private final int sliceLength ;
133- private final int [] mutationBuffer ;
134- private final int [] outputBuffer ;
135135 private final CircularBuffer mutatedSamplesBuffer ;
136136 private int cursor = 0 ;
137137
@@ -140,20 +140,18 @@ public SoundInstance(final int[] samples, final float pitch, final float volume,
140140 this .pitch = pitch ;
141141 this .volume = volume ;
142142 this .panning = panning ;
143- this .sliceLength = (int ) (JavaxSoundSystem .this .samplesPerTick / FORMAT .getChannels () * pitch ) * FORMAT .getChannels () + FORMAT .getChannels ();
144- this .mutationBuffer = new int [JavaxSoundSystem .this .samplesPerTick * 2 ];
145- this .outputBuffer = new int [JavaxSoundSystem .this .samplesPerTick ];
143+ this .sliceLength = (int ) (JavaxSoundSystem .this .samplesPerTick * pitch / FORMAT .getChannels ()) * FORMAT .getChannels () + FORMAT .getChannels ();
146144 this .mutatedSamplesBuffer = new CircularBuffer (JavaxSoundSystem .this .samplesPerTick * 3 );
147145 }
148146
149- public void render () {
147+ public void render (final int [] mutationBuffer ) {
150148 if (!this .hasDataToRender ()) return ;
151149
152150 final int sliceLength = Math .min (this .samples .length - this .cursor , this .sliceLength );
153- final long result = SoundSampleUtil .mutate (FORMAT , this .samples , this .cursor , sliceLength , this .pitch , this .volume , this .panning , this . mutationBuffer );
151+ final long result = SoundSampleUtil .mutate (FORMAT , this .samples , this .cursor , sliceLength , this .pitch , this .volume , this .panning , mutationBuffer );
154152 final int mutationBufferAvailable = (int ) (result >> 32 );
155153 if (this .mutatedSamplesBuffer .hasSpaceFor (mutationBufferAvailable )) {
156- this .mutatedSamplesBuffer .addAll (this . mutationBuffer , mutationBufferAvailable );
154+ this .mutatedSamplesBuffer .addAll (mutationBuffer , mutationBufferAvailable );
157155 if ((int ) result > 0 ) {
158156 this .cursor += (int ) result ;
159157 } else {
@@ -162,13 +160,13 @@ public void render() {
162160 }
163161 }
164162
165- public void write (final long [] buffer ) {
166- if (buffer .length < this . outputBuffer .length ) {
163+ public void write (final long [] samples , final int [] outputBuffer ) {
164+ if (samples .length < outputBuffer .length ) {
167165 throw new IllegalArgumentException ("Buffer is too small" );
168166 }
169- this .mutatedSamplesBuffer .takeAllSafe (this . outputBuffer );
170- for (int i = 0 ; i < buffer .length ; i ++) {
171- buffer [i ] += this . outputBuffer [i ];
167+ this .mutatedSamplesBuffer .takeAllSafe (outputBuffer );
168+ for (int i = 0 ; i < samples .length ; i ++) {
169+ samples [i ] += outputBuffer [i ];
172170 }
173171 }
174172
0 commit comments