SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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
 

Más de roxlu

openFrameworks 007 - video
openFrameworks 007 - videoopenFrameworks 007 - video
openFrameworks 007 - videoroxlu
 
openFrameworks 007 - graphics
openFrameworks 007 - graphicsopenFrameworks 007 - graphics
openFrameworks 007 - graphicsroxlu
 
openFrameworks 007 - events
openFrameworks 007 - eventsopenFrameworks 007 - events
openFrameworks 007 - eventsroxlu
 
openFrameworks 007 - 3D
openFrameworks 007 - 3DopenFrameworks 007 - 3D
openFrameworks 007 - 3Droxlu
 
openFrameworks 007 - GL
openFrameworks 007 - GL openFrameworks 007 - GL
openFrameworks 007 - GL roxlu
 
openFrameworks 007 - utils
openFrameworks 007 - utilsopenFrameworks 007 - utils
openFrameworks 007 - utilsroxlu
 
ofxFlashCommunication
ofxFlashCommunicationofxFlashCommunication
ofxFlashCommunicationroxlu
 
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 Candlesroxlu
 
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...roxlu
 

Más 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...
 

Último

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 

Último (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 

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); }

Notas del editor

  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