Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it possible to change audio frequency (pitch) while listening to a music?
I searched in google but I was told to convert / change the whole file. I'm planning to create two button (up and down) to change the frequency while listening to a music.
Thanks before.
Posted
Comments
ashok rathod 26-Jun-14 6:31am    
can u please let me know in which specific technology you are talking about frequency change ( Winform ,WPF) ?
Haseito 26-Jun-14 6:35am    
I use WinForm native Windows Media Player control in Visual Studio 2010, and I play .dat file in karaoke cds. @ashokrathod

Use this code to read the wave file:
C#
// convert two bytes to one double in the range -1 to 1
static double bytesToDouble(byte firstByte, byte secondByte) {
    // convert two bytes to one short (little endian)
    short s = (secondByte << 8) | firstByte;
    // convert to range from -1 to (just below) 1
    return s / 32768.0;
}

// Returns left and right double arrays. 'right' will be null if sound is mono.
public void openWav(string filename, out double[] left, out double[] right)
{
    byte[] wav = File.ReadAllBytes(filename);

    // Determine if mono or stereo
    int channels = wav[22];     // Forget byte 23 as 99.999% of WAVs are 1 or 2 channels

    // Get past all the other sub chunks to get to the data subchunk:
    int pos = 12;   // First Subchunk ID from 12 to 16

    // Keep iterating until we find the data chunk (i.e. 64 61 74 61 ...... (i.e. 100 97 116 97 in decimal))
    while(!(wav[pos]==100 && wav[pos+1]==97 && wav[pos+2]==116 && wav[pos+3]==97)) {
        pos += 4;
        int chunkSize = wav[pos] + wav[pos + 1] * 256 + wav[pos + 2] * 65536 + wav[pos + 3] * 16777216;
        pos += 4 + chunkSize;
    }
    pos += 8;

    // Pos is now positioned to start of actual sound data.
    int samples = (wav.Length - pos)/2;     // 2 bytes per sample (16 bit sound mono)
    if (channels == 2) samples /= 2;        // 4 bytes per sample (16 bit stereo)

    // Allocate memory (right will be null if only mono sound)
    left = new double[samples];
    if (channels == 2) right = new double[samples];
    else right = null;

    // Write to double array/s:
    int i=0;
    while (pos < length) {
        left[i] = bytesToDouble(wav[pos], wav[pos + 1]);
        pos += 2;
        if (channels == 2) {
            right[i] = bytesToDouble(wav[pos], wav[pos + 1]);
            pos += 2;
        }
        i++;
    }
}


Make the modifications to the c# array in memory:
Changing pitch means re-sampling the array at a lower or higher sample rate, in effect stretching or shrinking the waveform to adjust the frequency.
Write out the array into a new Wave file;
Useful Link: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
 
Share this answer
 
Comments
Haseito 26-Jun-14 7:09am    
I need to change the frequency while the audio is being played. Something like in Realtek HD Audio Manager, i can change from -4 to 4, the sound changes and didn't disturb the audio.
Then you have to handle the playback of audio yourself - that's a lot of work.
When you've accomplished that, take a look at the buffers of audio data you send to the audio device: these buffers need to be changed. That means, you do a Fourier transformation to get the frequencies, then shift the frequencies, and do the Fourier transformation back to the audio data.
 
Share this answer
 
Comments
Kareem Abou Saad 26-Jun-14 11:58am    
Probably you'll have to use MATLAB

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900