summaryrefslogtreecommitdiff
path: root/src/gui.rs
blob: aa4d817d8f5d022e808ff963f54169cef746059b (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use gtk;
use gtk::prelude::*;
use std::cell::RefCell;
use portaudio as pa;
use std::rc::Rc;
use std::io;
use std::io::Write;
use std::thread;
use std::sync::mpsc::*;

struct RustyUi {
    window: gtk::Window,
    dropdown: gtk::ComboBoxText,
    pitch_label: gtk::Label,
    freq_chart: gtk::DrawingArea
}

struct ApplicationState {
    pa: pa::PortAudio,
    pa_stream: Option<pa::Stream<pa::NonBlocking, pa::Input<f32>>>,
    freq_spectrum: Vec<::transforms::FrequencyBucket>,
    ui: RustyUi
}

pub fn start_gui() -> Result<(), String> {
    let pa = try!(::audio::init().map_err(|e| e.to_string()));
    let microphones = try!(::audio::get_device_list(&pa).map_err(|e| e.to_string()));

    try!(gtk::init().map_err(|_| "Failed to initialize GTK."));

    let state = Rc::new(RefCell::new(ApplicationState {
        pa: pa,
        pa_stream: None,
        freq_spectrum: Vec::new(),
        ui: create_window(microphones)
    }));

    //let ui = create_window(microphones);
    
    let (mic_sender, mic_receiver) = channel();
    let (pitch_sender, pitch_receiver) = channel();
    let (freq_sender, freq_receiver) = channel();

    connect_dropdown_choose_microphone(mic_sender, state.clone());
    
    start_processing_audio(mic_receiver, pitch_sender, freq_sender);
    setup_pitch_label_callbacks(pitch_receiver, state.clone());
    setup_drawing_area_callbacks(freq_receiver, state.clone());

    gtk::main();
    Ok(())
}

fn create_window(microphones: Vec<(u32, String)>) -> RustyUi {
    let window = gtk::Window::new(gtk::WindowType::Toplevel);
    window.set_title("Rusty Microphone");
    window.connect_delete_event(|_, _| {
        gtk::main_quit();
        Inhibit(false)
    });

    let layout_box = gtk::Box::new(gtk::Orientation::Vertical, 5);
    window.add(&layout_box);

    let dropdown = gtk::ComboBoxText::new();
    set_dropdown_items(&dropdown, microphones);
    layout_box.add(&dropdown);

    let pitch_label = gtk::Label::new(None);
    layout_box.add(&pitch_label);

    let freq_chart = gtk::DrawingArea::new();
    freq_chart.set_size_request(600, 400);
    layout_box.add(&freq_chart);

    window.show_all();
    
    RustyUi {
        window: window,
        dropdown: dropdown,
        pitch_label: pitch_label,
        freq_chart: freq_chart
    }
}

fn set_dropdown_items(dropdown: &gtk::ComboBoxText, microphones: Vec<(u32, String)>) {
    for (index, name) in microphones {
        dropdown.append(Some(format!("{}", index).as_ref()), name.as_ref());
    }
}

fn connect_dropdown_choose_microphone(mic_sender: Sender<Vec<f64>>, state: Rc<RefCell<ApplicationState>>) {
    let outer_state = state.clone();
    let ref dropdown = outer_state.borrow().ui.dropdown;
    dropdown.connect_changed(move |dropdown: &gtk::ComboBoxText| {
        match state.borrow_mut().pa_stream {
            Some(ref mut stream) => {stream.stop().ok();},
            _ => {}
        }
        let selected_mic = match dropdown.get_active_id().and_then(|id| id.parse().ok()) {
            Some(mic) => mic,
            None => {return;}
        };
        let stream = ::audio::start_listening(&state.borrow().pa, selected_mic, mic_sender.clone()).ok();
        if stream.is_none() {
            writeln!(io::stderr(), "Failed to open audio channel").ok();
        }
        state.borrow_mut().pa_stream = stream;
    });
}

fn start_processing_audio(mic_receiver: Receiver<Vec<f64>>, pitch_sender: Sender<String>, freq_sender: Sender<Vec<::transforms::FrequencyBucket>>) {
    thread::spawn(move || {
        for samples in mic_receiver {
            let frequency_domain = ::transforms::fft(samples, 44100.0);
            freq_sender.send(frequency_domain.clone()).ok();
            let fundamental = ::transforms::find_fundamental_frequency(&frequency_domain);
            let pitch = match fundamental {
                Some(fundamental) => ::transforms::hz_to_pitch(fundamental),
                None => "".to_string()
            };
            pitch_sender.send(pitch).ok();
        }
    });
}

fn setup_pitch_label_callbacks(pitch_receiver: Receiver<String>, state: Rc<RefCell<ApplicationState>>) {
    gtk::timeout_add(100, move || {
        let mut pitch = None;
        loop {
            let next = pitch_receiver.try_recv().ok();
            if next.is_none() {
                break;
            }
            pitch = next;
        }
        match pitch {
            Some(pitch) => {state.borrow().ui.pitch_label.set_label(pitch.as_ref());},
            None => {}
        };
        state.borrow().ui.freq_chart.queue_draw();
        gtk::Continue(true)
    });
}

fn setup_drawing_area_callbacks(spectrum_receiver: Receiver<Vec<::transforms::FrequencyBucket>>, state: Rc<RefCell<ApplicationState>>) {
    setup_frequency_spectrum_callback(spectrum_receiver, state.clone());

    let outer_state = state.clone();
    let ref canvas = outer_state.borrow().ui.freq_chart;
    canvas.connect_draw(move |ref canvas, ref context| {
        let ref spectrum = state.borrow().freq_spectrum;
        let width = canvas.get_allocated_width() as f64;
        let height = canvas.get_allocated_height() as f64;
        let max = spectrum.iter().map(|x| x.intensity).fold(0.0, |max, x| if max > x { max } else { x });
        let len = spectrum.len() as f64;
        
        context.new_path();
        context.move_to(0.0, 0.0);
        
        for (i, bucket) in spectrum.iter().enumerate() {
            let x = i as f64 * width / len;
            let y = height - (bucket.intensity * height / max);
            context.line_to(x, y);
        }
        
        context.stroke();
        
        gtk::Inhibit(false)
    });
}

fn setup_frequency_spectrum_callback(spectrum_receiver: Receiver<Vec<::transforms::FrequencyBucket>>, state: Rc<RefCell<ApplicationState>>) {
    gtk::timeout_add(100, move || {
        let mut spectrum = None;
        loop {
            let next = spectrum_receiver.try_recv().ok();
            if next.is_none() {
                break;
            }
            spectrum = next;
        }
        match spectrum {
            Some(spectrum) => {
                state.borrow_mut().freq_spectrum = spectrum;
            },
            None => {}
        };
        gtk::Continue(true)
    });
}