-
Notifications
You must be signed in to change notification settings - Fork 18
/
talentedhack.c
371 lines (334 loc) · 10.4 KB
/
talentedhack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* talentedhack.c
An auto-tuning LV2 plugin.
by Jeremy A. Salwen
VERSION 1.82
Jul 30, 2010
Based on Autotalent, which is
Free software by Thomas A. Baran.
http://web.mit.edu/tbaran/www/autotalent.html
VERSION 0.2
March 20, 2010
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "talentedhack.h"
static LV2_Descriptor *TalentedHackDescriptor = NULL;
#ifdef DEBUGPLOT
int updateScreen() {
printed=0;
while(1) {
if(printed) {
SDL_Flip(screen);
printed=0;
}
SDL_Delay(2);
}
return 0;
}
#endif
static void cleanupTalentedHack(LV2_Handle instance)
{
TalentedHack * ATInstance=(TalentedHack*)instance;
fft_des(ATInstance->fmembvars);
free(ATInstance->buffer.cbi);
free(ATInstance->buffer.cbf);
free(ATInstance->pshifter.cbo);
free(ATInstance->pdetector.cbwindow);
free(ATInstance->pshifter.hannwindow);
free(ATInstance->pdetector.acwinv);
free(ATInstance->pshifter.frag);
CleanupFormantCorrector(&ATInstance->fcorrector);
#ifdef DEBUGPLOT
SDL_FreeSurface(screen);
//Quit SDL
SDL_Quit();
#endif
free(ATInstance);
}
static void connectPortTalentedHack(LV2_Handle instance, uint32_t port, void *data)
{
TalentedHack *plugin = (TalentedHack *)instance;
switch (port) {
case AT_MIDI_OUT:
plugin->quantizer.p_midi_out=data;
break;
case AT_MIDI_IN:
plugin->quantizer.p_midi_in=data;
break;
case AT_AUDIO_IN:
plugin->p_InputBuffer=data;
break;
case AT_AUDIO_OUT:
plugin->p_OutputBuffer=data;
break;
case AT_MIX:
plugin->p_mix=data;
break;
case AT_PULLPITCH_AMOUNT:
plugin->quantizer.p_amount=data;
break;
case AT_PITCH_SMOOTH:
plugin->psmoother.p_pitchsmooth=data;
break;
case AT_MIN_PITCH:
plugin->pdetector.p_pmin=data;
break;
case AT_MAX_PITCH:
plugin->pdetector.p_pmax=data;
break;
case AT_VOICED_THRESH:
plugin->pdetector.p_vthresh=data;
break;
case AT_MPM_K:
plugin->pdetector.p_ppickthresh=data;
break;
case AT_FCORR:
plugin->fcorrector.p_Fcorr=data;
break;
case AT_FWARP:
plugin->fcorrector.p_Fwarp=data;
break;
case AT_CORR_MIDIOUT:
plugin->p_correct_midiout=data;
break;
case AT_LFO_QUANT:
plugin->lfo.p_quant=data;
break;
case AT_LFO_AMP:
plugin->lfo.p_amp=data;
break;
case AT_LFO_RATE:
plugin->lfo.p_rate=data;
break;
case AT_LFO_SHAPE:
plugin->lfo.p_shape=data;
break;
case AT_LFO_SYMM:
plugin->lfo.p_symm=data;
break;
case AT_AREF:
plugin->quantizer.p_aref=data;
break;
case AT_DA:
plugin->quantizer.inotes.A=data;
break;
case AT_DAA:
plugin->quantizer.inotes.Bb=data;
break;
case AT_DB:
plugin->quantizer.inotes.B=data;
break;
case AT_DC:
plugin->quantizer.inotes.C=data;
break;
case AT_DCC:
plugin->quantizer.inotes.Db=data;
break;
case AT_DD:
plugin->quantizer.inotes.D=data;
break;
case AT_DDD:
plugin->quantizer.inotes.Eb=data;
break;
case AT_DE:
plugin->quantizer.inotes.E=data;
break;
case AT_DF:
plugin->quantizer.inotes.F=data;
break;
case AT_DFF:
plugin->quantizer.inotes.Gb=data;
break;
case AT_DG:
plugin->quantizer.inotes.G=data;
break;
case AT_DGG:
plugin->quantizer.inotes.Ab=data;
break;
case AT_OA:
plugin->quantizer.onotes.A=data;
break;
case AT_OAA:
plugin->quantizer.onotes.Bb=data;
break;
case AT_OB:
plugin->quantizer.onotes.B=data;
break;
case AT_OC:
plugin->quantizer.onotes.C=data;
break;
case AT_OCC:
plugin->quantizer.onotes.Db=data;
break;
case AT_OD:
plugin->quantizer.onotes.D=data;
break;
case AT_ODD:
plugin->quantizer.onotes.Eb=data;
break;
case AT_OE:
plugin->quantizer.onotes.E=data;
break;
case AT_OF:
plugin->quantizer.onotes.F=data;
break;
case AT_OFF:
plugin->quantizer.onotes.Gb=data;
break;
case AT_OG:
plugin->quantizer.onotes.G=data;
break;
case AT_OGG:
plugin->quantizer.onotes.Ab=data;
break;
case AT_ACCEPT_MIDI:
plugin->quantizer.p_accept_midi=data;
break;
case AT_LATENCY:
plugin->p_latency=data;
break;
default:
printf("Error, didn't connect port #%i",port);
}
}
static LV2_Handle instantiateTalentedHack(const LV2_Descriptor *descriptor,
double s_rate, const char *path,
const LV2_Feature * const* features)
{
TalentedHack *membvars = (TalentedHack *)malloc(sizeof(TalentedHack));
InstantiateCircularBuffer(&membvars->buffer,s_rate);
unsigned long N=membvars->buffer.cbsize;
membvars->fmembvars = fft_con(N);
membvars->fs = s_rate;
membvars->noverlap = 4;
InstantiatePitchDetector(&membvars->pdetector, membvars->fmembvars, N, s_rate);
InstantiateLFO(&membvars->lfo);
FormantCorrectorInit(&membvars->fcorrector,s_rate,N);
#ifdef DEBUGPLOT
//Start SDL
SDL_Init( SDL_INIT_EVERYTHING );
//Set up screen
screen = SDL_SetVideoMode( 1024, 100, 8, SDL_HWSURFACE);
if ( (screen->flags & SDL_HWSURFACE) != SDL_HWSURFACE ) {
printf("Can't get hardware surface\n");
}
printf("%i bpp\n",screen->format->BitsPerPixel);
SDL_CreateThread(updateScreen, NULL);
#endif
PitchShifterInit(&membvars->pshifter, s_rate,N);
InitializePitchSmoother(&membvars->psmoother, N, membvars->noverlap, s_rate);
QuantizerInit(&membvars->quantizer,features);
return membvars;
}
inline void IncrementPointer(CircularBuffer * buffer) {
// Input write pointer logic
buffer->cbiwr++;
if (buffer->cbiwr >= buffer->cbsize) {
buffer->cbiwr = 0;
}
}
static void runTalentedHack(LV2_Handle instance, uint32_t sample_count)
{
TalentedHack* psTalentedHack = (TalentedHack *)instance;
unsigned long N = psTalentedHack->buffer.cbsize;
unsigned long Nf = psTalentedHack->buffer.corrsize;
float fs = psTalentedHack->fs;
UpdateFormantWarp(&psTalentedHack->fcorrector);
UpdateQuantizer(&psTalentedHack->quantizer);
UpdateLFO(&psTalentedHack->lfo,N,psTalentedHack->noverlap,fs);
const float* pfInput=psTalentedHack->p_InputBuffer;
float* pfOutput=psTalentedHack->p_OutputBuffer;
int fcorr=*(psTalentedHack->fcorrector.p_Fcorr);
/*******************
* MAIN DSP LOOP *
*******************/
unsigned long lSampleIndex;
for (lSampleIndex = 0; lSampleIndex < sample_count; lSampleIndex++) {
// load data into circular buffer
float in = (float) *(pfInput++);
psTalentedHack->buffer.cbi[psTalentedHack->buffer.cbiwr] = in;
if (fcorr>=1) {
RemoveFormants(&psTalentedHack->fcorrector,&psTalentedHack->buffer,in);
}
else {
psTalentedHack->buffer.cbf[psTalentedHack->buffer.cbiwr] = in;
}
IncrementPointer(&psTalentedHack->buffer);
// Every N/noverlap samples, run pitch estimation / manipulation code
if ((psTalentedHack->buffer.cbiwr)%(N/psTalentedHack->noverlap) == 0) {
// ---- Calculate pitch and confidence ----
float pperiod=get_pitch_period(&psTalentedHack->pdetector, obtain_autocovariance(&psTalentedHack->pdetector,psTalentedHack->fmembvars,&psTalentedHack->buffer,N),Nf,fs);
if(pperiod>0) {
MidiPitch note;
note=pperiod_to_midi(&psTalentedHack->quantizer,pperiod);
if(*psTalentedHack->p_correct_midiout) {
PullToInTune(&psTalentedHack->quantizer, ¬e);
}
SendMidiOutput(&psTalentedHack->quantizer,note,lSampleIndex);
//Now we begin to modify the note, to determine what pitch we want to shift to
MidiPitch input=FetchLatestMidiNote(&psTalentedHack->quantizer,lSampleIndex);
note=MixMidiIn(&psTalentedHack->quantizer,note,input);
note.note=SnapToKey(psTalentedHack->quantizer.oNotes, note.note, note.pitchbend>0);
if(!*psTalentedHack->p_correct_midiout) {
PullToInTune(&psTalentedHack->quantizer, ¬e);
}
note.note=addquantizedLFO(&psTalentedHack->lfo,psTalentedHack->quantizer.oNotes,note.note);
float outpitch=midi_to_semitones(note);
outpitch=addunquantizedLFO(&psTalentedHack->lfo,outpitch);
outpitch=SmoothPitch(&psTalentedHack->psmoother,outpitch);
float outpperiod=semitones_to_pperiod(&psTalentedHack->quantizer, outpitch);
// Compute variables for pitch shifter that depend on pitch
ComputePitchShifterVariables(&psTalentedHack->pshifter, pperiod,outpperiod,fs);
} else {
UnVoiceMidi(&psTalentedHack->quantizer,lSampleIndex);
ResetPitchSmoother(&psTalentedHack->psmoother);
}
}
in=ShiftPitch(&psTalentedHack->pshifter,&psTalentedHack->buffer, N);
unsigned int twoahead = (psTalentedHack->buffer.cbiwr + 2)%N;
if (*psTalentedHack->fcorrector.p_Fcorr>=1) {
in=AddFormants(&psTalentedHack->fcorrector,in,twoahead);
} else {
psTalentedHack->fcorrector.fmute = 0;
}
// Write audio to output of plugin
// Mix (blend between original (delayed) =0 and processed =1)
*(pfOutput++)=(*psTalentedHack->p_mix)*in + (1-(*psTalentedHack->p_mix))*psTalentedHack->buffer.cbi[twoahead];
}
FetchLatestMidiNote(&psTalentedHack->quantizer,sample_count-1);
// Tell the host the algorithm latency
*(psTalentedHack->p_latency) = (N-1);
}
static void init()
{
TalentedHackDescriptor =
(LV2_Descriptor *)malloc(sizeof(LV2_Descriptor));
TalentedHackDescriptor->URI = TALENTEDHACK_URI;
TalentedHackDescriptor->activate = NULL;
TalentedHackDescriptor->cleanup = cleanupTalentedHack;
TalentedHackDescriptor->connect_port = connectPortTalentedHack;
TalentedHackDescriptor->deactivate = NULL;
TalentedHackDescriptor->instantiate = instantiateTalentedHack;
TalentedHackDescriptor->run = runTalentedHack;
TalentedHackDescriptor->extension_data = NULL;
}
LV2_SYMBOL_EXPORT
const LV2_Descriptor *lv2_descriptor(uint32_t index)
{
if (!TalentedHackDescriptor) init();
switch (index) {
case 0:
return TalentedHackDescriptor;
default:
return NULL;
}
}