Project 01 - Basic Audio Sounds Demo

<< Click to Display Table of Contents >>

Navigation:  Projects >

Project 01 - Basic Audio Sounds Demo

The Basic Audio Sounds project demonstrates how to play the various inbuilt instrument sounds.





This project uses the following component(s) -


56mm, 80hm 0.5W(RMS) / 1W(Peak) Internal CleO speaker






The following sample code snippet shows how the project starts by loading the icons -


void setup() {


 icon_speaker = CleO.LoadIcon("@Icons/m48.ftico", ICON_VOLUME_UP);

 icon_note = CleO.LoadIcon("@Icons/m48.ftico", ICON_MUSIC_NOTE);

 icon_play[0] = CleO.LoadIcon("@Icons/m48.ftico", ICON_PLAY_CIRCLE_OUTLINE);

 icon_play[1] = CleO.LoadIcon("@Icons/m48.ftico", ICON_PLAY_CIRCLE_FILLED);



In the loop routine(), it draws the following four elements - Instruments to select, Play icon, Note adjust line and Volume control line.


The Instruments are stored in the following structure:


struct {

 const char *name;

 int code;

} instruments[] = {

 {"Square",      SQUAREWAVE},

 {"Sine",        SINEWAVE},

 {"Sawtooth",    SAWTOOTH},

 {"Triangle",    TRIANGLE},

 {"Beeping",     BEEPING},

 {"Alarm",       ALARM},

 {"Warble",      WARBLE},

 {"Carousel",    CAROUSEL},

 {"Harp",        HARP},

 {"Xylophone",   XYLOPHONE},

 {"Tuba",        TUBA},

 {"Glockenspiel", GLOCKENSPIEL},

 {"Organ",       ORGAN},

 {"Trumpet",     TRUMPET},

 {"Piano",       PIANO},

 {"Chimes",      CHIMES},

 {"Musicbox",    MUSICBOX},

 {"Bell",        BELL},

 {"Click",       CLICK},

 {"Switch",      SWITCH},

 {"Cowbell",     COWBELL},

 {"Notch",       NOTCH},

 {"Hihat",       HIHAT},

 {"Kickdrum",    KICKDRUM},

 {"Pop",         POP},

 {"Clack",       CLACK},

 {"Chack",       CHACK},



These instrument texts are drawn using the string (name in above structure), tags (code in above structure). The following sample code snippet shows how to draw these instruments:


for (int i = 0; i < 27; i++) {

int x = (i % 3) * 130 + 80;

int y = (i / 3) * 55;

uint32_t color = (sound == instruments[i].code) ? WHITE : GRAY70;


CleO.StringExt(FONT_SANS_3, x, y, color, TM, 0, 0, instruments[i].name);



The rescale() command is used to control the touch on the line. This command rescales x from the range (x0, x1) to the range (y0, y1) as shown in the code below:


float rescale(int x, int x0, int x1, int y0, int y1) {

 x = max(x0, min(x1, x));

 return y0 + ((x - x0) * long(y1 - y0) / float(x1 - x0));



The control() function keeps checking for the touch inside the loop() routine. When the screen is touched, it first checks if the instrument text is touched and then it stores the tag as sound. If the pitch line was affected, then it finds the correct pitch value by mapping the x touch coordinate between 21 and 109. Similar action will be taken for the volume line touch as shown in the sample code snippet below, from the control() routine:


CleO.TouchCoordinates(x, y, dur, tag);

if (dur) {

 if ((SQUAREWAVE <= tag) && (tag <= CHACK))

   sound = tag;

 if (tag == 200)

   pitch = int(rescale(x, pitch_line_x1, pitch_line_x2, 21, 109));

 if (tag == 201)

    volume = int(rescale(x, pitch_line_x1, pitch_line_x2, 0, 255));



The control() routine tracks the previous touch by storing the dur value into prevdur. The current dur, predur and tag values are used to detect whether the play icon was touched and if any touch is detected, it plays the sound using following code snippet:


if (dur && !prevdur && (tag == 202)) {

 CleO.SetSound(sound, pitch, volume, 0);


 playing = 1;



If no touch is detected, then it stops playing the sound using following code snippet:


if (!dur) {


 playing = 0;