SlideShare une entreprise Scribd logo
1  sur  14
openFrameworks
     Sound
Sound
openFrameworks has some great support for
playing sounds. Using the ofSoundPlayer class you
call loadSound once to load the sound file (i.e.
mp3, wav) then call play to start playing.

Using ofSoundStream you can generate sounds or
use a microphone to get an input stream.
Playing sounds
Put a sound file into your data directory and use an
ofSoundPlayer to play the file.


testApp.h                           testApp.cpp
class 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();
                                     }
Multi play
A ofSoundPlayer can play the same sound object
multiple times simultaniously by setting the
multiplay flag. Call setMultiPlay(bool) to set
multiplay on or off. With the code below, try
pressing 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();
            }
Volume and panning
To change the volume of a single sound use
my_sound.setVolume(float). A value of 0.0 means
no sound, 1.0 full volume. To pan the sound from
left to right use my_sound.setPan(float). A value of
-1.0 means play only left speaker, a value of 1.0
means 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);
          
 }
          }
Pause
To temporarily stop a sound you call
my_sound.setPaused(bool).



             void testApp::keyReleased(int key){
             
 if(key == 'p') {
             
 
      my_sound.setPaused(true);
             
 }
             }
Loop
If you want the sound to repeat over an d over set
it to loop mode using my_sound.setLoop(bool).



              void testApp::keyReleased(int key){
              
 if(key == 'l') {
              
 
      my_sound.setLoop(true);
              
 }
              }
Changing the speed
With my_sound.setSpeed(float) you change how
fast the sound is played back. A value near 0.0 is
really 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);
              
 }
              }
Set position
Using my_sound.setPosition(float) you can scrub
the playhead. A value of 0.0 brings you to the start
of the sound. Value 1.0 to the end.




         void testApp::update(){
         
 my_sound.setPosition((float)ofGetMouseX()/ofGetWidth());
         }
ofSoundPlayer
void 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)
Helpers

These two functions operate on all sounds.



void ofSoundStopAll()

void ofSoundSetVolume(float vol)
Generating sounds
With the ofSoundStream class you can generate
sound or get sound from a microphone into your
application file.

Starting with OF-007 we have a new function in
our testApp called audioIn. This function is called
periodically with samples from the input device.
Generating sounds
To 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);
      }
roxlu
www.roxlu.com

Contenu connexe

Tendances (7)

Augeas
AugeasAugeas
Augeas
 
The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.8 book - Part 55 of 202The Ring programming language version 1.8 book - Part 55 of 202
The Ring programming language version 1.8 book - Part 55 of 202
 
Listing program
Listing programListing program
Listing program
 
[EN] Ada Lovelace Day 2014 - Tampon run
[EN] Ada Lovelace Day 2014  - Tampon run[EN] Ada Lovelace Day 2014  - Tampon run
[EN] Ada Lovelace Day 2014 - Tampon run
 
Slicing
SlicingSlicing
Slicing
 
Basics
BasicsBasics
Basics
 
earthquake.gem
earthquake.gemearthquake.gem
earthquake.gem
 

Plus de roxlu (9)

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - video
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphics
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - events
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3D
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utils
 
ofxFlashCommunication
ofxFlashCommunicationofxFlashCommunication
ofxFlashCommunication
 
openFrameworks freakDays S03E02 Tim Olden - Computational Candles
openFrameworks freakDays S03E02 Tim Olden - Computational CandlesopenFrameworks freakDays S03E02 Tim Olden - Computational Candles
openFrameworks freakDays S03E02 Tim Olden - Computational Candles
 
openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...
openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...
openFrameworks freakDay S03E02 Diederick Huijbers - C++/Physics/Cloth Animati...
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

openFrameworks 007 - sound

  • 2. Sound openFrameworks has some great support for playing sounds. Using the ofSoundPlayer class you call loadSound once to load the sound file (i.e. mp3, wav) then call play to start playing. Using ofSoundStream you can generate sounds or use a microphone to get an input stream.
  • 3. Playing sounds Put a sound file into your data directory and use an ofSoundPlayer to play the file. testApp.h testApp.cpp class 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. Multi play A ofSoundPlayer can play the same sound object multiple times simultaniously by setting the multiplay flag. Call setMultiPlay(bool) to set multiplay on or off. With the code below, try pressing 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. Volume and panning To change the volume of a single sound use my_sound.setVolume(float). A value of 0.0 means no sound, 1.0 full volume. To pan the sound from left to right use my_sound.setPan(float). A value of -1.0 means play only left speaker, a value of 1.0 means 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. Pause To temporarily stop a sound you call my_sound.setPaused(bool). void testApp::keyReleased(int key){ if(key == 'p') { my_sound.setPaused(true); } }
  • 7. Loop If you want the sound to repeat over an d over set it to loop mode using my_sound.setLoop(bool). void testApp::keyReleased(int key){ if(key == 'l') { my_sound.setLoop(true); } }
  • 8. Changing the speed With my_sound.setSpeed(float) you change how fast the sound is played back. A value near 0.0 is really 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. Set position Using my_sound.setPosition(float) you can scrub the playhead. A value of 0.0 brings you to the start of the sound. Value 1.0 to the end. void testApp::update(){ my_sound.setPosition((float)ofGetMouseX()/ofGetWidth()); }
  • 10. ofSoundPlayer void 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. Helpers These two functions operate on all sounds. void ofSoundStopAll() void ofSoundSetVolume(float vol)
  • 12. Generating sounds With the ofSoundStream class you can generate sound or get sound from a microphone into your application file. Starting with OF-007 we have a new function in our testApp called audioIn. This function is called periodically with samples from the input device.
  • 13. Generating sounds To 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); }

Notes de l'éditeur

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n