SoundTouchRateTransposer.java (2783B)
1 /* 2 * _______ _____ _____ _____ 3 * |__ __| | __ \ / ____| __ \ 4 * | | __ _ _ __ ___ ___ ___| | | | (___ | |__) | 5 * | |/ _` | '__/ __|/ _ \/ __| | | |\___ \| ___/ 6 * | | (_| | | \__ \ (_) \__ \ |__| |____) | | 7 * |_|\__,_|_| |___/\___/|___/_____/|_____/|_| 8 * 9 * ------------------------------------------------------------- 10 * 11 * TarsosDSP is developed by Joren Six at IPEM, University Ghent 12 * 13 * ------------------------------------------------------------- 14 * 15 * Info: http://0110.be/tag/TarsosDSP 16 * Github: https://github.com/JorenSix/TarsosDSP 17 * Releases: http://0110.be/releases/TarsosDSP/ 18 * 19 * TarsosDSP includes modified source code by various authors, 20 * for credits and info, see README. 21 * 22 */ 23 24 package be.tarsos.dsp.resample; 25 26 import be.tarsos.dsp.AudioDispatcher; 27 import be.tarsos.dsp.AudioEvent; 28 import be.tarsos.dsp.AudioProcessor; 29 30 /** 31 * Currently not working sample rate transposer, works only for integer factors. 32 * Changes sample rate by using linear interpolation. 33 * 34 * Together with the time stretcher this can be used for pitch shifting. 35 * @author Joren Six 36 * @author Olli Parviainen 37 */ 38 public class SoundTouchRateTransposer implements AudioProcessor { 39 40 private double rate; 41 int slopeCount; 42 double prevSample; 43 private AudioDispatcher dispatcher; 44 45 public void setDispatcher(AudioDispatcher newDispatcher){ 46 this.dispatcher = newDispatcher; 47 } 48 49 public SoundTouchRateTransposer(double d){ 50 this.rate = d; 51 } 52 53 @Override 54 public boolean process(AudioEvent audioEvent) { 55 int i, used; 56 float[] src = audioEvent.getFloatBuffer(); 57 float[] dest = new float[(int) Math.round(audioEvent.getBufferSize() / rate)]; 58 used = 0; 59 i = 0; 60 61 // Process the last sample saved from the previous call first... 62 while (slopeCount <= 1.0f) { 63 dest[i] = (float)((1.0f - slopeCount) * prevSample + slopeCount * src[0]); 64 i++; 65 slopeCount += rate; 66 } 67 slopeCount -= 1.0f; 68 end: 69 while(true){ 70 while (slopeCount > 1.0f) { 71 slopeCount -= 1.0f; 72 used++; 73 if (used >= src.length - 1) 74 break end; 75 } 76 if(i < dest.length){ 77 dest[i] = (float)((1.0f - slopeCount) * src[used] + slopeCount * src[used + 1]); 78 } 79 i++; 80 slopeCount += rate; 81 } 82 83 //Store the last sample for the next round 84 prevSample = src[src.length - 1]; 85 dispatcher.setStepSizeAndOverlap(dest.length, 0); 86 audioEvent.setFloatBuffer(dest); 87 return true; 88 } 89 90 @Override 91 public void processingFinished() { 92 93 } 94 95 }