SAF
Loading...
Searching...
No Matches
pitch_shifter.c
Go to the documentation of this file.
1/*
2 * Copyright 2020 Leo McCormack
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
26#include "pitch_shifter.h"
28
30(
31 void ** const phPS
32)
33{
35 *phPS = (void*)pData;
36
37 /* Default user parameters */
38 pData->new_nChannels = pData->nChannels = 1;
39 pData->pitchShift_factor = 1.0f;
40 pData->osamp_option = PITCH_SHIFTER_OSAMP_4;
41 pData->fftsize_option = PITCH_SHIFTER_FFTSIZE_4096;
42
43 /* internals */
44 pData->hSmb = NULL;
45 pData->progressBar0_1 = 0.0f;
47 strcpy(pData->progressBarText,"");
48 pData->sampleRate = 0.0f;
49 pData->fftFrameSize = 4096; /* Not important to get right, as it will be overwritten upon init */
50 pData->stepsize = 1024; /* same here */
51
52 /* flags */
55
56 /* set FIFO buffers */
57 pData->sampleRate = 48000.0f;
58 pData->FIFO_idx = 0;
59 pData->inFIFO = (float**)calloc2d(MAX_NUM_CHANNELS, PITCH_SHIFTER_FRAME_SIZE, sizeof(float));
60 pData->outFIFO = (float**)calloc2d(MAX_NUM_CHANNELS, PITCH_SHIFTER_FRAME_SIZE, sizeof(float));
61 //memset(FLATTEN2D(pData->inFIFO), 0, MAX_NUM_CHANNELS*PITCH_SHIFTER_FRAME_SIZE*sizeof(float));
62 //memset(FLATTEN2D(pData->outFIFO), 0, MAX_NUM_CHANNELS*PITCH_SHIFTER_FRAME_SIZE*sizeof(float));
63}
64
66(
67 void ** const phPS
68)
69{
70 pitch_shifter_data *pData = (pitch_shifter_data*)(*phPS);
71
72 if (pData != NULL) {
73 /* not safe to free memory during intialisation/processing loop */
74 while (pData->codecStatus == CODEC_STATUS_INITIALISING ||
76 SAF_SLEEP(10);
77 }
78
79 if (pData->hSmb != NULL)
81 free(pData->inFIFO);
82 free(pData->outFIFO);
83 free(pData);
84 pData = NULL;
85 *phPS = NULL;
86 }
87}
88
90(
91 void * const hPS,
92 int sampleRate
93)
94{
96
97 if(pData->sampleRate != (float)sampleRate){
98 pData->sampleRate = (float)sampleRate;
100 }
101}
102
104(
105 void* const hPS
106)
107{
108 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
109 int nChannels, fftSize, osamp;
110
112 return; /* re-init not required, or already happening */
113 while (pData->procStatus == PROC_STATUS_ONGOING){
114 /* re-init required, but we need to wait for the current processing loop to end */
115 pData->codecStatus = CODEC_STATUS_INITIALISING; /* indicate that we want to init */
116 SAF_SLEEP(10);
117 }
118
119 /* for progress bar */
121 strcpy(pData->progressBarText,"Initialising pitch shifter");
122 pData->progressBar0_1 = 0.0f;
123
124 nChannels = pData->new_nChannels;
125
126 /* destroy current handle*/
127 if (pData->hSmb != NULL)
128 smb_pitchShift_destroy(&(pData->hSmb));
129
130 /* Config */
131 switch(pData->osamp_option){
132 default:
133 /* fall through */
134 case PITCH_SHIFTER_OSAMP_2: osamp = 2; break;
135 case PITCH_SHIFTER_OSAMP_4: osamp = 4; break;
136 case PITCH_SHIFTER_OSAMP_8: osamp = 8; break;
137 case PITCH_SHIFTER_OSAMP_16: osamp = 16; break;
138 case PITCH_SHIFTER_OSAMP_32: osamp = 32; break;
139 }
140 switch(pData->fftsize_option){
141 default:
142 /* fall through */
143 case PITCH_SHIFTER_FFTSIZE_512: fftSize = 512; break;
144 case PITCH_SHIFTER_FFTSIZE_1024: fftSize = 1024; break;
145 case PITCH_SHIFTER_FFTSIZE_2048: fftSize = 2048; break;
146 case PITCH_SHIFTER_FFTSIZE_4096: fftSize = 4096; break;
147 case PITCH_SHIFTER_FFTSIZE_8192: fftSize = 8192; break;
148 case PITCH_SHIFTER_FFTSIZE_16384: fftSize = 16384; break;
149 }
150 pData->fftFrameSize = fftSize;
151 pData->stepsize = fftSize/osamp;
152
153 /* Create new handle */
154 smb_pitchShift_create(&(pData->hSmb), nChannels, fftSize, osamp, pData->sampleRate);
155 pData->nChannels = nChannels;
156
157 /* done! */
158 strcpy(pData->progressBarText,"Done!");
159 pData->progressBar0_1 = 1.0f;
161}
162
164(
165 void * const hPS,
166 const float *const * inputs,
167 float* const* const outputs,
168 int nInputs,
169 int nOutputs,
170 int nSamples
171)
172{
173 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
174 int s, ch, nChannels;
175 nChannels = pData->nChannels;
176
177 /* Loop over all samples */
178 for(s=0; s<nSamples; s++){
179 /* Load input signals into inFIFO buffer */
180 for(ch=0; ch<SAF_MIN(nInputs,nChannels); ch++)
181 pData->inFIFO[ch][pData->FIFO_idx] = inputs[ch][s];
182 for(; ch<nChannels; ch++) /* Zero any channels that were not given */
183 pData->inFIFO[ch][pData->FIFO_idx] = 0.0f;
184
185 /* Pull output signals from outFIFO buffer */
186 for(ch=0; ch<SAF_MIN(nOutputs, nChannels); ch++)
187 outputs[ch][s] = pData->outFIFO[ch][pData->FIFO_idx];
188 for(; ch<nOutputs; ch++) /* Zero any extra channels */
189 outputs[ch][s] = 0.0f;
190
191 /* Increment buffer index */
192 pData->FIFO_idx++;
193
194 /* Process frame if inFIFO is full and codec is ready for it */
196 pData->FIFO_idx = 0;
198
199 /* load input */
200 for(ch=0; ch<nChannels; ch++)
201 memcpy(pData->inputFrame[ch], pData->inFIFO[ch], PITCH_SHIFTER_FRAME_SIZE*sizeof(float));
202
203 /* Apply pitch shifting */
204 smb_pitchShift_apply(pData->hSmb, pData->pitchShift_factor, PITCH_SHIFTER_FRAME_SIZE, (float*)pData->inputFrame, (float*)pData->outputFrame);
205
206 /* Copy to output */
207 for(ch=0; ch<nChannels; ch++)
208 memcpy(pData->outFIFO[ch], pData->outputFrame[ch], PITCH_SHIFTER_FRAME_SIZE*sizeof(float));
209 }
210 else if(pData->FIFO_idx >= PITCH_SHIFTER_FRAME_SIZE){
211 /* clear outFIFO if codec was not ready */
212 pData->FIFO_idx = 0;
213 memset(FLATTEN2D(pData->outFIFO), 0, MAX_NUM_CHANNELS*PITCH_SHIFTER_FRAME_SIZE*sizeof(float));
214 }
215 }
216
218}
219
220/* sets */
221
226
227void pitch_shifter_setPitchShiftFactor(void* const hPS, float newValue)
228{
229 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
230 pData->pitchShift_factor = newValue;
231}
232
233void pitch_shifter_setNumChannels(void* const hPS, int newValue)
234{
235 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
236 pData->new_nChannels = newValue;
238}
239
241(
242 void* const hPS,
244)
245{
246 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
247 pData->fftsize_option = newOption;
249}
250
252(
253 void* const hPS,
255)
256{
257 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
258 pData->osamp_option = newOption;
260}
261
262
263/* gets */
264
269
271{
272 pitch_shifter_data *pData = (pitch_shifter_data*)(hBin);
273 return pData->codecStatus;
274}
275
276float pitch_shifter_getProgressBar0_1(void* const hBin)
277{
278 pitch_shifter_data *pData = (pitch_shifter_data*)(hBin);
279 return pData->progressBar0_1;
280}
281
282void pitch_shifter_getProgressBarText(void* const hBin, char* text)
283{
284 pitch_shifter_data *pData = (pitch_shifter_data*)(hBin);
285 memcpy(text, pData->progressBarText, PROGRESSBARTEXT_CHAR_LENGTH*sizeof(char));
286}
287
289{
290 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
291 return pData->pitchShift_factor;
292}
293
299
301{
302 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
303 return pData->osamp_option;
304}
305
307{
308 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
309 return pData->new_nChannels;
310}
311
313{
314 pitch_shifter_data *pData = (pitch_shifter_data*)(hPS);
315 return PITCH_SHIFTER_FRAME_SIZE + pData->fftFrameSize - (pData->stepsize);
316}
317
#define MAX_NUM_CHANNELS
Maximum number of input/output channels supported.
Definition _common.h:230
#define PROGRESSBARTEXT_CHAR_LENGTH
Length of progress bar string.
Definition _common.h:227
@ PROC_STATUS_ONGOING
Codec is processing input audio, and should not be reinitialised at this time.
Definition _common.h:220
@ PROC_STATUS_NOT_ONGOING
Codec is not processing input audio, and may be reinitialised if needed.
Definition _common.h:222
CODEC_STATUS
Current status of the codec.
Definition _common.h:201
@ CODEC_STATUS_NOT_INITIALISED
Codec has not yet been initialised, or the codec configuration has changed.
Definition _common.h:204
@ CODEC_STATUS_INITIALISED
Codec is initialised and ready to process input audio.
Definition _common.h:202
@ CODEC_STATUS_INITIALISING
Codec is currently being initialised, input audio should not be processed.
Definition _common.h:207
void smb_pitchShift_create(void **hSmb, int nCH, int fftFrameSize, int osamp, float sampleRate)
Creates an instance of SMB PitchShifter.
void smb_pitchShift_apply(void *hSmb, float pitchShift, int frameSize, float *indata, float *outdata)
Performs pitch shifting of the input signals, while retaining the same time duration as the original ...
void smb_pitchShift_destroy(void **const hSmb)
Destroys an instance of SMB PitchShifter.
#define SAF_MIN(a, b)
Returns the minimum of the two values.
void * malloc1d(size_t dim1_data_size)
1-D malloc (same as malloc, but with error checking)
Definition md_malloc.c:59
void ** calloc2d(size_t dim1, size_t dim2, size_t data_size)
2-D calloc (contiguously allocated, so use free() as usual to deallocate)
Definition md_malloc.c:102
#define FLATTEN2D(A)
Use this macro when passing a 2-D dynamic multi-dimensional array to memset, memcpy or any other func...
Definition md_malloc.h:65
void pitch_shifter_setOSampOption(void *const hPS, PITCH_SHIFTER_OSAMP_OPTIONS newOption)
Sets the oversampling factor used by the algorithm (see PITCH_SHIFTER_OSAMP_OPTIONS enum)
void pitch_shifter_setPitchShiftFactor(void *const hPS, float newValue)
Sets the pitch shift factor, 1: no change, 2: up one octave, 0.5: down one octave.
CODEC_STATUS pitch_shifter_getCodecStatus(void *const hBin)
Returns current codec status (see CODEC_STATUS enum)
void pitch_shifter_create(void **const phPS)
Creates an instance of pitch_shifter.
int pitch_shifter_getProcessingDelay(void *const hPS)
Returns the processing delay in samples (may be used for delay compensation features)
void pitch_shifter_setFFTSizeOption(void *const hPS, PITCH_SHIFTER_FFTSIZE_OPTIONS newOption)
Sets the FFT size used by the algorithm (see PITCH_SHIFTER_FFTSIZE_OPTIONS enum)
void pitch_shifter_destroy(void **const phPS)
Destroys an instance of pitch_shifter.
void pitch_shifter_initCodec(void *const hPS)
Intialises the codec variables, based on current global/user parameters.
int pitch_shifter_getNCHrequired(void *const hPS)
Returns the number of channels required by the current configuration.
float pitch_shifter_getPitchShiftFactor(void *const hPS)
Returns the pitch shift factor, 1: no change, 2: up one octave, 0.5: down one octave.
PITCH_SHIFTER_OSAMP_OPTIONS pitch_shifter_getOSampOption(void *const hPS)
Returns the oversampling factor used by the algorithm (see PITCH_SHIFTER_OSAMP_OPTIONS enum)
float pitch_shifter_getProgressBar0_1(void *const hBin)
(Optional) Returns current intialisation/processing progress, between 0..1
PITCH_SHIFTER_FFTSIZE_OPTIONS pitch_shifter_getFFTSizeOption(void *const hPS)
Returns the FFT size used by the algorithm (see PITCH_SHIFTER_FFTSIZE_OPTIONS enum)
void pitch_shifter_process(void *const hPS, const float *const *inputs, float *const *const outputs, int nInputs, int nOutputs, int nSamples)
Pitch shifts the input signals.
void pitch_shifter_getProgressBarText(void *const hBin, char *text)
(Optional) Returns current intialisation/processing progress text
int pitch_shifter_getFrameSize(void)
Returns the processing framesize (i.e., number of samples processed with every _process() call )
void pitch_shifter_init(void *const hPS, int sampleRate)
Initialises an instance of pitch_shifter with default settings.
void pitch_shifter_setNumChannels(void *const hPS, int newValue)
Sets the number channels to pitch shift.
void pitch_shifter_refreshParams(void *const hPS)
Sets all intialisation flags to 1; re-initialising all settings/variables as pitch_shifter is current...
A very basic multichannel pitch shifter.
PITCH_SHIFTER_FFTSIZE_OPTIONS
Available FFT size options.
PITCH_SHIFTER_OSAMP_OPTIONS
Available oversampling options.
void pitch_shifter_setCodecStatus(void *const hPS, CODEC_STATUS newStatus)
Sets codec status (see CODEC_STATUS enum)
A very basic multichannel pitch shifter.
#define PITCH_SHIFTER_FRAME_SIZE
Framesize, in time-domain samples.
Main struct for the pitch_shifter.
float inputFrame[MAX_NUM_CHANNELS][PITCH_SHIFTER_FRAME_SIZE]
Current input frame.
float outputFrame[MAX_NUM_CHANNELS][PITCH_SHIFTER_FRAME_SIZE]
Current output frame.
float pitchShift_factor
1: no shift, 0.5: down one octave, 2: up one octave
float ** inFIFO
Input FIFO buffer.
char * progressBarText
Current (re)initialisation step, string.
int new_nChannels
(current value will be replaced by this after next re-init)
float progressBar0_1
Current (re)initialisation progress, between [0..1].
int FIFO_idx
FIFO buffer index.
int stepsize
Hop size in samples.
void * hSmb
pitch-shifter handle
float ** outFIFO
Output FIFO buffer.
PITCH_SHIFTER_OSAMP_OPTIONS osamp_option
see PITCH_SHIFTER_OSAMP_OPTIONS
PROC_STATUS procStatus
see PROC_STATUS
float sampleRate
Host sampling rate, in Hz.
int nChannels
Current number of input/output channels.
PITCH_SHIFTER_FFTSIZE_OPTIONS fftsize_option
see PITCH_SHIFTER_FFTSIZE_OPTIONS
CODEC_STATUS codecStatus
see CODEC_STATUS