示例#1
0
文件: main.c 项目: chandonnet/FTB2015
int main(int argc, char *argv[])
{
    FMOD_CHANNEL    *channel[2] = { 0,0 };
    FMOD_RESULT       result;
    int               key, outputrate, slot = 0, count, numdrivers;
    unsigned int      version;
    
    printf("=============================================================================\n");
    printf("Granular Synthesis SetDelay example.\n");
    printf("Copyright (c) Firelight Technologies 2004-2014.\n");
    printf("=============================================================================\n");
    printf("\n");
    printf("TOGGLE #define USE_STREAMS ON/OFF TO SWITCH BETWEEN STREAMS/STATIC SAMPLES.\n");
    printf("Press space to pause, Esc to quit\n");
    printf("\n");
    
    /*
        ===============================================================================================================
        RECOMMENDED STARTUP SEQUENCE BEGIN
        ===============================================================================================================
    */

    result = FMOD_System_Create(&gSystem);
    ERRCHECK(result);
    
    result = FMOD_System_GetVersion(gSystem, &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_GetNumDrivers(gSystem, &numdrivers);
    ERRCHECK(result);

    if (numdrivers == 0)
    {
        result = FMOD_System_SetOutput(gSystem, FMOD_OUTPUTTYPE_NOSOUND);
        ERRCHECK(result);
    }
    else
    {
        FMOD_CAPS caps;
        FMOD_SPEAKERMODE speakermode;
        char name[256];
        
        result = FMOD_System_GetDriverCaps(gSystem, 0, &caps, 0, &speakermode);
        ERRCHECK(result);

        result = FMOD_System_SetSpeakerMode(gSystem, 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(gSystem, 1024, 10);
            ERRCHECK(result);
        }

        result = FMOD_System_GetDriverInfo(gSystem, 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(gSystem, 48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0,0, FMOD_DSP_RESAMPLER_LINEAR);
            ERRCHECK(result);
        }
    }

    result = FMOD_System_Init(gSystem, 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(gSystem, FMOD_SPEAKERMODE_STEREO);
        ERRCHECK(result);
            
        result = FMOD_System_Init(gSystem, 100, FMOD_INIT_NORMAL, 0);/* ... and re-init. */
        ERRCHECK(result);
    }
    
    /*
        ===============================================================================================================
        RECOMMENDED STARTUP SEQUENCE END
        ===============================================================================================================
    */
        
    result = FMOD_System_GetSoftwareFormat(gSystem, &outputrate, 0,0,0,0,0);
    ERRCHECK(result);   
   
#if !defined(USE_STREAMS)
    for (count = 0; count < NUMSOUNDS; count++)
    {
        result = FMOD_System_CreateSound(gSystem, soundname[count], FMOD_IGNORETAGS, 0, &sound[count]);
        ERRCHECK(result);
    }
#endif

    /*
        Kick off the first 2 sounds.  First one is immediate, second one will be triggered to start after the first one.
    */
    channel[slot] = queue_next_sound(outputrate, channel[1-slot], rand()%NUMSOUNDS, slot);
    slot = 1-slot;  /* flip */
    channel[slot] = queue_next_sound(outputrate, channel[1-slot], rand()%NUMSOUNDS, slot);
    slot = 1-slot;  /* flip */

    /*
        Main loop.
    */
    do
    {
        int isplaying;
        static int paused = 0;

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

            switch (key)
            {
                case ' ' :
                {
                    set_paused(channel, paused);
                    paused = !paused;
                    break;
                }
            }
        }

        FMOD_System_Update(gSystem);

        /*
            Replace the sound that just finished with a new sound, to create endless seamless stitching!
        */
        result = FMOD_Channel_IsPlaying(channel[slot], &isplaying);
        if (!isplaying && !paused)
        {
            printf("sound %d finished, start a new one\n", slot);
            #ifdef USE_STREAMS
            /* 
                Release the sound that isn't playing any more. 
            */
            result = FMOD_Sound_Release(sound[slot]);       
            ERRCHECK(result);
            sound[slot] = 0;
            #endif
   
            /*
                Replace sound that just ended with a new sound, queued up to trigger exactly after the other sound ends.
            */
            channel[slot] = queue_next_sound(outputrate, channel[1-slot], rand()%NUMSOUNDS, slot);
            slot = 1-slot;  /* flip */
        }
        
        Sleep(10);  /* If you wait too long, ie longer than the length of the shortest sound, you will get gaps. */

    } while (key != 27);

    printf("\n");

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

    return 0;
}
示例#2
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;    
    int                    iscoreaudio = 0;

    /*
        Global Settings
    */
    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;
    }

    /*
        Enumerate playback devices
    */

    result = FMOD_System_GetNumDrivers(system, &numdrivers);
    ERRCHECK(result);

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

        result = FMOD_System_GetDriverInfo(system, 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 = FMOD_System_SetDriver(system, driver);
    ERRCHECK(result);

    /*
        Enumerate record devices
    */

    result = FMOD_System_GetRecordNumDrivers(system, &numdrivers);
    ERRCHECK(result);

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

        result = FMOD_System_GetRecordDriverInfo(system, 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 = FMOD_System_SetSoftwareFormat(system, OUTPUTRATE, FMOD_SOUND_FORMAT_PCM16, 2, 0, FMOD_DSP_RESAMPLER_LINEAR);
    ERRCHECK(result);

    result = FMOD_System_Init(system, 32, FMOD_INIT_NORMAL, NULL);
    ERRCHECK(result);

    FMOD_System_GetSoftwareFormat(system, &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      = 2;
    exinfo.defaultfrequency = OUTPUTRATE;
    if (iscoreaudio)
    {
        exinfo.format = FMOD_SOUND_FORMAT_PCMFLOAT;
        exinfo.length = exinfo.defaultfrequency * sizeof(float) * exinfo.numchannels * 5;
    }
    else
    {
        exinfo.format = FMOD_SOUND_FORMAT_PCM16;
        exinfo.length = exinfo.defaultfrequency * sizeof(short) * exinfo.numchannels * 5;
    }

    result = FMOD_System_CreateSound(system, 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 = FMOD_System_RecordStart(system, recorddriver, sound, TRUE);
    ERRCHECK(result);
    
    Sleep(200);                         /* Give it some time to record something */
    
    result = FMOD_System_PlaySound(system, 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 = FMOD_Channel_SetVolume(channel, 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 = FMOD_Channel_GetSpectrum(channel, 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("\rDetected rate : %7.1f -> %7.1f hz.  Detected musical note. %-3s (%7.1f hz)", dominanthz, ((float)bin + 0.99f) * BINSIZE, note[dominantnote], notefreq[dominantnote]);
        fflush(stdout);

        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;
}
示例#3
0
void FMOD_Startup (void)
{
	FMOD_CAPS			caps;
	FMOD_SPEAKERMODE	speakermode;
	FMOD_OUTPUTTYPE		fmod_output;
	unsigned int		version;
	int numdrivers;
	char name[256];

	result = FMOD_System_Create(&fmod_system);
    FMOD_ERROR(result, true, false);

    result = FMOD_System_GetVersion(fmod_system, &version);
    FMOD_ERROR(result, true, false);

    if (version < FMOD_VERSION)
    {
		Con_Printf("\nFMOD version incorrect, found v%1.2f, requires v%1.2f or newer\n", version, FMOD_VERSION);
        return;
    }

	result = FMOD_System_GetNumDrivers(fmod_system, &numdrivers);
	FMOD_ERROR(result, true, false);

	if (numdrivers == 0)
	{
		result = FMOD_System_SetOutput(fmod_system, FMOD_OUTPUTTYPE_NOSOUND);
		FMOD_ERROR(result, true, false);
	}
	else
	{
		result = FMOD_System_SetOutput(fmod_system, FMOD_OUTPUTTYPE_AUTODETECT);
		FMOD_ERROR(result, true, false);

		result = FMOD_System_GetDriverCaps(fmod_system, 0, &caps, NULL, &speakermode);
		FMOD_ERROR(result, true, false);

		// set the user selected speaker mode
		result = FMOD_System_SetSpeakerMode(fmod_system, FMOD_SPEAKERMODE_STEREO /*speakermode*/);
		FMOD_ERROR(result, true, false);

		if (caps & FMOD_CAPS_HARDWARE_EMULATED)
		{
			// the user has the 'Acceleration' slider set to off. this is really bad for latency!
			result = FMOD_System_SetDSPBufferSize(fmod_system, 1024, 10);
			FMOD_ERROR(result, true, false);

			Con_Printf("\nHardware Acceleration is turned off!\n");
		}

		result = FMOD_System_GetDriverInfo(fmod_system, 0, name, 256, NULL);
		FMOD_ERROR(result, true, false);
		
		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(fmod_system, 48000, FMOD_SOUND_FORMAT_PCMFLOAT, 0, 0, FMOD_DSP_RESAMPLER_LINEAR);
			FMOD_ERROR(result, true, false);
		}
	}

	result = FMOD_System_GetSoftwareChannels(fmod_system, &SND_SoftwareChannels);
	FMOD_ERROR(result, true, false);

	result = FMOD_System_GetSoftwareFormat(fmod_system, &SND_Rate, NULL, NULL, NULL, NULL, &SND_Bits);
	FMOD_ERROR(result, true, false);

	result = FMOD_System_Init(fmod_system, MAX_CHANNELS, FMOD_INIT_NORMAL, NULL);
    FMOD_ERROR(result, true, false);

	if (result == FMOD_ERR_OUTPUT_CREATEBUFFER)
	{
		// the speaker mode selected isn't supported by this soundcard. Switch it back to stereo...
		result = FMOD_System_SetSpeakerMode(fmod_system, FMOD_SPEAKERMODE_STEREO);
		FMOD_ERROR(result, true, false);

		// ... and re-init.
		result = FMOD_System_Init(fmod_system, MAX_CHANNELS, FMOD_INIT_NORMAL, NULL);
		FMOD_ERROR(result, true, false);
	}

	result = FMOD_System_GetSpeakerMode(fmod_system, &speakermode);
	FMOD_ERROR(result, true, false);

	result = FMOD_System_GetOutput(fmod_system, &fmod_output);
	FMOD_ERROR(result, true, false);

	result = FMOD_System_GetHardwareChannels(fmod_system, &SND_HardwareChannels);
	FMOD_ERROR(result, true, false);


	// print all the sound information to the console

	Con_Printf("\nFMOD version %01x.%02x.%02x\n", (FMOD_VERSION >> 16) & 0xff, (FMOD_VERSION >> 8) & 0xff, FMOD_VERSION & 0xff);

	switch(fmod_output)
	{
		case FMOD_OUTPUTTYPE_NOSOUND:
			Con_Printf("using No Sound\n");
			break;

		case FMOD_OUTPUTTYPE_DSOUND:
			Con_Printf("using Microsoft DirectSound\n");
			break;

		case FMOD_OUTPUTTYPE_WINMM:
			Con_Printf("using Windows Multimedia\n");
			break;

		case FMOD_OUTPUTTYPE_WASAPI:
			Con_Printf("using Windows Audio Session API\n");
			break;

		case FMOD_OUTPUTTYPE_ASIO:
			Con_Printf("using Low latency ASIO\n");
			break;
	}
	
	Con_Printf("   software channels: %i\n", SND_SoftwareChannels);
	Con_Printf("   hardware channels: %i\n", SND_HardwareChannels);
	Con_Printf("   %i bits/sample\n", SND_Bits);
	Con_Printf("   %i bytes/sec\n", SND_Rate);

	switch(speakermode)
	{
		case FMOD_SPEAKERMODE_RAW:
			Con_Printf("Speaker Output: Raw\n");
			break;

		case FMOD_SPEAKERMODE_MONO:
			Con_Printf("Speaker Output: Mono\n");
			break;

		case FMOD_SPEAKERMODE_STEREO:
			Con_Printf("Speaker Output: Stereo\n");
			break;

		case FMOD_SPEAKERMODE_QUAD:
			Con_Printf("Speaker Output: Quad\n");
			break;

		case FMOD_SPEAKERMODE_SURROUND:
			Con_Printf("Speaker Output: Surround\n");
			break;

		case FMOD_SPEAKERMODE_5POINT1:
			Con_Printf("Speaker Output: 5.1\n");
			break;

		case FMOD_SPEAKERMODE_7POINT1:
			Con_Printf("Speaker Output: 7.1\n");
			break;

		case FMOD_SPEAKERMODE_SRS5_1_MATRIX:
			Con_Printf("Speaker Output: Stereo compatible\n");
			break;

		case FMOD_SPEAKERMODE_MYEARS:
			Con_Printf("Speaker Output: Headphones\n");
			break;

		default:
			Con_Printf("Speaker Output: Unknown\n");
	}

	if(SND_File.data)
		free(SND_File.data);
	SND_File.data = NULL;
	SND_File.length = 0;
	strcpy(SND_File.filename, "\0");


	CDA_Startup(true);


	SND_Initialised = true;
}