Skip to content

Commit 59246d6

Browse files
committed
Remember about the microphone device last selected by the user when opening the application, as suggested by issue #3
1 parent e8e18d3 commit 59246d6

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ The following will produce back hearable tones from a given fingerprint, that sh
9898
./songrec fingerprint-to-lure 'data:audio/vnd.shazam.sig;base64,...' /tmp/output.wav
9999
```
100100

101+
When using the application, you may notice that certain information will be savec to `~/.local/share/SongRec` (or an equivalent directory depending on your operating system), including the CSV-format list of the last recognized songs and the last selected microphone input device (so that it is chosen back when restarting the app). You may want to delete this directory in case of persistent issues.
102+
101103
## Legal
102104

103105
This software is released under the [GNU GPL v3](https://www.gnu.org/licenses/gpl-3.0.html) license. It was created with the intent of providing interoperability between the remote Shazam services and Linux-based deskop systems.

src/gui/main_window.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use gio::prelude::*;
22
use glib::clone;
33
use gtk::prelude::*;
4+
use std::fs::File;
5+
use std::io::{Read, Write};
46
use gtk::ResponseType;
57
use gdk_pixbuf::Pixbuf;
68
use std::error::Error;
@@ -110,6 +112,20 @@ pub fn gui_main(recording: bool) -> Result<(), Box<dyn Error>> {
110112
let current_signature: Rc<RefCell<Option<DecodedSignature>>> = Rc::new(RefCell::new(None));
111113
let current_signature_2 = current_signature.clone();
112114

115+
// Remember about the saved last-used microphone device, if any
116+
117+
let device_name_savefile = SongHistoryInterface::obtain_csv_path().unwrap()
118+
.replace("song_history.csv", "device_name.txt");
119+
120+
let mut old_device_name: Option<String> = None;
121+
122+
if let Ok(mut file) = File::open(&device_name_savefile) {
123+
let mut old_device_name_string = String::new();
124+
file.read_to_string(&mut old_device_name_string).unwrap();
125+
126+
old_device_name = Some(old_device_name_string);
127+
}
128+
113129
// List available input microphones devices in the appropriate combo box
114130

115131
let combo_box: gtk::ComboBox = builder.get_object("microphone_source_select_box").unwrap();
@@ -124,21 +140,43 @@ pub fn gui_main(recording: bool) -> Result<(), Box<dyn Error>> {
124140
// through disabling stderr temporarily
125141

126142
let print_gag = Gag::stderr().unwrap();
143+
let mut old_device_index = 0;
144+
let mut current_index = 0;
127145

128146
for device in host.input_devices().unwrap() {
129-
combo_box_model.set(&combo_box_model.append(), &[0], &[&device.name().unwrap()]);
147+
let device_name = device.name().unwrap();
148+
149+
combo_box_model.set(&combo_box_model.append(), &[0], &[&device_name]);
150+
151+
if old_device_name == Some(device_name) {
152+
old_device_index = current_index;
153+
}
154+
current_index += 1;
130155
}
131156

132157
drop(print_gag);
133158

134-
combo_box.set_active(Some(0));
159+
combo_box.set_active(Some(old_device_index));
135160

136161
combo_box.connect_changed(clone!(@weak microphone_stop_button, @weak combo_box => move |_| {
137162

163+
let device_name = combo_box.get_active_id().unwrap().to_string();
164+
165+
// Save the selected microphone device name so that it is
166+
// remembered after relaunching the app
167+
168+
let mut file = File::create(&device_name_savefile).unwrap();
169+
170+
file.write_all(device_name.as_bytes()).unwrap();
171+
file.sync_all().unwrap();
172+
173+
drop(file);
174+
138175
if microphone_stop_button.is_visible() {
139176

140-
let device_name = combo_box.get_active_id().unwrap().to_string();
141-
177+
// Re-launch the microphone recording with the new selected
178+
// device
179+
142180
microphone_tx_4.send(MicrophoneMessage::MicrophoneRecordStop).unwrap();
143181
microphone_tx_4.send(MicrophoneMessage::MicrophoneRecordStart(device_name)).unwrap();
144182

0 commit comments

Comments
 (0)