0
0

Currently, I have perfect realtime playback of the microphone input in realtime. However, when I play back the sound from a saved wav file, it is horrendously scratchy. Am I missing something fundamental about the lock/unlock? I am including the code for my WinMain loop which is pretty much the chunk of it. Most code is referenced from examples (thanks brett!)

EDIT: Here is what the code is supposed to be doing: It is buffering 5 seconds of raw audio data in a buffer, to save it to a .wav file. Notice the redundant of rewriting data on the (else of the (pos < lastpos) check), this is just to get it working at first.

If the recordPosition has wrapped, then it takes the last chunk of the sound since the last loop iteration and appends it to the buffer and writes the file. Right now, very incorrectly. The header seems to be quite valid but the length of the sound in iTunes is only listed as 0:02 instead of the correct 0:05. What am I missing?!

buffer is a char array of SOUND_LENGTH * 44100;
SOUND_LENGTH is #define of the number of seconds to record (5 in this test case)

audio is a weak wrapper right now for FMOD. I initialize it before the loop with

[code:1xbadla7]
result = FMOD::System_Create(&system);
result = system->getVersion(&version);
result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
result = system->getNumDrivers(&numdrivers);
result = system->setDriver(0);
result = system->setRecordDriver(0);
result = system->init(32, FMOD_INIT_NORMAL, 0);
[/code:1xbadla7]

now on to the main loop of my broken program:

[code:1xbadla7]
while (TRUE)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// Test if this is a quit
if (msg.message == WM_QUIT)
break;

        //  Translate any accelerator keys
        TranslateMessage(&amp;msg);

        //  Send the message to the window proc
        DispatchMessage(&amp;msg);
    }

    bool isRec = false;

    audio.GetSystem()-&gt;isRecording(&amp;isRec);
    UINT pos = 0;
    audio.GetSystem()-&gt;getRecordPosition(&amp;pos);

    if (isRec)
    {
        if (!bPlay)
        {
        Sleep(100);  
        sound-&gt;setMode(FMOD_LOOP_NORMAL);
        audio.GetSystem()-&gt;playSound(FMOD_CHANNEL_REUSE, sound, false, &amp;channel);
        bPlay = true;
        }

        if (pos &lt; lastpos)
        {
            void *ptr1, *ptr2;
            unsigned int len1, len2;

            sound-&gt;lock(lastpos, (int(44100 * SOUND_LENGTH) - lastpos), &amp;ptr1, &amp;ptr2, &amp;len1, &amp;len2);   /* * exinfo.numchannels * 2 = stereo 16bit.  1 sample = 4 bytes. */
            memcpy(&amp;buffer[lastpos],ptr1,len1);
            sound-&gt;unlock(ptr1, ptr2, len1, len2);

         theFile = fopen(&quot;please.wav&quot;,&quot;w+&quot;);

         int channels;
         float rates;
         int bitss;
         sound-&gt;getFormat  (0, 0, &amp;channels, &amp;bitss);
         sound-&gt;getDefaults(&amp;rates, 0, 0, 0);

         sound-&gt;getLength(&amp;soundlength, FMOD_TIMEUNIT_PCM);
         WriteWavHeader(theFile,channels,bitss,rates,soundlength);
         fwrite(&amp;buffer[0],1,soundlength,theFile);
         fclose(theFile);

        }
        else 
        {
            // copy data into a buffer
            void *ptr1, *ptr2;
            unsigned int len1, len2;

            sound-&gt;lock(0, pos, &amp;ptr1, &amp;ptr2, &amp;len1, &amp;len2);  

            memcpy(buffer,ptr1,len1);

            sound-&gt;unlock(ptr1, ptr2, len1, len2);

        }
    }

    lastpos = pos;
    audio.GetSystem()-&gt;update();
}

[/code:1xbadla7]

Any and all help will be graciously accepted!

Brandon

  • You must to post comments
Showing 0 results
Your Answer

Please first to submit.