bool ModuleIrisAudio::MePlay(wstring filePath, int volume, int rate){ string sfilepath = WStringToString(filePath); const char* fpath = sfilepath.c_str(); if (channels == 0){ if (meChannel != NULL){ BOOL isPlaying; FMOD_Channel_IsPlaying(meChannel, &isPlaying); if (isPlaying) FMOD_Channel_Stop(meChannel); } } FMOD_RESULT result; result = FMOD_System_CreateStream(fmodSystem, fpath, FMOD_DEFAULT, 0, &me); if (result != FMOD_OK) return false; result = FMOD_System_PlaySound(fmodSystem, FMOD_CHANNEL_FREE, me, true, &meChannel); if (result != FMOD_OK) return false; FMOD_Channel_SetMode(meChannel, FMOD_LOOP_NORMAL); FMOD_Channel_SetVolume(meChannel, volume / 100.0f); float frequancy; FMOD_Channel_GetFrequency(meChannel, &frequancy); FMOD_Channel_SetFrequency(meChannel, frequancy * (rate / 100.0)); FMOD_Channel_SetPaused(meChannel, FALSE); return true; }
INT32 I_StartSound(sfxenum_t id, UINT8 vol, UINT8 sep, UINT8 pitch, UINT8 priority) { FMOD_SOUND *sound; FMOD_CHANNEL *chan; INT32 i; float frequency; sound = (FMOD_SOUND *)S_sfx[id].data; I_Assert(sound != NULL); FMR(FMOD_System_PlaySound(fsys, FMOD_CHANNEL_FREE, sound, true, &chan)); if (sep == 0) sep = 1; FMR(FMOD_Channel_SetVolume(chan, (vol / 255.0) * (sfx_volume / 31.0))); FMR(FMOD_Channel_SetPan(chan, (sep - 128) / 127.0)); FMR(FMOD_Sound_GetDefaults(sound, &frequency, NULL, NULL, NULL)); FMR(FMOD_Channel_SetFrequency(chan, (pitch / 128.0) * frequency)); FMR(FMOD_Channel_SetPriority(chan, priority)); //UNREFERENCED_PARAMETER(priority); //FMR(FMOD_Channel_SetPriority(chan, 1 + ((0xff-vol)>>1))); // automatic priority 1 - 128 based on volume (priority 0 is music) FMR(FMOD_Channel_GetIndex(chan, &i)); FMR(FMOD_Channel_SetPaused(chan, false)); return i; }
// ---------------------------------------------------------------------------- void ofxSoundPlayerFMOD::setSpeed(float spd) { if (getIsPlaying() == true){ FMOD_Channel_SetFrequency(channel, internalFreq * spd); } speed = spd; }
// ---------------------------------------------------------------------------- void ofxSoundPlayerFMOD::play() { // if it's a looping sound, we should try to kill it, no? // or else people will have orphan channels that are looping if (bLoop == true){ FMOD_Channel_Stop(channel); } // if the sound is not set to multiplay, then stop the current, // before we start another if (!bMultiPlay){ FMOD_Channel_Stop(channel); } FMOD_System_PlaySound(sys, FMOD_CHANNEL_FREE, sound, bPaused, &channel); FMOD_Channel_GetFrequency(channel, &internalFreq); FMOD_Channel_SetVolume(channel,volume); FMOD_Channel_SetPan(channel,pan); FMOD_Channel_SetFrequency(channel, internalFreq * speed); FMOD_Channel_SetMode(channel, (bLoop == true) ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF); //fmod update() should be called every frame - according to the docs. //we have been using fmod without calling it at all which resulted in channels not being able //to be reused. we should have some sort of global update function but putting it here //solves the channel bug FMOD_System_Update(sys); }
void whitgl_sound_play(int id, float adjust) { int index = -1; int i; for(i=0; i<num_sounds; i++) { if(sounds[i].id == id) { index = i; continue; } } if(index == -1) { WHITGL_LOG("ERR Cannot find sound %d", id); return; } FMOD_RESULT result = FMOD_System_PlaySound(fmodSystem, FMOD_CHANNEL_FREE, sounds[index].sound, true, &sounds[index].channel); _whitgl_sound_errcheck("FMOD_System_PlaySound", result); float defaultFrequency; result = FMOD_Sound_GetDefaults(sounds[index].sound, &defaultFrequency, NULL, NULL, NULL); _whitgl_sound_errcheck("FMOD_Sound_GetDefaults", result); result = FMOD_Channel_SetFrequency(sounds[index].channel, defaultFrequency*adjust); _whitgl_sound_errcheck("FMOD_Channel_SetFrequency", result); result = FMOD_Channel_SetPaused(sounds[index].channel, false); _whitgl_sound_errcheck("FMOD_Channel_SetPaused", result); }
boolean I_SetSongSpeed(float speed) { FMOD_RESULT e; float frequency; if (!music_stream) return false; if (speed > 250.0f) speed = 250.0f; //limit speed up to 250x #ifdef HAVE_LIBGME // Try to set GME speed if (gme) { gme_set_tempo(gme, speed); return true; } #endif // Try to set Mod/Midi speed e = FMOD_Sound_SetMusicSpeed(music_stream, speed); if (e == FMOD_ERR_FORMAT) { // Just change pitch instead for Ogg/etc. FMR(FMOD_Sound_GetDefaults(music_stream, &frequency, NULL, NULL, NULL)); FMR_MUSIC(FMOD_Channel_SetFrequency(music_channel, speed*frequency)); } else FMR_MUSIC(e); return true; }
void AudioManager::Play( const AudioManager::AudioType Type, const string& ID, const float Volume, const float Pitch, const float Pan, const int32_t LoopCount, const int32_t Priority, const FMOD_CHANNELINDEX ChannelIndex ) { // Create local variables. float Frequency = Null; FMOD_CHANNEL* Channel = nullptr; unordered_map< string, SoundData >::iterator AudioMapIterator; // Check arguments. if( Type == MaxAudioTypes ) throw exception(); // Playback specified audio sample or stream. if( Initialized ) { AudioMapIterator = AudioMaps[ Type ].Instance.find( ID ); if( AudioMapIterator == AudioMaps[ Type ].Instance.end() ) throw exception(); if( FMOD_System_PlaySound( SystemInstance, ChannelIndex, AudioMapIterator->second.Instance, true, &Channel ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetChannelGroup( Channel, AudioMapIterator->second.Group ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetLoopCount( Channel, LoopCount ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetPriority( Channel, Priority ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetPan( Channel, Pan ) != FMOD_OK ) throw exception(); if( FMOD_Channel_GetFrequency( Channel, &Frequency ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetFrequency( Channel, ( Frequency * Pitch ) ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetVolume( Channel, Volume ) != FMOD_OK ) throw exception(); if( FMOD_Channel_SetPaused( Channel, false ) != FMOD_OK ) throw exception(); } }
void AudioPlayer::setSoundFreq( SoundID sID , float freqFactor ) { if ( sID == ERROR_SOUND_ID ) return; Channel* channel = getChannel( sID ); if ( !channel ) return; float freq; FMOD_Channel_GetFrequency( channel , &freq ); FMOD_Channel_SetFrequency( channel , freqFactor * freq ); }
void sound_play(eSound snd, float vol, float pan, float freq) { static Sound* s; if (!s) s = &((Data*)SDLazy_GetData())->sound; FMOD_System_PlaySound(s->system, FMOD_CHANNEL_FREE, s->mp3[snd], 0, &s->chan); FMOD_Channel_SetVolume(s->chan, vol); FMOD_Channel_SetPan(s->chan, pan); FMOD_Channel_SetFrequency(s->chan, freq); FMOD_System_Update(s->system); }
// seems to never be called on an invalid channel (calls I_SoundIsPlaying first?) // so I'm not gonna worry about it. void I_UpdateSoundParams(INT32 handle, UINT8 vol, UINT8 sep, UINT8 pitch) { FMOD_CHANNEL *chan; FMOD_SOUND *sound; float frequency; FMR(FMOD_System_GetChannel(fsys, handle, &chan)); FMR(FMOD_Channel_SetVolume(chan, (vol / 255.0) * (sfx_volume / 31.0))); FMR(FMOD_Channel_SetPan(chan, (sep - 128) / 127.0)); FMR(FMOD_Channel_GetCurrentSound(chan, &sound)); FMR(FMOD_Sound_GetDefaults(sound, &frequency, NULL, NULL, NULL)); FMR(FMOD_Channel_SetFrequency(chan, (pitch / 128.0) * frequency)); //FMR(FMOD_Channel_SetPriority(chan, 1 + ((0xff-vol)>>1))); // automatic priority 1 - 128 based on volume (priority 0 is music) }
SoundID AudioPlayer::playSound( ResID id , float volume , bool beLoop , float freqFactor ) { SoundRes* res = getSound( id ); if ( res == NULL ) return ERROR_SOUND_ID; FMOD_CHANNEL* channel; FMOD_System_PlaySound( mFmodSys , FMOD_CHANNEL_FREE , res->sound , false , &channel ); FMOD_Channel_SetVolume( channel , volume * res->volume * mSoundVolume ); FMOD_Channel_SetCallback( channel ,ChannelCallBack ); float freq; FMOD_Channel_GetFrequency( channel , &freq ); FMOD_Channel_SetFrequency( channel , freqFactor * freq ); if ( beLoop ) FMOD_Channel_SetMode( channel ,FMOD_LOOP_NORMAL ); return registerChannel( channel ); }
void PlayStereoFrequency( short player, short which, short freq ) { if( soundOn ) { float oldFreq; FMOD_System_PlaySound( fmodSystem, FMOD_CHANNEL_FREE, sound[which], true, &soundChannel[player] ); // SetPan pans the sound from -1 (full left) to 1 (full right). // which means player 0 should be -1 and player 1 should be 1. // Except hard panning sounds kind of bad so player 0 should be // -0.75 and player 1 should be 0.75. if( playerWindowVisible[1] ) { FMOD_Channel_SetPan( soundChannel[player], -0.75f + 1.5f * player ); } else { FMOD_Channel_SetPan( soundChannel[player], 0.0f ); } FMOD_Channel_GetFrequency( soundChannel[player], &oldFreq ); FMOD_Channel_SetFrequency( soundChannel[player], oldFreq * (16 + freq)/ 16 ); FMOD_Channel_SetPaused( soundChannel[player], false ); } }
// ---------------------------------------------------------------------------- void ofSoundPlayer::play(){ // if it's a looping sound, we should try to kill it, no? // or else people will have orphan channels that are looping if (bLoop == true){ FMOD_Channel_Stop(channel); } // if the sound is not set to multiplay, then stop the current, // before we start another if (!bMultiPlay){ FMOD_Channel_Stop(channel); } FMOD_System_PlaySound(sys, FMOD_CHANNEL_FREE, sound, bPaused, &channel); FMOD_Channel_GetFrequency(channel, &internalFreq); FMOD_Channel_SetVolume(channel,volume); FMOD_Channel_SetPan(channel,pan); FMOD_Channel_SetFrequency(channel, internalFreq * speed); FMOD_Channel_SetMode(channel, (bLoop == true) ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF); }
FMOD_RESULT bmx_FMOD_Channel_SetFrequency(MAX_FMOD_CHANNEL *channel, float frequency) { return FMOD_Channel_SetFrequency(channel->channel, frequency); }
int main(int argc, char *argv[]) { FMOD_SYSTEM *system; FMOD_SOUND *sound; FMOD_CHANNEL *channel = 0; FMOD_RESULT result; int key; unsigned int version; printf("===================================================================\n"); printf("NetStream Example. Copyright (c) Firelight Technologies 2004-2011.\n"); printf("===================================================================\n\n"); if (argc < 2) { printf("Usage: netstream <url>\n"); printf("Example: netstream http://www.fmod.org/stream.mp3\n\n"); return -1; } /* 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, 1, FMOD_INIT_NORMAL, 0); ERRCHECK(result); /* Bump up the file buffer size a little bit for netstreams (to account for lag). */ result = FMOD_System_SetStreamBufferSize(system, 64*1024, FMOD_TIMEUNIT_RAWBYTES); ERRCHECK(result); result = FMOD_System_CreateSound(system, argv[1], FMOD_HARDWARE | FMOD_2D | FMOD_CREATESTREAM | FMOD_NONBLOCKING, 0, &sound); ERRCHECK(result); printf("Press space to pause, Esc to quit\n\n"); /* Main loop */ do { unsigned int ms = 0, percent = 0; int playing = FALSE; int paused = FALSE; int starving = FALSE; FMOD_OPENSTATE openstate; if (!channel) { result = FMOD_System_PlaySound(system, FMOD_CHANNEL_FREE, sound, FALSE, &channel); } if (_kbhit()) { key = _getch(); switch (key) { case ' ' : { if (channel) { int paused; FMOD_Channel_GetPaused(channel, &paused); FMOD_Channel_SetPaused(channel, !paused); } break; } } } FMOD_System_Update(system); for (;;) { FMOD_TAG tag; if (FMOD_Sound_GetTag(sound, 0, -1, &tag) != FMOD_OK) { break; } if (tag.datatype == FMOD_TAGDATATYPE_STRING) { printf("%s = %s (%d bytes) \n", tag.name, tag.data, tag.datalen); } else if (tag.type == FMOD_TAGTYPE_FMOD) { if (!strcmp(tag.name, "Sample Rate Change")) { FMOD_Channel_SetFrequency(channel, *((float *)tag.data)); } } } result = FMOD_Sound_GetOpenState(sound, &openstate, &percent, &starving, 0); ERRCHECK(result); if (channel) { result = FMOD_Channel_GetPaused(channel, &paused); ERRCHECK(result); result = FMOD_Channel_IsPlaying(channel, &playing); ERRCHECK(result); result = FMOD_Channel_GetPosition(channel, &ms, FMOD_TIMEUNIT_MS); ERRCHECK(result); result = FMOD_Channel_SetMute(channel, starving); ERRCHECK(result); } printf("Time %02d:%02d:%02d : %s : (%3d%%%) %s \r", ms / 1000 / 60, ms / 1000 % 60, ms / 10 % 100, openstate == FMOD_OPENSTATE_BUFFERING ? "Buffering..." : openstate == FMOD_OPENSTATE_CONNECTING ? "Connecting..." : paused ? "Paused " : playing ? "Playing " : "Stopped ", percent, starving ? "STARVING" : " "); Sleep(10); } while (key != 27); printf("\n"); printf("Shutting down.\n"); if (channel) { result = FMOD_Channel_Stop(channel); ERRCHECK(result); } /* If we pressed escape before it is ready, wait for it to finish opening before we release it. */ do { FMOD_OPENSTATE openstate; result = FMOD_Sound_GetOpenState(sound, &openstate, 0, 0, 0); ERRCHECK(result); if (openstate == FMOD_OPENSTATE_READY) { break; } printf("Waiting for sound to finish opening before trying to release it....\r"); Sleep(10); } while (1); /* Shut down */ result = FMOD_Sound_Release(sound); ERRCHECK(result); result = FMOD_System_Close(system); ERRCHECK(result); result = FMOD_System_Release(system); ERRCHECK(result); return 0; }
FMOD_CHANNEL *queue_next_sound(int outputrate, FMOD_CHANNEL *playingchannel, int newindex, int slot) { FMOD_RESULT result; FMOD_CHANNEL *newchannel; FMOD_SOUND *newsound; #ifdef USE_STREAMS /* Create a new stream */ FMOD_CREATESOUNDEXINFO info; memset(&info, 0, sizeof(FMOD_CREATESOUNDEXINFO)); info.cbsize = sizeof(FMOD_CREATESOUNDEXINFO); info.suggestedsoundtype = FMOD_SOUND_TYPE_OGGVORBIS; result = FMOD_System_CreateStream(gSystem, soundname[newindex], FMOD_IGNORETAGS | FMOD_LOWMEM, &info, &sound[slot]); ERRCHECK(result); newsound = sound[slot]; #else /* Use an existing sound that was passed into us */ newsound = sound[newindex]; #endif result = FMOD_System_PlaySound(gSystem, FMOD_CHANNEL_FREE, newsound, 1, &newchannel); ERRCHECK(result); result = FMOD_Channel_SetSpeakerMix(newchannel, 1,1,1,1,1,1,1,1); ERRCHECK(result); if (playingchannel) { unsigned int hi = 0, lo = 0, sound_length; float sound_frequency; FMOD_SOUND *playingsound; /* Get the start time of the playing channel. */ result = FMOD_Channel_GetDelay(playingchannel, FMOD_DELAYTYPE_DSPCLOCK_START, &hi, &lo); ERRCHECK(result); printf("playing sound started at %d\n", lo); /* Grab the length of the playing sound, and its frequency, so we can caluate where to place the new sound on the time line. */ result = FMOD_Channel_GetCurrentSound(playingchannel, &playingsound); ERRCHECK(result); result = FMOD_Sound_GetLength(playingsound, &sound_length, FMOD_TIMEUNIT_PCM); ERRCHECK(result); result = FMOD_Channel_GetFrequency(playingchannel, &sound_frequency); ERRCHECK(result); /* Now calculate the length of the sound in 'output samples'. Ie if a 44khz sound is 22050 samples long, and the output rate is 48khz, then we want to delay by 24000 output samples. */ sound_length *= outputrate; sound_length /= (int)sound_frequency; FMOD_64BIT_ADD(hi, lo, 0, sound_length); /* Add output rate adjusted sound length, to the clock value of the sound that is currently playing */ result = FMOD_Channel_SetDelay(newchannel, FMOD_DELAYTYPE_DSPCLOCK_START, hi, lo); /* Set the delay of the new sound to the end of the old sound */ ERRCHECK(result); } { unsigned int hi = 0, lo = 0; float val, variation; /* Randomize pitch/volume to make it sound more realistic / random. */ FMOD_Channel_GetFrequency(newchannel, &val); variation = (((float)(rand()%10000) / 5000.0f) - 1.0f); /* -1.0 to +1.0 */ val *= (1.0f + (variation * 0.02f)); /* @22khz, range fluctuates from 21509 to 22491 */ FMOD_Channel_SetFrequency(newchannel, val); FMOD_Channel_GetVolume(newchannel, &val); variation = ((float)(rand()%10000) / 10000.0f); /* 0.0 to 1.0 */ val *= (1.0f - (variation * 0.2f)); /* 0.8 to 1.0 */ FMOD_Channel_SetVolume(newchannel, val); FMOD_Channel_GetDelay(newchannel, FMOD_DELAYTYPE_DSPCLOCK_START, &hi, &lo); printf("new sound to start at %d (slot %d)\n", lo, slot); } result = FMOD_Channel_SetPaused(newchannel, FALSE); ERRCHECK(result); return newchannel; }
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; }
//------------------------------------------------------------ void ofFmodSoundPlayer::setSpeed(float spd){ if (isPlaying()){ FMOD_Channel_SetFrequency(channel, internalFreq * spd); } speed = spd; }
void sound::speed(float f) { FMOD_Channel_SetFrequency(_chan, _origFreq * f); }
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; }