File Exchange

image thumbnail


version 1.0 (2.7 KB) by

This function plays musical tunes. Write your own musical tune and call the function to play it.



View License

Editor's Note: This file was a File Exchange Pick of the Week

This function plays musical tunes. Write your own musical tune and call the function to play it.
[CDEFGAB] keys, 5 full octaves
[12345678] key/rest duration, with the default [1] semiquaver, [2] quaver, [4] crochet and [8] minim
[#] sharp / [b] flat / [.] rest / [^] raise octave / [_] lower octave

The musical scores for Canon in D (Pachelbel), Prelude No 1 (Bach), The Globe Trotter and Fantasie Impromptu (Chopin) are also attached.

Essential files: playtune.m
Accompanying files: canon_in_d.m, prelude_no_1.m, the_globe_trotter.m, fantasie_impromptu.m

Comments and Ratings (7)

Anran Wang

But still can be improved.
e.g: '4F4F'(command)='8F'(what i got),actually i want two F
and whatever the note is(semiquaver or quaver and so on),the duration of each note is decided by how many beats there are in one second(which is set by the composer),maybe we can add a input value to change the tempo?


Ameya Deoras

Ameya Deoras (view profile)

Ruoting Yang

S Murphy

Alan Tan

Hi John, I've taken your advice and added some examples and included the notation (above) in the help of playtune.

John D'Errico


I do wish the author had included a bit more help with playtune. For example, the notation is described in the website description, but not in the help for playtune. (Please fix this!) I've rated it a 5 anyway.



Added BSD license.

Added additional help and examples in playtune.

Edited description.

Tidied up the codes.

Enabled customizable tone generator; at the same time, set the lowest note to C(-2), i.e., two octaves below the middle C of the piano. Also added a new musical score "The Globe Trotter" which plays both right and left hands.

Added the musical score of Prelude No 1 by Bach.

Added residual phase to generating signals.

Edited the function for a more natural sounding tone.

MATLAB Release
MATLAB 5.3.1 (R11.1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video