maxl0rd / standingwave3

Flash ActionScript3 dynamic audio library
http://www.noteflight.com
160 stars 25 forks source link

ResamplingFilter plays half speed when given a stereo source #11

Closed pengowray closed 13 years ago

pengowray commented 13 years ago

ResamplingFilter doesn't work correctly for stereo input. Instead it acts as if the value of 'factor' is halved.

Mono resampling works fine.

Background: I'm writing a game in AS3, and would like to randomly change the speed on samples by up to 1.5 to create variability and reduce repetitiveness. I'm using a mix of stereo and mono embedded WAV samples,

Test case: I've made a simple test case. Originally I noticed this problem with samples created with WaveFile.createSample(), but I tested it with SineSource and it has the same problem, so I made a simpler test case (i.e. without embedded WAVs),

Hope this library is still being maintained.

Peter Halasz.

package 
{
    import com.noteflight.standingwave3.elements.AudioDescriptor;
    import com.noteflight.standingwave3.elements.IAudioSource;
    import com.noteflight.standingwave3.filters.ResamplingFilter;
    import com.noteflight.standingwave3.output.AudioPlayer;
    import com.noteflight.standingwave3.performance.AudioPerformer;
    import com.noteflight.standingwave3.performance.ListPerformance;
    import com.noteflight.standingwave3.sources.SineSource;
    import flash.display.Sprite;

    /**
     * Test the resampling capability of the standingwave3 library.
     * 
     * Note: factor of 1.0 is optimized away, so 1.001 is used.
     * A factor of .999 gives the same (incorrect) result.
     * 
     * Library version: maxl0rd-standingwave3-189bdad
     *  
     * @author Peter Halasz
     */
    public class ResampleAudioTestSimpler extends Sprite 
    {

        public function ResampleAudioTestSimpler() 
        {
            var playList:ListPerformance = new ListPerformance();

            //these four should all sound the same :
            playList.addSourceAt(0, monoSineWave());
            playList.addSourceAt(1, stereoSineWave());
            playList.addSourceAt(2, new ResamplingFilter(monoSineWave(), 1.001));   // plays perfectly
            playList.addSourceAt(3, new ResamplingFilter(stereoSineWave(), 1.001)); // plays at half speed (220 Hz)

            //these four should all sound the same too:
            playList.addSourceAt(6, monoSineWave(220));
            playList.addSourceAt(7, stereoSineWave(220));
            playList.addSourceAt(8, new ResamplingFilter(monoSineWave(440), .5));   // plays 220 Hz (correct)
            playList.addSourceAt(9, new ResamplingFilter(stereoSineWave(440), .5)); // plays 110 Hz (incorrect)

            var source:IAudioSource = new AudioPerformer(playList, new AudioDescriptor());
            var player:AudioPlayer = new AudioPlayer();
            player.play(source);

        }

        private function monoSineWave(pitch:Number = 440):SineSource {
            return new SineSource(new AudioDescriptor(44100, AudioDescriptor.CHANNELS_MONO), 0.2, pitch);
        }

        private function stereoSineWave(pitch:Number = 440):SineSource {
            return new SineSource(new AudioDescriptor(44100, AudioDescriptor.CHANNELS_STEREO), 0.2, pitch);
        }

    }

}
maxl0rd commented 13 years ago

Hi Peter

Thanks for the bug report. I've committed a one-liner that should fix this behavior with stereo samples. We don't use those resample methods that much, so this must have fallen through the cracks.

pengowray commented 13 years ago

Thanks for the speedy response!