Mercurial > hg > c4dm-chord-transcriptions
comparison chordtools/degree2semitone.m @ 1:8973548174c1 tip
adding tools to repo
author | christopherh |
---|---|
date | Mon, 06 May 2013 14:43:47 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:0a4ad3e72e75 | 1:8973548174c1 |
---|---|
1 % | |
2 % DEGREE2SEMITONE | |
3 % | |
4 % [semitone, success, errormessage] = degree2semitone(degree, accidental, {verbose}) | |
5 % | |
6 % Convert a degree and accidentals pair to a semitone value | |
7 % | |
8 % Success = 1 if degree converted correctly, 0 otherwise. | |
9 % | |
10 % If optional argument 'verbose' is 1, function prints any errormessage to | |
11 % the screen. | |
12 % | |
13 % returns: semitone (string) | |
14 % success (boolean) | |
15 % errormessage (string) | |
16 % | |
17 % Author: Christopher Harte, March 2009 | |
18 % | |
19 % Copyright: Centre for Digital Music, Queen Mary University of London 2005 | |
20 % | |
21 % This file is part of the C4DM Chord Toolkit V2.0 | |
22 % | |
23 % The C4DM Chord Toolkit is free software; you can redistribute it and/or | |
24 % modify it under the terms of the GNU General Public License as published | |
25 % by the Free Software Foundation; either version 2 of the License, or | |
26 % (at your option) any later version. | |
27 % | |
28 % The C4DM Chord Toolkit is distributed in the hope that it will be useful, | |
29 % but WITHOUT ANY WARRANTY; without even the implied warranty of | |
30 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
31 % GNU General Public License for more details. | |
32 % | |
33 % You should have received a copy of the GNU General Public License | |
34 % along with the C4DM Toolkit; if not, write to the Free Software | |
35 % Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
36 | |
37 % | |
38 function [semitone, success, errormessage] = degree2semitone(degree, accidentals, verbose) | |
39 | |
40 if nargin < 3 | |
41 % verbose defaults 0 | |
42 verbose = 0; | |
43 end | |
44 | |
45 errormessage = ''; | |
46 | |
47 % semitone equivalents for degrees | |
48 % degree 1,2,3,4,5,6,7 | |
49 %semitonetranslation = [0,2,4,5,7,9,11]; | |
50 | |
51 semitone = 0; | |
52 | |
53 success = 1; | |
54 | |
55 if (degree > 0) && (degree < 50) | |
56 | |
57 % semitone value is the number of semitones equivalent to the degree | |
58 % added to the number of accidentals (sharps are positive, flats are | |
59 % negative) and the number of octaves above the reference note to | |
60 % account for extensions | |
61 | |
62 %semitone = semitonetranslation(mod(degree,8) + fix(degree/8)) + accidentals + 12*fix(degree/8); | |
63 | |
64 semitone = 2*mod(degree-1,7) - floor((mod(degree-1,7)+1)/4) + 12*floor((degree-1)/7)+ accidentals; | |
65 | |
66 else | |
67 success = 0; | |
68 | |
69 errormessage = sprintf('Error in degree2semitone: out of range degree "%d"\n', degree); | |
70 | |
71 if verbose == 1 | |
72 fprintf(1,errormessage); | |
73 end | |
74 end | |
75 |