To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / _chordtools / interval2semitone.m @ 8:b5b38998ef3b

History | View | Annotate | Download (2.19 KB)

1
%
2
% INTERVAL2SEMITONE
3
% 
4
% [semitone, success, errormessage] = interval2semitone(interval, accidental, {verbose})
5
% 
6
% Convert an interval and accidentals pair to a semitone value
7
% 
8
% Success = 1 if interval 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,  August 2005
18
% 
19
% Copyright: Centre for Digital Music, Queen Mary University of London 2005 
20
%
21
% This file is part of the C4DM Chord Toolkit.  
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] = interval2semitone(interval, accidentals, verbose)
39

    
40
if nargin < 3 
41
    % verbose defaults 0
42
    verbose = 0;
43
end
44

    
45
errormessage = '';
46

    
47
% semitone equivalents for intervals
48
%           interval   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 (interval > 0) && (interval < 50)
56

    
57
% semitone value is the number of semitones equivalent to the interval
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(interval,8) + fix(interval/8)) + accidentals + 12*fix(interval/8);
63

    
64
else
65
    success = 0;
66
    
67
    errormessage = sprintf('Error in interval2semitone: out of range interval "%d"\n', interval);
68
    
69
    if verbose == 1
70
        fprintf(1,errormessage);
71
    end
72
end
73