10

Blog | Piano Application With App Designer | MATLAB Helper

 3 years ago
source link: https://matlabhelper.com/blog/piano-application-in-matlab/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Piano fundamentals

I am sure most of you would have heard some beautiful melodies or maybe can play some yourself, in this blog we will see how it's possible to create some tunes in MATLAB. In this blog, we will use the terms keyboard and piano interchangeably since we are going to make a virtual piano application.

Pianos come in many different key number layouts like 88,67,32 keys etc. We will be considering a 32 key piano as shown below and understand the layout and terms related to it.

The above piano configuration has 19 white keys and 13 black ones. Each of these keys plays a sound of different frequency when they are struck. An octave is an interval between two keys where one of the keys has double the frequency of the other. Conventionally ‘C’ is considered the first key of an octave. We can design a simple piano layout in MATLAB using push buttons which in our program looks like this:

Piano note frequencies

Since the different sounds played using a piano is dictated by their frequency we can make use of this formula to determine the same:

As we mentioned each octave starts with ‘C’ and is followed by ‘D’,’ E’,’ F’,’ G’,’ A’, and ‘B’. The keys have a letter followed by a number to represent the octave. In our program, we start with the 3rd octave and F note so the white keys will be ‘F3’,’ G3’,’ A3’,’ B3’,’ C4’,’ D4’ and so on. If we take a look at the black keys we see that they have 2 names: 1 for a higher note and 1 for a lower note. These are called half frequencies and they are called flat or sharp. For example, the note between F and G can be called as F sharp or G flat, in general, it is called as a  sharp of the lower note and flat of a higher note.

In the formula seen above, the letter ‘n’ refers to the note number which is assigned to each note in a piano. The note number of some notes can be referred from Wikipedia as shown below.

Hence we can just plug in the note number to the above formula to get the required frequency of a note. This frequency is then passed to a function that forms the waveform for the sound wave and then played using the sound function in MATLAB.

MATLAB function to produce the piano sound waveforms

function wave = wavefunc(app,notenum,time)
t = 0:1/app.samplefreq:4/time;
%Formula to determine frequency of anote from its notenumber
freq = 440*2^((notenum-49)/12);
%Code to waves to simulate the sound of a piano
wave = sin(1* 2 * pi *freq * t) .* exp(-0.0004 * 2 * pi * freq * t) / 1;
wave = wave+sin(2* 2 * pi *freq * t) .* exp(-0.0004 * 2 * pi * freq * t) / 2;
wave = wave+sin(3* 2 * pi *freq * t) .* exp(-0.0004 * 2 * pi * freq * t) / 4;
wave = wave+sin(4* 2 * pi *freq * t) .* exp(-0.0004 * 2 * pi * freq * t) / 8;
wave = wave+sin(5* 2 * pi *freq * t) .* exp(-0.0004 * 2 * pi * freq * t) / 16;
wave = wave+sin(6* 2 * pi *freq * t) .* exp(-0.0004 * 2 * pi * freq * t) / 32;
wave= wave+(wave.*wave.*wave);
wave=wave/6;
end

MATLAB code for callback of the push buttons   

 function A5ButtonPushed(app, event)                
notenum=61;                
wave=app.wavefunc(notenum,app.time);                
sound(wave,app.samplefreq,24);                
 if (app.rec==1)                    
app.song=[app.song wave];                

Improving the design and functionality of the application

Since our application is very bland at the current stage we can add a background image to make it look better. Also, to improve the functionality we add 3 more buttons:

  • Record: This push button allows us to record our piano keypresses and finally store them in a .wav file format
  • Long press: This state button allows us to enable and disable long-press which allows us to simulate the sound of a long press that is possible on a real piano.
  • Happy Birthday theme: This push button has a pre-defined code that automatically plays the happy birthday theme song.

After changing the look and adding more buttons, the application looks like this       

Happy birthday theme song

We can make use of the following image to write the pre-defined code to play the happy birthday song:

 SOURCE : GOOGLE

To write the code, we just have to follow the notes given above and pass the frequency to the ‘wavefunc’ and sound function accordingly to get the song. In our program, we start from the third octave. So the notes for the first line will be A3A3 B3A3 D4C4#.

Code for first line of the song

notenum=37; % A3
wave=app.wavefunc(notenum,app.time);
sound(wave,app.samplefreq,24);
pause(.65)
notenum=37; % A3
wave=app.wavefunc(notenum,app.time);
sound(wave,app.samplefreq,24);
pause(.65)
notenum=39; % B3
wave=app.wavefunc(notenum,app.time);
sound(wave,app.samplefreq,24);
pause(.65)
notenum=37; % A3
wave=app.wavefunc(notenum,app.time);
sound(wave,app.samplefreq,24);
pause(.65)
notenum=42; % D4
wave=app.wavefunc(notenum,app.time);
sound(wave,app.samplefreq,24);
pause(.65)
notenum=41; % C4#
wave=app.wavefunc(notenum,app.time/2);
sound(wave,app.samplefreq,24);
pause(1.4)

Sample audio of the song

Conclusion

We made use of the frequency properties of a piano and the sound function in MATLAB to make a piano application. This allows us to play tunes of our choice. Hence MATLAB can be used to make many interesting and fun applications like this.

Loved our Blog Post? Give us your valuable feedback through comments!

Thank you for reading this blog. Do share this blog if you found it helpful. If you have any queries, post them in the comments or get in touch with us by emailing your questions to [email protected]. Follow us on LinkedInFacebook, and Subscribe to our YouTube Channel. 

We will not share any code or resource which the author has not provided. If you are looking for free help, you can post your comment on the blog or video & wait for any community member to respond, which is not guaranteed. You can book Expert Help, a paid service, and get assistance in your requirement. If your timeline allows, we would recommend you to book the Research Assistance plan. If you want to get trained in MATLAB or Simulink, you may join one of our Training modules. 

If you are ready for the paid service, share your requirement with necessary attachments & inform us about any Service preference along with the timeline. Once evaluated, we will revert to you with more details and the next suggested step.

Education is our future. MATLAB is our feature. Happy MATLABing!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK