void FMOD_Sound::addEffect(int module_id, int type, float value) { float normalized_value = value / 100.0f; switch(type) { case Reverb: FMOD::DSP * reverb; system->createDSPByType(FMOD_DSP_TYPE_REVERB, &reverb); channelMap[module_id]->addDSP(reverb, 0); reverb->setParameter(FMOD_DSP_REVERB_ROOMSIZE, normalized_value); reverbMap[module_id] = reverb; break; case Pitch: FMOD::DSP * pitch; system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &pitch); channelMap[module_id]->addDSP(pitch, 0); pitch->setParameter(0, normalized_value); pitchMap[module_id] = pitch; break; case Distortion: FMOD::DSP * distortion; system->createDSPByType(FMOD_DSP_TYPE_DISTORTION, &distortion); channelMap[module_id]->addDSP(distortion, 0); distortion->setParameter(0, normalized_value); distortionMap[module_id] = distortion; break; case Echo: FMOD::DSP * echo; system->createDSPByType(FMOD_DSP_TYPE_ECHO, &echo); channelMap[module_id]->addDSP(echo, 0); echo->setParameter(0, normalized_value); echoMap[module_id] = echo; break; case Flange: FMOD::DSP * flange; system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &flange); channelMap[module_id]->addDSP(flange, 0); flange->setParameter(0, normalized_value); flangeMap[module_id] = flange; break; default: break; } }
int main(int argc, char *argv[]) { int numSounds = 1; FMOD::System *system; FMOD::Sound *sound[numSounds]; FMOD::Sound *applause; FMOD::Channel *channel[numSounds]; FMOD::Channel *applauseChannel; FMOD::DSP *dsppitch; FMOD::DSP *dsplowpass = 0; FMOD::DSP *dsphighpass = 0; FMOD::DSP *dspecho = 0; FMOD::DSP *dspflange = 0; FMOD::DSP *dspdistortion = 0; FMOD::DSP *dspchorus = 0; FMOD::DSP *dspparameq = 0; FMOD_RESULT result; int key; unsigned int version; float pan = 0; float volume; float frequency; int tempFrequency = 0; int tempPitch = 0; int frequencyCount = 0; int pitchCount = 0; int tempoChange = 0; float speed; float pitch = 1; float originalFrequency; string line; int lineCount = 0; int inc = 0; int count; float frequencyArray[numSounds]; float frequencyCountArray[numSounds]; float pitchfArray[numSounds]; float volumeArray[numSounds]; float panArray[numSounds]; float pitchf = 1.0f; /* 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->init(32, FMOD_INIT_NORMAL, 0); ERRCHECK(result); result = system->createSound("../Config/Seven Nation Army Drum.mp3", FMOD_SOFTWARE | FMOD_LOOP_NORMAL | FMOD_2D, 0, &sound[0]); ERRCHECK(result); printf("===============================================================================\n"); printf(" Maestro \n"); printf("===============================================================================\n"); printf("Press '1' to play left speaker only\n"); printf("Press '2' to play right speaker only\n"); printf("Press '3' to play from both speakers\n"); printf("Press '[' to pan sound left\n"); printf("Press ']' to pan sound right\n"); printf("Press 'v/V' to increase volume\n"); printf("Press 'd/D' to decrease volume\n"); printf("Press '6' pitch octave down\n"); printf("Press '7' pitch semitone down\n"); printf("Press '8' pitch semitone up\n"); printf("Press '9' pitch octave up\n"); printf("Press 'Esc' to quit\n"); printf("Press 'n' pitch scale down\n"); printf("Press 'm' pitch scale up\n"); printf("\n"); for (count = 0; count < numSounds; count++) { result = system->playSound(FMOD_CHANNEL_FREE, sound[count], false, &channel[count]); ERRCHECK(result); bool paused; channel[0]->getPaused(&paused); ERRCHECK(result); paused = !paused; result = channel[0]->setPaused(paused); ERRCHECK(result); } //Create the DSP effects. result = system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsppitch); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_LOWPASS, &dsplowpass); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_HIGHPASS, &dsphighpass); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dspecho); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &dspflange); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_DISTORTION, &dspdistortion); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_CHORUS, &dspchorus); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_PARAMEQ, &dspparameq); ERRCHECK(result); for (count = 0; count < numSounds; count++) { channel[count]->getVolume(&volume); channel[count]->setVolume(.3); channel[count]->getVolume(&volume); } printf("Initial Volume: %f \n", volume); channel[0]->getFrequency(&frequency); channel[0]->getFrequency(&originalFrequency); printf("Initial Frequency: %f \n", frequency); /* Main loop. */ do { ifstream myfile; myfile.open("../../Config/groupb.txt"); while ( myfile.good() ) { //printf("Line Count: %d \n", lineCount); for(int i = 0; i < lineCount; i++){ getline (myfile,line); } getline (myfile,line); if (line.compare("start") == 0){ bool paused; channel[0]->getPaused(&paused); ERRCHECK(result); paused = !paused; result = channel[0]->setPaused(paused); ERRCHECK(result); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("iVol") == 0){ channel[0]->getVolume(&volume); channel[0]->setVolume(1.0f); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("dVol") == 0){ channel[0]->getVolume(&volume); channel[0]->setVolume(.1); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("iPitch") == 0){ result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); inc++; pitch = pow(1.059f,inc); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitch); ERRCHECK(result); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("dPitch") == 0){ result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); inc--; pitch = pow(1.059,inc); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitch); ERRCHECK(result); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("left") == 0){ result = channel[0]->setSpeakerMix(1.0f, 0, 0, 0, 0, 0, 0, 0); ERRCHECK(result); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("right") == 0){ result = channel[0]->setSpeakerMix(0, 1.0f, 0, 0, 0, 0, 0, 0); ERRCHECK(result); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("forward") == 0){ result = channel[0]->setSpeakerMix(1.0f, 1.0f, 0, 0, 0, 0, 0, 0); ERRCHECK(result); lineCount++; //printf("Line Count: %d \n", lineCount); break; } if (line.compare("pitch") == 0){ bool active; result = dsppitch->getActive(&active); ERRCHECK(result); if (active) { result = dsppitch->remove(); ERRCHECK(result); } else { result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, 1.2f); ERRCHECK(result); } lineCount++; break; } if (line.compare("pause") == 0){ bool paused; channel[0]->getPaused(&paused); ERRCHECK(result); paused = !paused; result = channel[0]->setPaused(paused); ERRCHECK(result); lineCount++; break; } if (line.compare("lowpass") == 0){ bool active; result = dsplowpass->getActive(&active); ERRCHECK(result); if (active) { result = dsplowpass->remove(); ERRCHECK(result); } else { result = system->addDSP(dsplowpass, 0); ERRCHECK(result); } lineCount++; break; } if (line.compare("highpass") == 0){ bool active; result = dsphighpass->getActive(&active); ERRCHECK(result); if (active) { result = dsphighpass->remove(); ERRCHECK(result); } else { result = system->addDSP(dsphighpass, 0); ERRCHECK(result); } lineCount++; break; } if (line.compare("echo") == 0){ bool active; result = dspecho->getActive(&active); ERRCHECK(result); if (active) { result = dspecho->remove(); ERRCHECK(result); } else { result = system->addDSP(dspecho, 0); ERRCHECK(result); result = dspecho->setParameter(FMOD_DSP_ECHO_DELAY, 50.0f); ERRCHECK(result); } lineCount++; break; } if (line.compare("flange") == 0){ bool active; result = dspflange->getActive(&active); ERRCHECK(result); if (active) { result = dspflange->remove(); ERRCHECK(result); } else { result = system->addDSP(dspflange, 0); ERRCHECK(result); } lineCount++; break; } if (line.compare("distortion") == 0){ bool active; result = dspdistortion->getActive(&active); ERRCHECK(result); if (active) { result = dspdistortion->remove(); ERRCHECK(result); } else { result = system->addDSP(dspdistortion, 0); ERRCHECK(result); result = dspdistortion->setParameter(FMOD_DSP_DISTORTION_LEVEL, 0.8f); ERRCHECK(result); } lineCount++; break; } if (line.compare("chorus") == 0){ bool active; result = dspchorus->getActive(&active); ERRCHECK(result); if (active) { result = dspchorus->remove(); ERRCHECK(result); } else { result = system->addDSP(dspchorus, 0); ERRCHECK(result); } lineCount++; break; } if (line.compare("parameq") == 0){ bool active; result = dspparameq->getActive(&active); ERRCHECK(result); if (active) { result = dspparameq->remove(); ERRCHECK(result); } else { result = system->addDSP(dspparameq, 0); ERRCHECK(result); result = dspparameq->setParameter(FMOD_DSP_PARAMEQ_CENTER, 5000.0f); ERRCHECK(result); result = dspparameq->setParameter(FMOD_DSP_PARAMEQ_GAIN, 0.0f); ERRCHECK(result); } lineCount++; break; } if (line.compare("iTempo") == 0){ //44100 //62366.816406 channel[0]->getFrequency(&frequency); float base = 2.0f; float newTempo = frequency * pow(base,(6.0f/12.0f)); float pitchf = 1.0f; pitchf = pow(.9438f,6); result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); channel[0]->setFrequency(newTempo); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitchf); ERRCHECK(result); channel[0]->getFrequency(&frequency); lineCount++; break; } if (line.compare("dTempo") == 0){ channel[0]->getFrequency(&frequency); float base = 2.0f; float newTempo = frequency * pow(base,(-6.0f/12.0f)); float pitchf = 1.0f; result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); channel[0]->setFrequency(newTempo); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitchf); ERRCHECK(result); channel[0]->getFrequency(&frequency); lineCount++; break; } if (line.compare("quit") == 0){ key = 27; lineCount++; //printf("Line Count: %d \n", lineCount); break; } } myfile.close(); if (kbhit()) { key = getch(); switch (key) { case 's' : { bool paused; channel[0]->getPaused(&paused); ERRCHECK(result); paused = !paused; result = channel[0]->setPaused(paused); ERRCHECK(result); break; } case '1' : { // Play Sound From Left Speakers for (count = 0; count < numSounds; count++) { channel[count]->getVolume(&volume); result = channel[count]->setSpeakerMix(1.0f, 0, 0, 0, 0, 0, 0, 0); ERRCHECK(result); } break; } case '2' : { // Play Sound From Right Speakers for (count = 0; count < numSounds; count++) { channel[count]->getVolume(&volume); printf("Volume: %f\n", volume); result = channel[count]->setSpeakerMix(0, 1.0f, 0, 0, 0, 0, 0, 0); ERRCHECK(result); } break; } case '3' : { // Play Sound From Both Speakers for (count = 0; count < numSounds; count++) { channel[count]->getVolume(&volume); result = channel[count]->setSpeakerMix(1.0f, 1.0f, 0, 0, 0, 0, 0, 0); ERRCHECK(result); } break; } case '4' : { // Decrmenent Tempo //if(frequencyCount > -12){ //if (tempoChange + pitchCount > -12 && abs(pitchCount) < 12 && tempoChange > -12) { if (pitchf < 1.98f) { frequencyCount--; tempoChange++; printf("Pitch Count: %d\n", pitchCount); printf("Tempo Count: %d\n", tempoChange); channel[0]->getFrequency(&frequency); //printf("Z Initial Frequency: %f \n", frequency); float base = 2.0f; //printf("inc by: %f \n", pow(base,(-1.0f/12.0f))); float newTempo = frequency * pow(base,(-1.0f/12.0f)); //float pitchf = 1.0f; //if (pitchCount == 0 && tempoChange == 0) { // pitchf = 1.0f; //} if (pitchf == 1.0f) { pitchf = pow(1.059f,abs(tempoChange + pitchCount)); } else if (pitchf > 1.0f) { pitchf = pow(1.059f,abs(tempoChange + pitchCount)); } else if (pitchf < 1.0f) { pitchf = pow(.9438f,abs(tempoChange + pitchCount)); } printf("pitchf: %f\n", pitchf); printf("Frequency: %f \n", newTempo); result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); channel[0]->setFrequency(newTempo); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitchf); ERRCHECK(result); } else { printf("Reached Min Tempo\n"); } break; } case '5' : { // Incrmenent Tempo if (pitchf > 0.52f) { frequencyCount++; tempoChange--; printf("Pitch Count: %d\n", pitchCount); printf("Tempo Count: %d\n", tempoChange); channel[0]->getFrequency(&frequency); //printf("Z Initial Frequency: %f \n", frequency); float base = 2.0f; float newTempo = frequency * pow(base,(1.0f/12.0f)); if (pitchf == 1.0f) { //printf("tempo one\n"); pitchf = pow(.9438f,abs(tempoChange + pitchCount)); } else if (pitchf > 1.0f) { //printf("tempo inc\n"); pitchf = pow(1.059f,abs(tempoChange + pitchCount)); } else if (pitchf < 1.0f) { //printf("tempo dec\n"); pitchf = pow(.9438f,abs(tempoChange + pitchCount)); } printf("pitchf: %f\n", pitchf); printf("Frequency: %f \n", newTempo); result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); channel[0]->setFrequency(newTempo); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitchf); ERRCHECK(result); } else { printf("Reached Max Tempo\n"); } break; } case '6' : { // Pitch Shift Down Octave bool active; result = dsppitch->getActive(&active); ERRCHECK(result); if (active) { result = dsppitch->remove(); ERRCHECK(result); } else { result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, 0.5f); ERRCHECK(result); } break; } case '7' : { // Pitch Shift Down One Note bool active; result = dsppitch->getActive(&active); ERRCHECK(result); if (active) { result = dsppitch->remove(); ERRCHECK(result); } else { result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, .9438f); ERRCHECK(result); } break; } case '8' : { // Pitch Shift Up One Note bool active; result = dsppitch->getActive(&active); ERRCHECK(result); if (active) { result = dsppitch->remove(); ERRCHECK(result); } else { result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, 1.059f); ERRCHECK(result); } break; } case '9' : { // Pitch Shift Up Octave bool active; result = dsppitch->getActive(&active); ERRCHECK(result); if (active) { result = dsppitch->remove(); ERRCHECK(result); } else { result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, 2.0f); ERRCHECK(result); } break; } case 'v' : case 'V' : { // Increment Volume for (count = 0; count < numSounds; count++) { channel[count]->getVolume(&volume); volume += 0.1f; if (volume > 1) { volume = 1; } //printf("Volume: %f \n", volume); channel[count]->setVolume(volume); } break; } case 'd' : case 'D' : { // Decrement Volume for (count = 0; count < numSounds; count++) { channel[count]->getVolume(&volume); volume -= 0.1f; if (volume < 0) { volume = 0; } //printf("Volume: %f \n", volume); channel[count]->setVolume(volume); } break; } case '[' : { // Pan Left for (count = 0; count < numSounds; count++) { channel[count]->getPan(&pan); pan -= 0.1f; if (pan < -1) { pan = -1; } channel[count]->setPan(pan); } break; } case ']' : { // Pan Right for (count = 0; count < numSounds; count++) { channel[count]->getPan(&pan); pan += 0.1f; if (pan > 1) { pan = 1; } channel[count]->setPan(pan); } break; } case 'n' : { // Decremental Pitch if (pitchf > 0.52f) { frequencyCount--; pitchCount--; printf("Pitch Count: %d\n", pitchCount); printf("Tempo Count: %d\n", tempoChange); if (pitchf == 1.0f) { pitchf = pow(.9438f,abs(tempoChange + pitchCount)); } else if (pitchf > 1.0f) { pitchf = pow(1.059f,tempoChange + pitchCount); } else if (pitchf < 1.0f) { pitchf = pow(.9438f,abs(tempoChange + pitchCount)); } printf("pitchf: %f\n", pitchf); result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitchf); ERRCHECK(result); } else { printf("Reached Min Pitch\n"); } break; } case 'm' : { // Incremental Pitch if (pitchf < 1.98f) { frequencyCount++; pitchCount++; printf("Pitch Count: %d\n", pitchCount); printf("Tempo Count: %d\n", tempoChange); if (pitchf == 1.0f) { pitchf = pow(1.059f,abs(tempoChange + pitchCount)); } else if (pitchf > 1.0f) { pitchf = pow(1.059f,tempoChange + pitchCount); } else if (pitchf < 1.0f) { pitchf = pow(.9438f,abs(tempoChange + pitchCount)); } printf("pitchf: %f\n", pitchf); result = dsppitch->remove(); ERRCHECK(result); result = system->addDSP(dsppitch, 0); ERRCHECK(result); result = dsppitch->setParameter(FMOD_DSP_PITCHSHIFT_PITCH, pitchf); ERRCHECK(result); } else { printf("Reached Max Pitch\n"); } break; } case 'r' : { // Lowpass bool active; result = dsplowpass->getActive(&active); ERRCHECK(result); if (active) { result = dsplowpass->remove(); ERRCHECK(result); } else { result = system->addDSP(dsplowpass, 0); ERRCHECK(result); } break; } case 't' : { // Highpass bool active; result = dsphighpass->getActive(&active); ERRCHECK(result); if (active) { result = dsphighpass->remove(); ERRCHECK(result); } else { result = system->addDSP(dsphighpass, 0); ERRCHECK(result); } break; } case 'y' : { // Echo bool active; result = dspecho->getActive(&active); ERRCHECK(result); if (active) { result = dspecho->remove(); ERRCHECK(result); } else { result = system->addDSP(dspecho, 0); ERRCHECK(result); result = dspecho->setParameter(FMOD_DSP_ECHO_DELAY, 50.0f); ERRCHECK(result); } break; } case 'u' : { // Flange bool active; result = dspflange->getActive(&active); ERRCHECK(result); if (active) { result = dspflange->remove(); ERRCHECK(result); } else { result = system->addDSP(dspflange, 0); ERRCHECK(result); } break; } case 'i' : { // Distortion bool active; result = dspdistortion->getActive(&active); ERRCHECK(result); if (active) { result = dspdistortion->remove(); ERRCHECK(result); } else { result = system->addDSP(dspdistortion, 0); ERRCHECK(result); result = dspdistortion->setParameter(FMOD_DSP_DISTORTION_LEVEL, 0.8f); ERRCHECK(result); } break; } case 'o' : { // Chorus bool active; result = dspchorus->getActive(&active); ERRCHECK(result); if (active) { result = dspchorus->remove(); ERRCHECK(result); } else { result = system->addDSP(dspchorus, 0); ERRCHECK(result); } break; } case 'p' : { // Parameq bool active; result = dspparameq->getActive(&active); ERRCHECK(result); if (active) { result = dspparameq->remove(); ERRCHECK(result); } else { result = system->addDSP(dspparameq, 0); ERRCHECK(result); result = dspparameq->setParameter(FMOD_DSP_PARAMEQ_CENTER, 5000.0f); ERRCHECK(result); result = dspparameq->setParameter(FMOD_DSP_PARAMEQ_GAIN, 0.0f); ERRCHECK(result); } break; } } } system->update(); { int channelsplaying = 0; bool dsppitch_active; dsppitch ->getActive(&dsppitch_active); system->getChannelsPlaying(&channelsplaying); } fflush(stdout); Sleep(10); } while (key != 27); { for (count = 0; count < numSounds; count++) { //channel[count]->getFrequency(&frequency); channel[count]->getVolume(&volume); int count; //float initFreq = frequency; float initVol = volume; for (count = 0; count < 200; count++) { //printf("Volume: %f \n", volume); //channel[count]->setFrequency(frequency); channel[count]->setVolume(volume); volume -= (initVol / 200.0f); //frequency -= (initFreq / 200.0f); Sleep(2); } } } printf("\n"); /* Shut down */ channel[0]->setFrequency(originalFrequency); result = dsppitch->remove(); ERRCHECK(result); result = dspparameq->remove(); ERRCHECK(result); result = dspchorus->remove(); ERRCHECK(result); result = dspdistortion->remove(); ERRCHECK(result); result = dspflange->remove(); ERRCHECK(result); result = dspecho->remove(); ERRCHECK(result); result = dsplowpass->remove(); ERRCHECK(result); result = dsphighpass->remove(); ERRCHECK(result); for (count = 0; count < numSounds; count++) { result = sound[count]->release(); ERRCHECK(result); } result = system->createSound("../../Config/Recital Crowd Applause.aif", FMOD_SOFTWARE, 0, &applause); ERRCHECK(result); wait(.5); result = system->playSound(FMOD_CHANNEL_FREE, applause, false, &applauseChannel); ERRCHECK(result); wait(10); result = applause->release(); ERRCHECK(result); result = system->close(); ERRCHECK(result); result = system->release(); ERRCHECK(result); return 0; }
int main(int argc, char *argv[]) { FMOD::System *system = 0; FMOD::Sound *sound = 0; FMOD::Channel *channel = 0; FMOD::DSP *dsplowpass = 0; FMOD::DSP *dsphighpass = 0; FMOD::DSP *dspecho = 0; FMOD::DSP *dspflange = 0; FMOD::DSP *dspdistortion = 0; FMOD::DSP *dspchorus = 0; FMOD::DSP *dspparameq = 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); return 0; } result = system->init(32, FMOD_INIT_NORMAL, 0); ERRCHECK(result); result = system->createSound("../media/drumloop.wav", FMOD_SOFTWARE, 0, &sound); ERRCHECK(result); printf("=================================================================\n"); printf("Effects Example. Copyright (c) Firelight Technologies 2004-2011.\n"); printf("=================================================================\n"); printf("\n"); printf("Press SPACE to paused/unpause sound.\n"); printf("Press 1 to toggle dsplowpass effect.\n"); printf("Press 2 to toggle dsphighpass effect.\n"); printf("Press 3 to toggle dspecho effect.\n"); printf("Press 4 to toggle dspflange effect.\n"); printf("Press 5 to toggle dspdistortion effect.\n"); printf("Press 6 to toggle dspchorus effect.\n"); printf("Press 7 to toggle dspparameq effect.\n"); printf("Press 'Esc' to quit\n"); printf("\n"); result = system->playSound(FMOD_CHANNEL_FREE, sound, false, &channel); ERRCHECK(result); /* Create some effects to play with. */ result = system->createDSPByType(FMOD_DSP_TYPE_LOWPASS, &dsplowpass); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_HIGHPASS, &dsphighpass); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dspecho); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_FLANGE, &dspflange); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_DISTORTION, &dspdistortion); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_CHORUS, &dspchorus); ERRCHECK(result); result = system->createDSPByType(FMOD_DSP_TYPE_PARAMEQ, &dspparameq); ERRCHECK(result); /* Main loop. */ do { if (kbhit()) { key = getch(); switch (key) { case ' ' : { bool paused; channel->getPaused(&paused); ERRCHECK(result); paused = !paused; result = channel->setPaused(paused); ERRCHECK(result); break; } case '1' : { bool active; result = dsplowpass->getActive(&active); ERRCHECK(result); if (active) { result = dsplowpass->remove(); ERRCHECK(result); } else { result = system->addDSP(dsplowpass, 0); ERRCHECK(result); } break; } case '2' : { bool active; result = dsphighpass->getActive(&active); ERRCHECK(result); if (active) { result = dsphighpass->remove(); ERRCHECK(result); } else { result = system->addDSP(dsphighpass, 0); ERRCHECK(result); } break; } case '3' : { bool active; result = dspecho->getActive(&active); ERRCHECK(result); if (active) { result = dspecho->remove(); ERRCHECK(result); } else { result = system->addDSP(dspecho, 0); ERRCHECK(result); result = dspecho->setParameter(FMOD_DSP_ECHO_DELAY, 50.0f); ERRCHECK(result); } break; } case '4' : { bool active; result = dspflange->getActive(&active); ERRCHECK(result); if (active) { result = dspflange->remove(); ERRCHECK(result); } else { result = system->addDSP(dspflange, 0); ERRCHECK(result); } break; } case '5' : { bool active; result = dspdistortion->getActive(&active); ERRCHECK(result); if (active) { result = dspdistortion->remove(); ERRCHECK(result); } else { result = system->addDSP(dspdistortion, 0); ERRCHECK(result); result = dspdistortion->setParameter(FMOD_DSP_DISTORTION_LEVEL, 0.8f); ERRCHECK(result); } break; } case '6' : { bool active; result = dspchorus->getActive(&active); ERRCHECK(result); if (active) { result = dspchorus->remove(); ERRCHECK(result); } else { result = system->addDSP(dspchorus, 0); ERRCHECK(result); } break; } case '7' : { bool active; result = dspparameq->getActive(&active); ERRCHECK(result); if (active) { result = dspparameq->remove(); ERRCHECK(result); } else { result = system->addDSP(dspparameq, 0); ERRCHECK(result); result = dspparameq->setParameter(FMOD_DSP_PARAMEQ_CENTER, 5000.0f); ERRCHECK(result); result = dspparameq->setParameter(FMOD_DSP_PARAMEQ_GAIN, 0.0f); ERRCHECK(result); } break; } } } system->update(); { bool paused = 0; bool dsplowpass_active; bool dsphighpass_active; bool dspecho_active; bool dspflange_active; bool dspdistortion_active; bool dspchorus_active; bool dspparameq_active; dsplowpass ->getActive(&dsplowpass_active); dsphighpass ->getActive(&dsphighpass_active); dspecho ->getActive(&dspecho_active); dspflange ->getActive(&dspflange_active); dspdistortion->getActive(&dspdistortion_active); dspchorus ->getActive(&dspchorus_active); dspparameq ->getActive(&dspparameq_active); if (channel) { result = channel->getPaused(&paused); if ((result != FMOD_OK) && (result != FMOD_ERR_INVALID_HANDLE) && (result != FMOD_ERR_CHANNEL_STOLEN)) { ERRCHECK(result); } } printf("%s : lowpass[%c] highpass[%c] echo[%c] flange[%c] dist[%c] chorus[%c] parameq[%c]\r", paused ? "Paused " : "Playing", dsplowpass_active ? 'x' : ' ', dsphighpass_active ? 'x' : ' ', dspecho_active ? 'x' : ' ', dspflange_active ? 'x' : ' ', dspdistortion_active ? 'x' : ' ', dspchorus_active ? 'x' : ' ', dspparameq_active ? 'x' : ' '); fflush(stdout); } Sleep(10); } while (key != 27); printf("\n"); /* Shut down */ result = sound->release(); ERRCHECK(result); result = system->close(); ERRCHECK(result); result = system->release(); ERRCHECK(result); return 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; }
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 = 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->init(32, FMOD_INIT_NORMAL, 0); ERRCHECK(result); /* Create an oscillator DSP unit for the tone. */ result = system->createDSPByType(FMOD_DSP_TYPE_OSCILLATOR, &dsp); ERRCHECK(result); result = dsp->setParameter(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 = system->playDSP(FMOD_CHANNEL_REUSE, dsp, true, &channel); channel->setVolume(0.5f); result = dsp->setParameter(FMOD_DSP_OSCILLATOR_TYPE, 0); ERRCHECK(result); channel->setPaused(false); break; } case '2' : { result = system->playDSP(FMOD_CHANNEL_REUSE, dsp, true, &channel); channel->setVolume(0.125f); result = dsp->setParameter(FMOD_DSP_OSCILLATOR_TYPE, 1); ERRCHECK(result); channel->setPaused(false); break; } case '3' : { result = system->playDSP(FMOD_CHANNEL_REUSE, dsp, true, &channel); channel->setVolume(0.5f); result = dsp->setParameter(FMOD_DSP_OSCILLATOR_TYPE, 2); ERRCHECK(result); channel->setPaused(false); break; } case '4' : { result = system->playDSP(FMOD_CHANNEL_REUSE, dsp, true, &channel); channel->setVolume(0.125f); result = dsp->setParameter(FMOD_DSP_OSCILLATOR_TYPE, 4); ERRCHECK(result); channel->setPaused(false); break; } case '5' : { result = system->playDSP(FMOD_CHANNEL_REUSE, dsp, true, &channel); channel->setVolume(0.25f); result = dsp->setParameter(FMOD_DSP_OSCILLATOR_TYPE, 5); ERRCHECK(result); channel->setPaused(false); break; } case 's' : { channel->stop(); break; } case 'v' : { float volume; channel->getVolume(&volume); volume -= 0.1f; channel->setVolume(volume); break; } case 'V' : { float volume; channel->getVolume(&volume); volume += 0.1f; channel->setVolume(volume); break; } case 'f' : { float frequency; channel->getFrequency(&frequency); frequency -= 500.0f; channel->setFrequency(frequency); break; } case 'F' : { float frequency; channel->getFrequency(&frequency); frequency += 500.0f; channel->setFrequency(frequency); break; } case '[' : { float pan; channel->getPan(&pan); pan -= 0.1f; channel->setPan(pan); break; } case ']' : { float pan; channel->getPan(&pan); pan += 0.1f; channel->setPan(pan); break; } } } system->update(); { float frequency = 0, volume = 0, pan = 0; bool playing = false; if (channel) { channel->getFrequency(&frequency); channel->getVolume(&volume); channel->getPan(&pan); channel->isPlaying(&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 = dsp->release(); ERRCHECK(result); result = system->close(); ERRCHECK(result); result = system->release(); ERRCHECK(result); return 0; }