Rev 2009 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
964 | jag | 1 | /* |
2 | * note.cc |
||
2097 | jag | 3 | * DIN Is Noise is copyright (c) 2006-2024 Jagannathan Sampath |
1713 | jag | 4 | * DIN Is Noise is released under GNU Public License 2.0 |
1479 | jag | 5 | * For more information, please visit https://dinisnoise.org/ |
964 | jag | 6 | */ |
7 | |||
8 | #include "note.h" |
||
9 | #include "main.h" |
||
10 | #include "scale_info.h" |
||
1607 | jag | 11 | #include "utils.h" |
964 | jag | 12 | #include <math.h> |
13 | #include <map> |
||
14 | |||
15 | extern char BUFFER []; |
||
1695 | jag | 16 | extern const char spc; |
1393 | jag | 17 | extern int NOTATION; |
964 | jag | 18 | |
19 | note::note () { |
||
20 | hz = 0; |
||
21 | step = 0; |
||
22 | scale_pos = 0; |
||
23 | octave = 0; |
||
24 | } |
||
25 | |||
26 | void note::set_freq (float f) { |
||
27 | hz = f; |
||
28 | hz2step (hz, step); |
||
29 | sprintf (BUFFER, "%0.3f", hz); |
||
30 | hz_name = BUFFER; |
||
31 | } |
||
32 | |||
33 | void note::set_freq (float key, float interval) { |
||
34 | set_freq (key * interval * pow (2.0, octave)); |
||
35 | } |
||
36 | |||
37 | void note::set_name (const std::string& n) { |
||
38 | name = n; |
||
39 | } |
||
40 | |||
41 | void note::set_name (const std::string& interval, int offset) { |
||
42 | if (NOTATION == WESTERN) { |
||
43 | extern std::map<std::string, int> NOTE_POS; |
||
44 | extern const char* WESTERN_FLAT []; |
||
45 | int np = NOTE_POS[interval]; |
||
46 | name = WESTERN_FLAT[(offset + np) % 12]; |
||
1829 | jag | 47 | } else if (NOTATION == NUMERIC) { |
48 | set_name (interval); |
||
964 | jag | 49 | } else { |
1829 | jag | 50 | extern map<string, string> INT2IND; |
51 | set_name (INT2IND[interval]); |
||
52 | } |
||
964 | jag | 53 | } |
54 | |||
55 | void note::change_scale_pos (int j, scale_info& si) { |
||
1607 | jag | 56 | int i = sign(j); |
57 | int k = abs(j); |
||
58 | while (k--) { |
||
59 | scale_pos += i; |
||
60 | if (scale_pos < 0) { |
||
61 | scale_pos = si.second_last_note; |
||
62 | --octave; |
||
63 | } |
||
64 | else if (scale_pos > si.last_note) { |
||
65 | scale_pos = 1; |
||
66 | ++octave; |
||
67 | } |
||
68 | } |
||
964 | jag | 69 | } |
70 | |||
71 | std::ofstream& note::save (std::ofstream& f) { |
||
1695 | jag | 72 | f << name << spc << hz_name << spc << hz << spc << step << spc << scale_pos << spc << octave << spc; |
964 | jag | 73 | return f; |
74 | } |
||
75 | |||
76 | std::ifstream& note::load (std::ifstream& f) { |
||
77 | f >> name >> hz_name >> hz >> step >> scale_pos >> octave; |
||
78 | return f; |
||
79 | } |