1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
pub mod scale;
pub mod chord;
mod util;
mod attribute;
mod json;
use self::chord::Chord;
#[macro_use]
extern crate serde_json;
fn get_notes(keystr: &str, scalestr: &str) -> Vec<(char, i8)> {
let key = util::str_to_note(keystr);
let chromatic_notes: Vec<(char, i8)> = scale::chromatic_notes(key);
let scale = scale::get_scale(scalestr);
chromatic_notes.into_iter().enumerate()
.filter(|&(index, _)| scale.contains(&(index as u8)))
.map(|(_, e)| e)
.collect()
}
fn get_chords(root_note: (char, i8), notes: &Vec<(char, i8)>, extended: bool) -> Vec<Chord> {
let mut chords = vec![];
let root_str = util::note_to_str(root_note);
let root_index = notes.iter().position(|¬e| note == root_note)
.expect("Failed to find root index!");
let a = notes[..root_index].to_vec();
let b = notes[root_index..].to_vec();
let mut flipped = vec![];
flipped.extend(b);
flipped.extend(a);
let chromatic_notes: Vec<(char, i8)> = scale::chromatic_notes(root_note);
let mut intervals = vec![];
for v in &flipped[1..] {
let interval = chromatic_notes.iter()
.position(|¬e| note == *v || note == util::alt_note(*v))
.expect("Failed to find interval position for note");
intervals.push(interval as u8);
}
if intervals.len() > 3 {
chords.push(Chord::new(&root_str, vec![0, intervals[1], intervals[3]], false));
chords.push(Chord::new(&root_str, vec![0, intervals[2], intervals[3]], false));
}
if intervals.len() > 4 {
chords.push(Chord::new(&root_str, vec![0, intervals[1], intervals[3], intervals[4]], false));
chords.push(Chord::new(&root_str, vec![0, intervals[2], intervals[3], intervals[4]], false));
if extended {
chords.push(Chord::new(&root_str, vec![0, intervals[1], intervals[2], intervals[3], intervals[4]], true));
}
}
if intervals.len() > 5 {
chords.push(Chord::new(&root_str, vec![0, intervals[1], intervals[3], intervals[5]], false));
if extended {
chords.push(Chord::new(&root_str, vec![0, intervals[2], intervals[3], intervals[5]], true));
chords.push(Chord::new(&root_str, vec![0, intervals[1], intervals[3], intervals[4], intervals[5]], true));
chords.push(Chord::new(&root_str, vec![0, intervals[2], intervals[3], intervals[4], intervals[5]], true));
chords.push(Chord::new(&root_str, vec![0, intervals[1], intervals[2], intervals[3], intervals[4], intervals[5]], true));
}
}
if intervals.len() > 6 {
if extended {
chords.push(Chord::new(&root_str, vec![0,
intervals[1],
intervals[2],
intervals[3],
intervals[4],
intervals[5],
intervals[6]],
true));
}
}
for chord in chords.iter_mut() {
chord.format_notes(notes);
}
chords
}
pub fn analyze(key: &str, scale: &str, extended: bool) -> (Vec<String>,Vec<Chord>) {
let mut notes = get_notes(&key, &scale);
match scale {
"chromatic" => {},
_ => notes = util::formatted_notes(notes)
}
let mut chords: Vec<Chord> = vec![];
if scale != "chromatic" {
for v in ¬es {
chords.extend(get_chords(*v, ¬es, extended));
}
}
(notes.into_iter().map(|note| util::note_to_str(note).to_uppercase()).collect::<Vec<String>>(), chords)
}
pub fn analyze_json(key: &str, scale: &str, extended: bool) -> String {
let result = analyze(key, scale, extended);
json::serialize(result.0, result.1)
}
pub fn supported_scales() -> Vec<String> {
scale::supported_scales()
}
pub fn supported_scales_json() -> String {
json!({"scales": json!(scale::supported_scales())}).to_string()
}
extern crate libc;
use libc::c_char;
use std::ffi::CStr;
use std::ffi::CString;
#[no_mangle]
pub extern fn c_analyze(key: *const c_char, scale: *const c_char, extended: bool) -> *const c_char {
let c_key = unsafe {
assert!(!key.is_null());
CStr::from_ptr(key)
};
let c_scale = unsafe {
assert!(!scale.is_null());
CStr::from_ptr(scale)
};
CString::new(analyze_json(c_key.to_str().unwrap(), c_scale.to_str().unwrap(), extended)).unwrap().into_raw()
}