Book Image

OpenFrameworks Essentials

Book Image

OpenFrameworks Essentials

Overview of this book

Table of Contents (19 chapters)
openFrameworks Essentials
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Capturing sound from a sound card's input and measuring its level


openFrameworks has an opportunity to capture live sound from a sound card's input, such as a microphone or a line input.

Note

The code in this section will work properly only if your computer has a sound input. Most probably, your laptop already has a built-in microphone. If your computer has no sound input, you can use an external sound card connected via USB or any other type of connection.

To enable sound capture and measure its level, perform the following steps:

  1. Add the declarations of a new function and a variable to the ofApp class:

    void audioIn(float *input, int bufferSize, int nChannels);
    float soundLevel;

    The first line declares the audioIn() function, which will be called by openFrameworks when a new part of an input sound has arrived from a sound card. We will consider its arguments in the next step. The second line defines a variable, soundLevel, which will store the current sound level value.

  2. Initialize the soundLevel...