0
0
  • Using c++ with vs2008…
  • Problem comes in my slightly modified ‘usergenerated sound’-example,
    which works just fine, so I think no initialization issues.

If I try to feed the data in the callback-function with an array, I get a click noise (with the rate set in channels->setfrequency). I don’t think that it’s my data, cause I just take the same sine-function as in the example and save it into my array.

Here’s the code:

[u:fgjp83h9]Declaration in ‘main.h'[/u:fgjp83h9]

[code:fgjp83h9]
signed short wave_data_gl[4096];
[/code:fgjp83h9]

[u:fgjp83h9]Filling the array[/u:fgjp83h9]

[code:fgjp83h9]
float t3=0;
for (int i =0 ;i<4096;i++) {
signed short a = sin(t3) *15000.0f;
wave_data_gl[i]=(signed short) (sin(t3) *15000.0f);
t3+=0.01f;
if (t3>=6.283185f) t3-=6.283185f;
}
[/code:fgjp83h9]

[u:fgjp83h9]Callback-Function[/u:fgjp83h9]

[code:fgjp83h9]
FMOD_RESULT F_CALLBACK pcmreadcallback_0(FMOD_SOUND *sound, void *data, unsigned int datalen)
{
unsigned int count;
signed short *stereo16bitbuffer = (signed short *)data;

for (count=0; count&lt;datalen&gt;&gt;2; count++)        // &gt;&gt;2 = 16bit stereo (4 bytes per sample)
{
    *stereo16bitbuffer++ = wave_data_gl[count];    // left channel
    *stereo16bitbuffer++ = wave_data_gl[count];    // right channel
}
return FMOD_OK;

}
[/code:fgjp83h9]

I also need some explanation about the code of the example. Could someone explain what’s happening in this line, please:

[code:fgjp83h9]
*stereo16bitbuffer++ = (signed short) (sin(t3) *15000.0f);
[/code:fgjp83h9]

I got the [i:fgjp83h9](sin(t3) *15000.0f)[/i:fgjp83h9]..

Thx for any help

  • You must to post comments
0
0

[quote:1fsl4sq5] buff = malloc(len); [/quote:1fsl4sq5]
I presume this means buff is a void**, you should make it a short
:

[code:1fsl4sq5]
short buff = NULL;
buff = (short
)malloc(len);
[/code:1fsl4sq5]

I also noticed there are two values for length ‘wave_data_len’ and ‘len’. The createsoundexinfo.length is the number of bytes and so is malloc so they should be the same value.

I noticed you’re using FMOD_HARDWARE sounds, if there is no specific reason for doing that I would recommend you instead use FMOD_SOFTWARE sounds.

I assume ‘magic_thing’ is filling the buffer with data. I agree with curious george, the function signature looks wrong for that one.

As george pointed out you would probably have better luck just using your array directly:
[code:1fsl4sq5]
result = system->createSound((const char *)data_array, mode, &createsoundexinfo, &sound); [/code:1fsl4sq5]

  • You must to post comments
0
0

The click is coming from your data. If you want the end of your data to stitch seemlessly with the start you need to make sure that you’re at the same point in the cycle of the sine wave.

in other words:
(4096 x 0.01) / (2 x pi) ~ 6.5

This means there is 6 and a half cycles of the sine function in your data. That half cycle at the end is causing the end of your data to misalign with the start. This extreme difference causes the click sound.

-Pete

  • You must to post comments
0
0

That’s what I thought.. but why does it work just fine if I calculate the sine right in the callback-function with the same code?

[code:2w37stus]
FMOD_RESULT F_CALLBACK pcmreadcallback_0(FMOD_SOUND *sound, void *data, unsigned int datalen)
{
unsigned int count;
static float t3 = 0
signed short *stereo16bitbuffer = (signed short *)data;

for (count=0; count<datalen>>2; count++) // >>2 = 16bit stereo (4 bytes per sample)
{
*stereo16bitbuffer++ = (signed short) (sin(t3) *15000.0f); // left channel
*stereo16bitbuffer++ = (signed short) (sin(t3) *15000.0f); // right channel
t3+=0.01f;
if (t3>=6.283185f) t3-=6.283185f;

}
return FMOD_OK;
} [/code:2w37stus]

No clicks with this one..

  • You must to post comments
0
0

That doesn’t click because t3 is not being reset. In that example it is a static variable which retains it’s value after the function returns. If you make t3 non-static then that is what your array is actually doing.

[code:7ygoduk3]FMOD_RESULT F_CALLBACK pcmreadcallback_0(FMOD_SOUND *sound, void *data, unsigned int datalen)
{
unsigned int count;
float t3 = 0;
signed short *stereo16bitbuffer = (signed short *)data;

for (count=0; count<datalen>>2; count++) // >>2 = 16bit stereo (4 bytes per sample)
{
*stereo16bitbuffer++ = (signed short) (sin(t3) *15000.0f); // left channel
*stereo16bitbuffer++ = (signed short) (sin(t3) *15000.0f); // right channel
t3+=0.01f;
if (t3>=6.283185f) t3-=6.283185f;

}
return FMOD_OK;
} [/code:7ygoduk3]

  • You must to post comments
0
0

Ok thank you, but now another problem occured:
I’ve recognized that the datalen is a bit unstable. In my program, it drops from 4095 down to 3140 (depending on the channels frequency). And with that drop, my data is chopped too what causes another click..
Is there a way to fix this or do I have to deal with those ‘buffer underruns’ ?

Thx for the help so far,

Streikbrecher

  • You must to post comments
0
0

Have you tried to just declare your buffer as a void * and then malloc the appropriate amount of bytes necessary to fill the buffer?

Don’t forget to free them when you’re done!

  • George
  • You must to post comments
0
0

[quote:3m0cmg5m]do I have to deal with those ‘buffer underruns'[/quote:3m0cmg5m]
It’s pretty much up to you to make sure you’re feeding FMOD the right data. You could make count a static variable so that you’re feeding FMOD continuous data.

Out of interest, why do you want the data to be in an array?

-Pete

  • You must to post comments
0
0

I dont have the click sound.

but now I notify sometimes there is vibration when I stopped the car and press the clutch, dont know if it is the clutch vibrate or the car?

  • You must to post comments
0
0

Ok, now the data is right and the clicks are gone.

But I realized that I rather load the data from memory than streaming it, and now I’m having trouble with that. I’ve already looked through the [b:1f73satd]loadfrommemory[/b:1f73satd]-example and some relating topics here, but I still can’t figure out how to get from my pcm-data ([b:1f73satd]signed short data_array[len][/b:1f73satd]) to the data I have to pass to [b:1f73satd]system->createSound[/b:1f73satd].

I know I should malloc and free the used part of the memory, but since I come from far VB-lands, I don’t know how to convert/adress my data properly..the mode and the exinfo should be right though:

[code:1f73satd]

FMOD::System           *system;
FMOD::Sound            *sound;
FMOD::Channel          *channel;
FMOD_RESULT             result;
FMOD_MODE               mode = FMOD_2D | FMOD_OPENMEMORY |  FMOD_OPENRAW | FMOD_LOOP_OFF | FMOD_HARDWARE; 
FMOD_CREATESOUNDEXINFO  createsoundexinfo;

result = FMOD::System_Create(&system);

result = system->init(32, FMOD_INIT_NORMAL, 0);

memset(&amp;createsoundexinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
createsoundexinfo.cbsize            = sizeof(FMOD_CREATESOUNDEXINFO);            
createsoundexinfo.length            = wave_data_len; 
createsoundexinfo.format            = FMOD_SOUND_FORMAT_PCM16;
createsoundexinfo.defaultfrequency  = 44000;
createsoundexinfo.numchannels       = 1;

*buff = malloc(len);
magic_thing(*buff,data_array[0],len);

result = system-&gt;createSound((const char *) buff, mode, &amp;createsoundexinfo, &amp;sound);

free(buff);

result = system-&gt;playSound(FMOD_CHANNEL_FREE, sound, false, &amp;channel);

[/code:1f73satd]

For there’s no example how to load just the data (and no file) from memory this could be useful for other c++ beginners.

Thanks in advance,

Streikbrecher

  • You must to post comments
0
0

The thing with createSound is that it will load the file off disk the first time and decompress it into memory, as opposed to createStream which will just open the file and then read data as is necessary.

Perhaps all you really need is createSound and none of the rest?

Edit: Ah, yes, sorry, forgot that you are generating all the sounds yourself! Though you could write them to file and have FMOD read them back again, but that would kind of be a waste. I recently did this, but with actual audio files.

I’ll take a look at my code and see if there is anything that might be helpful.

  • George
  • You must to post comments
0
0

Ok, I presume that the following is to fill the memory buffer with data:

[code:18jxrbcy]magic_thing(*buff,data_array[0],len);
[/code:18jxrbcy]
But, it doesn’t look like it’s being called correctly. It should look a little more like:

[code:18jxrbcy]magic_thing(&buff,data_array,len);
[/code:18jxrbcy]
And your declaration of the function would look a little like this:

[code:18jxrbcy]void magic_thing(void **buff, const char *data, int len);[/code:18jxrbcy]

Then your function would malloc the buff to the size of len, then do a memcpy between data to buff.

Although… you might just be able to use your data_array in createSound.

I hope this is somewhat helpful.

  • George
  • You must to post comments
Showing 11 results
Your Answer

Please first to submit.