Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

openFrameworks 007 - sound

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Login to see the comments

openFrameworks 007 - sound

  1. 1. openFrameworks Sound
  2. 2. SoundopenFrameworks has some great support forplaying sounds. Using the ofSoundPlayer class youcall loadSound once to load the sound file (i.e.mp3, wav) then call play to start playing.Using ofSoundStream you can generate sounds oruse a microphone to get an input stream.
  3. 3. Playing soundsPut a sound file into your data directory and use anofSoundPlayer to play the file.testApp.h testApp.cppclass testApp : public ofBaseApp{ public: void testApp::setup(){ ofSoundPlayer my_sound; my_sound.loadSound("clong.mp3"); ofSoundPlayer my_singer; }} void testApp::keyReleased(int key){ my_sound.play(); }
  4. 4. Multi playA ofSoundPlayer can play the same sound objectmultiple times simultaniously by setting themultiplay flag. Call setMultiPlay(bool) to setmultiplay on or off. With the code below, trypressing your space bar multiple times. void testApp::setup(){ ofBackground(33,33,33); my_sound.loadSound("clong.mp3"); my_singer.loadSound("violet.mp3"); my_sound.setMultiPlay(true); my_singer.setMultiPlay(true); } void testApp::keyReleased(int key){ my_sound.play(); }
  5. 5. Volume and panningTo change the volume of a single sound usemy_sound.setVolume(float). A value of 0.0 meansno sound, 1.0 full volume. To pan the sound fromleft to right use my_sound.setPan(float). A value of-1.0 means play only left speaker, a value of 1.0means play only right speaker. void testApp::mouseReleased(int x, int y, int button){ if(button == 0) { my_sound.setPan(-1.0); } else { my_sound.setPan(1.0); } }
  6. 6. PauseTo temporarily stop a sound you callmy_sound.setPaused(bool). void testApp::keyReleased(int key){ if(key == p) { my_sound.setPaused(true); } }
  7. 7. LoopIf you want the sound to repeat over an d over setit to loop mode using my_sound.setLoop(bool). void testApp::keyReleased(int key){ if(key == l) { my_sound.setLoop(true); } }
  8. 8. Changing the speedWith my_sound.setSpeed(float) you change howfast the sound is played back. A value near 0.0 isreally slooooww and the higher the faster. void testApp::keyReleased(int key){ if(key == n) { my_sound.setSpeed(0.1f); } else if(key == g) { my_sound.setSpeed(40.0f); } }
  9. 9. Set positionUsing my_sound.setPosition(float) you can scrubthe playhead. A value of 0.0 brings you to the startof the sound. Value 1.0 to the end. void testApp::update(){ my_sound.setPosition((float)ofGetMouseX()/ofGetWidth()); }
  10. 10. ofSoundPlayervoid loadSound(string fileName, bool stream = false)void unloadSound()void play()void stop()void setVolume(float vol)void setPan(float vol)void setSpeed(bool speed)void setPaused(bool paused)void setLoop(bool loop)void setMultiPlay(bool mp)void setPosition(float pos)
  11. 11. HelpersThese two functions operate on all sounds.void ofSoundStopAll()void ofSoundSetVolume(float vol)
  12. 12. Generating soundsWith the ofSoundStream class you can generatesound or get sound from a microphone into yourapplication file.Starting with OF-007 we have a new function inour testApp called audioIn. This function is calledperiodically with samples from the input device.
  13. 13. Generating soundsTo get input from a microphone you call setup(...)and create an audioIn(..) function. testApp.h class testApp : public ofBaseApp{ public: ofSoundStream my_sstream; void audioIn(float* input, int bufferSize, int nChannels); } testApp.cpp void testApp::setup(){ my_sstream.setup(this, 0, 2, 44100, 256, 4); }
  14. 14. roxluwww.roxlu.com

×