Object oriented programming and building an Additive Synthesis plugin using Objective C

11
Applied Music Technology. Object oriented programming and building an Additive Synthesis plugin using Objective C Student: S.Scutt ID:10011159 Tutor: Dr.A.Seago. Module: MD5003 Introduction. The aim of this report is to describe the process of building a simple additive synthesis plug in for Mac OSX using the objective C programming language within the X-Code programming environment. Object Oriented Programming (OOP) and the fundamentals of Objective C will be explained as will a description of their use in the creation of an audio application in the form of an additive synthesis plug in whereby a user of the application can create different periodic wave shapes based upon the addition of multiple harmonics of differing amplitudes, the values of which, can be altered using a simple GUI with sliders and buttons. OOP, Objective C and X-Code. The project was developed using the Objective C programming language in the Mac OSX X-Code programming environment (version 4.2.) Objective C is a subset of the C language and is based upon the original C language and another messaging language developed in the 1970s. Object Oriented Programming is different from procedural programming languages like C and assembly in that everything in an OOP program is grouped into objects which can have messages sent to them and send messages between themselves. An example could be of 2 objects; a car low on petrol and the driver. Both are objects in the case of OOP, the driver sending messages to the car to perform the tasks of running the engine, turning on lights, accelerating etc and the car sending the driver messages such as needing petrol or oil or telling the driver they are going too fast. Objects can consist of variables of all data types and of functions, which consist of groups of variables and commands. In the consideration of a car as an object, it has many dozens of attributes and functions. Objective C allows us to introduce as many of these same objects as we like to 1 ID:10011159

description

Building an additive synthesis sound generator using Objective C.

Transcript of Object oriented programming and building an Additive Synthesis plugin using Objective C

Page 1: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

Student: S.Scutt

ID:10011159

Tutor: Dr.A.Seago.

Module: MD5003

Introduction.

The aim of this report is to describe the process of building a simple additive synthesis plug in for Mac OSX using the objective C programming language within the X-Code programming environment.

Object Oriented Programming (OOP) and the fundamentals of Objective C will be explained as will a description of their use in the creation of an audio application in the form of an additive synthesis plug in whereby a user of the application can create different periodic wave shapes based upon the addition of multiple harmonics of differing amplitudes, the values of which, can be altered using a simple GUI with sliders and buttons.

OOP, Objective C and X-Code.

The project was developed using the Objective C programming language in the Mac OSX X-Code programming environment (version 4.2.) Objective C is a subset of the C language and is based upon the original C language and another messaging language developed in the 1970s.

Object Oriented Programming is different from procedural programming languages like C and assembly in that everything in an OOP program is grouped into objects which can have messages sent to them and send messages between themselves. An example could be of 2 objects; a car low on petrol and the driver. Both are objects in the case of OOP, the driver sending messages to the car to perform the tasks of running the engine, turning on lights, accelerating etc and the car sending the driver messages such as needing petrol or oil or telling the driver they are going too fast. Objects can consist of variables of all data types and of functions, which consist of groups of variables and commands. In the consideration of a car as an object, it has many dozens of attributes and functions. Objective C allows us to introduce as many of these same objects as we like to the program. An occurrence of a class is known as an instance. Once your object has been initialised with its attributes and functions, it is possible to introduce several instances of this object to the program.

The X-Code software development suite is a powerful, fully integrated programming environment for Macintosh OSX giving the user access to both textual and graphical programming for the OSX and iOS environments using the Cocoa Application programming interface which is based upon Objective C. X-code allows for real time debugging through code analysis as it is typed. Any mistakes or discrepancies in the code will trigger symbols and coding messages for the user to correct from. Cocoa is a broad, rich development environment used widely in the Macintosh and iOS community.

1ID:10011159

Page 2: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

Project Idea: Objective C Additive synthesis generator with interactive GUI.

After some consideration about whether to create an application that plays and manipulates sound samples in some way or experiment with sound synthesis itself, the second option was chosen. Additive synthesis works on the principles of Joseph Fourier who discovered that any waveform shape could be created by adding sine waves of differing harmonics with different amplitudes (Russ, 2009.) Our tutor had provided some tutorials on getting started using graphical interfaces, controllers and audio. Cocoa programming in X-code relies on a “main” file very much like the C language as well as external files that are given the extension .h (as in C) for header files which contain initialisations of instances for objects and functions and .m for the files containing the functions of objects required for the program to be functional. In C++ these .m files would be labelled xxx.cpp. The two basic graphical controller and audio controller files we were given were called maincontroller (.h and .m files,) and audiocontroller (.h and .m files.)

The mainController files are where names of sliders, buttons, text fields and other controller objects and graphical elements are initialised. When opening a new cocoa project space to work in, a file called mainmenu.xib is created. This file gives the user a GUI template to work with incorporating menus, a window space and graphical, text and controller objects to work with. This is known as the Runtime Interface. An example of this with the object library to the right is shown in figure 1. The object library is a library of buttons, sliders, text fields and other controllers including the NSObject which links objects within the GUI to functions and variables within the code.

Figure 1. Mainmenu.xib. A GUI controller template showing window, menus and the objects library to the right.

2ID:10011159

Page 3: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

All controller objects are initialised and given names in maincontroller.h. Objects are given the prefix IBOutlet and functions are given the prefix IBAction. Variables are declared according to their type. Data types include int (integer), double (double precision floating point), float (floating point) and char (a character or an unsigned number between 0 and 255.)

An example of the objects, actions and variables is shown below in figure 2.

Figure 2. Maincontroller.h examples of objects, actions and variables.

The Audiocontroller files are in charge of turning audio on and off and generating the sine waves needed for the additive synthesis algorithm. These files call upon the AudioToolbox framework. X-code cocoa programming includes many frameworks (new frameworks are being added all the time.) A framework is a library that deals with low-level tasks and makes programming easier through enabling the programmer to deal with such aspects of graphics, audio and hardware with a minimum of having to understand exactly what is going on at the lower levels of the computer. The audio toolbox framework contains functions for reading, writing and streaming audio of different formats, gathering data from audio sample information such as metadata, amplitudes, audio spectrum information etc. as well as being able to handle external hardware issues such as USB MIDI.

3ID:10011159

Page 4: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

One of the most important files used from the audio toolbox is AUGraph. This is responsible for starting and stopping audio, connecting audio inputs and outputs and gathering information about the rendered audio. In order to generate waves for audio output, a callback function loads a table of numbers according to the trigonometric sine function into a buffer. This call-back calls for data from outside itself using pointers in order for it to refresh itself therefore when it has done what it does, it hands back control to the main program thereby saving processing power and memory.

The code that generates the sine wave is shown below:for (frame = 0;frame < inNumberFrames;++frame) { // this is a number of sine waves added together with harmonics of x 2,3,4,5 & 6 Float32 *data = (Float32 *) ioData->mBuffers[0].mData; (data) [frame] = (Float32) sin(2 * M_PI * (j/cycleLength)) + (Float32) amplitude1 * sin(2 * M_PI * 2 *(j/cycleLength)) + (Float32) amplitude2* sin(2 * M_PI * 3 *(j/cycleLength))+ (Float32) amplitude3* sin(2 * M_PI * 4 *(j/cycleLength)) + (Float32) amplitude4* sin(2 * M_PI * 5 *(j/cycleLength))+ (Float32) amplitude5* sin(2 * M_PI * 6 *(j/cycleLength)); // for a sine wave (left channel) data = (Float32 *) ioData->mBuffers[1].mData; (data) [frame] =(Float32) sin(2 * M_PI * (j/cycleLength)) + // for a sine wave (right channel) (Float32) amplitude1 * sin(2 * M_PI * 2 *(j/cycleLength)) + (Float32) amplitude2* sin(2 * M_PI * 3 *(j/cycleLength))+ (Float32) amplitude3* sin(2 * M_PI * 4 *(j/cycleLength))+ (Float32) amplitude4* sin(2 * M_PI * 5 *(j/cycleLength))+ (Float32) amplitude5* sin(2 * M_PI * 6 *(j/cycleLength)); // NSLog(@"%f",modulator1Frequency); j += 1.0; if (j > cycleLength) j -= cycleLength; } THIS->startingFrameCount = j;

It works by filling a data buffer with numbers generated by the trigonometric function sin(2 * M_PI * (j/cycleLength)) where cycleLength consists of the sample rate being used (44100Hz) divided by the value in Hz of the frequency slider. The variable j is an incrementing variable that reverses when it reaches the length of the sample rate ensuring that both negative and positive values of the sine wave are generated. Seven of these waves are summed together, each with a different harmonic and amplitudes that can be changed with sliders. There are four combinations of harmonics that can be generated and each type is triggered using GUI controller buttons, which affect a case statement. A case statement could be considered to work like a multipole switch. The case statement relies on a variable that executes different sets of instructions according to the value of a variable, which, in the case of this program, changes according to which button is pressed.

4ID:10011159

Page 5: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

Different buttons change the value of a variable, which in this case is called WaveFormType. The pseudo code syntax of a switch statement is shown below:switch (waveformType) { case (1): play the first set of harmonics break;

case (2):

play the second set of harmonics break; case(3):

play the third set of harmonics

break;

default:

do something else;

break;

The Synthesizer has one horizontal slider for changing the fundamental frequency of the wave, five vertical sliders for changing the amplitudes of the harmonics of the added sine waves, four buttons for selecting different combinations of harmonics and a stop button for halting the audio process.

The way the GUI controls communicate with the synthesis call-back is through the maincontroller files and through pointers. Pointers are variables that can be declared in the .h file, initialised in the .m file and accessed via other files. In the case of changing the fundamental frequency of the sine wave, a slider is declared in maincontroller.h as: IBOutlet NSSlider *frequencySlider;

and a function to change the frequency: - (IBAction)changeFrequencyValue: (id)sender;.

The change frequency value function is set up in maincontroller.m to put the value of the slider into a variable called frequency and this value is sent to a function in audio controller called setFrequency(). The setfrequency function changes a variable called freq, which is a double data type, declared in audiocontroller.m, as is another double variable called sinefrequency. Sine frequency is declared as a pointer to the freq variable like so: double sineFrequency = THIS->freq;.Freq is initialized at the top of audiocontroller.m in a function called init, which initializes variables used at runtime, with a value of 110 so that upon playing the wave, it starts at 110Hz. The -> syntax indicates that this is a pointer to the freq variable which is changed in the setfrequency function thus:

5ID:10011159

Page 6: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

-(void)setFrequency :(double) f /// slider frequency{ freq=f; }Where f is returned from the setFrequency function.This is how the GUI, maincontroller and audiocontroller files communicate with each other. For the slider to be active, an NSObject must be implemented within the mainmenu.xib file. This appears in the object library as a blue cube, which is dragged to the left hand side of the mainmenu.xib workspace. This object allows controllers such as sliders, buttons and number boxes to be linked to functions as in the case of the frequency slider. By depressing the CTRL key and clicking the mouse over the slider and dragging the mouse to the cube, upon releasing the mouse button a list of the functions implemented is shown in a menu. The correct function is selected (in this case, changefrequencyvalue) and the sliders values are now part of the function. This is a very quick and simple way of aligning controllers to functions. The amplitude sliders use the same method. Each slider is set to have a value between 0.0 and 1.0. Figure 3 shows the finished application’s GUI.

Figure 3. Additive Synthesis Application GUI.

Each of the buttons trigger the addition of a different set of harmonics added to the fundamental sine wave. The first includes the harmonics 1 to 6. This pattern of harmonics with the right amplitudes, which are reciprocals of the harmonic, generates a saw shaped wave. The second button makes use of odd harmonics from 1-13 and with reciprocal amplitudes can produce a square wave, the third takes all 5th harmonics and upon inspection with a wave viewer, generates a slightly trapezoid shape (figure 4.) and the final button produces random harmonics between 0 and 20. This

6ID:10011159

Page 7: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

was achieved using a variable declared in audiocontroller.h, initialised in audiocontroller.m and used as pointers within the audio call-back. The most effective way of generating random numbers in objective C was to use the arc4random() command. There are others (random() and rand() but these have been found to be unreliable. In order to generate random numbers from 1-20, the variables were declared like this:r2=1+arc4random() %20; r3=1+arc4random() %20; r4=1+arc4random() %20; r5=1+arc4random() %20; r6=1+arc4random() %20;

Figure 4. Trapezoid shaped wave generated with 5 th harmonics of the fundamental.

Once the project was working properly and complete, it was saved as a workspace file and burned to disk.

Summary & Conclusion.

X-code and the cocoa framework was a difficult language to understand straight away. Not having had any experience with OOP programming before, it took a while to understand the syntax of the language and the way objects and functions connect together. The process of executing what seemed to be simple things seemed long-winded and laborious. Apart from one book, there were very few references to audio programming using this language and environment. However, once the concept of objects and methods was understood a little more clearly, things began to fall into place. The run time GUI was very easy to understand, very flexible and a great help in the application programming process. The end product is a simple, experimental additive synthesis tool showing only a tiny glimpse of what can be done using the X-Code/Cocoa IDE.

Bibliography & References.

Adamson,Chris & Avila, Kevin. Learning Core Audio. Addison Wesley, 2011.

Russ, Martin. Sound Synthesis and Sampling. Elsevier LTD. 2009.

7ID:10011159

Page 8: Object oriented programming and building an Additive Synthesis plugin using Objective C

Applied Music Technology.Object oriented programming and building an Additive Synthesis plugin using Objective C

8ID:10011159