Csound Csound-dev Csound-tekno Search About

[Csnd] Convert Sr

Date2020-03-20 21:28
FromEnrico Francioni <00000005323c8739-dmarc-request@LISTSERV.HEANET.IE>
Subject[Csnd] Convert Sr
Hello to all,

does a UDO already exist in Csound to convert the SR into the code?

Something like this:

             double old_sr = 48000.0;
            double new_sr = current_csound_sr;

            if (old_sr != new_sr) {
                double fmax = new_sr / 2.;
                double window_width = 7;
                int num_channels = reader->numChannels;

                double factor = new_sr / old_sr;
                int num_in_frames = bufferSamples / num_channels;
                int num_out_frames = ( int) ceil(num_in_frames * factor);

                // create output buffer
                AudioSampleBuffer bufDest(num_channels, num_out_frames);
                float *bufferResampledWrite = bufDest.getWritePointer( 0);
                const float *bufferResampled = bufDest.getReadPointer( 0);

                // resampe process
                for ( long ch = 0; ch < num_channels; ++ch) {
                    for ( long s = 0; s < num_out_frames; ++s) {
                        long i, j;
                        double x = s / factor;
                        double r_w, r_a, r_snc;
                        double r_g = 2 * fmax / new_sr; // Calc gain correction factor
                        double r_y = 0;
                        for (i = -window_width / 2; i < window_width / 2; ++i) { // For 1 window width
                            j = ( long) (x + i);          // Calc input sample index
                            //rem calculate von Hann Window. Scale and calculate Sinc
                            r_w = 0.5 - 0.5 * cos(TWOPI * ( 0.5 + (j - x) / window_width));
                            r_a = TWOPI * (j - x) * fmax / new_sr;
                            r_snc = (r_a != 0 ? r_snc = sin(r_a) / r_a : 1);
                            if (j >= 0 && j < num_in_frames)
                                r_y = r_y + r_g * r_w * r_snc * bufferSource[num_channels * j + ch];
                        }
                        bufferResampledWrite[num_channels * s + ch] = r_y; // Return new filtered sample
                    }
                }

            }


thanks,
Enrico

Csound mailing list
Csound@listserv.heanet.ie
https://listserv.heanet.ie/cgi-bin/wa?A0=CSOUND
Send bugs reports to
        https://github.com/csound/csound/issues
Discussions of bugs and features can be posted here