Back to Top

Home/Tutorials

Guitar Tuner


Did you know that playing a musical instrument will help boost your confidence? Not to mention that it will help you increase brain capacity. So, the code below will have a significant educational value, because it allows you to build a fully functional guitar tuner.


The snippet is quite big, but that shouldn't worry you, because it has six similar sections, one for each guitar chord.


Everything starts with definitions of variables, function prototypes, the actual chord sounds, which can be recorded using a well-tuned guitar, as well as a bunch of bitmaps.


guitar tunervar chord1_handle;

var chord2_handle;

var chord3_handle;

var chord4_handle;

var chord5_handle;

var chord6_handle;


function chord1();

function chord2();

function chord3();

function chord4();

function chord5();

function chord6();


SOUND* one_wav = "1.wav";

SOUND* two_wav = "2.wav";

SOUND* three_wav = "3.wav";

SOUND* four_wav = "4.wav";

SOUND* five_wav = "5.wav";

SOUND* six_wav = "6.wav";


BMAP* pointer_tga = "pointer.tga";

BMAP* chord1on_tga = "chord1on.tga";

BMAP* chord1off_tga = "chord1off.tga";

BMAP* chord1over_tga = "chord1over.tga";

BMAP* chord2on_tga = "chord2on.tga";

BMAP* chord2off_tga = "chord2off.tga";

BMAP* chord2over_tga = "chord2over.tga";

BMAP* chord3on_tga = "chord3on.tga";

BMAP* chord3off_tga = "chord3off.tga";

BMAP* chord3over_tga = "chord3over.tga";

BMAP* chord4on_tga = "chord4on.tga";

BMAP* chord4off_tga = "chord4off.tga";

BMAP* chord4over_tga = "chord4over.tga";

BMAP* chord5on_tga = "chord5on.tga";

BMAP* chord5off_tga = "chord5off.tga";

BMAP* chord5over_tga = "chord5over.tga";

BMAP* chord6on_tga = "chord6on.tga";

BMAP* chord6off_tga = "chord6off.tga";

BMAP* chord6over_tga = "chord6over.tga";


PANEL* main_pan =

{

bmap = "main.png";

pos_x = 0;

pos_y = 0;

button (31, 16, chord1on_tga, chord1off_tga, chord1over_tga, chord1, NULL, NULL);

button (76, 16, chord2on_tga, chord2off_tga, chord2over_tga, chord2, NULL, NULL);

button (120, 16, chord3on_tga, chord3off_tga, chord3over_tga, chord3, NULL, NULL);

button (166, 15, chord4on_tga, chord4off_tga, chord4over_tga, chord4, NULL, NULL);

button (210, 16, chord5on_tga, chord5off_tga, chord5over_tga, chord5, NULL, NULL);

button (254, 16, chord6on_tga, chord6off_tga, chord6over_tga, chord6, NULL, NULL);

flags = SHOW;

}


The main tool panel uses the main.png bitmap, is placed in the upper left corner of the screen and has six buttons, each one of them representing one of the guitar chords that can be picked/clicked with the mouse. We choose to show the main_pan panel from the very beginning, of course.


void main()

{

video_border(bmap_create("main.png"), 0, 0, 294, 480);

}


Function main does a single thing, but it's an important one: it sets the size of our tool to the one that's given by the main panel bitmap: 294x480 pixels. You should change it to the size of your panel image, of course.


function mouse_startup()

{  

mouse_mode = 2;

mouse_map = pointer_tga;

while (1)

{  

  vec_set(mouse_pos, mouse_cursor);

  wait(1);

}

}


I'm pretty sure that we've never used startup functions before – though I may be wrong. Anyway, just like function main() a startup function is executed automatically as soon as we run our program. This function allows us to move the mouse pointer around, by keeping its coordinates in sync with the mouse_cursor vector, which gets updated every frame.


function chord1()

{

snd_stop(chord1_handle);

snd_stop(chord2_handle);

snd_stop(chord3_handle);

snd_stop(chord4_handle);

snd_stop(chord5_handle);

snd_stop(chord6_handle);

chord1_handle = snd_play(one_wav, 100, 0); // then play the chord sound only once

}


function chord2()

{

snd_stop(chord1_handle);

snd_stop(chord2_handle);

snd_stop(chord3_handle);

snd_stop(chord4_handle);

snd_stop(chord5_handle);

snd_stop(chord6_handle);

chord2_handle = snd_play(two_wav, 100, 0);

}


function chord3()

{

snd_stop(chord1_handle);

snd_stop(chord2_handle);

snd_stop(chord3_handle);

snd_stop(chord4_handle);

snd_stop(chord5_handle);

snd_stop(chord6_handle);

chord3_handle = snd_play(three_wav, 100, 0);

}


function chord4()

{

snd_stop(chord1_handle);

snd_stop(chord2_handle);

snd_stop(chord3_handle);

snd_stop(chord4_handle);

snd_stop(chord5_handle);

snd_stop(chord6_handle);

chord4_handle = snd_play(four_wav, 100, 0);

}


function chord5()

{

snd_stop(chord1_handle);

snd_stop(chord2_handle);

snd_stop(chord3_handle);

snd_stop(chord4_handle);

snd_stop(chord5_handle);

snd_stop(chord6_handle);

chord5_handle = snd_play(five_wav, 100, 0);

}


function chord6()

{

snd_stop(chord1_handle);

snd_stop(chord2_handle);

snd_stop(chord3_handle);

snd_stop(chord4_handle);

snd_stop(chord5_handle);

snd_stop(chord6_handle);

chord6_handle = snd_play(six_wav, 100, 0); // then play the chord sound only once

}


See? I have told you that we use similar code for those six chords, so we'll only discuss the lines that are used for the last one. Each one of these functions is triggered when the user clicks one of the corresponding chord/button.


So, when we click the sixth chord bitmap, all the sounds that were playing are stopped, and then the correct chord sound is played at a volume of 100%.


This concludes our article, but you shouldn't stop your coding experiments here! Now that you know how to build a guitar tuner, you can use the same code as a base for a small piano application, for example. So, be a good sport and do this homework – I guarantee that you are going to love the result of your work!