# Pitch detection and dsp

**URL:** https://qa.fmod.com/t/pitch-detection-and-dsp/12095
**Category:** FMOD Engine
**Created:** [October 16, 2015, 11:04am UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095 "2015-10-16T11:04:33Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Keitel](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Keitel](https://qa.fmod.com/u/Keitel)
#### Post date: [October 16, 2015, 11:04am UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/1 "2015-10-16T11:04:33Z")

</div>

I want to work with the pitch detection program that’s in Fmod’s example folder. However, the example is with the spectrum command which is obsolete in Fmod5. In the manual it is written that one should use dsp instead, but it seems to be more complicated, and I find it difficult to understand how to convert the pitch detection example program using dsp. Also, I am programming in C, and one of the commands needed is only for C++. I therefore wonder what the advantages actually are for using the dsp instead of the spectrum command, or if I just as well should run the example program with Fmodex.

Thanks in advance.

---

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [October 17, 2015, 10:40pm UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/2 "2015-10-17T22:40:58Z")

</div>

None of the commands in FMOD are C++ only, they all have C equivalents. Compare fmod.h vs fmod.hpp

The FMOD5 DSP method is probably easier, as all you have to do is

1. create the FFT dsp
2. use addDSP to add it to a channel or bus (channelgroup).
3. use DSP::getParameter with FMOD\_DSP\_FFT\_DOMINANT\_FREQ …

… and it just tells you which is the dominant frequency with a single floating point value, you dont need to calculate it yourself.

---

<div class="post-metadata">

### Author: ![Keitel](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Keitel](https://qa.fmod.com/u/Keitel)
#### Post date: [October 18, 2015, 3:24pm UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/3 "2015-10-18T15:24:43Z")

</div>

Thank you for your answer. I have tried to make the dsp solution work, but I must have misunderstood something, as it crashes on the GetParameterFloat command. It would be great if you could take a look at it. Here is what I did:

FMOD\_DSP \*mydsp;  
FMOD\_CHANNELGROUP \*channelgroup;  
int index = 0;  
int \*numparams = 0;  
char \*valuestring = “0”;  
int valuelength=0;  
int going = 1;  
float \*frq = 0;

result = FMOD\_System\_RecordStart(system, recorddriver, sound, TRUE);  
ERRCHECK(result);  
Sleep(200);  
result = FMOD\_System\_PlaySound(system, sound, NULL, 0, &channel);  
ERRCHECK(result);  
result = FMOD\_Channel\_SetVolume(channel, 0);  
ERRCHECK(result);  
result = FMOD\_System\_GetMasterChannelGroup(system, &channelgroup);  
result = FMOD\_System\_CreateDSPByType(system, FMOD\_DSP\_TYPE\_FFT, &mydsp);  
result = FMOD\_ChannelGroup\_AddDSP(channelgroup, FMOD\_CHANNELCONTROL\_DSP\_TAIL, mydsp);  
ERRCHECK(result);

while(going){  
if (\_kbhit()) going = 0;

result = FMOD\_DSP\_GetParameterFloat(mydsp, \*numparams, frq, valuestring, valuelength);

ERRCHECK(result);

printf(“Detected frequency : %f”, frq );

Sleep(200);

FMOD\_System\_Update(system);

}

Thanks in advance.

---

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [October 19, 2015, 3:57am UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/4 "2015-10-19T03:57:32Z")

</div>

> [@](#):
>
> Thank you for your answer. I have tried to make the dsp solution work, but I must have misunderstood something, as it crashes on the GetParameterFloat command. It would be great if you could take a look at it. Here is what I did:
> 
> FMOD\_DSP \*mydsp;  
> FMOD\_CHANNELGROUP \*channelgroup;  
> int index = 0;  
> int \*numparams = 0;  
> char \*valuestring = “0”;  
> int valuelength=0;  
> int going = 1;  
> float \*frq = 0;
> 
> result = FMOD\_System\_RecordStart(system, recorddriver, sound, TRUE);  
> ERRCHECK(result);  
> Sleep(200);  
> result = FMOD\_System\_PlaySound(system, sound, NULL, 0, &channel);  
> ERRCHECK(result);  
> result = FMOD\_Channel\_SetVolume(channel, 0);  
> ERRCHECK(result);  
> result = FMOD\_System\_GetMasterChannelGroup(system, &channelgroup);  
> result = FMOD\_System\_CreateDSPByType(system, FMOD\_DSP\_TYPE\_FFT, &mydsp);  
> result = FMOD\_ChannelGroup\_AddDSP(channelgroup, FMOD\_CHANNELCONTROL\_DSP\_TAIL, mydsp);  
> ERRCHECK(result);
> 
> while(going){  
> if (\_kbhit()) going = 0;
> 
> result = FMOD\_DSP\_GetParameterFloat(mydsp, \*numparams, frq, valuestring, valuelength);
> 
> ERRCHECK(result);
> 
> printf(“Detected frequency : %f”, frq );
> 
> Sleep(200);
> 
> FMOD\_System\_Update(system);
> 
> }
> 
> Thanks in advance.

int \*numparams = 0; followed by \*numparams.  
You are dereferencing a null pointer, so it is crashing on your own code not FMOD.  
You seem to not have grasped what a pointer is yet, I would stop where you are and learn about this first. You can’t just use null pointers an pass memory to fmod that consists of 1 or bytes (ie the “0”) when it should be much more than that.

You also don’t reference the thing that you’re wanting to query, which I said you should use, which is FMOD\_DSP\_FFT\_DOMINANT\_FREQ, so it should be FMOD\_DSP\_GetParameterFloat(mydsp, FMOD\_DSP\_FFT\_DOMINANT\_FREQ, &frq, 0, 0); but you still have to realize that you should be using real values not pointers.

---

<div class="post-metadata">

### Author: ![Keitel](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Keitel](https://qa.fmod.com/u/Keitel)
#### Post date: [October 21, 2015, 11:14am UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/5 "2015-10-21T11:14:29Z")

</div>

Thank you very much. Now it seems to work. However, I don’t understand what exactly is being measured. Is it the pitch of the moment of the measuring, or is it an average pitch for a certain time span, and how can I possibly measure the average pitch for different time spans?

Sincerely

---

<div class="post-metadata">

### Author: ![brett](https://yyz2.discourse-cdn.com/flex036/user_avatar/qa.fmod.com/brett/32/261_2.png) [@brett](https://qa.fmod.com/u/brett)
#### Post date: [October 22, 2015, 12:55am UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/6 "2015-10-22T00:55:32Z")

</div>

> [@](#):
>
> Thank you very much. Now it seems to work. However, I don’t understand what exactly is being measured. Is it the pitch of the moment of the measuring, or is it an average pitch for a certain time span, and how can I possibly measure the average pitch for different time spans?
> 
> Sincerely

the frequency returned is the frequency at the time of calling, you could keep a history yourself from calling it regularly if you wanted to build up an average over time.

---

<div class="post-metadata">

### Author: ![Keitel](https://avatars.discourse-cdn.com/v4/letter/k/e5b9ba/32.png) [@Keitel](https://qa.fmod.com/u/Keitel)
#### Post date: [October 22, 2015, 10:29am UTC](https://qa.fmod.com/t/pitch-detection-and-dsp/12095/7 "2015-10-22T10:29:18Z")

</div>

Ok. I understand. Thank you for your support.

Sincerely
