void do_thresher(t_thresher *x) { int i; t_fftease *fft = x->fft; t_float *channel = fft->channel; t_float damping_factor = x->damping_factor; int max_hold_frames = x->max_hold_frames; int *frames_left = x->frames_left; t_float *composite_frame = x->composite_frame; int N = fft->N; t_float move_threshold = x->move_threshold; fold(fft); rdft(fft,FFT_FORWARD); convert(fft); if( x->first_frame ){ for ( i = 0; i < N+2; i++ ){ composite_frame[i] = channel[i]; frames_left[i] = max_hold_frames; } x->first_frame = 0; } else { for( i = 0; i < N+2; i += 2 ){ if(fabs( composite_frame[i] - channel[i] ) > move_threshold || frames_left[i] <= 0 ){ composite_frame[i] = channel[i]; composite_frame[i+1] = channel[i+1]; frames_left[i] = max_hold_frames; } else { --(frames_left[i]); composite_frame[i] *= damping_factor; } } } // try memcpy here for ( i = 0; i < N+2; i++ ){ channel[i] = composite_frame[i]; } if(fft->obank_flag){ oscbank(fft); } else { unconvert(fft); rdft(fft,FFT_INVERSE); overlapadd(fft); } }
void do_leaker(t_leaker *x) { int i,odd,even; t_float a1,a2,b1,b2; t_fftease *fft = x->fft; t_fftease *fft2 = x->fft2; int N2 = fft->N2; t_float *buffer1 = fft->buffer; t_float *buffer2 = fft2->buffer; t_float *channel1 = fft->channel; int *sieve = x->sieve; t_float fade_value = x->fade_value; fold(fft); fold(fft2); rdft(fft,1); rdft(fft2,1); for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; if( fade_value <= 0 || fade_value < sieve[i] ){ a1 = ( i == N2 ? *(buffer1+1) : *(buffer1+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(buffer1+odd) ); *(channel1+even) = hypot( a1, b1 ) ; *(channel1+odd) = -atan2( b1, a1 ); *(buffer1+even) = *(channel1+even) * cos(*(channel1+odd)); if ( i != N2 ){ *(buffer1+odd) = -(*(channel1+even)) * sin(*(channel1+odd)); } } else { a2 = ( i == N2 ? *(buffer2+1) : *(buffer2+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(buffer2+odd) ); *(channel1+even) = hypot( a2, b2 ) ; *(channel1+odd) = -atan2( b2, a2 ); *(buffer1+even) = *(channel1+even) * cos(*(channel1+odd) ); if ( i != N2 ){ *(buffer1+odd) = -(*(channel1+even)) * sin( *(channel1+odd) ); } } } rdft(fft,-1); overlapadd(fft); }
void PLC_dofe(G711_PLC_state *PLC, short *out) { if (erasecnt == 0) { ippsCopy_16s(history, pitchbuf, HISTORYLEN); /* get history */ pitch = findpitch(PLC); /* find pitch */ poverlap = pitch >> 2; /* OLA 1/4 wavelength */ /* save original last poverlap samples */ ippsCopy_16s(pitchbufend - poverlap, lastq, poverlap); poffset = 0; /* create pitch buffer with 1 period */ pitchblen = pitch; pitchbufstart = pitchbufend - pitchblen; overlapadd(lastq, pitchbufstart - poverlap, pitchbufend - poverlap, poverlap); /* update last 1/4 wavelength in history buffer */ ippsCopy_16s(pitchbufend - poverlap, &history[HISTORYLEN-poverlap], poverlap); getfespeech(PLC, out, FRAMESZ); /* get synthesized speech */ } else if (erasecnt == 1 || erasecnt == 2) {
/* * Generate the synthetic signal. * At the beginning of an erasure determine the pitch, and extract * one pitch period from the tail of the signal. Do an OLA for 1/4 * of the pitch to smooth the signal. Then repeat the extracted signal * for the length of the erasure. If the erasure continues for more than * 10 ms, increase the number of periods in the pitchbuffer. At the end * of an erasure, do an OLA with the start of the first good frame. * The gain decays as the erasure gets longer. */ void LowcFE::dofe(short *out) { pitchbufend = &pitchbuf[HISTORYLEN]; if (erasecnt == 0) { convertsf(history, pitchbuf, HISTORYLEN); /* get history */ pitch = findpitch(); /* find pitch */ poverlap = pitch >> 2; /* OLA 1/4 wavelength */ /* save original last poverlap samples */ copyf(pitchbufend - poverlap, lastq, poverlap); poffset = 0; /* create pitch buffer with 1 period */ pitchblen = pitch; pitchbufstart = pitchbufend - pitchblen; overlapadd(lastq, pitchbufstart - poverlap, pitchbufend - poverlap, poverlap); /* update last 1/4 wavelength in history buffer */ convertfs(pitchbufend - poverlap, &history[HISTORYLEN-poverlap], poverlap); getfespeech(out, FRAMESZ); /* get synthesized speech */ } else if (erasecnt == 1 || erasecnt == 2) {
/* ------------------------------------------------------------------- run -- */ int SPECTACLE_BASE :: run() { if (first_time) { /* create segmented input buffer */ int num_segments = (window_len / RTBUFSAMPS) + 2; int extrasamps = RTBUFSAMPS - framesToRun(); if (extrasamps) num_segments++; int inbuf_samps = num_segments * RTBUFSAMPS * inputChannels(); inbuf = new float [inbuf_samps]; for (int i = 0; i < inbuf_samps; i++) inbuf[i] = 0.0; /* Read ptr chases write ptr by <window_len>. Set read ptr to a position <window_len> frames from right end of input buffer. Set write ptr to beginning of buffer, or, if framesToRun() is not the same as the RTcmix buffer size, set it so that the next run invocation after this one will find the write ptr at the start of the second buffer segment. */ inbuf_readptr = inbuf + (inbuf_samps - (window_len * inputChannels())); inbuf_startptr = inbuf + (extrasamps * inputChannels()); inbuf_writeptr = inbuf_startptr; inbuf_endptr = inbuf + inbuf_samps; int outbuf_samps = num_segments * RTBUFSAMPS * outputChannels(); outbuf = new float [outbuf_samps]; for (int i = 0; i < outbuf_samps; i++) outbuf[i] = 0.0; outbuf_readptr = outbuf + (outbuf_samps - (framesToRun() * outputChannels())); outbuf_writeptr = outbuf_readptr; outbuf_endptr = outbuf + outbuf_samps; first_time = 0; DPRINT3("framesToRun()=%d, extrasamps=%d, num_segments=%d\n", framesToRun(), extrasamps, num_segments); DPRINT3("inbuf_samps=%d, inbuf_readptr=%p, inbuf_writeptr=%p\n", inbuf_samps, inbuf_readptr, inbuf_writeptr); DPRINT3("outbuf_samps=%d, outbuf_readptr=%p, outbuf_writeptr=%p\n", outbuf_samps, outbuf_readptr, outbuf_writeptr); } const int insamps = framesToRun() * inputChannels(); if (currentFrame() < total_insamps) rtgetin(inbuf_writeptr, this, insamps); int iterations = framesToRun() / decimation; if (framesToRun() < RTBUFSAMPS) iterations++; DPRINT1("iterations=%d\n", iterations); for (int i = 0; i < iterations; i++) { if (currentFrame() < input_end_frame) { DPRINT1("taking input...cursamp=%d\n", currentFrame()); shiftin(); fold(currentFrame()); JGrfft(fft_buf, half_fft_len, FORWARD); leanconvert(); } else flush_dry_delay(); modify_analysis(); leanunconvert(); JGrfft(fft_buf, half_fft_len, INVERSE); overlapadd(currentFrame()); shiftout(); increment(decimation); } if (currentFrame() < input_end_frame) { inbuf_writeptr += insamps; if (inbuf_writeptr >= inbuf_endptr) inbuf_writeptr = inbuf; } rtbaddout(outbuf_readptr, framesToRun()); outbuf_readptr += framesToRun() * outputChannels(); if (outbuf_readptr >= outbuf_endptr) outbuf_readptr = outbuf; return framesToRun(); }
t_int *thresher_perform(t_int *w) { float sample, outsamp ; int i,j; t_thresher *x = (t_thresher *) (w[1]); float *in = (t_float *)(w[2]); float *inthresh = (t_float *)(w[3]); float *damping = (t_float *)(w[4]); float *out = (t_float *)(w[5]); int n = (int)(w[6]); float *input = x->input; float *output = x->output; float *buffer = x->buffer; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *channel = x->channel; float damping_factor = x->damping_factor; int max_hold_frames = x->max_hold_frames; int *frames_left = x->frames_left; float *composite_frame = x->composite_frame; float *c_lastphase_in = x->c_lastphase_in; float *c_lastphase_out = x->c_lastphase_out; float c_fundamental = x->c_fundamental; float c_factor_in = x->c_factor_in; float c_factor_out = x->c_factor_out; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland; float mult = x->mult; int in_count = x->in_count; int R = x->R; int N = x->N; int N2 = x->N2; int D = x->D; int Nw = x->Nw; float move_threshold = x->move_threshold; if( x->mute ) { for( j = 0; j < D; j++) { *out++ = 0.0 ; } return (w+7); } if ( x->bypass ) { for( j = 0; j < D; j++) { *out++ = *in++ ; } return (w+7); } if( x->thresh_connected ) { move_threshold = *inthresh ; } if( x->damping_connected ) { damping_factor = *damping ; } in_count += D; for ( j = 0 ; j < Nw - D ; j++ ) input[j] = input[j+D]; for ( j = Nw - D; j < Nw; j++ ) { input[j] = *in++; } fold( input, Wanal, Nw, buffer, N, in_count ); rdft( N, 1, buffer, bitshuffle, trigland ); convert( buffer, channel, N2, c_lastphase_in, c_fundamental, c_factor_in ); if( x->first_frame ){ for ( i = 0; i < N+2; i++ ){ composite_frame[i] = channel[i]; frames_left[i] = max_hold_frames; } x->first_frame = 0; } else { for( i = 0; i < N+2; i += 2 ){ if(fabs( composite_frame[i] - channel[i] ) > move_threshold || frames_left[i] <= 0 ){ composite_frame[i] = channel[i]; composite_frame[i+1] = channel[i+1]; frames_left[i] = max_hold_frames; } else { --(frames_left[i]); composite_frame[i] *= damping_factor; } } } unconvert( composite_frame, buffer, N2, c_lastphase_out, c_fundamental, c_factor_out ); rdft( N, -1, buffer, bitshuffle, trigland ); overlapadd( buffer, N, Wsyn, output, Nw, in_count ); for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; x->in_count = in_count; x->damping_factor = damping_factor; return (w+7); }
/*********************************************************************** Vocoder: tratto da Ceres di ([email protected]) ***********************************************************************/ ULONG WavVocoder (PCSZ name, LONG argc, const RXSTRING * argv, PCSZ queuename, PRXSTRING retstr) { PSHORT pCh, pCh2, ptemp; APIRET rc; int i, frame, FDec, nCamp; int campioni, puntifft, Kn2, Koverlap; double soglia; if (argc != 4) { SendMsg (FUNC_VOCODER, ERR_NUMERO_PARAMETRI); return INVALID_ROUTINE; } if (!sscanf (argv[0].strptr, "%d", &pCh)) { SendMsg (FUNC_VOCODER, ERR_PUNTATORE_ERRATO); return INVALID_ROUTINE; } if (!sscanf (argv[1].strptr, "%d", &pCh2)) { SendMsg (FUNC_VOCODER, ERR_PUNTATORE_ERRATO); return INVALID_ROUTINE; } nCamp = atol (argv[2].strptr); if (nCamp < 1) { SendMsg (FUNC_VOCODER, ERR_VALORE_INVALIDO); return INVALID_ROUTINE; } pCh = (PSHORT) AllineaCh (pCh, (ULONG) nCamp, FUNC_VOCODER); if (!pCh) return INVALID_ROUTINE; pCh2 = (PSHORT) AllineaCh (pCh2, (ULONG) nCamp, FUNC_VOCODER); if (!pCh2) return INVALID_ROUTINE; soglia = atof (argv[3].strptr) / 1000000; if ((soglia < 0) | (soglia > 1)) { SendMsg (FUNC_VOCODER, ERR_VALORE_INVALIDO); return INVALID_ROUTINE; } fvec (inout, K_PUNTI); fvec (buffer, K_PUNTI); fvec (dativoc, K_PUNTI); fvec (WAnalisi, K_PUNTI); fvec (WSintesi, K_PUNTI); campioni = K_PUNTI; puntifft = K_PUNTI / 2; Kn2 = K_PUNTI / 2; Koverlap = 2; FDec = campioni / Koverlap; makewindows (WAnalisi, WSintesi, campioni, campioni, FDec); for (frame = 0; frame < (10 * nCamp / K_PUNTI); frame++) { for (i = 0; i < campioni; i++) inout[i] = (double) *pCh++ / (double) MAX_CAMPIONE; fold (inout, WAnalisi, campioni, buffer, campioni, frame * campioni); for (i = 0; i < campioni; i++) inout[i] = (double) 0; /* */ rfft (buffer, puntifft, FORWARD); convert (buffer, dativoc, Kn2, FDec, FreqCamp, 1); /* if(frame==100) for (i=0; i<Kn2; i++) { printf("freq %f, amp %f\n", dativoc[i+i+1], dativoc[i+i] * 1000); } */ for (i = 0; i < Kn2; i++) { if (dativoc[i + i] < soglia) dativoc[i + i] = 0; } unconvert (dativoc, buffer, Kn2, FDec, FreqCamp, 1); rfft (buffer, puntifft, INVERSE); overlapadd (buffer, campioni, WSintesi, inout, campioni, frame * campioni); for (i = 0; i < campioni; i++) *pCh2++ = *pCh2 + (SHORT) (inout[i] * MAX_CAMPIONE); pCh = pCh - (SHORT) (K_PUNTI * 0.9); pCh2 = pCh2 - (SHORT) (K_PUNTI * 0.9); } free (inout); free (buffer); free (dativoc); free (WAnalisi); free (WSintesi); sprintf (retstr->strptr, "%f", ERR_OK); retstr->strlength = strlen (retstr->strptr); return VALID_ROUTINE; }
t_int *ether_perform(t_int *w) { int i,j, inCount, R, N, N2, D, Nw, invert = 1, even, odd, *bitshuffle; float maxamp, threshMult = 1., mult, a1, b1, a2, b2, *inputOne, *inputTwo, *bufferOne, *bufferTwo, *output, *Wanal, *Wsyn, *channelOne, *channelTwo, *trigland; /* get our inlets and outlets */ t_ether *x = (t_ether *) (w[1]); t_float *inOne = (t_float *)(w[2]); t_float *inTwo = (t_float *)(w[3]); t_float *vec_threshMult = (t_float *)(w[4]); t_float *out = (t_float *)(w[5]); t_int n = w[6]; short *connected = x->connected; /* dereference structure */ inputOne = x->inputOne; inputTwo = x->inputTwo; bufferOne = x->bufferOne; bufferTwo = x->bufferTwo; inCount = x->inCount; R = x->R; N = x->N; N2 = x->N2; D = x->D; Nw = x->Nw; Wanal = x->Wanal; Wsyn = x->Wsyn; output = x->output; channelOne = x->channelOne; channelTwo = x->channelTwo; bitshuffle = x->bitshuffle; trigland = x->trigland; mult = x->mult; invert = x->invert; if(connected[2]){ threshMult = *vec_threshMult; } else if ( x->threshMult != 0. ){ threshMult = x->threshMult; } else { threshMult = 1.0; } if(x->mute){ while(n--) *out++ = 0.0; return w+7; } /* fill our retaining buffers */ inCount += D; for ( j = 0 ; j < Nw - D ; j++ ) { inputOne[j] = inputOne[j+D]; inputTwo[j] = inputTwo[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { inputOne[j] = *inOne++; inputTwo[j] = *inTwo++; } /* apply hamming window and fold our window buffer into the fft buffer */ fold( inputOne, Wanal, Nw, bufferOne, N, inCount ); fold( inputTwo, Wanal, Nw, bufferTwo, N, inCount ); /* do an fft */ rdft( N, 1, bufferOne, bitshuffle, trigland ); rdft( N, 1, bufferTwo, bitshuffle, trigland ); /* use slow fft */ /* use redundant coding for speed, even though moving the invert variable comparison outside of the for loop will give us only a minimal performance increase (hypot and atan2 are the most intensive portions of this code). consider adding a table lookup for atan2 instead. */ if (invert) { /* convert to polar coordinates from complex values */ for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) ); a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) ); *(channelOne+even) = hypot( a1, b1 ); *(channelOne+odd) = -atan2( b1, a1 ); *(channelTwo+even) = hypot( a2, b2 ); *(channelTwo+odd) = -atan2( b2, a2 ); /* use simple threshold for inverse compositing */ if ( *(channelOne+even) > *(channelTwo+even) * threshMult ) *(channelOne+even) = *(channelTwo+even); if ( *(channelOne+odd) == 0. ) *(channelOne+odd) = *(channelTwo+odd); } } else { /* convert to polar coordinates from complex values */ for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) ); a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) ); *(channelOne+even) = hypot( a1, b1 ); *(channelOne+odd) = -atan2( b1, a1 ); *(channelTwo+even) = hypot( a2, b2 ); *(channelTwo+odd) = -atan2( b2, a2 ); /* use simple threshold for compositing */ if ( *(channelOne+even) < *(channelTwo+even) * threshMult ) *(channelOne+even) = *(channelTwo+even); if ( *(channelOne+odd) == 0. ) *(channelOne+odd) = *(channelTwo+odd); } } /* convert back to complex form, read for the inverse fft */ for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; *(bufferOne+even) = *(channelOne+even) * cos( *(channelOne+odd) ); if ( i != N2 ) *(bufferOne+odd) = -(*(channelOne+even)) * sin( *(channelOne+odd) ); } /* do an inverse fft */ rdft( N, -1, bufferOne, bitshuffle, trigland ); /* dewindow our result */ overlapadd( bufferOne, N, Wsyn, output, Nw, inCount); /* set our output and adjust our retaining output buffer */ for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->inCount = inCount % Nw; return (w+7); }
void do_ether(t_ether *x) { t_fftease *fft = x->fft; t_fftease *fft2 = x->fft2; int i; int N2 = fft->N2; float a1, b1, a2, b2; int even, odd; int invert = x->invert; t_float threshMult = x->threshMult; t_float *bufferOne = fft->buffer; t_float *bufferTwo = fft2->buffer; t_float *channelOne = fft->channel; t_float *channelTwo = fft2->channel; fold(fft); fold(fft2); rdft(fft,1); rdft(fft2,1); if (invert) { for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) ); a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) ); *(channelOne+even) = hypot( a1, b1 ); *(channelOne+odd) = -atan2( b1, a1 ); *(channelTwo+even) = hypot( a2, b2 ); *(channelTwo+odd) = -atan2( b2, a2 ); if ( *(channelOne+even) > *(channelTwo+even) * threshMult ) *(channelOne+even) = *(channelTwo+even); if ( *(channelOne+odd) == 0. ) *(channelOne+odd) = *(channelTwo+odd); } } else { for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) ); a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) ); *(channelOne+even) = hypot( a1, b1 ); *(channelOne+odd) = -atan2( b1, a1 ); *(channelTwo+even) = hypot( a2, b2 ); *(channelTwo+odd) = -atan2( b2, a2 ); if ( *(channelOne+even) < *(channelTwo+even) * threshMult ) *(channelOne+even) = *(channelTwo+even); if ( *(channelOne+odd) == 0. ) *(channelOne+odd) = *(channelTwo+odd); } } for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; *(bufferOne+even) = *(channelOne+even) * cos( *(channelOne+odd) ); if ( i != N2 ) *(bufferOne+odd) = -(*(channelOne+even)) * sin( *(channelOne+odd) ); } rdft(fft, -1); overlapadd(fft); }
t_int *shapee_perform(t_int *w) { int n, i,j, inCount, R, N, N2, D, Nw, invert = 1, shapeWidth, remainingWidth, even, odd, *bitshuffle; float maxamp, threshMult = 1., mult, a1, b1, a2, b2, *inputOne, *inputTwo, *bufferOne, *bufferTwo, *output, *Wanal, *Wsyn, *channelOne, *channelTwo, *trigland; t_float *inOne, *inTwo, *inShape, *out; /* get our inlets and outlets */ t_shapee *x = (t_shapee *) (w[1]); inOne = (t_float *) (w[2]); inTwo = (t_float *) (w[3]); inShape = (t_float *) (w[4]); out = (t_float *)(w[5]); n = (t_int) (w[6]); /* get our shapeWidth -- from either a signal our float input */ #if MSP shapeWidth = x->widthConnected ? (int) *inShape : (int) x->shapeWidth; #endif #if PD shapeWidth = (int) *inShape; #endif /* dereference structure */ if(x->mute){ while(n--) *out++ = 0.0; return w+7; } inputOne = x->inputOne; inputTwo = x->inputTwo; bufferOne = x->bufferOne; bufferTwo = x->bufferTwo; inCount = x->inCount; R = x->R; N = x->N; N2 = x->N2; D = x->D; Nw = x->Nw; Wanal = x->Wanal; Wsyn = x->Wsyn; output = x->output; channelOne = x->channelOne; channelTwo = x->channelTwo; bitshuffle = x->bitshuffle; trigland = x->trigland; mult = x->mult; if(shapeWidth < 1 || shapeWidth > N2) shapeWidth = 1; inCount += D; for ( j = 0 ; j < Nw - D ; j++ ) { inputOne[j] = inputOne[j+D]; inputTwo[j] = inputTwo[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { inputOne[j] = *inOne++; inputTwo[j] = *inTwo++; } /* apply hamming window and fold our window buffer into the fft buffer */ fold( inputOne, Wanal, Nw, bufferOne, N, inCount ); fold( inputTwo, Wanal, Nw, bufferTwo, N, inCount ); /* do an fft */ rdft( N, 1, bufferOne, bitshuffle, trigland ); rdft( N, 1, bufferTwo, bitshuffle, trigland ); /* convert to polar coordinates from complex values */ for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(bufferOne+1) : *(bufferOne+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(bufferOne+odd) ); a2 = ( i == N2 ? *(bufferTwo+1) : *(bufferTwo+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(bufferTwo+odd) ); /* replace signal one's phases with those of signal two */ *(channelOne+even) = hypot( a1, b1 ); *(channelOne+odd) = -atan2( b1, a1 ); *(channelTwo+even) = hypot( a2, b2 ); *(channelTwo+odd) = -atan2( b2, a2 ); } /* constrain our shapeWidth value */ if ( shapeWidth > N2 ) shapeWidth = N2; if ( shapeWidth < 1 ) shapeWidth = 1; /* lets just shape the entire signal by the shape width */ for ( i=0; i < N; i += shapeWidth << 1 ) { float amplSum = 0., freqSum = 0., factor; for ( j = 0; j < shapeWidth << 1; j += 2 ) { amplSum += *(channelTwo+i+j); freqSum += *(channelOne+i+j); } if(freqSum <= 0.001){ freqSum = 1.0; } if (amplSum < 0.000000001) factor = 0.000000001; else factor = amplSum / freqSum; for ( j = 0; j < shapeWidth * 2; j += 2 ) *(channelOne+i+j) *= factor; } /* copy remaining magnitudes */ if ( (remainingWidth = N2 % shapeWidth) ) { int bindex = (N2 - remainingWidth) << 1; float amplSum = 0., freqSum = 0., factor; for ( j = 0; j < remainingWidth * 2; j += 2 ) { amplSum += *(channelTwo+bindex+j); freqSum += *(channelOne+bindex+j); } if(freqSum <= 0.00001){ freqSum = 1.0; } if (amplSum < 0.000000001) factor = 0.000000001; else factor = amplSum / freqSum; for ( j = 0; j < remainingWidth * 2; j += 2 ) *(channelOne+bindex+j) *= factor; } /* convert from polar to cartesian */ for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; *(bufferOne+even) = *(channelOne+even) * cos( *(channelOne+odd) ); if ( i != N2 ) *(bufferOne+odd) = (*(channelOne+even)) * -sin( *(channelOne+odd) ); } /* do an inverse fft */ rdft( N, -1, bufferOne, bitshuffle, trigland ); /* dewindow our result */ overlapadd( bufferOne, N, Wsyn, output, Nw, inCount); /* set our output and adjust our retaining output buffer */ for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->inCount = inCount % Nw; return (w+7); }
t_int *leaker_perform(t_int *w) { int i,j,odd,even; float a1,a2,b1,b2; t_leaker *x = (t_leaker *) (w[1]); t_float *in1 = (t_float *)(w[2]); t_float *in2 = (t_float *)(w[3]); t_float *in3 = (t_float *)(w[4]); t_float *out = (t_float *)(w[5]); t_int n = w[6]; float fade_value = x->fade_value; float *input1 = x->input1; float *input2 = x->input2; int inCount = x->inCount; int R = x->R; int N = x->N; int N2 = x->N2; int D = x->D; int Nw = x->Nw; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *output = x->output; float *buffer1 = x->buffer1; float *buffer2 = x->buffer2; float *channel1 = x->channel1; float *channel2 = x->channel2; int *sieve = x->sieve; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland; float mult = x->mult; /* dereference struncture */ if( x->mute) { while(n--){ *out++ = 0.; } return (w+7); } if( x->bypass ) { while(n--){ *out++ = *in1++; } return (w+7); } #if MSP if(x->fade_connected) fade_value = *in3++ * (float) N2; #endif #if PD fade_value = *in3++ * (float) N2; #endif inCount += D; for ( j = 0 ; j < Nw - D ; j++ ){ input1[j] = input1[j+D]; input2[j] = input2[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { input1[j] = *in1++; input2[j] = *in2++; } fold(input1, Wanal, Nw, buffer1, N, inCount); fold(input2, Wanal, Nw, buffer2, N, inCount); rdft(N, 1, buffer1, bitshuffle, trigland); rdft(N, 1, buffer2, bitshuffle, trigland); for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; if( fade_value <= 0 || fade_value < sieve[i] ){ a1 = ( i == N2 ? *(buffer1+1) : *(buffer1+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(buffer1+odd) ); *(channel1+even) = hypot( a1, b1 ) ; *(channel1+odd) = -atan2( b1, a1 ); *(buffer1+even) = *(channel1+even) * cos(*(channel1+odd)); if ( i != N2 ){ *(buffer1+odd) = -(*(channel1+even)) * sin(*(channel1+odd)); } } else { a2 = ( i == N2 ? *(buffer2+1) : *(buffer2+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(buffer2+odd) ); *(channel1+even) = hypot( a2, b2 ) ; *(channel1+odd) = -atan2( b2, a2 ); *(buffer1+even) = *(channel1+even) * cos(*(channel1+odd) ); if ( i != N2 ){ *(buffer1+odd) = -(*(channel1+even)) * sin( *(channel1+odd) ); } } } rdft( N, -1, buffer1, bitshuffle, trigland ); overlapadd( buffer1, N, Wsyn, output, Nw, inCount); for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; x->inCount = inCount % Nw; return (w+7); }
t_int *drown_perform(t_int *w) { int i,j; float frame_peak = 0.0; float local_thresh; t_drown *x = (t_drown *) (w[1]); t_float *in = (t_float *)(w[2]); t_float *in2 = (t_float *)(w[3]); t_float *in3 = (t_float *)(w[4]); t_float *out = (t_float *)(w[5]); t_int n = w[6]; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *input = x->input; float *Hwin = x->Hwin; float *buffer = x->buffer; float *channel = x->channel; float *output = x->output; int D = x->D; int I = D; int R = x->R; int Nw = x->Nw; int N = x->N ; int N2 = x-> N2; int Nw2 = x->Nw2; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland; float mult = x->mult; int inCount = x->inCount ; int on = inCount; float threshold = x->threshold; float drownmult = x->drownmult; short *connected = x->connected; // get multiplier from sig inlet /* dereference struncture */ if( x->mute ){ while( n-- ){ *out++ = 0.0; } return (w+7); } if( connected[1] ) threshold = *in2++ ; if( connected[2] ) drownmult = *in3++ ; inCount += D; for ( j = 0 ; j < Nw - D ; j++ ){ input[j] = input[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { input[j] = *in++; } fold( input, Wanal, Nw, buffer, N, inCount ); rdft(N, 1, buffer, bitshuffle, trigland); if( x->peakflag ){ leanconvert( buffer, channel, N2); for(i = 0; i <N; i+= 2){ if(frame_peak < channel[i]) frame_peak = channel[i]; } local_thresh = frame_peak * threshold; for(i = 0; i <N; i+= 2){ if(channel[i] < local_thresh) channel[i] *= drownmult; } leanunconvert( channel, buffer, N2); } else { nudist( buffer, channel, threshold, drownmult, N2 ); } rdft( N, -1, buffer, bitshuffle, trigland ); overlapadd( buffer, N, Wsyn, output, Nw, inCount); for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->inCount = inCount % Nw; return (w+7); }
t_int *cavoc_perform(t_int *w) { int i,j; float oldfrac,newfrac; t_cavoc *x = (t_cavoc *)(w[1]); float *trigger_vec = (t_float *)(w[2]); float *out = (t_float *)(w[3]); t_int n = w[4]; int frames_left = x->frames_left; float *input = x->input; float *buffer = x->buffer; int in_count = x->in_count; int R = x->R; int N = x->N; int N2 = x->N2; int D = x->D; int Nw = x->Nw; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *output = x->output; float *channel = x->channel; float mult = x->mult ; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland ; int hold_frames = x->hold_frames; short *rule = x->rule; short left = x->left; short right = x->right; short center = x->center; float *last_frame = x->last_frame; float *c_lastphase_out = x->c_lastphase_out; float c_fundamental = x->c_fundamental; float c_factor_out = x->c_factor_out; short external_trigger = x->external_trigger; short new_event = 0; in_count += D; if( x->mute ){ while( n-- ){ *out++ = 0.0; } return (w+5); } if(external_trigger){// only accurate to within a vector because of FFT for(i=0;i<n;i++){ if(trigger_vec[i]){ new_event = 1; break; } } } else if(!--frames_left){ frames_left = hold_frames; new_event = 1; } if(new_event){ for( i = 2; i < N; i+=2 ){ left = last_frame[ i - 2]; center = last_frame[i] ; right = last_frame[i+2]; channel[i] = cavoc_apply_rule(left, right, center, rule ); } /* boundary cases */ center = last_frame[0]; right = last_frame[2]; left = last_frame[N]; channel[0] = cavoc_apply_rule(left, right, center, rule ); center = last_frame[N]; right = last_frame[0]; left = last_frame[N - 2]; channel[N] = cavoc_apply_rule(left, right, center, rule ); /* save composite frame for next time */ for( i = 0; i < N+1; i++ ){ last_frame[i] = channel[i]; } } unconvert( channel, buffer, N2, c_lastphase_out, c_fundamental, c_factor_out ); rdft( N, -1, buffer, bitshuffle, trigland ); overlapadd( buffer, N, Wsyn, output, Nw, in_count); for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->in_count = in_count % Nw; x->frames_left = frames_left; return (w+5); }
t_int *resent_perform(t_int *w) { int iphase, amp, freq, i, j; float fincr; float fpos; ////////////////////////////////////////////// t_resent *x = (t_resent *) (w[1]); float *frame_incr = x->frame_incr ; float *frame_phase = x->frame_phase ; float *composite_frame = x->composite_frame ; float in_sample; t_float *in = (t_float *)(w[2]); t_float *out = (t_float *)(w[3]); t_float *sync_vec = (t_float *)(w[4]); t_int n = w[5]; /* dereference structure */ int inCount = x->inCount; int R = x->R; int N = x->N; int N2 = x->N2; int D = x->D; int Nw = x->Nw; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *input = x->input; float *output = x->output; float *buffer = x->buffer; float *channel = x->channel; float fframe = x->current_frame ; float last_fpos = x->last_fpos ; int framecount = x->framecount; float mult = x->mult ; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland ; float *c_lastphase_in = x->c_lastphase_in; float *c_lastphase_out = x->c_lastphase_out; float c_fundamental = x->c_fundamental; float c_factor_in = x->c_factor_in; float c_factor_out = x->c_factor_out; float sync = x->sync; sync = (float)x->frames_read / (float)x->framecount; if(x->mute || x->lock){ while(n--){ *out++ = 0.0; *sync_vec++ = sync; } return (w+6); } inCount += D; if(x->read_me){ for ( j = 0 ; j < Nw - D ; j++ ){ input[j] = input[j+D]; } if(x->playthrough){ for (j = Nw - D; j < Nw; j++) { in_sample = input[j] = *in++; *out++ = in_sample * 0.5; // scale down *sync_vec++ = sync; } } else{ for (j = Nw - D; j < Nw; j++) { input[j] = *in++; *out++ = 0.0; *sync_vec++ = sync; } } fold( input, Wanal, Nw, buffer, N, inCount ); rdft( N, 1, buffer, bitshuffle, trigland ); convert( buffer, x->loveboat[(x->frames_read)++], N2, c_lastphase_in, c_fundamental, c_factor_in ); if(x->frames_read >= x->framecount){ x->read_me = 0; } } else { for( i = 0 ; i < N2; i++ ){ amp = i<<1; freq = amp + 1 ; iphase = frame_phase[i] ; if( iphase < 0 ) iphase = 0; if( iphase > framecount - 1 ) iphase = framecount - 1; composite_frame[amp] = x->loveboat[iphase][amp]; composite_frame[freq] = x->loveboat[iphase][freq]; frame_phase[i] += frame_incr[i] ; while( frame_phase[i] > framecount - 1) frame_phase[i] -= framecount ; while( frame_phase[i] < 0. ) frame_phase[i] += framecount ; } unconvert(composite_frame, buffer, N2, c_lastphase_out, c_fundamental, c_factor_out); rdft( N, -1, buffer, bitshuffle, trigland ); overlapadd( buffer, N, Wsyn, output, Nw, inCount ); for (j = 0; j < D; j++){ *out++ = output[j] * mult; *sync_vec++ = sync; } for (j = 0; j < Nw - D; j++){ output[j] = output[j+D]; } for (j = Nw - D; j < Nw; j++){ output[j] = 0.; } } /* restore state variables */ x->inCount = inCount %Nw; x->current_frame = fframe; x->last_fpos = last_fpos; x->sync = sync; return (w+6); }
t_int *pvcompand_perform(t_int *w) { float sample, outsamp ; int i,j; float maxamp ; float fmult; float cutoff; float avr, new_avr, rescale; t_pvcompand *x = (t_pvcompand *) (w[1]); t_float *in = (t_float *)(w[2]); t_float *in2 = (t_float *)(w[3]); t_float *out = (t_float *)(w[4]); int n = (int)(w[5]); int inCount = x->inCount; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *input = x->input; float *Hwin = x->Hwin; float *buffer = x->buffer; float *channel = x->channel; float *output = x->output; int D = x->D; int I = D; int R = x->R; int Nw = x->Nw; int N = x->N ; int N2 = x-> N2; int Nw2 = x->Nw2; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland; float mult = x->mult; int count = x->count; float *atten = x->atten; float *curthresh = x->curthresh; float *thresh = x->thresh; float max_atten = x->max_atten; if( x->mute ){ while( n-- ){ *out++ = 0.0; } return (w+6); } if( x->bypass ){ while( n-- ){ *out++ = *in++ * 0.5; // gain compensation } return (w+6); } #if MSP if( x->connected[1] ){ max_atten = *in2++ ; if(max_atten != x->last_max_atten) { x->last_max_atten = x->max_atten = max_atten; update_thresholds(x); } } #endif #if PD max_atten = *in2++ ; if(max_atten != x->last_max_atten) { x->last_max_atten = x->max_atten = max_atten; update_thresholds(x); } #endif inCount += D; for ( j = 0 ; j < Nw - D ; j++ ){ input[j] = input[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { input[j] = *in++; } fold( input, Wanal, Nw, buffer, N, inCount ); rdft( N, 1, buffer, bitshuffle, trigland ); leanconvert(buffer, channel, N2); maxamp = 0.; avr = 0; for( i = 0; i < N; i+= 2 ){ avr += channel[i]; if( maxamp < channel[i] ){ maxamp = channel[i] ; } } if(count <= 1){ // post("count too low!"); count = 1; } for( i = 0; i < count; i++ ){ curthresh[i] = thresh[i]*maxamp ; } cutoff = curthresh[count-1]; new_avr = 0; for( i = 0; i < N; i += 2){ if( channel[i] > cutoff ){ j = count-1; while( channel[i] > curthresh[j] ){ j--; if( j < 0 ){ j = 0; break; } } channel[i] *= atten[j]; } new_avr += channel[i] ; } leanunconvert( channel,buffer, N2); rdft( N, -1, buffer, bitshuffle, trigland ); overlapadd( buffer, N, Wsyn, output, Nw, inCount); if( x->norml ) { if( new_avr <= 0 ){ new_avr = .0001; } rescale = avr / new_avr ; mult *= rescale ; } else { mult *= pvcompand_ampdb( max_atten * -.5); ; } for ( j = 0; j < D; j++ ){ *out++ = output[j] * mult; } for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->inCount = inCount % Nw; return (w+6); }
void do_cross(t_cross *x) { t_fftease *fft = x->fft; t_fftease *fft2 = x->fft2; int i; int N2 = fft->N2; t_float a1, b1, a2, b2; t_float *buffer1 = fft->buffer; t_float *buffer2 = fft2->buffer; t_float *channel1 = fft->channel; short autonorm = x->autonorm; int N = fft->N; t_float mult = fft->mult; int even, odd; t_float gainer; t_float threshie = x->threshie; t_float ingain = 0; t_float outgain, rescale; t_float mymult; fold(fft); fold(fft2); rdft(fft,1); rdft(fft2,1); /* changing algorithm for window flexibility */ if(autonorm){ ingain = 0; for(i = 0; i < N; i+=2){ ingain += hypot(buffer1[i], buffer1[i+1]); } } for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(buffer1+1) : *(buffer1+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(buffer1+odd) ); a2 = ( i == N2 ? *(buffer2+1) : *(buffer2+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(buffer2+odd) ); gainer = hypot(a2, b2); if( gainer > threshie ) *(channel1+even) = hypot( a1, b1 ) * gainer; *(channel1+odd) = -atan2( b1, a1 ); *(buffer1+even) = *(channel1+even) * cos( *(channel1+odd) ); if ( i != N2 ) *(buffer1+odd) = -(*(channel1+even)) * sin( *(channel1+odd) ); } if(autonorm){ outgain = 0; for(i = 0; i < N; i+=2){ outgain += hypot(buffer1[i], buffer1[i+1]); } if(ingain <= .0000001){ // post("gain emergency!"); rescale = 1.0; } else { rescale = ingain / outgain; } //post("ingain %f outgain %f rescale %f",ingain, outgain, rescale); x->normult = mult * rescale; } else { x->normult = mult; //post("mymult: %f", mymult); } rdft(fft, -1); overlapadd(fft); }
void makeWaves( struct FFTSound *fftsound, struct RSynthData *rsd, void(*waveconsumer)(struct FFTSound *fftsound,void *pointer,double **samples,int num_samples), bool (*progressupdate)(int pr,void *pointer), void *pointer, int start,int end, bool obank, bool keep_formants ) { double a0=0.0; long point=0, point2=0; int ch; int on=(-fftsound->Nw*fftsound->I)/fftsound->Dn, in=-fftsound->Nw; int j,i; double coef[fftsound->numcoef2+2]; int keepform=0; if(obank){ fprintf(stderr,"\n\nWarning! The result of the additiv resynthesis might be buggy. Please listen to the result to check that its okey or use the IFFT type.\n\n"); } // init_again(); // rsd=getRSynthData(fftsound); memset(coef,0.0,sizeof(double)*(fftsound->numcoef2+2)); if (keep_formants && fftsound->numcoef2!=0) keepform=1; for (j=start; j<end; j++) { on+=fftsound->I; in+=fftsound->Dn; for (ch=0; ch<fftsound->samps_per_frame; ch++) { point = ch*fftsound->horiz_num*fftsound->numchannels + j*fftsound->numchannels; point2=ch*lpc_horiz_num*(fftsound->numcoef2+2)+j*(fftsound->numcoef2+2); if(fftsound->usemem==false) pthread_mutex_lock(&fftsound->mutex); for (i=0; i<fftsound->numchannels; i++) { rsd->channel[i+i]=MEGAMP_GET(point+i); //ch*fftsound->horiz_num + i,j rsd->channel[i+i+1]=MEGFREQ_GET(point+i); } if(fftsound->usemem==false) pthread_mutex_unlock(&fftsound->mutex); // printf("j2: %d\n",j); if (obank==true) { if (j==start){ for (i=0; i<fftsound->N2+1; i++) { rsd->lastfreq[ch][i]=rsd->channel[i+i+1]; rsd->lastamp[ch][i]=rsd->channel[i+i]; rsd->indx[ch][i]=0.; } } if (j>=lpc_horiz_num) keepform=0; if (keepform) { for (i=0; i<fftsound->numcoef2+2; i++) { coef[i]=lpc_coef[point2]; point2++; } a0=coef[fftsound->numcoef2+1]; } oscbank( fftsound, rsd,rsd->channel, fftsound->N2, fftsound->R, fftsound->I, rsd->output[ch], ch, fftsound->Nw,coef, fftsound->numcoef2*keepform,a0 ); } else { unconvert(rsd, rsd->channel, rsd->buffer, fftsound->N2, fftsound->I, fftsound->R, ch); rfft(rsd->buffer, fftsound->N2, INVERSE); overlapadd(rsd->buffer, fftsound->N, fftsound->Wsyn, rsd->output[ch], fftsound->Nw, on); } } // end for(ch=0; ch<fftsound->samps_per_frame; ch++) if (obank==true){ shiftout( fftsound, waveconsumer, pointer, rsd->output, fftsound->Nw, fftsound->I, in+fftsound->Nw2+fftsound->Dn ); }else{ shiftout( fftsound, waveconsumer, pointer, rsd->output, fftsound->Nw, fftsound->I, on ); } if(progressupdate!=NULL){ if((*progressupdate)(j,pointer)==false)break; } } // end for(j=start; j<end; j++) // returnRSynthData(rsd); }
t_int *bthresher_perform(t_int *w) { float sample, outsamp ; int i, j, on; t_bthresher *x = (t_bthresher *) (w[1]); float *in = (t_float *)(w[2]); float *inthresh = (t_float *)(w[3]); float *damping = (t_float *)(w[4]); float *out = (t_float *)(w[5]); t_int n = w[6]; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland; float mult = x->mult; int in_count = x->in_count; int R = x->R; int N = x->N; int N2 = x->N2; int D = x->D; int Nw = x->Nw; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *damping_factor = x->damping_factor; float *move_threshold = x->move_threshold; float *input = x->input; float *output = x->output; float *buffer = x->buffer; float *channel = x->channel; float *composite_frame = x->composite_frame; int max_hold_frames = x->max_hold_frames; int *frames_left = x->frames_left; float thresh_scalar = x->thresh_scalar; float damp_scalar = x->damp_scalar; short inf_hold = x->inf_hold; if( x->mute ) { for( j = 0; j < D; j++) { *out++ = 0.0 ; } } else if ( x->bypass ) { for( j = 0; j < D; j++) { *out++ = *in++ * 0.5; } } else { #if MSP if( x->thresh_connected ) { thresh_scalar = *inthresh++; } if( x->damping_connected ) { damp_scalar = *damping++; } #endif #if PD thresh_scalar = *inthresh++; damp_scalar = *damping++; #endif in_count += D; for ( j = 0 ; j < Nw - D ; j++ ) input[j] = input[j+D]; for ( j = Nw-D; j < Nw; j++ ) { input[j] = *in++; } fold( input, Wanal, Nw, buffer, N, in_count ); rdft( N, 1, buffer, bitshuffle, trigland ); convert( buffer, channel, N2, x->c_lastphase_in, x->c_fundamental, x->c_factor_in ); if( x->first_frame ){ for ( i = 0; i < N+2; i++ ){ composite_frame[i] = channel[i]; x->frames_left[i] = max_hold_frames; } x->first_frame = 0; } else { if( thresh_scalar < .999 || thresh_scalar > 1.001 || damp_scalar < .999 || damp_scalar > 1.001 ) { for(i = 0, j = 0; i < N+2; i += 2, j++ ){ if( fabs( composite_frame[i] - channel[i] ) > move_threshold[j] * thresh_scalar|| frames_left[j] <= 0 ){ composite_frame[i] = channel[i]; composite_frame[i+1] = channel[i+1]; frames_left[j] = max_hold_frames; } else { if(!inf_hold){ --(frames_left[j]); } composite_frame[i] *= damping_factor[j] * damp_scalar; } } } else { for( i = 0, j = 0; i < N+2; i += 2, j++ ){ if( fabs( composite_frame[i] - channel[i] ) > move_threshold[j] || frames_left[j] <= 0 ){ composite_frame[i] = channel[i]; composite_frame[i+1] = channel[i+1]; frames_left[j] = max_hold_frames; } else { if(!inf_hold){ --(frames_left[j]); } composite_frame[i] *= damping_factor[j]; } } } } unconvert(x->composite_frame, buffer, N2, x->c_lastphase_out, x->c_fundamental, x->c_factor_out); rdft(N, -1, buffer, bitshuffle, trigland); overlapadd(buffer, N, Wsyn, output, Nw, in_count); for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; } x->in_count = in_count % Nw; x->thresh_scalar = thresh_scalar; x->damp_scalar = damp_scalar; return (w+7); }
t_int *crossx_perform(t_int *w) { int i, j; float a1, a2, b1, b2; int even, odd; int amp, freq; float gainer, threshie; float ingain = 0; float outgain, rescale; float mymult; t_crossx *x = (t_crossx *) (w[1]); t_float *in1 = (t_float *)(w[2]); t_float *in2 = (t_float *)(w[3]); t_float *in3 = (t_float *)(w[4]); t_float *out = (t_float *)(w[5]); t_int n = w[6]; /* dereference struncture */ float *input1 = x->input1; float *input2 = x->input2; float *buffer1 = x->buffer1; float *buffer2 = x->buffer2; int inCount = x->inCount; int R = x->R; int N = x->N; int N2 = x->N2; int D = x->D; int Nw = x->Nw; float *Wanal = x->Wanal; float *Wsyn = x->Wsyn; float *output = x->output; float *channel1 = x->channel1; float *channel2 = x->channel2; float *last_channel = x->last_channel; int *bitshuffle = x->bitshuffle; float *trigland = x->trigland; float mult = x->mult; float *c_lastphase_in1 = x->c_lastphase_in1; float *c_lastphase_in2 = x->c_lastphase_in2; float *c_lastphase_out = x->c_lastphase_out; float c_fundamental = x->c_fundamental; float c_factor_in = x->c_factor_in; float c_factor_out = x->c_factor_out; short autonorm = x->autonorm; if(x->mute){ while(n--){ *out++ = 0; } return w+7; } if( x->thresh_connected ){ threshie = *in3++; } else { threshie = x->threshie; } inCount += D; for ( j = 0 ; j < Nw - D ; j++ ){ input1[j] = input1[j+D]; input2[j] = input2[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { input1[j] = *in1++; input2[j] = *in2++; } fold( input1, Wanal, Nw, buffer1, N, inCount ); fold( input2, Wanal, Nw, buffer2, N, inCount ); rdft( N, 1, buffer1, bitshuffle, trigland ); rdft( N, 1, buffer2, bitshuffle, trigland ); /* changing algorithm for window flexibility */ if(autonorm){ ingain = 0; for(i = 0; i < N; i+=2){ ingain += hypot(buffer1[i], buffer1[i+1]); } } for ( i = 0; i <= N2; i++ ) { odd = ( even = i<<1 ) + 1; a1 = ( i == N2 ? *(buffer1+1) : *(buffer1+even) ); b1 = ( i == 0 || i == N2 ? 0. : *(buffer1+odd) ); a2 = ( i == N2 ? *(buffer2+1) : *(buffer2+even) ); b2 = ( i == 0 || i == N2 ? 0. : *(buffer2+odd) ); gainer = hypot(a2, b2); if( gainer > threshie ) *(channel1+even) = hypot( a1, b1 ) * gainer; *(channel1+odd) = -atan2( b1, a1 ); *(buffer1+even) = *(channel1+even) * cos( *(channel1+odd) ); if ( i != N2 ) *(buffer1+odd) = -(*(channel1+even)) * sin( *(channel1+odd) ); } if(autonorm){ outgain = 0; for(i = 0; i < N; i+=2){ outgain += hypot(buffer1[i], buffer1[i+1]); } if(ingain <= .0000001){ // post("gain emergency!"); rescale = 1.0; } else { rescale = ingain / outgain; } // post("ingain %f outgain %f rescale %f",ingain, outgain, rescale); mymult = mult * rescale; } else { mymult = mult; } rdft( N, -1, buffer1, bitshuffle, trigland ); overlapadd( buffer1, N, Wsyn, output, Nw, inCount); for ( j = 0; j < D; j++ ) *out++ = output[j] * mymult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->inCount = inCount % Nw; return (w+7); }
t_int *xsyn_perform(t_int *w) { t_float *in1,*out, *in2; float sample, outsamp ; float *input1, *input2, *output, *buffer1, *buffer2, *Wanal, *Wsyn, *channel1, *channel2; int n, i,j, inCount, R, N, N2, D, Nw; float maxamp ; int *bitshuffle; float *trigland; float mult; float a1, a2, b1, b2; int even, odd; t_xsyn *x = (t_xsyn *) (w[1]); in1 = (t_float *)(w[2]); in2 = (t_float *)(w[3]); out = (t_float *)(w[4]); n = (int)(w[5]); /* dereference struncture */ input1 = x->input1; input2 = x->input2; buffer1 = x->buffer1; buffer2 = x->buffer2; inCount = x->inCount; R = x->R; N = x->N; N2 = x->N2; D = x->D; Nw = x->Nw; Wanal = x->Wanal; Wsyn = x->Wsyn; output = x->output; buffer1 = x->buffer1; buffer2 = x->buffer2; channel1 = x->channel1; channel2 = x->channel2; bitshuffle = x->bitshuffle; trigland = x->trigland; mult = x->mult; if(x->mute){ while(n--){ *out++ = 0.0; } return (w+6); } x->inCount += D; for ( j = 0 ; j < Nw - D ; j++ ){ input1[j] = input1[j+D]; input2[j] = input2[j+D]; } for ( j = Nw - D; j < Nw; j++ ) { input1[j] = *in1++; input2[j] = *in2++; } fold( input1, Wanal, Nw, buffer1, N, inCount ); fold( input2, Wanal, Nw, buffer2, N, inCount ); rdft( N, 1, buffer1, bitshuffle, trigland ); rdft( N, 1, buffer2, bitshuffle, trigland ); leanconvert( buffer1, channel1, N2 ); leanconvert( buffer2, channel2, N2 ); maxamp = 0 ; for( i = 0; i < N; i+= 2 ) { if( channel2[i] > maxamp ) { maxamp = channel2[i]; } } if( maxamp == 0.0 ) maxamp = 1.0 ; for( i = 0; i < N; i+= 2 ) { channel1[i] *= (channel2[i] / maxamp ); } leanunconvert( channel1, buffer1, N2 ); rdft( N, -1, buffer1, bitshuffle, trigland ); overlapadd( buffer1, N, Wsyn, output, Nw, inCount); for ( j = 0; j < D; j++ ) *out++ = output[j] * mult; for ( j = 0; j < Nw - D; j++ ) output[j] = output[j+D]; for ( j = Nw - D; j < Nw; j++ ) output[j] = 0.; /* restore state variables */ x->inCount = inCount; return (w+6); }