1 /****************************************************************************
2 *
3 * NAME: smbPitchShift.cpp
4 * VERSION: 1.2
5 * HOME URL: http://www.dspdimension.com
6 * KNOWN BUGS: none
7 *
8 * SYNOPSIS: Routine for doing pitch shifting while maintaining
9 * duration using the Short Time Fourier Transform.
10 *
11 * DESCRIPTION: The routine takes a pitchShift factor value which is between 0.5
12 * (one octave down) and 2. (one octave up). A value of exactly 1 does not change
13 * the pitch. numSampsToProcess tells the routine how many samples in indata[0...
14 * numSampsToProcess-1] should be pitch shifted and moved to outdata[0 ...
15 * numSampsToProcess-1]. The two buffers can be identical (ie. it can process the
16 * data in-place). fftFrameSize defines the FFT frame size used for the
17 * processing. Typical values are 1024, 2048 and 4096. It may be any value <=
18 * MAX_FRAME_LENGTH but it MUST be a power of 2. osamp is the STFT
19 * oversampling factor which also determines the overlap between adjacent STFT
20 * frames. It should at least be 4 for moderate scaling ratios. A value of 32 is
21 * recommended for best quality. sampleRate takes the sample rate for the signal
22 * in unit Hz, ie. 44100 for 44.1 kHz audio. The data passed to the routine in
23 * indata[] should be in the range [-1.0, 1.0), which is also the output range
24 * for the data, make sure you scale the data accordingly (for 16bit signed integers
25 * you would have to divide (and multiply) by 32768).
26 *
27 * COPYRIGHT 1999-2006 Stephan M. Bernsee <smb [AT] dspdimension [DOT] com>
28 *
29 * 						The Wide Open License (WOL)
30 *
31 * Permission to use, copy, modify, distribute and sell this software and its
32 * documentation for any purpose is hereby granted without fee, provided that
33 * the above copyright notice and this license appear in all source copies.
34 * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF
35 * ANY KIND. See http://www.dspguru.com/wol.htm for more information.
36 *
37 *****************************************************************************/
38 
39 
40 #ifndef PITCH_H
41 #define PITCH_H
42 
43 #include <string.h>
44 #include <math.h>
45 #include <stdio.h>
46 #include <fftw3.h>
47 
48 #define MAX_FRAME_LENGTH 2048
49 class PitchShifter
50 {
51 public:
52     PitchShifter (long fftFrameSize, long osamp, float sampleRate);
53     ~PitchShifter ();
54     void smbPitchShift (float pitchShift, long numSampsToProcess,
55                         long fftFrameSize, long osamp, float sampleRate,
56                         float *indata, float *outdata);
57     void smbFft (float *fftBuffer, long fftFrameSize, long sign);
58     double smbAtan2 (double x, double y);
59     float ratio;
60 private:
61     void makeWindow(long fftFrameSize);
62     float gInFIFO[MAX_FRAME_LENGTH];
63     float gOutFIFO[MAX_FRAME_LENGTH];
64     float gFFTworksp[2 * MAX_FRAME_LENGTH];
65     float gLastPhase[MAX_FRAME_LENGTH / 2 + 1];
66     float gSumPhase[MAX_FRAME_LENGTH / 2 + 1];
67     float gOutputAccum[2 * MAX_FRAME_LENGTH];
68     float gAnaFreq[MAX_FRAME_LENGTH];
69     float gAnaMagn[MAX_FRAME_LENGTH];
70     float gSynFreq[MAX_FRAME_LENGTH];
71     float gSynMagn[MAX_FRAME_LENGTH];
72     double window[MAX_FRAME_LENGTH];
73     double dfftFrameSize, coef_dfftFrameSize, dpi_coef;
74     double magn, phase, tmp, real, imag;
75     double freqPerBin, expct, coefPB, coef_dpi, coef_mpi;
76     long k, qpd, index, inFifoLatency, stepSize, fftFrameSize2, gRover, FS_osamp;
77 
78     //FFTW variables
79     fftw_complex fftw_in[MAX_FRAME_LENGTH], fftw_out[MAX_FRAME_LENGTH];
80     fftw_plan ftPlanForward, ftPlanInverse;
81 };
82 
83 
84 #endif /*  */
85 
86