You are on page 1of 3

Generating Audio Using C++

Getting Started
Generating audio in the form of a sine wave through the use of a programming language is something that you, a
novice programmer, will understand better after underatking the process yourself. The process includes installing
an IDE, adding code, integrating a graphical audio interface (GUI), and including a mute button. Creating this
audio will allow you to further understand how to create and modify your own sound projects or VST software.
Failure to follow these instructions will result in faulty code and a dysfunctional program.
Requirements
Computer with Windows operating system Speakers or Headphones
JUCE software with C++ compiler Internet Connection

Installation of juce and C++ compiler


1. Download the JUCE library from https://www.juce.com/get-juce.
2. Open software then click New Project.
3. Select Create Simple Audio App.
4. Enter the name of your project.

Adding The Code


1. Declare the sine wave variables as float data within the header file that was created.
WARNING: Declaring the variables as anything other than float will result in pitch difficulties.

2. Set the sine wave variables within the private class to the following:

private:

m_amplitude = 0.5;
m_frequency = 500;
m_phase = 0.0;
Fig. 1
m_time = 0.0; Smith, Hunter.
m_deltaTime = 1 / sampleRate;

Fig. 1 shows the sine wave that the member variables have created.

3. Initialize the function prepareToPlay() within the cpp file using the void data type with parameters of (int
samplesPerBlock, double sampleRate) override.

4. Initialize a pointer to the output buffer by calling getWritePointer() for each channel.

5. Increase the time variable by deltaTime using the sine wave formula to generate values in the last loop.

6. Set int sampleRate equal to .0000226.


WARNING: Excluding any of the zeroes will result in a low quality sound.
7. Define the parameters of the output sound using
data type of double.

See fig. 2 for the specific values of each data type.

Fig. 2
Smith, Hunter.
Gui Integration
1. Include the Slider and Label classes in the main .cpp file (see fig.3).
2. Create the class members in the private section.

3. Initialize the GUI elements within the class


constructor (see fig.4).

Fig. 3
Smith, Hunter.

Fig. 4
Smith, Hunter.

Mute button
1. Declare the function buttonClicked as a void data type.
WARNING: Failure to include a mute button results in sound until the program is terminated.

2. Include the if statement for the button to be equal to the


address of muteButton, which will pause the sound.

3. Set the parameter of the addAndMakeVisible class as


m_muteButton.

4. Declare void buttonClicked as an override function.

Fig. 5
Smith, Hunter.

2 C, Oleh. Part 1. Lets write a simple sine wave generator with C++ and JUCE. Medium, https://medium.com/@olehch/lets-write-a-simple-sine-wave-generator-with-c-
and-juce-c8ab42d1f54f. Accessed 12 Oct 2017.

Lampert, G. Basic C Tone Generator. Code Review, 8 Mar 2015, https://codereview.stackexchange.com/questions/83504/basic-c-tone-generator. Accessed 10 Oct 2017.

3 Rouse, Margaret. GUI ( graphical user interface). Tech Target, Oct 2006, http://searchwindevelopment.techtarget.com/definition/GUI. Accessed 12 Oct 2017