Digital Sound & Music: Concepts, Applications, & Science, Chapter 3, last updated 6/25/2013
3.3.4 Experimenting with Music in MATLAB
Chapter 2 introduces some command line arguments for evaluating sine waves, playing sounds,
and plotting graphs in MATLAB. For this chapter, you may find it more convenient to write
functions in an external program. Files that contain code in the MATLAB language are called
M-files, ending in the .m suffix. Here’s how you proceed when working with M-files:
Create an M-file using MATLAB's built-in text editor (or any text editor)
Write a function in the M-file.
Name the M-file fun1.m where fun1 is the name of function in the file.
Place the M-file in your current MATLAB directory.
Call the function from the command line, giving input arguments as appropriate and
accepting the output by assigning it to a variable if necessary.
The M-file can contain internal functions that are called from the main function.
We refer you to MATLAB’s Help for details of syntax and program structure,
but offer the program in Algorithm 3.1 to get you started. This program allows
the user to create major and minor scales beginning with a start note. The start
note is represented as a number of semitones offset from middle C. The
function plays eight seconds of sound at a sampling rate of 44,100 samples per
second and returns the raw data to the user, where it can be assigned to a
variable on the command line if desired.
function outarray = MakeScale(startnoteoffset, isminor)
%outarray is an array of sound samples on the scale of (-1,1)
%outarray contains the 8 notes of a diatonic musical scale
%each note is played for one second
%the sampling rate is 44100 samples/s
%startnoteoffset is the number of semitones up or down from middle C at
%which the scale should start.
%If isminor == 0, a major scale is played; otherwise a minor scale is played.
sr = 44100;
s = 8;
outarray = zeros(1,sr*s);
majors=[0 2 4 5 7 9 11 12];
minors=[0 2 3 5 7 8 10 12];
if(isminor == 0)
scale = majors;
scale = minors;
scale = scale/12;
scale = 2.^scale;
%.^ is element-by-element exponentiation
t = [1:sr]/sr;
%the statement above is equivalent to
startnote = 220*(2^((startnoteoffset+3)/12))
scale = startnote * scale;
%Yes, ^ is exponentiation in MATLAB, rather than bitwise XOR as in C++
for i = 1:8
outarray(1+(i-1)*sr:sr*i) = sin((2*pi*scale(i))*t);
Algorithm 3.1 Generating scales