Beispiel #1
0
int main(int argc, char *argv[])
{
    FMOD_SYSTEM       *system;
    FMOD_SOUND        *sound[5];
    FMOD_CHANNEL      *channel[5];
    FMOD_CHANNELGROUP *groupA, *groupB;
    FMOD_DSP          *dspreverb, *dspflange, *dsplowpass;
    FMOD_RESULT        result;
    int                key, count;
    unsigned int       version;

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

    result = FMOD_System_GetVersion(system, &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 = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, 0);
    ERRCHECK(result);

    result = FMOD_System_CreateSound(system, "../media/drumloop.wav", FMOD_SOFTWARE | FMOD_LOOP_NORMAL, 0, &sound[0]);
    ERRCHECK(result);
    result = FMOD_System_CreateSound(system, "../media/jaguar.wav", FMOD_SOFTWARE | FMOD_LOOP_NORMAL, 0, &sound[1]);
    ERRCHECK(result);
    result = FMOD_System_CreateSound(system, "../media/c.ogg", FMOD_SOFTWARE | FMOD_LOOP_NORMAL, 0, &sound[2]);
    ERRCHECK(result);
    result = FMOD_System_CreateSound(system, "../media/d.ogg", FMOD_SOFTWARE | FMOD_LOOP_NORMAL, 0, &sound[3]);
    ERRCHECK(result);
    result = FMOD_System_CreateSound(system, "../media/e.ogg", FMOD_SOFTWARE | FMOD_LOOP_NORMAL, 0, &sound[4]);
    ERRCHECK(result);

    result = FMOD_System_CreateChannelGroup(system, "Group A", &groupA);
    ERRCHECK(result);

    result = FMOD_System_CreateChannelGroup(system, "Group B", &groupB);
    ERRCHECK(result);

    printf("======================================================================\n");
    printf("Sub-mixing example.  Copyright (c) Firelight Technologies 2004-2005.\n");
    printf("======================================================================\n");
    printf("                                                       (drumloop.wav) \n");
    printf("                                                      /               \n");
    printf("                                              (groupA)                \n");
    printf("                                     (reverb)/        \\               \n");
    printf("                                    /                  (jaguar.wav)   \n");
    printf("(soundcard)--(lowpass)--(systemhead)                                  \n");
    printf("                                    \\                  (c.ogg)        \n");
    printf("                                     (flange)         /                \n");
    printf("                                             \\(groupB)--(d.ogg)       \n");
    printf("                                                      \\               \n");
    printf("                                                       (e.ogg)        \n");
    printf("Press 'A' to mute/unmute group A\n");
    printf("Press 'B' to mute/unmute group B\n");
    printf("\n");
    printf("Press 'R' to place reverb on group A\n");
    printf("Press 'F' to place flange on group B\n");
    printf("Press 'L' to place lowpass on everything\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

    /*
        Start all the sounds!
    */
    for (count = 0; count < 5; count++)
    {
        result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound[count], TRUE, &channel[count]);
        ERRCHECK(result);
        if (count < 2)
        {
            result = FMOD_Channel_SetChannelGroup(channel[count], groupA);
        }
        else
        {
            result = FMOD_Channel_SetChannelGroup(channel[count], groupB);
        }
        ERRCHECK(result);
        result = FMOD_Channel_SetPaused(channel[count], FALSE);
        ERRCHECK(result);
    }

    /*
        Create the DSP effects we want to apply to our submixes.
    */  
    result = FMOD_System_CreateDSPByType(system, FMOD_DSP_TYPE_REVERB, &dspreverb);
    ERRCHECK(result);

    result = FMOD_System_CreateDSPByType(system, FMOD_DSP_TYPE_FLANGE, &dspflange);
    ERRCHECK(result);
    result = FMOD_DSP_SetParameter(dspflange, FMOD_DSP_FLANGE_RATE, 1.0f);
    ERRCHECK(result);

    result = FMOD_System_CreateDSPByType(system, FMOD_DSP_TYPE_LOWPASS, &dsplowpass);
    ERRCHECK(result);
    result = FMOD_DSP_SetParameter(dsplowpass, FMOD_DSP_LOWPASS_CUTOFF, 500.0f);
    ERRCHECK(result);

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

            switch (key)
            {
                case 'a' : 
                case 'A' : 
                {
                    static int mute = TRUE;

                    FMOD_ChannelGroup_SetMute(groupA, mute);

                    mute = !mute;
                    break;
                }
                case 'b' : 
                case 'B' : 
                {
                    static int mute = TRUE;

                    FMOD_ChannelGroup_SetMute(groupB, mute);

                    mute = !mute;
                    break;
                }
                case 'r' : 
                case 'R' : 
                {
                    static int reverb = TRUE;

                    if (reverb)
                    {
                        FMOD_ChannelGroup_AddDSP(groupA, dspreverb);
                    }
                    else
                    {
                        FMOD_DSP_Remove(dspreverb);
                    }

                    reverb = !reverb;
                    break;
                }
                case 'f' : 
                case 'F' : 
                {
                    static int flange = TRUE;

                    if (flange)
                    {
                        FMOD_ChannelGroup_AddDSP(groupB, dspflange);
                    }
                    else
                    {
                        FMOD_DSP_Remove(dspflange);
                    }

                    flange = !flange;
                    break;
                }
                case 'l' : 
                case 'L' : 
                {
                    static int lowpass = TRUE;

                    if (lowpass)
                    {
                        FMOD_System_AddDSP(system, dsplowpass);
                    }
                    else
                    {
                        FMOD_DSP_Remove(dsplowpass);
                    }

                    lowpass = !lowpass;
                    break;
                }
            }
        }

        FMOD_System_Update(system);

        {
            int  channelsplaying = 0;

            FMOD_System_GetChannelsPlaying(system, &channelsplaying);

            printf("Channels Playing %2d\r", channelsplaying);
        }

        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Shut down
    */
    for (count = 0; count < 5; count++)
    {
        result = FMOD_Sound_Release(sound[count]);
        ERRCHECK(result);
    }

    result = FMOD_DSP_Release(dspreverb);
    ERRCHECK(result);
    result = FMOD_DSP_Release(dspflange);
    ERRCHECK(result);
    result = FMOD_DSP_Release(dsplowpass);
    ERRCHECK(result);

    result = FMOD_ChannelGroup_Release(groupA);
    ERRCHECK(result);
    result = FMOD_ChannelGroup_Release(groupB);
    ERRCHECK(result);

    result = FMOD_System_Close(system);
    ERRCHECK(result);
    result = FMOD_System_Release(system);
    ERRCHECK(result);

    return 0;
}
Beispiel #2
0
int main(int argc, char *argv[])
{
    FMOD_SYSTEM    *system;
    FMOD_CHANNEL   *channel = 0;
    FMOD_DSP       *dsp     = 0;
    FMOD_RESULT     result;
    int             key;
    unsigned int    version;

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

    result = FMOD_System_GetVersion(system, &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 = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL);
    ERRCHECK(result);

    /*
        Create DSP units for each type of noise we want.
    */
    result = FMOD_System_CreateDSPByType(system, FMOD_DSP_TYPE_OSCILLATOR, &dsp);
    ERRCHECK(result);
    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_OSCILLATOR_RATE, 440.0f);
    ERRCHECK(result);

    printf("======================================================================\n");
    printf("GenerateTone Example.  Copyright (c) Firelight Technologies 2004-2011.\n");
    printf("======================================================================\n\n");
    printf("\n");
    printf("Press '1' to play a sine wave\n");
    printf("Press '2' to play a square wave\n");
    printf("Press '3' to play a triangle wave\n");
    printf("Press '4' to play a saw wave\n");
    printf("Press '5' to play a white noise\n");
    printf("Press 's' to stop channel\n");
    printf("\n");
    printf("Press 'v'/'V' to change channel volume\n");
    printf("Press 'f'/'F' to change channel frequency\n");
    printf("Press '['/']' to change channel pan\n");
    printf("Press 'Esc' to quit\n");
    printf("\n");

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

            switch (key)
            {
                case '1' :
                {
                    result = FMOD_System_PlayDSP(system, FMOD_CHANNEL_REUSE, dsp, TRUE, &channel);
                    
                    FMOD_Channel_SetVolume(channel, 0.5f);
                    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_OSCILLATOR_TYPE, 0);
                    ERRCHECK(result);
                    FMOD_Channel_SetPaused(channel, FALSE);
                    break;
                }
                case '2' :
                {
                    result = FMOD_System_PlayDSP(system, FMOD_CHANNEL_REUSE, dsp, TRUE, &channel);
                    FMOD_Channel_SetVolume(channel, 0.125f);
                    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_OSCILLATOR_TYPE, 1);
                    ERRCHECK(result);
                    FMOD_Channel_SetPaused(channel, FALSE);
                    break;
                }
                case '3' :
                {
                    result = FMOD_System_PlayDSP(system, FMOD_CHANNEL_REUSE, dsp, TRUE, &channel);
                    FMOD_Channel_SetVolume(channel, 0.5f);
                    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_OSCILLATOR_TYPE, 2);
                    ERRCHECK(result);
                    FMOD_Channel_SetPaused(channel, FALSE);
                    break;
                }
                case '4' :
                {
                    result = FMOD_System_PlayDSP(system, FMOD_CHANNEL_REUSE, dsp, TRUE, &channel);
                    FMOD_Channel_SetVolume(channel, 0.125f);
                    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_OSCILLATOR_TYPE, 4);
                    ERRCHECK(result);
                    FMOD_Channel_SetPaused(channel, FALSE);
                    break;
                }
                case '5' :
                {
                    result = FMOD_System_PlayDSP(system, FMOD_CHANNEL_REUSE, dsp, TRUE, &channel);
                    FMOD_Channel_SetVolume(channel, 0.25f);
                    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_OSCILLATOR_TYPE, 5);
                    ERRCHECK(result);
                    FMOD_Channel_SetPaused(channel, FALSE);
                    break;
                }
                case 's' :
                {
                    FMOD_Channel_Stop(channel);
                    break;
                }
                case 'v' :
                {
                    float volume;

                    FMOD_Channel_GetVolume(channel, &volume);
                    volume -= 0.1f;
                    FMOD_Channel_SetVolume(channel, volume);
                    break;
                }
                case 'V' :
                {
                    float volume;

                    FMOD_Channel_GetVolume(channel, &volume);
                    volume += 0.1f;
                    FMOD_Channel_SetVolume(channel, volume);
                    break;
                }
                case 'f' :
                {
                    float frequency;

                    FMOD_Channel_GetFrequency(channel, &frequency);
                    frequency -= 500.0f;
                    FMOD_Channel_SetFrequency(channel, frequency);
                    break;
                }
                case 'F' :
                {
                    float frequency;

                    FMOD_Channel_GetFrequency(channel, &frequency);
                    frequency += 500.0f;
                    FMOD_Channel_SetFrequency(channel, frequency);
                    break;
                }
                case '[' :
                {
                    float pan;

                    FMOD_Channel_GetPan(channel, &pan);
                    pan -= 0.1f;
                    FMOD_Channel_SetPan(channel, pan);
                    break;
                }
                case ']' :
                {
                    float pan;

                    FMOD_Channel_GetPan(channel, &pan);
                    pan += 0.1f;
                    FMOD_Channel_SetPan(channel, pan);
                    break;
                }
            }
        }

        FMOD_System_Update(system);

        {
            float frequency = 0, volume = 0, pan = 0;
            int playing = FALSE;

            if (channel)
            {
                FMOD_Channel_GetFrequency(channel, &frequency);
                FMOD_Channel_GetVolume(channel, &volume);
                FMOD_Channel_GetPan(channel, &pan);
                FMOD_Channel_IsPlaying(channel, &playing);
            }

            printf("Channel %s : Frequency %.1f Volume %.1f Pan %.1f  \r", playing ? "playing" : "stopped", frequency, volume, pan);
            fflush(stdout);
        }
        
        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Shut down
    */
    result = FMOD_DSP_Release(dsp);
    ERRCHECK(result);
    result = FMOD_System_Close(system);
    ERRCHECK(result);
    result = FMOD_System_Release(system);
    ERRCHECK(result);

    return 0;
}
Beispiel #3
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 = FMOD_System_GetVersion(system, &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 = FMOD_System_GetNumDrivers(system, &numdrivers);
    ERRCHECK(result);

    if (numdrivers == 0)
    {
        result = FMOD_System_SetOutput(system, FMOD_OUTPUTTYPE_NOSOUND);
        ERRCHECK(result);
    }
    else
    {
        result = FMOD_System_GetDriverCaps(system, 0, &caps, 0, &speakermode);
        ERRCHECK(result);

        result = FMOD_System_SetSpeakerMode(system, 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 = FMOD_System_SetDSPBufferSize(system, 1024, 10);
            ERRCHECK(result);
        }
#ifdef LOWLATENCY
        else
        {
            result = FMOD_System_SetDSPBufferSize(system, 256, 4);
        }
#endif
        result = FMOD_System_GetDriverInfo(system, 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 = FMOD_System_SetSoftwareFormat(system, 48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
            ERRCHECK(result);
        }
    }

    result = FMOD_System_Init(system, 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 = FMOD_System_SetSpeakerMode(system, FMOD_SPEAKERMODE_STEREO);
        ERRCHECK(result);
            
        result = FMOD_System_Init(system, 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 = FMOD_System_CreateSound(system, 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-2011.\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 = FMOD_System_RecordStart(system, 0, sound, TRUE);
    ERRCHECK(result);

    result = FMOD_Sound_GetLength(sound, &soundlength, FMOD_TIMEUNIT_PCM);
    ERRCHECK(result);

    /*
        Create a DSP effect to play with.
    */
    result = FMOD_System_CreateDSPByType(system, FMOD_DSP_TYPE_FLANGE, &dsp);
    ERRCHECK(result);
    result = FMOD_DSP_SetParameter(dsp, FMOD_DSP_FLANGE_RATE, 4.0f);
    ERRCHECK(result);
    result = FMOD_DSP_SetBypass(dsp, 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')
        {
            int bypass;
            FMOD_DSP_GetBypass(dsp, &bypass);
            FMOD_DSP_SetBypass(dsp, !bypass);
            if (bypass)
            {
                FMOD_REVERB_PROPERTIES prop = FMOD_PRESET_CONCERTHALL;
                FMOD_System_SetReverbProperties(system, &prop);
            }
            else
            {
                FMOD_REVERB_PROPERTIES prop = FMOD_PRESET_OFF;
                FMOD_System_SetReverbProperties(system, &prop);
            }
            printf("\n\n *** TURN DSP EFFECT %s ** \n\n", bypass ? "ON" : "OFF");
        }
        
        FMOD_System_GetRecordPosition(system, 0, &recordpos);
        ERRCHECK(result);

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

		if (samplesrecorded >= adjustedlatency && !channel)
		{
		    result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, 0, &channel);
            ERRCHECK(result);
            
            result = FMOD_Channel_AddDSP(channel, 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;
                }
            }

            FMOD_Channel_GetPosition(channel, &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 */
            {
                FMOD_Channel_SetFrequency(channel, 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 */
            {
                FMOD_Channel_SetFrequency(channel, RECORDRATE + (RECORDRATE / 50)); /* Increase speed by 2% */
                adjusting = 2;
            }
            else
            {
                FMOD_Channel_SetFrequency(channel, 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;
        
        FMOD_System_Update(system);

        Sleep(10);

    } while (key != 27);

    printf("\n");

    /*
        Shut down
    */
    result = FMOD_Sound_Release(sound);
    ERRCHECK(result);

    result = FMOD_System_Release(system);
    ERRCHECK(result);

    return 0;
}