0
0

All I am trying to do is record audio from a microphone and read the data from the buffers, but I dont even know where to start. I’ve tried editing the recording example, and searching through the forums, but I cannot find anything.

Please Help.

Thank you!

  • You must to post comments
0
0

The recording example works fine to show hooking up a microphone and having the sound come back out the speakers.

To copy the sound into a buffer, you can use Channel::getWaveData(). If you want to analyze the sound using FFT, you can use Channel::getSpectrum().

  • Rick
  • You must to post comments
0
0

Thanks for your help.

I am storing it into 256 bars (so I can graph it in excel), but i still feel as if I am reading nonsensical values.

Here’s my code:

[code:17shzxmr]

include <windows.h>

include <stdio.h>

include <conio.h>

include "../../api/inc/fmod.hpp"

include "../../api/inc/fmod_errors.h"

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
exit(-1);
}
}

if defined(WIN32) || defined(WATCOMC) || defined(_WIN32) || defined(WIN32)

#define __PACKED                         /* dummy */

else

#define __PACKED __attribute__((packed)) /* gcc packed */

endif

/*
[
[DESCRIPTION]
Writes out the contents of a record buffer to a file.

[PARAMETERS]

[RETURN_VALUE]
void

[REMARKS]

]
*/
void SaveToWav(FMOD::Sound *sound){
FILE *fp;
int channels, bits;
float rate;
void *ptr1, *ptr2;
unsigned int lenbytes, len1, len2;

if (!sound)
{
    return;
}

sound-&gt;getFormat  (0, 0, &amp;channels, &amp;bits);
sound-&gt;getDefaults(&amp;rate, 0, 0, 0);
sound-&gt;getLength  (&amp;lenbytes, FMOD_TIMEUNIT_PCMBYTES);

{
    #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
    #pragma pack(1)
    #endif

    /*
        WAV Structures
    */
    typedef struct
    {
        signed char id[4];
        int         size;
    } RiffChunk;

    struct
    {
        RiffChunk       chunk           __PACKED;
        unsigned short  wFormatTag      __PACKED;    /* format type  */
        unsigned short  nChannels       __PACKED;    /* number of channels (i.e. mono, stereo...)  */
        unsigned int    nSamplesPerSec  __PACKED;    /* sample rate  */
        unsigned int    nAvgBytesPerSec __PACKED;    /* for buffer estimation  */
        unsigned short  nBlockAlign     __PACKED;    /* block size of data  */
        unsigned short  wBitsPerSample  __PACKED;    /* number of bits per sample of mono data */
    } FmtChunk  = { {{'f','m','t',' '}, sizeof(FmtChunk) - sizeof(RiffChunk) }, 1, channels, (int)rate, (int)rate * channels * bits / 8, 1 * channels * bits / 8, bits } __PACKED;

    struct
    {
        RiffChunk   chunk;
    } DataChunk = { {{'d','a','t','a'}, lenbytes } };

    struct
    {
        RiffChunk   chunk;
        signed char rifftype[4];
    } WavHeader = { {{'R','I','F','F'}, sizeof(FmtChunk) + sizeof(RiffChunk) + lenbytes }, {'W','A','V','E'} };

    #if defined(WIN32) || defined(_WIN64) || defined(__WATCOMC__) || defined(_WIN32) || defined(__WIN32__)
    #pragma pack()
    #endif

    fp = fopen(&quot;record.wav&quot;, &quot;wb&quot;);

    /*
        Write out the WAV header.
    */
    fwrite(&amp;WavHeader, sizeof(WavHeader), 1, fp);
    fwrite(&amp;FmtChunk, sizeof(FmtChunk), 1, fp);
    fwrite(&amp;DataChunk, sizeof(DataChunk), 1, fp);

    /*
        Lock the sound to get access to the raw data.
    */
    sound-&gt;lock(0, lenbytes, &amp;ptr1, &amp;ptr2, &amp;len1, &amp;len2);

    /*
        Write it to disk.
    */
    fwrite(ptr1, len1, 1, fp);

    /*
        Unlock the sound to allow FMOD to use it again.
    */
    sound-&gt;unlock(ptr1, ptr2, len1, len2);

    fclose(fp);
}

}

int main(int argc, char *argv[])
{
FMOD::System *system = 0;
FMOD::Sound *sound = 0;
FMOD::Channel *channel = 0;
FMOD_RESULT result;
FMOD_CREATESOUNDEXINFO exinfo;
int recorddriver, numdrivers;
unsigned int version;

/*
    Create a System object and initialize.
*/
result = FMOD::System_Create(&amp;system);
ERRCHECK(result);

result = system-&gt;getVersion(&amp;version);
ERRCHECK(result);

if (version &lt; FMOD_VERSION)
{
    printf(&quot;Error!  You are using an old version of FMOD %08x.  This program requires %08x\n&quot;, version, FMOD_VERSION);
    return 0;
}

result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
ERRCHECK(result);

result = system-&gt;getNumDrivers(&amp;numdrivers);
ERRCHECK(result);
char name[256];
    result = system-&gt;getDriverInfo(0, name, 256, 0);
    ERRCHECK(result);

result = system-&gt;setDriver(0);
ERRCHECK(result);

/*
    Enumerate record devices
*/

result = system-&gt;getRecordNumDrivers(&amp;numdrivers);
ERRCHECK(result);

    result = system-&gt;getRecordDriverInfo(0, name, 256, 0);
    ERRCHECK(result);

recorddriver = 0;

printf(&quot;\n&quot;);

result = system-&gt;init(32, FMOD_INIT_NORMAL, 0);
ERRCHECK(result);

memset(&amp;exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));

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

float                  spectrum [256];
float                  wavedata [512];

result = system-&gt;createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &amp;exinfo, &amp;sound);
ERRCHECK(result);

 /*
    Main loop.
*/

FILE* fp2 = fopen("FFT.txt","w");

    static bool  looping    = false;
    bool         recording  = false;
    bool         playing    = false;
    unsigned int recordpos = 0;
    unsigned int playpos = 0;
    unsigned int length;
    int          i;

while(1)
{
result = system->recordStart(recorddriver, sound, looping);
ERRCHECK(result);
Sleep(100);
result = system->playSound(FMOD_CHANNEL_REUSE, sound, 0, &channel);
ERRCHECK( result);
//channel->setVolume(0);

result = system->getWaveData(wavedata,512,0);
ERRCHECK(result);

result = system->getSpectrum(spectrum,256, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE);
ERRCHECK(result);

    for(i=0;i&lt;256;i++)
    {
        printf(&quot;%f\n&quot;, spectrum[i]);
        fprintf(fp2,&quot;%f\t&quot;,spectrum[i]);
    }

//Sleep(200);
fprintf(fp2,"\n");
}

/*
    Shut down
*/
result = sound-&gt;release();
ERRCHECK(result);

result = system-&gt;release();
ERRCHECK(result);

return 0;

}

[/code:17shzxmr]

Also, after a while, it just stops working, and I need to restart my computer and/or re-install drivers…is this a known issue?

  • You must to post comments
0
0

You should also be aware that you need to be careful that the read and write heads don’t overlap. Since when you use the microphone, you’re both writing and reading to the same buffer, you want to make sure the writing is ahead of the reading. If you’re recording for a long time these can drift and that might be where things are going awry for you.

Check out this thread where Mathew explains how to check the record and read position in the buffer:
http://52.88.2.202/forum/viewtopic.php?t=13220

-Pete

  • You must to post comments
0
0

Just in case anybody is interested/looking for a simple recording/FFT program, I was able to get it to work. The problem was that I wasn’t locking the sound after i started recording, and unlocking it after I finished analyzing. Anyway…here you go…

[code:2xkhebqw]

include <windows.h>

include <stdio.h>

include <conio.h>

include "../../api/inc/fmod.hpp"

include "../../api/inc/fmod_errors.h"

void ERRCHECK(FMOD_RESULT result)
{
if (result != FMOD_OK)
{
printf("FMOD error! (%d) %s\n", result, FMOD_ErrorString(result));
Sleep(5000);
exit(-1);
}
}

if defined(WIN32) || defined(WATCOMC) || defined(_WIN32) || defined(WIN32)

#define __PACKED                         // dummy

else

#define __PACKED __attribute__((packed)) // gcc packed

endif

//[
// [DESCRIPTION]
// Writes out the contents of a record buffer to a file.
//
// [PARAMETERS]
//
// [RETURN_VALUE]
// void
//
// [REMARKS]
//]

int main(int argc, char *argv[])
{
FMOD::System *system = 0;
FMOD::Sound *sound = 0;
FMOD::Channel *channel = 0;
FMOD_RESULT result;
FMOD_CREATESOUNDEXINFO exinfo;
int recorddriver, numdrivers;
unsigned int version;

//  Create a System object and initialize.

result = FMOD::System_Create(&amp;system);
ERRCHECK(result);

result = system-&gt;getVersion(&amp;version);
ERRCHECK(result);

if (version &lt; FMOD_VERSION)
{
    printf(&quot;Error!  You are using an old version of FMOD %08x.  This program requires %08x\n&quot;, version, FMOD_VERSION);
    return 0;
}

result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
ERRCHECK(result);

result = system-&gt;getNumDrivers(&amp;numdrivers);
ERRCHECK(result);
char name[256];
    result = system-&gt;getDriverInfo(0, name, 256, 0);
    ERRCHECK(result);

result = system-&gt;setDriver(0);
ERRCHECK(result);


//  Enumerate record devices


result = system-&gt;getRecordNumDrivers(&amp;numdrivers);
ERRCHECK(result);

    result = system-&gt;getRecordDriverInfo(0, name, 256, 0);
    ERRCHECK(result);

recorddriver = 0;

printf(&quot;\n&quot;);

result = system-&gt;init(32, FMOD_INIT_NORMAL, 0);
ERRCHECK(result);

memset(&amp;exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));

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



float                   spectrum [64];
float                   wavedata [64];


result = system-&gt;createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &amp;exinfo, &amp;sound);
ERRCHECK(result);


//  Main loop.

FILE* fp2 = fopen("FFT.txt","w");

    static bool  looping    = false;
    bool         recording  = false;
    bool         playing    = false;
    //unsigned int* readpos, recpos;
    unsigned int length;
    int          i,j,barsize;

result = system->recordStart(recorddriver, sound, looping);
ERRCHECK(result);
Sleep(1000);
system->update();

while(1)
{
result = system->recordStart(recorddriver, sound, looping);
ERRCHECK(result);
Sleep(100);
system->update();

unsigned int len;
sound->getLength( &len, FMOD_TIMEUNIT_MS );
channel->setPosition( 50, FMOD_TIMEUNIT_MS );
Sleep(50);
system->update();

//channel->getPosition(readpos,FMOD_TIMEUNIT_PCM);
//system->getRecordPosition(0,&recpos);

result = system->playSound(FMOD_CHANNEL_REUSE, sound, 0, &channel);
ERRCHECK( result);
channel->setVolume(1);
system->update();

void *ptr1, *ptr2;
unsigned int lenbytes, len1, len2;

sound->getLength (&lenbytes, FMOD_TIMEUNIT_PCMBYTES);
sound->lock(0, lenbytes, &ptr1, &ptr2, &len1, &len2);

//system->getWaveData(wavedata,64,0);
//ERRCHECK(result);

system->getSpectrum(spectrum,64, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE);

    for(i=4;i&lt;42;i+=2) //cut off the first few bars due to noise, and the last 24, which are too high to hear
    {
        barsize = (int) (1000000*((spectrum[i]+spectrum[i+1])/2));
        //printf(&quot;%d\n&quot;, (int) (1000000*((spectrum[i]+spectrum[i+1])/2)));
        for (j=min(37500,barsize);j&gt;=0;j-=500)//print as bars
        {
            printf(&quot;]&quot;);
        }
        printf(&quot;\n&quot;);
        fprintf(fp2,&quot;%f\t%f\t&quot;,spectrum[i],spectrum[i+1]);
    }
        printf(&quot;\n\n\n\n\n&quot;);


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

fprintf(fp2,"\n");

}

//  Shut down

result = sound-&gt;release();
ERRCHECK(result);

result = system-&gt;release();
ERRCHECK(result);

return 0;

}

[/code:2xkhebqw]

Hope this helps.

  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.