0
0

Hello,

I´m trying to write an application where sound from the microphone is constantly recorded and after a short while (50 ms) the sound should be played back automatically. I managed to get the recording and playback working, and I set a small pause – sleep(50) – between the recording and the playback statement (values below 45 ms caused a clicking sound). But I´m having a few problems:

1 – Even though I set a 50ms pause between recording and playback, I´m actually getting 150+ ms playback delay. Below is the portion of my code that deals with the record/playback functions.

2 – In Windows Vista I have to set the sound output to FMOD_OUTPUTTYPE_WINMM or else I get very distorted sound

3 – More noticeably in Windows Vista, I´m getting sort of a "ghost" delay where the sound keeps playing back many times, but increasing the time between playbacks and becoming fainter every time.

Any help will be appreciated, especially with the first issue.

//DEFINES

define OUTPUTRATE 22050

define SPECTRUMSIZE 2048

define SPECTRUMRANGE ((float)OUTPUTRATE / 2.0f) /* 0 to nyquist */

define BINSIZE (SPECTRUMRANGE / (float)SPECTRUMSIZE)

define WIDTH (int)256;

define OscWindowHeight (int)100;

//FMOD Global Variables
FMOD::System *gsystem = 0;
FMOD::Sound *sound = 0, *sound1 = 0;
FMOD::Channel *channel1 = 0, *channel2 = 0;
FMOD::DSP *dsppitch = 0, *dspecho = 0;
FMOD_RESULT result;
FMOD_CREATESOUNDEXINFO exinfo;
FILE *fp;
unsigned int version;
unsigned int datalength =0, soundlength;
int key, driver, numdrivers, count, bin;

//Initialize FMOD Sound System.

result = FMOD::System_Create(&gsystem);
ERRCHECK(result);

result = gsystem->getVersion(&version);
ERRCHECK(result);

if (version < FMOD_VERSION)
{
MessageBox(NULL,"Error! You are using an old version of FMOD ","Error", MB_OK | MB_ICONERROR);
return 0;
}

//Set Playback Driver
result=gsystem->setDriver(0);
ERRCHECK(result);

//Set Recording Driver
result=gsystem->setRecordDriver(0);
ERRCHECK(result);

//SetSoftwareFormat
result=gsystem->setSoftwareFormat(OUTPUTRATE, FMOD_SOUND_FORMAT_PCM16, 1, 0,
FMOD_DSP_RESAMPLER_LINEAR);
ERRCHECK(result);

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

//Create Recording Sound
memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));

exinfo.cbsize       = sizeof(FMOD_CREATESOUNDEXINFO);
exinfo.numchannels  = 1;
exinfo.format       = FMOD_SOUND_FORMAT_PCM16;
exinfo.defaultfrequency = OUTPUTRATE;
exinfo.length       = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 2;

result = gsystem->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER,
&exinfo, &sound1);
ERRCHECK(result);

//Start Recording
result = gsystem->recordStart(sound1, true);
ERRCHECK(result);

//Let the record buffer fill a little
Sleep(50);

//start Playback
result = gsystem->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel2);
ERRCHECK(result);

gsystem->update();

  • You must to post comments
0
0

I posted the almost exact same problem a while back. no answers. The only way I got it to work was to set it up exactly like the demo…

otherwise I have tic sounds and ghosts sound that was recorded 5 seconds prior…

[edit]

I forgot to mention; Do a system update right after creating the channel with the mic recording then set the channel position to 140/5000;

The default buffer size being 5 seconds, this is the closest position to the recording I was able to be without hearing cracking sounds.

  • You must to post comments
0
0

[quote] this is the closest position to the recording I was able to be without hearing cracking sounds.[/quote]
I saw a hint of this in one of the examples, but no real explanation of why ‘too close’ matters ?
I have what may be a similar ‘artifact’ issue – in my case it all works, but some frequencies are better than others.

  • You must to post comments
0
0

I´ve tried the suggestions posted but in my case they didn´t work. Right now I´m testing with different recording buffer sizes to see if it changes anything.

I would like to hear an opinion from someone from the FMOD development team (Brett, maybe?). Since issues posted after mine have already been replied, I´m beginning to think it´s more complicated than I thought.

Brett, I have one question about the code below. Is it guaranteed that the playback thread will be delayed exactly (or close to) 50 ms after the recording thread and that it will stay that way while to program is executing? Or is it possible that the recording and the playback pointers be altered by some factor?

//Start Recording
result = gsystem->recordStart(sound1, true);
ERRCHECK(result);
//Let the record buffer fill a little
Sleep(50);
//start Playback
result = gsystem->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel2);
ERRCHECK(result);

  • You must to post comments
0
0

What I´ve done to reduce the lag from 150ms to about 80ms:

-Update to Fmod 4.26

  • In startup routine, decrease DSP Buffer size

    result = gsystem->setDSPBufferSize(512, 5);

  • Start recording, wait for 40 ms, start playback and push the playback cursor forward

    result = gsystem->recordStart(0,sound1, true);
    ERRCHECK(result);
    gsystem->update();
    Sleep(40);

    result = gsystem->playSound(FMOD_CHANNEL_FREE, sound1, false, &channel2);

    channel2->getPosition(&playbackpos, FMOD_TIMEUNIT_PCM);
    channel2->setPosition(playbackpos + 1200, FMOD_TIMEUNIT_PCM);

    gsystem->update();

I´m monitoring the time difference between the recording and playback cursors, and it keeps varying between 5 and 35ms. Since I´m getting around 80 ms total lag I guess there must be some uknown overhead in Fmod.

Also, when I reduce the time difference (setPosition + "x" PCM Samples) I start to get cracking sounds.

My last test will be to reduce the sampling rate to 22Khz and see if it helps in any way.

Any ideas are welcome.

  • You must to post comments
0
0

I did find when trying to measure Frequency that getting the read position too close to the write position was not good for disturbances.

In my case, I really don’t care about absolute delays, so avoiding this
‘too close’ artifact was tolerable.

Increasing the EXE priority helped a little in my case, so you could try that ?.

Any solution that reduces these ‘disturbances’ I am also interested in.

  • You must to post comments
Showing 5 results
Your Answer

Please first to submit.