How can change frequency of audio file

Hello Guys,
I am loading and playing “.wav” audio file, I can control its relative volume by using slider and by changing the value in function “setSample”.
Now, I want to operate its frequency by using slider. So, how can I do that?.

How are you ‘playing’ the file? If you are directly accessing samples, then you can easily change the playback rate by skipping X number of samples each time you output one. To slow down the playback rate you can interpolate between successive samples.

Look into the GenericInterpolator classes, they accept double speedRatio as an argument to their process methods. The WindowedSincInterpolator will give you the highest quality.

1 Like

Okay,
Let me give some more details how i am loading and playing audio file. So, you can understand more in details.

    void AudioProcessor::loadFile(juce::File file)
    {             
        std::cout << "File path: " << file.getFullPathName() << std::endl;    
        myFormatReader = formatManager.createReaderFor (file);
                    
        if (myFormatReader != nullptr)
        {
            fileBuffer.setSize (myFormatReader->numChannels, (int)myFormatReader->lengthInSamples);   
            
            std::cout << "Number of samples in buffer "<<fileBuffer.getNumSamples()<<std::endl;
            std::cout << "lengthInSamples "<<myFormatReader->lengthInSamples<<std::endl;
            
            myFormatReader->read (&fileBuffer, 0, (int) myFormatReader->lengthInSamples, 0, true, true);        
            thumbnail.setSource (new juce::FileInputSource (file));   
        }
    }
 

    void AudioProcessor::processBlock (juce::AudioBuffer<float>& buffer, juce::MidiBuffer& midiMessages)
    {
        std::cout << "processBlock called......"<< std::endl;    
        
        if(fileBuffer.getNumSamples() == 0)
            return;
        
        std::cout << "samples in buffer "<<fileBuffer.getNumSamples()<<std::endl;    
        
        if(play == true)
        {   
            for (int i = 0; i < buffer.getNumSamples(); ++i)	
            {   
                for (int j = 0; j < buffer.getNumChannels(); ++j)
                {
                    int chantoread = j % fileBuffer.getNumChannels();
                    
                    if(mute == false)
                    {
                        buffer.setSample(j, i, volume_level *fileBuffer.getSample(chantoread,filepos_start));
                        //--buffer.applyGain(volume_level);
                        //--buffer.applyGain(filepos_start_temp, filepos_start, volume_level);
                    }
                }
                ++filepos_start;
            
                //if (filepos_start == fileBuffer.getNumSamples())
                if (filepos_start == filepos_end)
                    filepos_start = filepos_start_temp;	
            }
        }
        else
            return;
    }

So, you can see that how i am loading file as per call “loadfile” function.
and also used some flags in ProcessBlock function to play/stop/mute audio file.

You can quickly and crudely change the playback speed by changing the amount you increment filepos_start by. If for example you do filepos_start+=2 the file will play back an octave higher.

On other other hand, it seems that the classes suggested by @maxwellp777 will give you much better quality and control over the process. I’ve never used them, but they definitely seem the way to go.

Before proposing how to do it, I’d like to clarify what @kishanpatel is actually trying to achieve :wink: “operate its frequency” is no real technical term, so I can image that the author means

  • Changing the Pitch (without changing the playback speed)
  • Channing the Pitch (by changing the playback speed)
  • Attenuating or boosting certain frequencies → applying some equalizer

Before not knowing this all answers are more or less useless. I’m not even sure if the author is aware of stuff like time domain and frequency domain representation of a signal and that changing the pitch is a much more complicated thing than changing the volume.

Last but not least, you can simplify and with that speed up your processing code a lot by first swiping the channel and sample loop (always loop over channels in the outer loop and over samples in the inner loop, this is more friendly to the CPU cache) and then using FloatVectorOperations to substitute the inner sample loop :slight_smile:

The STK has a pitch shift class that you could implement:

Yeah,
Actually i am developing plugins to load and play audio file. Then operate its by using sliders like as
volume slider, frequency slider, pitch slider, CFA slider.
Currently i am working to operate frequency, then move to pitch and CFA.

But what do you actually mean by

This is simply no technical term I’m aware of and I guess I know most technical terms when it comes to audio processing :grin:. Do you mean resampling?

Hello PluginPenguin,
Yes, we can say that resampling. Means change frequency of playing audio file with realtime.

i think what everyone’s trying to tell you is: a ‘frequency’ is simply the term for a thing that repeats a bunch of times per sec. the amount of repetitions is the frequency in hz. but when you have an audio signal, that contains more sound than just a sine wave, then it’s technically made of a lot of frequencies oscillating at different magnitudes and phases. if you use certain filters you can in- or decrease the volume around a frequency of the signal. but resampling is a completely different process. you just play back the sample at a different speed making it appear at a different pitch, but it’s relative to its origin and you can’t tune it to a certain frequency.

that being said: do the thing with the interpolator for resampling that was suggested earlier

What we’re trying to determine is if you mean that you want to change the sample rate of the sampled audio (such as from 48000 samples per second to 96000 samples per second, which is done via resampling), or if you want to change the pitch of the audio, such as from the key of C to the key of F (which usually involves using FFTs). Which of these are you talking about? Talking only about “frequency” is not enough information.

Hello,
Actually, in my application, we required both features. We provide 2 slider button: one for operate frequency and second one to operate pitch. Means we can change frequency and pitch of audio file by using 2 different buttons.

This still doesn’t really answer the question that everyone is asking.

Frequency and pitch are the same thing. They are just frequencies.

If you could find some examples of what you are trying to do then perhaps someone could be of more help to you.

Changing the sample rate of a file using a slider doesn’t make much sense. Why would you want that kind of continuous control over the sample rate? Most DAWs have the ability to import a file while converting its data to a new sample rate, but I’ve never heard of one that tries to convert the sample rate using a slider.

For example, suppose you have a file that was recorded at 48000 samples per second. Your project (or whatever your host calls it) may be set to 96000 samples per second, so when importing that file into your project, the host may ask you if you wish to resample the file at 96000 samples per second or not. Failing to do so would result in that file playing back at a different speed than it was recorded, which wouldn’t sound correct. So normally you’d let the host resample that file when importing.

Another use for resampling is downsampling, such as changing the sample rate (and thus the number of samples), in order to save CPU time/power for complex calculations.

But neither of those scenarios is intended to affect how the audio “sounds”. So I just don’t understand why you would ever want a slider to change the sample rate. It’s not going to affect how the audio sounds. So what is the purpose?

As far as I can gather, the purpose is to get the “incorrect” sound (playing at different speed and pitch from the original file). For special effects purposes, without having to use a separate sampler plugin or something?

Oh, like changing the turntable speed from 33 to 45 to 78, without changing the record that’s being played? To get the Chipmunk effect? :slight_smile: Very odd.

If that’s the case, then look up “resampling”, here or elsewhere online. (Also, see “upsampling” and “downsampling”.)

But resampling the audio to play it back at a different speed will obviously change the pitch as well (–> the chipmunk effect)
But @kishanpatel told us here

that whatever they are trying to achieve should be separate from changing the pitch. You really need to describe better what you actually want to do, @kishanpatel :smile:

I wonder if this is some kind of a language translation problem? Maybe by “frequency” the original poster meant “rate/play rate/tempo”? In which case this would be about implementing independent time stretching and pitch shifting features.

Hello Guys,
First of all thanks to all for your support. Let me share some link to describe that how i want to use frequency slider. Please check below link. There is one SSP device in which these functionalities developed.

I hope you can understand that why we want to use slider to operate frequency and pitch.