Example #1
0
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                    key, driver, recorddriver, numdrivers, count;
    unsigned int           version;    

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

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

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

    /* 
        System initialization
    */
    printf("---------------------------------------------------------\n");    
    printf("Select OUTPUT type\n");    
    printf("---------------------------------------------------------\n");    
    printf("1 :  OSS        - Open Sound System\n");
    printf("2 :  ALSA       - Advanced Linux Sound Architecture\n");
    printf("3 :  ESD        - Enlightenment Sound Daemon\n");
    printf("4 :  PULSEAUDIO - Pulse Audio Sound Server\n");
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        key = getch();
    } while (key != 27 && key < '1' && key > '5');
    
    switch (key)
    {
        case '1' :  result = system->setOutput(FMOD_OUTPUTTYPE_OSS);
                    break;
        case '2' :  result = system->setOutput(FMOD_OUTPUTTYPE_ALSA);
                    break;
        case '3' :  result = system->setOutput(FMOD_OUTPUTTYPE_ESD);
                    break;
        case '4' :  result = system->setOutput(FMOD_OUTPUTTYPE_PULSEAUDIO);
                    break;
        default  :  return 1; 
    }  
    ERRCHECK(result);
    
    /*
        Enumerate playback devices
    */

    result = system->getNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a PLAYBACK driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        key = getch();
        if (key == 27)
        {
            return 0;
        }
        driver = key - '1';
    } while (driver < 0 || driver >= numdrivers);

    result = system->setDriver(driver);
    ERRCHECK(result);

    /*
        Enumerate record devices
    */

    result = system->getRecordNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a RECORD driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getRecordDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    recorddriver = 0;
    do
    {
        key = getch();
        if (key == 27)
        {
            return 0;
        }
        recorddriver = key - '1';
    } while (recorddriver < 0 || recorddriver >= numdrivers);

    printf("\n");
  
    result = system->init(32, FMOD_INIT_NORMAL, 0);
    ERRCHECK(result);

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

    exinfo.cbsize           = sizeof(FMOD_CREATESOUNDEXINFO);
    exinfo.numchannels      = 2;
    exinfo.format           = FMOD_SOUND_FORMAT_PCM16;
    exinfo.defaultfrequency = 44100;
    exinfo.length           = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5;
    
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &sound);
    ERRCHECK(result);

    printf("===================================================================\n");
    printf("Recording example.  Copyright (c) Firelight Technologies 2004-2011.\n");
    printf("===================================================================\n");
    printf("\n");
    printf("Press 'r' to record a 5 second segment of audio and write it to a wav file.\n");
    printf("Press 'p' to play the 5 second segment of audio.\n");
    printf("Press 'l' to turn looping on/off.\n");
    printf("Press 's' to stop recording and playback.\n");
    printf("Press 'w' to save the 5 second segment to a wav file.\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    /*
        Main loop.
    */
    do
    {
        static FMOD::Channel *channel = 0;
        static bool  looping    = false;
        bool         recording  = false;
        bool         playing    = false;
        unsigned int recordpos = 0;
        unsigned int playpos = 0;
        unsigned int length;

        if (kbhit())
        {
            key = getch();

            switch (key)
            {
                case 'r' :
                case 'R' :
                {
                    result = system->recordStart(recorddriver, sound, looping);
                    ERRCHECK(result);
                    break;
                }
                case 'p' :
                case 'P' :
                {
                    if (looping)
                    {
                        sound->setMode(FMOD_LOOP_NORMAL);
                    }
                    else
                    {
                        sound->setMode(FMOD_LOOP_OFF);
                    }
                    ERRCHECK(result);

                    result = system->playSound(FMOD_CHANNEL_REUSE, sound, false, &channel);
                    ERRCHECK(result);
                    break;
                }
                case 'l' :
                case 'L' :
                {
                    looping = !looping;
                    break;
                }
                case 's' :
                case 'S' :
                {
                    result = system->recordStop(recorddriver);
                    if (channel)
                    {
                        channel->stop();
                        channel = 0;
                    }
                    break;
                }
                case 'w' :
                case 'W' :
                {
                    printf("Writing to record.wav ...                                                     \r");

                    SaveToWav(sound);
                    Sleep(500);
                    break;
                }
            }
        }

        sound->getLength(&length, FMOD_TIMEUNIT_PCM);
        ERRCHECK(result);

        system->isRecording(recorddriver, &recording);
        ERRCHECK(result);

        system->getRecordPosition(recorddriver, &recordpos);
        ERRCHECK(result);

        if (channel)
        {
            channel->isPlaying(&playing);
            ERRCHECK(result);

            channel->getPosition(&playpos, FMOD_TIMEUNIT_PCM);
            ERRCHECK(result);
        }

        printf("State: %-19s. Record pos = %6d : Play pos = %6d : Loop %-3s\r", recording ? playing ? "Recording / playing" : "Recording" : playing ? "Playing" : "Idle", recordpos, playpos, looping ? "On" : "Off");
        fflush(stdout);

        system->update();

        fflush(stdout);
        Sleep(10);

    } while (key != 27);

    printf("\n");

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

    result = system->release();
    ERRCHECK(result);

    return 0;
}
int main(int argc, char *argv[])
{
    FMOD::System           *system;
    FMOD::Sound            *sound;
    FMOD::Channel          *channel = 0;
    FMOD_RESULT             result;
    FMOD_MODE               mode = FMOD_2D | FMOD_HARDWARE | FMOD_CREATESTREAM;
    unsigned int version;

    if (argc != 3) {
        std::cout << "unpacker.exe fsbPath outdirPath" << std::endl;
        return 1;
    }

    auto fsbPath = std::string(argv[1]);
    auto outPath = std::string(argv[2]);

    //fsbPath = "LoL_SFX_ziggs.fsb";
    //fsbPath = "LoL_SFX_karma_base.fsb";
    //fsbPath = "LoL_SFX_fiddlesticks.fsb";


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

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

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

    //system->setOutput(FMOD_OUTPUTTYPE_WAVWRITER);
    result = system->init(1, FMOD_INIT_NORMAL, 0);
    ERRCHECK(result);

    auto codecHandle = registerLeagueCodec(system, 50);

    int numsubsounds;
    FMOD::Sound *subSound = nullptr;
    char name[256];
    int soundNum = 0;

    try {
        result = system->createSound(fsbPath.c_str(), mode, nullptr, &sound);
        ERRCHECK(result);

        result = sound->getNumSubSounds(&numsubsounds);
        ERRCHECK(result);
        soundNum = 0;


        sound->getSubSound(0, &subSound);
        subSound->getName(name, 256);
        subSound->release();

        makePath(outPath.c_str());
        sound->release();
        system->close();
        system->release();

        std::set<std::string> writtenFiles;

        FMOD::Channel* channel;
        bool playing;
        for (int sndIdx = 0; sndIdx < numsubsounds; sndIdx++) {
            ERRCHECK(FMOD::System_Create(&system));
            ERRCHECK(system->getVersion(&version));
            system->setOutput(FMOD_OUTPUTTYPE_WAVWRITER_NRT);
            auto outFilePath = outPath + "\\" + std::string(name) + ".wav";
            if (writtenFiles.find(outFilePath) != writtenFiles.end()) {
                int cnt = 1;
                char arr[80];
                do {
                    _itoa_s(cnt, arr, 10);
                    outFilePath = outPath + "\\" + std::string(name) + "_" + std::string(arr) + ".wav";
                    cnt++;
                } while (writtenFiles.find(outFilePath) != writtenFiles.end());
            }
            writtenFiles.insert(outFilePath);
            ERRCHECK(system->init(1, FMOD_INIT_STREAM_FROM_UPDATE, (void*)outFilePath.c_str()));
            auto codecHandle = registerLeagueCodec(system, 50);
            system->createSound(fsbPath.c_str(), mode, nullptr, &sound);
            sound->getSubSound(sndIdx, &subSound);
            system->playSound(FMOD_CHANNEL_FREE, subSound, false, &channel);
            do {
                
                system->update();
                channel->isPlaying(&playing);
            } while (playing);
            subSound->release();


            if (sndIdx < numsubsounds - 1) {
                sound->getSubSound(sndIdx+1, &subSound);
                subSound->getName(name, 256);
                subSound->release();
                outFilePath = outPath + "\\" + std::string(name) + ".wav";
            }
            sound->release();
            system->close();
            system->release();
        }
    }
    catch (const std::runtime_error& error) {
        std::cout << "Exception caught:" << std::endl;
        std::cout << "   " << error.what() << std::endl;
    }

    return 0;
}
Example #3
0
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                    key, driver, recorddriver, numdrivers, count, outputfreq, bin;
    unsigned int           version;    

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

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

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

    /* 
        System initialization
    */
    printf("---------------------------------------------------------\n");    
    printf("Select OUTPUT type\n");    
    printf("---------------------------------------------------------\n");    
    printf("1 :  OSS        - Open Sound System\n");
    printf("2 :  ALSA       - Advanced Linux Sound Architecture\n");
    printf("3 :  ESD        - Enlightenment Sound Daemon\n");
    printf("4 :  PULSEAUDIO - Pulse Audio Sound Server\n");
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        key = getch();
    } while (key != 27 && key < '1' && key > '5');
    
    switch (key)
    {
        case '1' :  result = system->setOutput(FMOD_OUTPUTTYPE_OSS);
                    break;
        case '2' :  result = system->setOutput(FMOD_OUTPUTTYPE_ALSA);
                    break;
        case '3' :  result = system->setOutput(FMOD_OUTPUTTYPE_ESD);
                    break;
        case '4' :  result = system->setOutput(FMOD_OUTPUTTYPE_PULSEAUDIO);
                    break;                   
        default  :  return 1; 
    }  
    ERRCHECK(result);
    
    /*
        Enumerate playback devices
    */

    result = system->getNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a PLAYBACK driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        key = getch();
        if (key == 27)
        {
            return 0;
        }
        driver = key - '1';
    } while (driver < 0 || driver >= numdrivers);

    result = system->setDriver(driver);
    ERRCHECK(result);

    /*
        Enumerate record devices
    */

    result = system->getRecordNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a RECORD driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getRecordDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    recorddriver = 0;
    do
    {
        key = getch();
        if (key == 27)
        {
            return 0;
        }
        recorddriver = key - '1';
    } while (recorddriver < 0 || recorddriver >= numdrivers);

    printf("\n");
 
    result = system->setSoftwareFormat(OUTPUTRATE, FMOD_SOUND_FORMAT_PCM16, 1, 0, FMOD_DSP_RESAMPLER_LINEAR);
    ERRCHECK(result);

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

    system->getSoftwareFormat(&outputfreq, 0, 0, 0, 0, 0);
    ERRCHECK(result);

    /*
        Create a sound to record to.
    */
    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 * 5;
    
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &exinfo, &sound);
    ERRCHECK(result);

    /*
        Start the interface
    */
    printf("=========================================================================\n");
    printf("Pitch detection example.  Copyright (c) Firelight Technologies 2004-2014.\n");
    printf("=========================================================================\n");
    printf("\n");
    printf("Record something through the selected recording device and FMOD will\n");
    printf("Determine the pitch.  Sustain the tone for at least a second to get an\n");
    printf("accurate reading.\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    result = system->recordStart(recorddriver, sound, true);
    ERRCHECK(result);
    
    Sleep(200);      /* Give it some time to record something */
    
    result = system->playSound(FMOD_CHANNEL_REUSE, sound, false, &channel);
    ERRCHECK(result);

    /* Dont hear what is being recorded otherwise it will feedback.  Spectrum analysis is done before volume scaling in the DSP chain */
    result = channel->setVolume(0);
    ERRCHECK(result);

    bin = 0;

    /*
        Main loop.
    */
    do
    {
        static float spectrum[SPECTRUMSIZE];
        float        dominanthz = 0;
        float        max;
        int          dominantnote = 0;
        float        binsize = BINSIZE;

        if (kbhit())
        {
            key = getch();
        }

        result = channel->getSpectrum(spectrum, SPECTRUMSIZE, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE);
        ERRCHECK(result);

        max = 0;

        for (count = 0; count < SPECTRUMSIZE; count++)
        {
            if (spectrum[count] > 0.01f && spectrum[count] > max)
            {
                max = spectrum[count];
                bin = count;
            }
        }        

        dominanthz  = (float)bin * BINSIZE;       /* dominant frequency min */

        dominantnote = 0;
        for (count = 0; count < 120; count++)
        {
             if (dominanthz >= notefreq[count] && dominanthz < notefreq[count + 1])
             {
                /* which is it closer to.  This note or the next note */
                if (fabs(dominanthz - notefreq[count]) < fabs(dominanthz - notefreq[count+1]))
                {
                    dominantnote = count;
                }
                else
                {
                    dominantnote = count + 1;
                }
                break;
             }
        }

        printf("Detected rate : %7.1f -> %7.1f hz.  Detected musical note. %-3s (%7.1f hz)\r", dominanthz, ((float)bin + 0.99f) * BINSIZE, note[dominantnote], notefreq[dominantnote]);
        fflush(stdout);

        system->update();

        Sleep(10);

    } while (key != 27);

    printf("\n");

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

    result = system->release();
    ERRCHECK(result);

    return 0;
}
Example #4
0
int main(int argc, char *argv[])
{
    FMOD::System     *system;
    FMOD::Sound      *sound1, *sound2, *sound3;
    FMOD::Channel    *channel = 0;
    FMOD_RESULT       result;
    int               key;
    unsigned int      version;

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

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

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

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

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

    result = system->createSound("../media/drumloop.wav", FMOD_SOFTWARE, 0, &sound1);
    ERRCHECK(result);
    result = sound1->setMode(FMOD_LOOP_OFF);
    ERRCHECK(result);

    result = system->createSound("../media/jaguar.wav", FMOD_SOFTWARE, 0, &sound2);
    ERRCHECK(result);

    result = system->createSound("../media/swish.wav", FMOD_SOFTWARE, 0, &sound3);
    ERRCHECK(result);

    printf("===================================================================\n");
    printf("PlaySound Example.  Copyright (c) Firelight Technologies 2004-2014.\n");
    printf("===================================================================\n");
    printf("\n");
    printf("Press '1' to Play a mono sound using software mixing\n");
    printf("Press '2' to Play a mono sound using software mixing\n");
    printf("Press '3' to Play a stereo sound using software mixing\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    /*
        Main loop.
    */
    do
    {
        if (kbhit())
        {
            key = getch();

            switch (key)
            {
                case '1' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, 0, &channel);
                    ERRCHECK(result);
                    break;
                }
                case '2' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound2, 0, &channel);
                    ERRCHECK(result);
                    break;
                }
                case '3' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound3, 0, &channel);
                    ERRCHECK(result);
                    break;
                }
            }
        }

        system->update();

        {
            unsigned int ms = 0;
            unsigned int lenms = 0;
            bool         playing = 0;
            bool         paused = 0;
            int          channelsplaying = 0;

            if (channel)
            {
                FMOD::Sound *currentsound = 0;

                result = channel->isPlaying(&playing);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel->getPaused(&paused);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }
               
                channel->getCurrentSound(&currentsound);
                if (currentsound)
                {
                    result = currentsound->getLength(&lenms, FMOD_TIMEUNIT_MS);
                    if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                    {
                        ERRCHECK(result);
                    }
                }
            }

            system->getChannelsPlaying(&channelsplaying);

            printf("Time %02d:%02d:%02d/%02d:%02d:%02d : %s : Channels Playing %2d\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped", channelsplaying);
            fflush(stdout);
        }

        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Shut down
    */
    result = sound1->release();
    ERRCHECK(result);
    result = sound2->release();
    ERRCHECK(result);
    result = sound3->release();
    ERRCHECK(result);
    result = system->close();
    ERRCHECK(result);
    result = system->release();
    ERRCHECK(result);

    return 0;
}
Example #5
0
int main(int argc, char *argv[])
{
    FMOD::System    *system;
    FMOD::Sound     *sound1, *sound2, *sound3;
    FMOD::Channel   *channel1 = 0, *channel2 = 0, *channel3 = 0;
    FMOD_RESULT      result;
    int              key, numdrivers;
    bool             listenerflag = true;
    FMOD_VECTOR      listenerpos  = { 0.0f, 0.0f, -1.0f * DISTANCEFACTOR };
    unsigned int     version;
    FMOD_SPEAKERMODE speakermode;
    FMOD_CAPS        caps;
    char             name[256];

    printf("===============================================================\n");
    printf("3d Example.  Copyright (c) Firelight Technologies 2004-2015.\n");
    printf("===============================================================\n");
    printf("This example plays 2 3D sounds in hardware.  Optionally you can\n");
    printf("play a 2D hardware sound as well.\n");
    printf("===============================================================\n\n");

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

    if (version < FMOD_VERSION)
    {
        printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
        return 0;
    }
    
    result = system->getNumDrivers(&numdrivers);
    ERRCHECK(result);

    if (numdrivers == 0)
    {
        result = system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);
        ERRCHECK(result);
    }
    else
    {
        result = system->getDriverCaps(0, &caps, 0, &speakermode);
        ERRCHECK(result);

        result = system->setSpeakerMode(speakermode);       /* Set the user selected speaker mode. */
        ERRCHECK(result);

        if (caps & FMOD_CAPS_HARDWARE_EMULATED)             /* The user has the 'Acceleration' slider set to off!  This is really bad for latency!. */
        {                                                   /* You might want to warn the user about this. */
            result = system->setDSPBufferSize(1024, 10);
            ERRCHECK(result);
        }

        result = system->getDriverInfo(0, name, 256, 0);
        ERRCHECK(result);

        if (strstr(name, "SigmaTel"))   /* Sigmatel sound devices crackle for some reason if the format is PCM 16bit.  PCM floating point output seems to solve it. */
        {
            result = system->setSoftwareFormat(48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
            ERRCHECK(result);
        }
    }

    result = system->init(100, FMOD_INIT_NORMAL, 0);
    if (result == FMOD_ERR_OUTPUT_CREATEBUFFER)         /* Ok, the speaker mode selected isn't supported by this soundcard.  Switch it back to stereo... */
    {
        result = system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
        ERRCHECK(result);
            
        result = system->init(100, FMOD_INIT_NORMAL, 0);/* ... and re-init. */
        ERRCHECK(result);
    }

    
    /*
        Set the distance units. (meters/feet etc).
    */
    result = system->set3DSettings(1.0, DISTANCEFACTOR, 1.0f);
    ERRCHECK(result);

    /*
        Load some sounds
    */
    result = system->createSound("../media/drumloop.wav", FMOD_3D, 0, &sound1);
    ERRCHECK(result);
    result = sound1->set3DMinMaxDistance(0.5f * DISTANCEFACTOR, 5000.0f * DISTANCEFACTOR);
    ERRCHECK(result);
    result = sound1->setMode(FMOD_LOOP_NORMAL);
    ERRCHECK(result);

    result = system->createSound("../media/jaguar.wav", FMOD_3D, 0, &sound2);
    ERRCHECK(result);
    result = sound2->set3DMinMaxDistance(0.5f * DISTANCEFACTOR, 5000.0f * DISTANCEFACTOR);
    ERRCHECK(result);
    result = sound2->setMode(FMOD_LOOP_NORMAL);
    ERRCHECK(result);

    result = system->createSound("../media/swish.wav", FMOD_SOFTWARE | FMOD_2D, 0, &sound3);
    ERRCHECK(result);

    /*
        Play sounds at certain positions
    */
    {
        FMOD_VECTOR pos = { -10.0f * DISTANCEFACTOR, 0.0f, 0.0f };
        FMOD_VECTOR vel = {  0.0f, 0.0f, 0.0f };

        result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel1);
        ERRCHECK(result);
        result = channel1->set3DAttributes(&pos, &vel);
        ERRCHECK(result);
        result = channel1->setPaused(false);
        ERRCHECK(result);
    }

    {
        FMOD_VECTOR pos = { 15.0f * DISTANCEFACTOR, 0.0f, 0.0f };
        FMOD_VECTOR vel = { 0.0f, 0.0f, 0.0f };

        result = system->playSound(FMOD_CHANNEL_FREE, sound2, true, &channel2);
        ERRCHECK(result);
        result = channel2->set3DAttributes(&pos, &vel);
        ERRCHECK(result);
        result = channel2->setPaused(false);
        ERRCHECK(result);
    }

    /*
        Display help
    */
    {
        int numchannels;

        result = system->getHardwareChannels(&numchannels);
        ERRCHECK(result);
    
        printf("Hardware channels : %d\n", numchannels);
    }

    printf("=========================================================================\n");
    printf("Press 1        Pause/Unpause 16bit 3D sound at any time\n");
    printf("      2        Pause/Unpause 8bit 3D sound at any time\n");
    printf("      3        Play 16bit STEREO 2D sound at any time\n");
    printf("      <        Move listener left (in still mode)\n");
    printf("      >        Move listener right (in still mode)\n");
    printf("      SPACE    Stop/Start listener automatic movement\n");
    printf("      ESC      Quit\n");
    printf("=========================================================================\n");

    /*
        Main loop
    */
    do
    {
        if (_kbhit())
        {
            key = _getch();

            if (key == '1') 
            {
                bool paused;
                channel1->getPaused(&paused);
                channel1->setPaused(!paused);
            }

            if (key == '2') 
            {
                bool paused;
                channel2->getPaused(&paused);
                channel2->setPaused(!paused);
            }

            if (key == '3') 
            {
                result = system->playSound(FMOD_CHANNEL_FREE, sound3, false, &channel3);
                ERRCHECK(result);
            }

            if (key == ' ')
            {
                listenerflag = !listenerflag;
            }

            if (!listenerflag)
            {
                if (key == '<') 
                {
                    listenerpos.x -= 1.0f * DISTANCEFACTOR;
                    if (listenerpos.x < -35 * DISTANCEFACTOR)
                    {
                        listenerpos.x = -35 * DISTANCEFACTOR;
                    }
                }
                if (key == '>') 
                {
                    listenerpos.x += 1.0f * DISTANCEFACTOR;
                    if (listenerpos.x > 36 * DISTANCEFACTOR)
                    {
                        listenerpos.x = 36 * DISTANCEFACTOR;
                    }
                }
            }
        }

        // ==========================================================================================
        // UPDATE THE LISTENER
        // ==========================================================================================
        {
            static float t = 0;
            static FMOD_VECTOR lastpos = { 0.0f, 0.0f, 0.0f };
            FMOD_VECTOR forward        = { 0.0f, 0.0f, 1.0f };
            FMOD_VECTOR up             = { 0.0f, 1.0f, 0.0f };
            FMOD_VECTOR vel;

            if (listenerflag)
            {
                listenerpos.x = (float)sin(t * 0.05f) * 33.0f * DISTANCEFACTOR; // left right pingpong
            }

            // ********* NOTE ******* READ NEXT COMMENT!!!!!
            // vel = how far we moved last FRAME (m/f), then time compensate it to SECONDS (m/s).
            vel.x = (listenerpos.x - lastpos.x) * (1000 / INTERFACE_UPDATETIME);
            vel.y = (listenerpos.y - lastpos.y) * (1000 / INTERFACE_UPDATETIME);
            vel.z = (listenerpos.z - lastpos.z) * (1000 / INTERFACE_UPDATETIME);

            // store pos for next time
            lastpos = listenerpos;

            result = system->set3DListenerAttributes(0, &listenerpos, &vel, &forward, &up);
            ERRCHECK(result);

            t += (30 * (1.0f / (float)INTERFACE_UPDATETIME));    // t is just a time value .. it increments in 30m/s steps in this example

            // print out a small visual display
            {
                char s[80];

                sprintf(s, "|.......................<1>......................<2>....................|");

                s[(int)(listenerpos.x / DISTANCEFACTOR) + 35] = 'L';
                printf("%s\r", s);
            }
        }

        system->update();

        Sleep(INTERFACE_UPDATETIME - 1);

    } while (key != 27);

    printf("\n");

    /*
        Shut down
    */
    result = sound1->release();
    ERRCHECK(result);
    result = sound2->release();
    ERRCHECK(result);
    result = sound3->release();
    ERRCHECK(result);

    result = system->close();
    ERRCHECK(result);
    result = system->release();
    ERRCHECK(result);

    return 0;
}
Example #6
0
int main(int argc, char *argv[])
{
    FMOD::System          *system  = 0;
    FMOD::Sound           *sound   = 0;
    FMOD_RESULT            result;
    FMOD_CREATESOUNDEXINFO exinfo;
    int                    key, recorddriver, numdrivers, count;
    unsigned int           version;    
    FILE                  *fp;
    unsigned int           datalength = 0, soundlength;

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

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

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

    /* 
        System initialization
    */
    printf("---------------------------------------------------------\n");    
    printf("Select OUTPUT type\n");    
    printf("---------------------------------------------------------\n");    
    printf("1 :  DirectSound\n");
    printf("2 :  Windows Multimedia WaveOut\n");
    printf("3 :  ASIO\n");
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        key = _getch();
    } while (key != 27 && key < '1' && key > '5');
    
    switch (key)
    {
        case '1' :  result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
                    break;
        case '2' :  result = system->setOutput(FMOD_OUTPUTTYPE_WINMM);
                    break;
        case '3' :  result = system->setOutput(FMOD_OUTPUTTYPE_ASIO);
                    break;
        default  :  return 1; 
    }  
    ERRCHECK(result);
    
    /*
        Enumerate record devices
    */

    result = system->getRecordNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a RECORD driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getRecordDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    recorddriver = 0;
    do
    {
        key = _getch();
        if (key == 27)
        {
            return 0;
        }
        recorddriver = key - '1';
    } while (recorddriver < 0 || recorddriver >= numdrivers);

    printf("\n");
  
    result = system->init(32, FMOD_INIT_NORMAL, 0);
    ERRCHECK(result);

    memset(&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;
    
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &sound);
    ERRCHECK(result);

    printf("========================================================================\n");
    printf("Record to disk example.  Copyright (c) Firelight Technologies 2004-2014.\n");
    printf("========================================================================\n");
    printf("\n");
    printf("Press a key to start recording to record.wav\n");
    printf("\n");

    _getch();

    result = system->recordStart(recorddriver, sound, true);
    ERRCHECK(result);

    printf("Press 'Esc' to quit\n");
    printf("\n");

    fp = fopen("record.wav", "wb");
    if (!fp)
    {
        printf("ERROR : could not open record.wav for writing.\n");
        return 1;
    }

    /*
        Write out the wav header.  As we don't know the length yet it will be 0.
    */
    WriteWavHeader(fp, sound, datalength);

    result = sound->getLength(&soundlength, FMOD_TIMEUNIT_PCM);
    ERRCHECK(result);

    /*
        Main loop.
    */
    do
    {
        static unsigned int lastrecordpos = 0;
        unsigned int recordpos = 0;

        if (_kbhit())
        {
            key = _getch();
        }

        system->getRecordPosition(recorddriver, &recordpos);
        ERRCHECK(result);

        if (recordpos != lastrecordpos)        
        {
            void *ptr1, *ptr2;
            int blocklength;
            unsigned int len1, len2;
            
            blocklength = (int)recordpos - (int)lastrecordpos;
            if (blocklength < 0)
            {
                blocklength += soundlength;
            }

            /*
                Lock the sound to get access to the raw data.
            */
            sound->lock(lastrecordpos * exinfo.numchannels * 2, blocklength * exinfo.numchannels * 2, &ptr1, &ptr2, &len1, &len2);   /* * exinfo.numchannels * 2 = stereo 16bit.  1 sample = 4 bytes. */

            /*
                Write it to disk.
            */
            if (ptr1 && len1)
            {
                datalength += fwrite(ptr1, 1, len1, fp);
            }
            if (ptr2 && len2)
            {
                datalength += fwrite(ptr2, 1, len2, fp);
            }

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

        lastrecordpos = recordpos;

        printf("%-23s. Record buffer pos = %6d : Record time = %02d:%02d\r", (timeGetTime() / 500) & 1 ? "Recording to record.wav" : "", recordpos, datalength / exinfo.defaultfrequency / exinfo.numchannels / 2 / 60, (datalength / exinfo.defaultfrequency / exinfo.numchannels / 2) % 60);

        system->update();

        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Write back the wav header now that we know its length.
    */
    WriteWavHeader(fp, sound, datalength);

    fclose(fp);

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

    result = system->release();
    ERRCHECK(result);

    return 0;
}
int PitchDetector::DetectPitch()
{
    FMOD::System          *system  = 0;
    FMOD::Sound           *sound   = 0;
    FMOD::Channel         *channel = 0;
    FMOD_RESULT            result;
    FMOD_CREATESOUNDEXINFO exinfo;
    int                    key, driver, recorddriver, numdrivers, count, bin;
    unsigned int           version;    
	
    /*
        Create a System object and initialize.
    */
    result = FMOD::System_Create(&system);
    ERRCHECK(result);

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

    if (version < FMOD_VERSION)
    {
        printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
		return(-1);
    }

    /* 
        System initialization
    */
    printf("---------------------------------------------------------\n");    
    printf("Select OUTPUT type\n");    
    printf("---------------------------------------------------------\n");    
    printf("1 :  DirectSound\n");
    printf("2 :  Windows Multimedia WaveOut\n");
    printf("3 :  ASIO\n");
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        //key = _getch();
		key = '1'; //TODO: this uses the default io device rather than using user input
    } while (key != 27 && key < '1' && key > '5');
    
    switch (key)
    {
        case '1' :  result = system->setOutput(FMOD_OUTPUTTYPE_DSOUND);
                    break;
        case '2' :  result = system->setOutput(FMOD_OUTPUTTYPE_WINMM);
                    break;
        case '3' :  result = system->setOutput(FMOD_OUTPUTTYPE_ASIO);
                    break;
        default  :  return(0); 
    }  
    ERRCHECK(result);
    
    /*
        Enumerate playback devices
    */

    result = system->getNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a PLAYBACK driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    do
    {
        //key = _getch();
		key = '1'; //TODO: io devices
        if (key == 27)
        {
            return(0);
        }
        driver = key - '1';
    } while (driver < 0 || driver >= numdrivers);

    result = system->setDriver(driver);
    ERRCHECK(result);

    /*
        Enumerate record devices
    */

    result = system->getRecordNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a RECORD driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getRecordDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    recorddriver = 0;
    do
    {
        //key = _getch();
		key = '1'; //TODO: io devices
        if (key == 27)
        {
            return(0);
        }
        recorddriver = key - '1';
    } while (recorddriver < 0 || recorddriver >= numdrivers);

    printf("\n");
 
    result = system->setSoftwareFormat(OUTPUT_RATE, FMOD_SOUND_FORMAT_PCM16, 1, 0, FMOD_DSP_RESAMPLER_LINEAR);
    ERRCHECK(result);

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

    /*
        Create a sound to record to.
    */
    memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));

    exinfo.cbsize           = sizeof(FMOD_CREATESOUNDEXINFO);
    exinfo.numchannels      = 1;
    exinfo.format           = FMOD_SOUND_FORMAT_PCM16;
    exinfo.defaultfrequency = OUTPUT_RATE;
    exinfo.length           = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5;
    
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &exinfo, &sound);
    ERRCHECK(result);

    /*
        Start the interface
    */
    printf("=========================================================================\n");
    printf("Pitch detection example.  Copyright (c) Firelight Technologies 2004-2011.\n");
    printf("=========================================================================\n");
    printf("\n");
    printf("Record something through the selected recording device and FMOD will\n");
    printf("Determine the pitch.  Sustain the tone for at least a second to get an\n");
    printf("accurate reading.\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    result = system->recordStart(recorddriver, sound, true);
    ERRCHECK(result);
    
    Sleep(100);      /* Give it some time to record something */
    
    result = system->playSound(FMOD_CHANNEL_REUSE, sound, false, &channel);
    ERRCHECK(result);

    /* Dont hear what is being recorded otherwise it will feedback.  Spectrum analysis is done before volume scaling in the DSP chain */
    result = channel->setVolume(0);
    ERRCHECK(result);

    bin = 0;

    /*
        Main loop.
    */
    do
    {
        static float spectrum[SPECTRUM_SIZE];
        float       dominantHz = 0;
        float       max;
        int         dominantNote = 0;
        float       binSize = BIN_SIZE;
		bool		hasUpdated = false;
		char		windowTitle[sizeof("Pitch Detector: ---")] = "Pitch Detector: ---";
		int			noteIndex;
		double		noteDeviation;
		
        if (_kbhit())
        {
            key = _getch();
        }

        result = channel->getSpectrum(spectrum, SPECTRUM_SIZE, 0, FMOD_DSP_FFT_WINDOW_TRIANGLE);
        ERRCHECK(result);

        max = 0;

        for (count = 0; count < SPECTRUM_SIZE; count++)
        {
            if (spectrum[count] > 0.01f && spectrum[count] > max)
            {
                max = spectrum[count];
                bin = count;
				hasUpdated = true;
            }
        }        

        dominantHz  = (float)bin * BIN_SIZE;       /* dominant frequency min */

        dominantNote = 0;
        for (count = 0; count < 120; count++)
        {
             if (dominantHz >= noteFreq[count] && dominantHz < noteFreq[count + 1])
             {
                /* which is it closer to.  This note or the next note */
                if (fabs(dominantHz - noteFreq[count]) < fabs(dominantHz - noteFreq[count+1]))
                {
                    dominantNote = count;
                }
                else
                {
                    dominantNote = count + 1;
                }
                break;
             }
        }

        printf("Detected rate : %7.1f -> %7.1f hz.  Detected musical note. %-3s (%7.1f hz)\r", 
			dominantHz, ((float)bin + 0.99f) * BIN_SIZE, note[dominantNote], noteFreq[dominantNote]);

		// Sets the window title to Pitch Detector: (NOTE)(#|)([0-9]) \| (KEY_MAPPING)
		string noteStr = note[dominantNote];
		noteStr = noteStr[0];
		string keyMapping = " | " + mappingsInterface->GetBoxText(noteStr);
		strncpy(windowTitle + sizeof("Pitch Detector: ") - 1, note[dominantNote], 3);
		if (!hasUpdated) 
		{
			strncpy(windowTitle + sizeof("Pitch Detector: ") - 1, "---", 3);
			keyMapping = "";
		}
		string windowTitleStr = windowTitle;
		emit UpdateTitleSignal(QString((windowTitleStr + keyMapping).c_str()));

		// Sets the feedback of what note is being played
		if (!hasUpdated) 
		{
			noteIndex = -1;
			noteDeviation = 0;
		} 
		else
		{
			noteIndex = NoteToIndex(dominantNote);
			noteDeviation = GetNoteDeviation(dominantHz, dominantNote);
		}
		emit UpdateNoteDisplaySignal(noteIndex, noteDeviation);

        system->update();
        Sleep(10);
    } while (key != 27);

    printf("\n");

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

    result = system->release();
    ERRCHECK(result);
	return 0;
}
Example #8
0
int main(int argc, char *argv[])
{
    FMOD::System          *system  = 0;
    FMOD::Sound           *sound   = 0;
    FMOD_RESULT            result;
    FMOD_CREATESOUNDEXINFO exinfo;
    int                    key, recorddriver, numdrivers, count;
    unsigned int           version;    
    FILE                  *fp;
    unsigned int           datalength = 0, soundlength;
    bool                   iscoreaudio = false;

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

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

    if (version < FMOD_VERSION)
    {
        printf("Error!  You are using an old version of FMOD %08x.  This program requires %08x\n", version, FMOD_VERSION);
        return 0;
    }
    
    result = system->setOutput(FMOD_OUTPUTTYPE_COREAUDIO);
    ERRCHECK(result);

    /*
        Enumerate record devices
    */

    result = system->getRecordNumDrivers(&numdrivers);
    ERRCHECK(result);

    printf("---------------------------------------------------------\n");    
    printf("Choose a RECORD driver\n");
    printf("---------------------------------------------------------\n");    
    for (count=0; count < numdrivers; count++)
    {
        char name[256];

        result = system->getRecordDriverInfo(count, name, 256, 0);
        ERRCHECK(result);

        printf("%d : %s\n", count + 1, name);
    }
    printf("---------------------------------------------------------\n");
    printf("Press a corresponding number or ESC to quit\n");

    recorddriver = 0;
    do
    {
        key = getch();
        if (key == 27)
        {
            return 0;
        }
        recorddriver = key - '1';
    } while (recorddriver < 0 || recorddriver >= numdrivers);

    printf("\n");

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

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

    exinfo.cbsize           = sizeof(FMOD_CREATESOUNDEXINFO);
    exinfo.numchannels      = 2;
    exinfo.defaultfrequency = 44100;
   
    if (iscoreaudio)
    {
        exinfo.format = FMOD_SOUND_FORMAT_PCMFLOAT;
        exinfo.length = exinfo.defaultfrequency * sizeof(float) * exinfo.numchannels * 2;
    }
    else
    {
        exinfo.format = FMOD_SOUND_FORMAT_PCM16;
        exinfo.length = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 2;
    }
 
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_OPENUSER, &exinfo, &sound);
    ERRCHECK(result);

    printf("========================================================================\n");
    printf("Record to disk example.  Copyright (c) Firelight Technologies 2004-2014.\n");
    printf("========================================================================\n");
    printf("\n");
    printf("Press a key to start recording to record.wav\n");
    printf("\n");

    getch();

    result = system->recordStart(recorddriver, sound, true);
    ERRCHECK(result);

    printf("Press 'Esc' to quit\n");
    printf("\n");

    fp = fopen("record.wav", "wb");
    if (!fp)
    {
        printf("ERROR : could not open record.wav for writing.\n");
        return 1;
    }

    /*
        Write out the wav header.  As we don't know the length yet it will be 0.
    */
    WriteWavHeader(fp, sound, datalength);

    result = sound->getLength(&soundlength, FMOD_TIMEUNIT_PCM);
    ERRCHECK(result);

    /*
        Main loop.
    */
    do
    {
        static unsigned int lastrecordpos = 0;
        unsigned int recordpos = 0;

        if (kbhit())
        {
            key = getch();
        }

        system->getRecordPosition(recorddriver, &recordpos);
        ERRCHECK(result);

        if (recordpos != lastrecordpos)        
        {
            void *ptr1, *ptr2;
            int blocklength;
            unsigned int len1, len2;
            
            blocklength = (int)recordpos - (int)lastrecordpos;
            if (blocklength < 0)
            {
                blocklength += soundlength;
            }

            /*
                Lock the sound to get access to the raw data.
            */
            if(iscoreaudio)
            {
                sound->lock(lastrecordpos * 8, blocklength * 8, &ptr1, &ptr2, &len1, &len2);   /* *8 = stereo 32bit.  1 sample = 8 bytes. */
            }
            else
            {
                sound->lock(lastrecordpos * 4, blocklength * 4, &ptr1, &ptr2, &len1, &len2);   /* *4 = stereo 16bit.  1 sample = 4 bytes. */
            }

            /*
                Write it to disk.
            */
            if (ptr1 && len1)
            {
                #ifdef __BIG_ENDIAN__
                if (exinfo.format == FMOD_SOUND_FORMAT_PCM16)
                {
                    signed short *wptr = (signed short *)ptr1;

                    for (int count = 0; count < len1 >> 1; count++)
                    {
                        wptr[count] = SWAPENDIAN_WORD(wptr[count]);
                    }
                }
                else if (exinfo.format == FMOD_SOUND_FORMAT_PCMFLOAT)
                {
                    float *fptr = (float *)ptr1;
    
                    for (int count = 0; count < len1 >> 2; count++)
                    {
                        SWAPENDIAN_FLOAT(fptr[count]);
                    }
                }
                #endif

                datalength += fwrite(ptr1, 1, len1, fp);
            }
            if (ptr2 && len2)
            {
                #ifdef __BIG_ENDIAN__
                if (exinfo.format == FMOD_SOUND_FORMAT_PCM16)
                {
                    signed short *wptr = (signed short *)ptr2;

                    for (int count = 0; count < len2 >> 1; count++)
                    {
                        wptr[count] = SWAPENDIAN_WORD(wptr[count]);
                    }
                }
                else if (exinfo.format == FMOD_SOUND_FORMAT_PCMFLOAT)
                {
                    float *fptr = (float *)ptr2;
    
                    for (int count = 0; count < len2 >> 2; count++)
                    {
                        SWAPENDIAN_FLOAT(fptr[count]);
                    }
                }
                #endif

                datalength += fwrite(ptr2, 1, len2, fp);
            }

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

        lastrecordpos = recordpos;

        printf("\rRecord buffer pos = %6d : Record time = %02d:%02d", recordpos, datalength / exinfo.defaultfrequency / 4 / 60, (datalength / exinfo.defaultfrequency / 4) % 60);
        fflush(stdout);

        system->update();

        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Write back the wav header now that we know its length.
    */
    WriteWavHeader(fp, sound, datalength);

    fclose(fp);

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

    result = system->release();
    ERRCHECK(result);

    return 0;
}
int FMOD_Main()
{
	cout << "Tellale Music Extractor" << endl;
	cout << "FMOD Bank Extractor" << endl;
	cout << "Copyright (c) 2015 Bennyboy" << endl;
	cout << "Http://quickandeasysoftware.net" << endl << endl;

	//cout << "Dumping to " << argv[0] << endl << endl;
	cout << "Dumping will take some time, please be patient." << endl << endl;




	void *extraDriverData = 0;
	Common_Init(&extraDriverData);

	//Studio high level system
	FMOD::Studio::System* system = NULL;
	FMOD_RESULT result = FMOD::Studio::System::create(&system);
	ERRCHECK(result);

	//Low level system
	//FMOD::System* lowLevel;
	//system->getLowLevelSystem(&lowLevel);

	//Set output to wav file. NRT means we control the playback speed with update()
	//lowLevel->setOutput(FMOD_OUTPUTTYPE_WAVWRITER_NRT);    




	//Initialise the system. 
	result = system->initialize(32, FMOD_STUDIO_INIT_ALLOW_MISSING_PLUGINS, FMOD_INIT_NORMAL, extraDriverData);
	ERRCHECK(result);


	//Load all .bank files from dir into vector
	std::vector<std::string> FilesInDir;
	GetBankFilesInDirectory(FilesInDir, "C:/Program Files (x86)/FMOD SoundSystem/FMOD Studio API Windows/api/studio/examples/test/");


	//Create vector to hold all the bank instances
	std::vector<FMOD::Studio::Bank* > vbank_;
	int FileArraySize = FilesInDir.size();

	//Set size of the vector to the number of banks and initialise each bank to null 
	vbank_.resize(FileArraySize);
	for (int i = 0; i <= FileArraySize - 1; i++)
	{
		vbank_[i] = NULL; //initialise banks
	}

	//Loop through and load the banks from our bank instances
	for (int i = 0; i <= FileArraySize - 1; i++)
	{
		system->loadBankFile(FilesInDir[i].c_str(), FMOD_STUDIO_LOAD_BANK_NORMAL, &vbank_[i]);
		//Bank loading is asynchronous - so make sure its finished loading before we do operations on it. Easier just to wait here after we load each bank.
		FMOD_STUDIO_LOADING_STATE  bankloadingState = FMOD_STUDIO_LOADING_STATE_LOADING;
		while (bankloadingState != FMOD_STUDIO_LOADING_STATE_LOADED)
		{
			system->update();
			ERRCHECK(vbank_[i]->getLoadingState(&bankloadingState));
		}
	}


	//Preload all samples in all banks
	//for ( int i = 0 ; i <= FileArraySize-1 ; i++ )
	//{
	//	vbank_[i]->loadSampleData();
	//	FMOD_STUDIO_LOADING_STATE  loadingState = FMOD_STUDIO_LOADING_STATE_UNLOADED;
	//	while (loadingState !=FMOD_STUDIO_LOADING_STATE_LOADED)
	//	{
	//		if (vbank_[i]->getSampleLoadingState(&loadingState)  == FMOD_OK)
	//		{
	//			system->update();
	//		}
	//		else
	//		{}
	//	}
	//}


	//Create an array to hold all the Event ID's
	std::vector<std::string> idVector;

	//Get all the event ID's from the banks and store them in idVector
	for (int i = 0; i <= FileArraySize - 1; i++)
	{
		int eventCount = 0;
		int maxLen = 255;
		char buffer[255 + 1];

		vbank_[i]->getEventCount(&eventCount); //get the number of events in the bank

											   //Create an array to hold the eventdescriptions
		FMOD::Studio::EventDescription** eventArray = (FMOD::Studio::EventDescription**)malloc(eventCount * sizeof(void*));

		//Fill the array with the events from the bank
		result = vbank_[i]->getEventList(eventArray, eventCount, &eventCount);
		if (result == FMOD_OK)
		{
			for (int j = 0; j < eventCount; j++)
			{
				//Get the ID
				result = eventArray[j]->getPath(buffer, maxLen, 0);
				if (result == FMOD_OK)
				{
					//OutputDebugString(buffer); OutputDebugString("\n");
					if (strncmp(buffer, "event", 5) == 0) //only add "event" ID's
					{
						idVector.push_back(buffer); //add it as a new element at the end of the vector
					}
				}
				else
				{
					OutputDebugString("OI! couldnt get ID!");
				}
			}
		}

		free(eventArray);
	}


	//Free the system so we can create a new one and control the output filename
	result = system->release();
	ERRCHECK(result);


	//Loop through each ID
	for (int i = 0; i <= idVector.size() - 1; i++)
	{

		//Studio high level system
		FMOD::Studio::System* system = NULL;
		FMOD_RESULT result = FMOD::Studio::System::create(&system);
		ERRCHECK(result);

		//Low level system
		FMOD::System* lowLevel;
		system->getLowLevelSystem(&lowLevel);

		//Set output to wav file. NRT means we control the playback speed with update()
		lowLevel->setOutput(FMOD_OUTPUTTYPE_WAVWRITER_NRT);


		//Extract filename
		std::string tempString = idVector[i];//.c_str();
		std::string filename;
		unsigned found = tempString.find_last_of("/\\");
		filename = tempString.substr(found + 1);
		filename.append(".wav");



		//Initialise the system. 
		//Need STREAM_FROM_UPDATE as we're using WAVWRITER_NRT. Need the synchronous here?? Thread unsafe for low level - dunno
		result = system->initialize(32, FMOD_STUDIO_INIT_SYNCHRONOUS_UPDATE, FMOD_INIT_STREAM_FROM_UPDATE | FMOD_INIT_THREAD_UNSAFE, (char*)filename.c_str());
		ERRCHECK(result);


		//Loop through and load the banks from our bank instances (banks already stored in array earlier)
		for (int j = 0; j <= FileArraySize - 1; j++)
		{
			system->loadBankFile(FilesInDir[j].c_str(), FMOD_STUDIO_LOAD_BANK_NORMAL, &vbank_[j]);

			//Bank loading is asynchronous - so make sure its finished loading before we do operations on it. Easier just to wait here after we load each bank.
			system->update();
			FMOD_STUDIO_LOADING_STATE  bankloadingState = FMOD_STUDIO_LOADING_STATE_LOADING;
			ERRCHECK(vbank_[j]->getLoadingState(&bankloadingState));
			while (bankloadingState == FMOD_STUDIO_LOADING_STATE_LOADING)
			{

				ERRCHECK(vbank_[j]->getLoadingState(&bankloadingState));
			}
		}




		//Playback and save the file
		outputEventByID(system, idVector[i].c_str());


		//Free the system so we can create a new one and control the output filename
		result = system->release();
		ERRCHECK(result);
		lowLevel->release();
	}




	Common_Close();

	return 0;
}
Example #10
0
int main()
{
	// ================================================================================================
	// Application-independent initialization
	// ================================================================================================

	FMOD::System *system;
	FMOD_RESULT result;
	unsigned int version;
	int numDrivers;
	FMOD_SPEAKERMODE speakerMode;
	FMOD_CAPS caps;
	char name[256];

	// Create FMOD interface object
	result = FMOD::System_Create(&system);
	FMODErrorCheck(result);

	// Check version
	result = system->getVersion(&version);
	FMODErrorCheck(result);

	if (version < FMOD_VERSION)
	{
		std::cout << "Error! You are using an old version of FMOD " << version << ". This program requires " << FMOD_VERSION << std::endl;
		return 0;
	}
	
	// Get number of sound cards
	result = system->getNumDrivers(&numDrivers);
	FMODErrorCheck(result);
	
	// No sound cards (disable sound)
	if (numDrivers == 0)
	{
		result = system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);
		FMODErrorCheck(result);
	}

	// At least one sound card
	else
	{
		// Get the capabilities of the default (0) sound card
		result = system->getDriverCaps(0, &caps, 0, &speakerMode);
		FMODErrorCheck(result);
	
		// Set the speaker mode to match that in Control Panel
		result = system->setSpeakerMode(speakerMode);
		FMODErrorCheck(result);
	
		// Increase buffer size if user has Acceleration slider set to off
		if (caps & FMOD_CAPS_HARDWARE_EMULATED)
		{
			result = system->setDSPBufferSize(1024, 10);
			FMODErrorCheck(result);
		}
	
		// Get name of driver
		result = system->getDriverInfo(0, name, 256, 0);
		FMODErrorCheck(result);
	
		// SigmaTel sound devices crackle for some reason if the format is PCM 16-bit.
		// PCM floating point output seems to solve it.
		if (strstr(name, "SigmaTel"))
		{
			result = system->setSoftwareFormat(48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0, 0, FMOD_DSP_RESAMPLER_LINEAR);
			FMODErrorCheck(result);
		}
	}

	// Initialise FMOD
	result = system->init(100, FMOD_INIT_NORMAL, 0);

	// If the selected speaker mode isn't supported by this sound card, switch it back to stereo
	if (result == FMOD_ERR_OUTPUT_CREATEBUFFER)
	{
		result = system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
		FMODErrorCheck(result);
	
		result = system->init(100, FMOD_INIT_NORMAL, 0);
	}
	FMODErrorCheck(result);

	// ================================================================================================
	// Application-specific code
	// ================================================================================================

	bool quit = false;
	bool fading = false;
	int fadeLength = 3000;
	int fadeStartTick;

	// Open music as a stream
	FMOD::Sound *song1, *song2, *effect;
	result = system->createStream("Song1.mp3", FMOD_DEFAULT, 0, &song1);
	FMODErrorCheck(result);
	result = system->createStream("Song2.mp3", FMOD_DEFAULT, 0, &song2);
	FMODErrorCheck(result);

	// Load sound effects into memory (not streaming)
	result = system->createSound("Effect.mp3", FMOD_DEFAULT, 0, &effect);
	FMODErrorCheck(result);

	// Assign each song to a channel and start them paused
	FMOD::Channel *channel1, *channel2;
	result = system->playSound(FMOD_CHANNEL_FREE, song1, true, &channel1);
	FMODErrorCheck(result);
	result = system->playSound(FMOD_CHANNEL_FREE, song2, true, &channel2);
	FMODErrorCheck(result);

	// Songs should repeat forever
	channel1->setLoopCount(-1);
	channel2->setLoopCount(-1);

	// Print instructions
	std::cout <<
		"FMOD Simple Demo - (c) Katy Coe 2012 - www.djkaty.com" << std::endl <<
		"=====================================================" << std::endl << std::endl <<
		"Press:" << std::endl << std::endl <<
		"  1 - Toggle song 1 pause on/off" << std::endl <<
		"  2 - Toggle song 2 pause on/off" << std::endl <<
		"  F - Fade from song 1 to song 2" << std::endl <<
		"  S - Play one-shot sound effect" << std::endl <<
		"  Q - Quit" << std::endl;

	while (!quit)
	{
		// Per-frame FMOD update
		FMODErrorCheck(system->update());

		// Q - Quit
		if (GetAsyncKeyState('Q'))
			quit = true;

		// 1 - Toggle song 1 pause state
		if (GetAsyncKeyState('1'))
		{
			bool isPaused;
			channel1->getPaused(&isPaused);
			channel1->setPaused(!isPaused);
			while (GetAsyncKeyState('1'));
		}

		// 2 - Toggle song 2 pause state
		if (GetAsyncKeyState('2'))
		{
			bool isPaused;
			channel2->getPaused(&isPaused);
			channel2->setPaused(!isPaused);
			while (GetAsyncKeyState('2'));
		}

		// F - Begin fade from song 1 to song 2
		if (GetAsyncKeyState('F'))
		{
			channel1->setVolume(1.0f);
			channel2->setVolume(0.0f);
			channel1->setPaused(false);
			channel2->setPaused(false);
			fading = true;
			fadeStartTick = GetTickCount();

			while (GetAsyncKeyState('F'));
		}

		// Play one-shot sound effect (without storing channel handle)
		if (GetAsyncKeyState('S'))
		{
			system->playSound(FMOD_CHANNEL_FREE, effect, false, 0);

			while (GetAsyncKeyState('S'));
		}

		// Fade function if fade is in progress
		if (fading)
		{
			// Get volume from 0.0f - 1.0f depending on number of milliseconds elapsed since fade started
			float volume = min(static_cast<float>(GetTickCount() - fadeStartTick) / fadeLength, 1.0f);

			// Fade is over if song 2 has reached full volume
			if (volume == 1.0f)
			{
				fading = false;
				channel1->setPaused(true);
				channel1->setVolume(1.0f);
			}

			// Translate linear volume into a smooth sine-squared fade effect
			volume = static_cast<float>(sin(volume * M_PI / 2));
			volume *= volume;

			// Fade song 1 out and song 2 in
			channel1->setVolume(1.0f - volume);
			channel2->setVolume(volume);
		}
	}

	// Free resources
	FMODErrorCheck(song1->release());
	FMODErrorCheck(song2->release());
	FMODErrorCheck(effect->release());
	FMODErrorCheck(system->release());
}
Example #11
0
int main(int argc, char *argv[])
{
    FMOD::System          *system  = 0;
    FMOD::Sound           *sound   = 0;
    FMOD::Channel         *channel = 0;
    FMOD::DSP             *dsp     = 0;
    FMOD_RESULT            result;
    FMOD_CREATESOUNDEXINFO exinfo;
    FMOD_SPEAKERMODE       speakermode;
    FMOD_CAPS              caps;
    int                    key, numdrivers;
    unsigned int           version;    
    unsigned int           datalength = 0, soundlength;
    char                   name[256];
    unsigned int           adjustedlatency;

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

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

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

    /* 
        System initialization (recommended startup sequence)
    */
    result = system->getNumDrivers(&numdrivers);
    ERRCHECK(result);

    if (numdrivers == 0)
    {
        result = system->setOutput(FMOD_OUTPUTTYPE_NOSOUND);
        ERRCHECK(result);
    }
    else
    {
        result = system->getDriverCaps(0, &caps, 0, &speakermode);
        ERRCHECK(result);

        result = system->setSpeakerMode(speakermode);       /* Set the user selected speaker mode. */
        ERRCHECK(result);

        if (caps & FMOD_CAPS_HARDWARE_EMULATED)             /* The user has the 'Acceleration' slider set to off!  This is really bad for latency!. */
        {                                                   /* You might want to warn the user about this. */
            result = system->setDSPBufferSize(1024, 10);
            ERRCHECK(result);
        }
#ifdef LOWLATENCY
        else
        {
            result = system->setDSPBufferSize(256, 4);
        }
#endif
        result = system->getDriverInfo(0, name, 256, 0);
        ERRCHECK(result);

        if (strstr(name, "SigmaTel"))   /* Sigmatel sound devices crackle for some reason if the format is PCM 16bit.  PCM floating point output seems to solve it. */
        {
            result = system->setSoftwareFormat(48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
            ERRCHECK(result);
        }
    }

    result = system->init(100, FMOD_INIT_NORMAL, 0);
    if (result == FMOD_ERR_OUTPUT_CREATEBUFFER)         /* Ok, the speaker mode selected isn't supported by this soundcard.  Switch it back to stereo... */
    {
        result = system->setSpeakerMode(FMOD_SPEAKERMODE_STEREO);
        ERRCHECK(result);
            
        result = system->init(100, FMOD_INIT_NORMAL, 0);/* ... and re-init. */
        ERRCHECK(result);
    }
    /* 
        System initialization complete (recommended startup sequence)
    */
 

    /*
        Create user sound to record into.  Set it to loop for playback.
    */
    memset(&exinfo, 0, sizeof(FMOD_CREATESOUNDEXINFO));
    exinfo.cbsize           = sizeof(FMOD_CREATESOUNDEXINFO);
    exinfo.numchannels      = 1;
    exinfo.format           = FMOD_SOUND_FORMAT_PCM16;
    exinfo.defaultfrequency = RECORDRATE;
    exinfo.length           = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5; /* 5 second buffer, doesnt really matter how big this is, but not too small of course. */
    
    result = system->createSound(0, FMOD_2D | FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_OPENUSER, &exinfo, &sound);
    ERRCHECK(result);

    printf("========================================================================\n");
    printf("Record with realtime playback example.\n");
    printf("Copyright (c) Firelight Technologies 2004-2014.\n");
    printf("\n");
    printf("Try #define LOWLATENCY to reduce latency for more modern machines!\n");
    printf("========================================================================\n");
    printf("\n");
    printf("Press a key to start recording.  Playback will start %d samples (%d ms) later.\n", LATENCY, LATENCY * 1000 / RECORDRATE);
    printf("\n");

    _getch();

    printf("Press 'E' to toggle an effect on/off.\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    result = system->recordStart(0, sound, true);
    ERRCHECK(result);

    result = sound->getLength(&soundlength, FMOD_TIMEUNIT_PCM);
    ERRCHECK(result);

    /*
        Create a DSP effect to play with.
    */
    result = system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &dsp);
    ERRCHECK(result);
    result = dsp->setParameter(FMOD_DSP_FLANGE_RATE, 4.0f);
    ERRCHECK(result);
    result = dsp->setBypass(true);   
    ERRCHECK(result);
    
    adjustedlatency = LATENCY;  /* This might change depending on record block size. */
    
    /*
        Main loop.
    */
    do
    {
        static unsigned int lastrecordpos = 0, samplesrecorded = 0;
        unsigned int recordpos = 0, recorddelta;
        
        key = 0;
        if (_kbhit())
        {
            key = _getch();
        }

        if (key == 'e' || key == 'E')
        {
            bool bypass;
            dsp->getBypass(&bypass);
            dsp->setBypass(!bypass);
            if (bypass)
            {
                FMOD_REVERB_PROPERTIES prop = FMOD_PRESET_CONCERTHALL;
                system->setReverbProperties(&prop);
            }
            else
            {
                FMOD_REVERB_PROPERTIES prop = FMOD_PRESET_OFF;
                system->setReverbProperties(&prop);
            }
            printf("\n\n *** TURN DSP EFFECT %s ** \n\n", bypass ? "ON" : "OFF");
        }
        
        system->getRecordPosition(0, &recordpos);
        ERRCHECK(result);

        recorddelta = recordpos >= lastrecordpos ? recordpos - lastrecordpos : recordpos + soundlength - lastrecordpos;
        samplesrecorded += recorddelta;

		if (samplesrecorded >= adjustedlatency && !channel)
		{
		    result = system->playSound(FMOD_CHANNEL_FREE, sound, 0, &channel);
            ERRCHECK(result);
            
            result = channel->addDSP(dsp, 0);
            ERRCHECK(result);
		}
				
        if (channel && recorddelta)
        {
            unsigned int playrecorddelta;
            unsigned int playpos = 0;
            int adjusting = 0;
            float smootheddelta;
            float dampratio = 0.97f;
            static unsigned int minrecorddelta = (unsigned int)-1;
            
            /*
                If the record driver steps the position of the record cursor in larger increments than the user defined latency value, then we should
                increase our latency value to match.
            */
            if (recorddelta < minrecorddelta)
            {
                minrecorddelta = recorddelta;
                if (adjustedlatency < recorddelta)
                {
                    adjustedlatency = recorddelta;
                }
            }

            channel->getPosition(&playpos, FMOD_TIMEUNIT_PCM);

            playrecorddelta = recordpos >= playpos ? recordpos - playpos : recordpos + soundlength - playpos;
            
	        /*
                Smooth total
            */
            {
                static float total = 0;
                
                total = total * dampratio;
	            total += playrecorddelta;
	            smootheddelta = total * (1.0f - dampratio);
            }
           
            if (smootheddelta < adjustedlatency - DRIFTTHRESHOLD || smootheddelta > soundlength / 2)   /* if play cursor is catching up to record (or passed it), slow playback down */
            {
                channel->setFrequency(RECORDRATE - (RECORDRATE / 50)); /* Decrease speed by 2% */
                adjusting = 1;
            }
            else if (smootheddelta > adjustedlatency + DRIFTTHRESHOLD)   /* if play cursor is falling too far behind record, speed playback up */
            {
                channel->setFrequency(RECORDRATE + (RECORDRATE / 50)); /* Increase speed by 2% */
                adjusting = 2;
            }
            else
            {
                channel->setFrequency(RECORDRATE);          /* Otherwise set to normal rate */
                adjusting = 0;
            }
            
            printf("REC %5d (REC delta %5d) : PLAY %5d, PLAY/REC diff %5d %s\r", recordpos, recorddelta, playpos, (int)smootheddelta, adjusting == 1 ? "DECREASE SPEED" : adjusting == 2 ? "INCREASE SPEED" : "              ");
        }
        
        lastrecordpos = recordpos;
        
        system->update();

        Sleep(10);

    } while (key != 27);

    printf("\n");

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

    result = system->release();
    ERRCHECK(result);

    return 0;
}
SoundManager::SoundManager()
: _systemVolumeMusic(1.0f)
, _systemVolumeFX(1.0f)
, _userVolumeMusic(1.0f)
, _userVolumeFX(1.0f)
, _expectedSystemVolumeFX(1.0f)
, _expectedSystemVolumeMusic(1.0f)
, _expectedUserVolumeFX(1.0f)
, _expectedUserVolumeMusic(1.0f)
{
    // Sound Init
#ifdef FMOD_ACTIVE
    //    void *extraDriverData = NULL;
    //    utilityInit(&extraDriverData);
    
    _system = NULL;
    ERRCHECK(FMOD::Studio::System::create(&_system) );
    
#if CC_TARGET_PLATFORM==CC_PLATFORM_ANDROID

    FMOD::System * lowLevelSystem = nullptr;
    ERRCHECK(_system->getLowLevelSystem(&lowLevelSystem));
    ERRCHECK(lowLevelSystem->setOutput(FMOD_OUTPUTTYPE_AUDIOTRACK));

    unsigned int bufferLength;
    int numBuffers;

    ERRCHECK(lowLevelSystem->getDSPBufferSize(&bufferLength, &numBuffers));

    CCLOG("SoundManager - Default Buffer Length %u", bufferLength);
    CCLOG("SoundManager - Default Number of Buffer %d", numBuffers);

    bufferLength = 256; // 512; // 1024;
    numBuffers = 2; // 2; // 8;

    ERRCHECK(lowLevelSystem->setDSPBufferSize(bufferLength, numBuffers));

    ERRCHECK(lowLevelSystem->getDSPBufferSize(&bufferLength, &numBuffers));

    CCLOG("SoundManager - New Buffer Length %u", bufferLength);
    CCLOG("SoundManager - New Number of Buffer %d", numBuffers);

    int sampleRate;
    FMOD_SPEAKERMODE speakerMode;
    int numRawSpeakers;

    ERRCHECK(lowLevelSystem->getSoftwareFormat(&sampleRate , &speakerMode, &numRawSpeakers));

    CCLOG("SoundManager - Default Sample Rate %d", sampleRate);
    CCLOG("SoundManager - Default Speaker mode %d", (int)speakerMode);
    CCLOG("SoundManager - Default Number of Raw Speakers %d", numRawSpeakers);

    sampleRate = 24000;

    ERRCHECK(lowLevelSystem->setSoftwareFormat(sampleRate, speakerMode, numRawSpeakers));

    ERRCHECK(lowLevelSystem->getSoftwareFormat(&sampleRate , &speakerMode, &numRawSpeakers));

    CCLOG("SoundManager - New Sample Rate %d", sampleRate);
    CCLOG("SoundManager - New Speaker mode %d", (int)speakerMode);
    CCLOG("SoundManager - New Number of Raw Speakers %d", numRawSpeakers);

#endif

    ERRCHECK(_system->initialize(64, FMOD_STUDIO_INIT_LIVEUPDATE, FMOD_INIT_NORMAL, 0) );
//    ERRCHECK(_system->initialize(64, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL, 0) );
    //    ERRCHECK(_system->initialize(32, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL, 0) );
    ERRCHECK(FMOD_Debug_Initialize(FMOD_DEBUG_LEVEL_WARNING, FMOD_DEBUG_MODE_TTY, 0, nullptr));
    //    ERRCHECK(_system->initialize(32, FMOD_STUDIO_INIT_NORMAL, FMOD_INIT_NORMAL, extraDriverData) );

//    _masterBank = NULL;
//    ERRCHECK(_system->loadBankFile(getMediaPath(FILE_BANK_MASTER_DATA_NAME), FMOD_STUDIO_LOAD_BANK_NORMAL, &_masterBank));
    
    //_stringsBank = static_cast<FMOD::Studio::Bank*>(this->loadAudioGroup(FILE_BANK_MASTER_META_NAME));
    _stringsBank = NULL;
    ERRCHECK(_system->loadBankFile(getMediaPath(FILE_BANK_MASTER_META_NAME), FMOD_STUDIO_LOAD_BANK_NORMAL, &_stringsBank));
    
    Director::getInstance()->getScheduler()->schedule(schedule_selector(SoundManager::updateSounds), this, 0.05, false);
#endif
}
Example #13
0
int main(int argc, char *argv[])
{
    FMOD::System     *system;
    FMOD::Sound      *sound1, *sound2;
    FMOD::Channel    *channel = 0;
    FMOD_RESULT       result;
    int               key;
    unsigned int      version;

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

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

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

    /*
        Multichannel only supported in ALSA mode in 5.1 and 7.1.
        If the user doesn't have 5.1 speakers then only the speakers
        they have will be audible.
    */

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

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

    result = system->createSound("../media/drumloop.wav", FMOD_SOFTWARE | FMOD_2D, 0, &sound1);
    ERRCHECK(result);
    result = sound1->setMode(FMOD_LOOP_OFF);
    ERRCHECK(result);


    result = system->createSound("../media/stereo.ogg", FMOD_SOFTWARE | FMOD_2D, 0, &sound2);
    ERRCHECK(result);

    printf("==============================================================================\n");
    printf("Multi Speaker Output Example.  Copyright (c) Firelight Technologies 2004-2011.\n");
    printf("==============================================================================\n");
    printf("\n");
    printf("Note!  You must have your speaker configuration set up correctly\n");
    printf("       in the windows control panel for this to work properly.\n");
    printf("\n");
    printf("Press '1' to play a mono sound on the FRONT LEFT speaker.\n");
    printf("Press '2' to play a mono sound on the FRONT RIGHT speaker.\n");
    printf("Press '3' to play a mono sound on the CENTER speaker.\n");
    printf("Press '4' to play a mono sound on the REAR LEFT speaker.\n");
    printf("Press '5' to play a mono sound on the REAR RIGHT speaker.\n");
    printf("Press '6' to play a mono sound on the SIDE LEFT speaker.\n");
    printf("Press '7' to play a mono sound on the SIDE RIGHT speaker.\n");
    printf("Press '8' to play a stereo sound on the front speakers.\n");
    printf("Press '9' to play a stereo sound on the front speakers but channel swapped.\n");
    printf("Press '0' to play the right part of a stereo sound on the CENTER speaker.\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    /*
        Main loop.
    */
    do
    {
        if (kbhit())
        {
            key = getch();

            switch (key)
            {
                case '1' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(1.0f, 0, 0, 0, 0, 0, 0, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);
                    break;
                }
                case '2' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(0, 1.0f, 0, 0, 0, 0, 0, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);
                    break;
                }
                case '3' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(0, 0, 1.0f, 0, 0, 0, 0, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);
                    break;
                }
                case '4' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(0, 0, 0, 0, 1.0f, 0, 0, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);
                    break;
                }
                case '5' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(0, 0, 0, 0, 0, 1.0f, 0, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);
                    break;
                }
                case '6' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(0, 0, 0, 0, 0, 0, 1.0f, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);
                    break;
                }
                case '7' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound1, true, &channel);
                    ERRCHECK(result);

                    result = channel->setSpeakerMix(0, 0, 0, 0, 0, 0, 0, 1.0f);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);

                    break;
                }
                case '8' :
                {
                    float levels[2] = { 0, 1.0f };

                    result = system->playSound(FMOD_CHANNEL_FREE, sound2, true, &channel);
                    ERRCHECK(result);

                    /*
                        By default a stereo sound would play in all right and all left speakers, so this forces it to just the front.
                    */
                    result = channel->setSpeakerMix(1.0f, 1.0f, 0, 0, 0, 0, 0, 0);
                    ERRCHECK(result);

                    result = channel->setPaused(false);
                    ERRCHECK(result);

                    break;
                }
                case '9' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound2, true, &channel);
                    ERRCHECK(result);

                    /*
                        Clear out all speakers first.
                    */
                    result = channel->setSpeakerMix(0, 0, 0, 0, 0, 0, 0, 0);
                    ERRCHECK(result);

                    /*
                        Put the left channel of the sound in the right speaker.
                    */
                    {
                        float levels[2] = { 0, 1.0f };    /* This array represents the source stereo sound.  l/r */

                        result = channel->setSpeakerLevels(FMOD_SPEAKER_FRONT_LEFT, levels, 2);
                        ERRCHECK(result);
                    }
                    /*
                        Put the right channel of the sound in the left speaker.
                    */
                    {
                        float levels[2] = { 1.0f, 0 };    /* This array represents the source stereo sound.  l/r */

                        result = channel->setSpeakerLevels(FMOD_SPEAKER_FRONT_RIGHT, levels, 2);
                        ERRCHECK(result);
                    }

                    result = channel->setPaused(false);
                    ERRCHECK(result);

                    break;
                }
                case '0' :
                {
                    result = system->playSound(FMOD_CHANNEL_FREE, sound2, true, &channel);
                    ERRCHECK(result);

                    /*
                        Clear out all speakers first.
                    */
                    result = channel->setSpeakerMix(0, 0, 0, 0, 0, 0, 0, 0);
                    ERRCHECK(result);

                    /*
                        Put the left channel of the sound in the right speaker.
                    */
                    {
                        float levels[2] = { 0, 1.0f };    /* This array represents the source stereo sound.  l/r */

                        result = channel->setSpeakerLevels(FMOD_SPEAKER_FRONT_CENTER, levels, 2);
                        ERRCHECK(result);
                    }

                    result = channel->setPaused(false);
                    ERRCHECK(result);

                    break;
                }
            }
        }

        system->update();

        {
            unsigned int ms = 0;
            unsigned int lenms = 0;
            bool         playing = false;
            bool         paused = false;
            int          channelsplaying = 0;

            if (channel)
            {
                FMOD::Sound *currentsound = 0;

                result = channel->isPlaying(&playing);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel->getPaused(&paused);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }

                result = channel->getPosition(&ms, FMOD_TIMEUNIT_MS);
                if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                {
                    ERRCHECK(result);
                }
               
                channel->getCurrentSound(&currentsound);
                if (currentsound)
                {
                    result = currentsound->getLength(&lenms, FMOD_TIMEUNIT_MS);
                    if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN))
                    {
                        ERRCHECK(result);
                    }
                }
            }

            system->getChannelsPlaying(&channelsplaying);

            printf("Time %02d:%02d:%02d/%02d:%02d:%02d : %s : Channels Playing %2d\r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, lenms / 1000 / 60, lenms / 1000 % 60, lenms / 10 % 100, paused ? "Paused " : playing ? "Playing" : "Stopped", channelsplaying);
        }

        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Shut down
    */
    result = sound1->release();
    ERRCHECK(result);
    result = sound2->release();
    ERRCHECK(result);
    result = system->close();
    ERRCHECK(result);
    result = system->release();
    ERRCHECK(result);

    return 0;
}