Google Answers Logo
View Question
 
Q: Source code for C++ pitch-shift function ( No Answer,   6 Comments )
Question  
Subject: Source code for C++ pitch-shift function
Category: Computers > Algorithms
Asked by: leon76-ga
List Price: $50.00
Posted: 10 Aug 2004 09:32 PDT
Expires: 09 Sep 2004 09:32 PDT
Question ID: 385917
I'm looking for working source code for a C++ pitch-shift function that will
allow me to specify a 1D array containing PCM audio data and modify
the content such that the array contains the pitch-shifted version of
the original after processing. The function should also accept a
signed integer as the amount of up-shifting or down-shifting required.
The pitch-shifted audio should be of the same duration as the original
audio. The function will need to work in Embedded VC++ on Pocket PC 2003.

Clarification of Question by leon76-ga on 10 Aug 2004 09:37 PDT
The parameter for degree of up-shifting or down-shifting should be a
double instead and will be in terms of number of octaves to shift.

Clarification of Question by leon76-ga on 12 Aug 2004 09:29 PDT
Thanks pianoboy77-ga for the heads-up. I've spent the whole of today
trawling the WWW. Turned up a lot of the links that you have mentioned
as well. The MATLAB code looks deceptively simple; unfortunately, as
you have mentioned, the library calls that it makes hide a whole array
of complex mathematical functions which I won't be able to recreate
easily, if at all. I'll give SOX a closer look to see if I can port
the pitchshift function to the PPC 2003 platform (eVC) and whether it
is computationally acceptable.

Thanks once again.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Source code for C++ pitch-shift function
From: 1anton1-ga on 10 Aug 2004 13:53 PDT
 
This does not answer your question fully, but here's a basic (and
correct theory) on how to do this:

To double the frequency, drop every second sample (e.g. turn a 64k
sample into 32k) 1234321 -> 1331

To halve the frequency, double every sample (e.g. turn a 64k sample
into a 128k sample) 1234321 -> 11223344332211

To get shifts in between, use every third, fouth, fifth etc. to
discard, or repeat - depending on whether you want to shift down or
up.

This, however, does not solve the problem of playing for the same
length of time .....

if you remove half the data, you do go up one octave, but the sample
plays twice as fast.

I'm sure there are better ways to do this.  Just offering a bit of advice.
Subject: Re: Source code for C++ pitch-shift function
From: crythias-ga on 10 Aug 2004 14:57 PDT
 
"an octave corresponds exactly to a doubling of pitch."
http://www.physlink.com/Education/AskExperts/ae165.cfm

So, if A is 440Hz, then 880Hz is the next octave. An octave selection,
f, would be pitch*2^f

This seems to only help the other half of the problem. I don't know if
this affects duration.
Subject: Re: Source code for C++ pitch-shift function
From: leon76-ga on 10 Aug 2004 18:24 PDT
 
Thanks for the information. Adding or dropping samples when increasing
or decreasing pitch by octaves is possible, however, as pointed out,
there is a need to retain the original sample playback duration.
Another problem with simply adding or dropping samples is when the
amount of pitch-shift is not in exact multiples of octaves e.g.
dropping by 0.3 octaves.
Subject: Re: Source code for C++ pitch-shift function
From: athenawiles-ga on 10 Aug 2004 22:49 PDT
 
There might be a simpler way to do the pitch-shift, but I've read that
a lot of sound transformations involve using fourier series
(complicated calculus) to analyze the frequencies involved, 'break'
the sound up into separate frequencies & then manipulate frequencies
independent of time...  that would be a very complicated function,
though; I hope someone can come up with a simpler way for you to
perform the desired pitch-shift.  Good luck ~
Subject: Re: Source code for C++ pitch-shift function
From: pianoboy77-ga on 12 Aug 2004 08:54 PDT
 
Implementing a good pitch shifter indeed would be very complicated. I
didn't find a nice clean c++ function for you -- hopefully a Google
Researcher can; However, I've found some links with source code that
might help you in your quest to create a simple pitch shifter.

-------------------------------------------------------
Audio Signal Processing class final project

An attempt to implement simple time-stretching and pitch-shifting
algorithms in Matlab. This is a good read and contains Matlab source
code. The pitch shift function is only about a page long.

http://web.uvic.ca/~bmcconne/

Matlab source code (text file):
http://web.uvic.ca/~bmcconne/proj_main/PitchScaleSOLA.m

I don't know Matlab source, but it looks like once you figure out how
the : (colon) and . (dot) operators work, it's very C++-like. The only
tricky thing is that it looks like a lot of the math may be vector or
matrix arithmetic, which Matlab can handle concisely. You might need
some sort of matrix math library to accompish the same thing in C++ or
else expand statements into loops. For example, something like "v1 =
v1 + v2" in matlab, where v1 and v2 are vectors (basically arrays),
would require you in c++ to loop over the n elements of v1, and for
each one add the same indexed element from v2 to it, if you don't use
some matrix math library.

I only noticed 2 'non-standard' functions in the source: 'xcorr' and 'hanningz'.

The definition for the hanningz(n) function in Matlab source,
according to ://www.google.ca/search?q=cache:nPBunBTgDOoJ:userportal.iha.dk/~shn/dafx/CH07/HANNINGZ.M+hanningz&hl=en
is:
w = .5*(1 - cos(2*pi*(0:n-1)'/(n)));

Unfortunately the xcorr function is much more complicated and uses
other complex matlab functions. If you can find some C++ source for
this function you'd be well on your way. I found something here:
http://astronomy.swin.edu.au/~pbourke/analysis/correlate/
But don't know if it's the same type of function.

----------------------------------------------------------

SOX - Sound eXchange - the "swiss army knife of sound processing programs".
http://sox.sourceforge.net/

This is an open source project. The source code for the pitch shift
function is in the downloadable source in the file 'pitch.c'.
The code is fairly complex and may require a bunch of custom
constructs they use for the projects, plus the author says the pitch
shift doesn't work that well... but hey, you may get some ideas.

----------------------------------------------------------

Also, maybe check out this book
http://phptr.com/title/0131791443

----------------------------------------------------------

Misc. Pitch Shift Overview
http://www.dspdimension.com/html/timepitch.html
----------------------------------------------------------

Hope you eventually find what you're looking for!
Subject: Re: Source code for C++ pitch-shift function
From: pianoboy77-ga on 13 Aug 2004 16:06 PDT
 
I may have found exactly what you're looking for! 
Go here:
http://www.dspdimension.com/start.html
and click on "downloads"

There is clean, well-documented c++ source for a pitch shift function,
which doesn't rely on any other 3rd party libraries or weird data
structures from a larger project or anything. It even seems to take
the inputs you want. Has a sample main.cpp file too and good
documentation. You should be able to just plop it in your source code
as is. You don't come along something this perfect very often. I have
no idea how well it works or what copyright issues might be involved,
or if you'll have issues with the source on your platform... Anyway, I
hope this saves you some time in writing your own!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy