PitchFrameProvider extends FrameProvider { public double[] getNextFrame() { double[] uncutFrame = super.getNextFrame(); if (uncutFrame == null) { cutFrame = null; return null; } int frameLength = super.getFrameLengthSamples(); cutFrame = new double[frameLength]; System.arraycopy(uncutFrame, 0, cutFrame, 0, frameLength); if (twoHalvesWindow != null) { assert periodLengths.length % 2 == 0 : "Using two half windows makes sense only for an even number of periods per frame"; int middle = 0; for (int i=0; i<periodLengths.length/2; i++) { middle += periodLengths[i]; } assert middle < frameLength : "Middle "+middle+" larger than framelength "+frameLength+"!"; twoHalvesWindow.applyInlineLeftHalf(cutFrame, 0, middle); twoHalvesWindow.applyInlineRightHalf(cutFrame, middle, frameLength-middle); } return cutFrame; } PitchFrameProvider(DoubleDataSource signal, DoubleDataSource pitchmarks,
            InlineDataProcessor processor, int samplingRate);  PitchFrameProvider(DoubleDataSource signal, DoubleDataSource pitchmarks,
            InlineDataProcessor processor, int samplingRate, int framePeriods, int shiftPeriods); double[] getNextFrame(); double[] getCurrentFrame(); int getFramePeriods(); int getShiftPeriods(); boolean hasMoreData(); static void main(String[] args);  }
@Test public void testIdentity1() throws Exception { AudioInputStream ais = AudioSystem.getAudioInputStream(PitchFrameProviderTest.class.getResourceAsStream("arctic_a0123.wav")); int samplingRate = (int) ais.getFormat().getSampleRate(); DoubleDataSource signal = new AudioDoubleDataSource(ais); double[] origSignal = signal.getAllData(); signal = new BufferedDoubleDataSource(origSignal); DoubleDataSource pitchmarks = new ESTTextfileDoubleDataSource(new InputStreamReader(PitchFrameProviderTest.class.getResourceAsStream("arctic_a0123.pm"))); double[] origPitchmarks = pitchmarks.getAllData(); double audioDuration = origSignal.length/(double)samplingRate; if (origPitchmarks[origPitchmarks.length-1] < audioDuration) { System.out.println("correcting last pitchmark to total audio duration: "+audioDuration); origPitchmarks[origPitchmarks.length-1] = audioDuration; } pitchmarks = new BufferedDoubleDataSource(origPitchmarks); PitchFrameProvider pfp = new PitchFrameProvider(signal, pitchmarks, null, samplingRate); double[] result = new double[origSignal.length]; double[] frame = null; int resultPos = 0; while ((frame = pfp.getNextFrame()) != null) { int periodLength = pfp.validSamplesInFrame(); System.arraycopy(frame, 0, result, resultPos, periodLength); resultPos += periodLength; } assertTrue("Got back "+resultPos+", expected "+origSignal.length, resultPos==origSignal.length); double err = MathUtils.sumSquaredError(origSignal, result); assertTrue("Error: " + err, err < 1.E-20); }
@Test public void testIdentity2() throws Exception { AudioInputStream ais = AudioSystem.getAudioInputStream(PitchFrameProviderTest.class.getResourceAsStream("arctic_a0123.wav")); int samplingRate = (int) ais.getFormat().getSampleRate(); DoubleDataSource signal = new AudioDoubleDataSource(ais); double[] origSignal = signal.getAllData(); signal = new BufferedDoubleDataSource(origSignal); DoubleDataSource pitchmarks = new ESTTextfileDoubleDataSource(new InputStreamReader(PitchFrameProviderTest.class.getResourceAsStream("arctic_a0123.pm"))); double[] origPitchmarks = pitchmarks.getAllData(); double audioDuration = origSignal.length/(double)samplingRate; if (origPitchmarks[origPitchmarks.length-1] < audioDuration) { System.out.println("correcting last pitchmark to total audio duration: "+audioDuration); origPitchmarks[origPitchmarks.length-1] = audioDuration; } pitchmarks = new BufferedDoubleDataSource(origPitchmarks); PitchFrameProvider pfp = new PitchFrameProvider(new SequenceDoubleDataSource(new DoubleDataSource[] {signal, new BufferedDoubleDataSource(new double[1000])}), pitchmarks, null, samplingRate, 2, 1); double[] result = new double[origSignal.length]; double[] frame = null; int resultPos = 0; while ((frame = pfp.getNextFrame()) != null) { int toCopy = Math.min(pfp.getFrameShiftSamples(), pfp.validSamplesInFrame()); System.arraycopy(frame, 0, result, resultPos, toCopy); resultPos += toCopy; } assertTrue("Got back "+resultPos+", expected "+origSignal.length, resultPos==origSignal.length); double err = MathUtils.sumSquaredError(origSignal, result); assertTrue("Error: " + err, err < 1.E-20); }