summaryrefslogtreecommitdiffstats
path: root/src/utils.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.rs')
-rw-r--r--src/utils.rs96
1 files changed, 25 insertions, 71 deletions
diff --git a/src/utils.rs b/src/utils.rs
index de9ed3c..d75971a 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,23 +1,16 @@
1use {EqualizerConfFormat, Filter}; 1use Filter;
2 2
3use cli::EqualizerConfFormat;
3use parsing::EqualizerApoParser; 4use parsing::EqualizerApoParser;
4 5
5use dbus_api::equalizing_manager::OrgPulseAudioExtEqualizing1Manager; 6use failure::Error;
6use dbus_api::server_lookup::OrgPulseAudioServerLookup1;
7use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer;
8
9use dbus::{BusType, ConnPath, Connection};
10use failure::{Error, ResultExt};
11use lalrpop_util; 7use lalrpop_util;
12 8
13use std::fmt; 9use std::fmt;
10use std::fs::File;
14use std::io; 11use std::io;
15 12
16#[derive(Fail, Debug)] 13#[derive(Fail, Debug)]
17#[fail(display = "No equalized sink found")]
18struct NoEqualizedSink;
19
20#[derive(Fail, Debug)]
21#[fail( 14#[fail(
22 display = "Could not parse using the {} format: {}", 15 display = "Could not parse using the {} format: {}",
23 format, 16 format,
@@ -28,66 +21,32 @@ struct ParseError {
28 message: String, 21 message: String,
29} 22}
30 23
31pub fn connect() -> Result<Connection, Error> { 24pub fn read_filearg_to_str(file: &str) -> Result<String, Error> {
32 Ok(connect_impl().context( 25 use std::io::Read;
33 "Could not connect to PulseAudio's D-Bus socket. Have you loaded the 'module-dbus-protocol' module?"
34 )?)
35}
36
37fn connect_impl() -> Result<Connection, Error> {
38 let pulse_sock_path = get_pulse_dbus_sock()?;
39 info!("PulseAudio's D-Bus socket path is: {}", pulse_sock_path);
40
41 trace!("Connecting to PulseAudio's D-Bus socket");
42 Ok(Connection::open_private(&pulse_sock_path)?)
43}
44
45pub fn get_equalized_sink<'a>(conn: &'a Connection) -> Result<ConnPath<'a, &'a Connection>, Error> {
46 Ok(get_equalized_sink_impl(conn).context(
47 "Could not find an equalized sink. Have you loaded the 'module-equalizer-sink' module?",
48 )?)
49}
50
51fn get_equalized_sink_impl<'a>(
52 conn: &'a Connection,
53) -> Result<ConnPath<'a, &'a Connection>, Error> {
54 let conn_manager = conn.with_path("org.PulseAudio.Core1", "/org/pulseaudio/equalizing1", 2000);
55 26
56 // TODO: make that a command-line option 27 let mut buffer = String::new();
57 trace!("Getting (one of) the equalized sink(s)"); 28 if file == "-" {
58 let mut sinks = conn_manager.get_equalized_sinks()?; 29 info!("Reading file from the command line");
59 let sink_path = sinks.pop().ok_or(NoEqualizedSink {})?; 30 let stdin = io::stdin();
60 info!("Using equalized sink: {:?}", sink_path.as_cstr()); 31 let mut handle = stdin.lock();
61 32 handle.read_to_string(&mut buffer)?;
62 trace!("Connecting to equalized sink"); 33 } else {
63 Ok(conn.with_path("org.PulseAudio.Core1", sink_path, 2000)) 34 let mut file = File::open(file)?;
35 file.read_to_string(&mut buffer)?;
36 }
37 Ok(buffer)
64} 38}
65 39
66pub fn send_filter(conn_sink: &ConnPath<&Connection>, filter: Filter) -> Result<(), Error> { 40pub fn read_filter_from_arg(file: &str) -> Result<Filter, Error> {
67 let channel = conn_sink.get_nchannels()?; 41 debug!("Reading filter from '{}' in the EqualizerAPO format", file);
68 info!("Using channel: {}", channel); 42 let content = read_filearg_to_str(file)?;
69 trace!("Sending filter: {:?}", filter); 43 parse_filter(&content)
70 conn_sink.seed_filter(
71 channel,
72 filter.frequencies,
73 filter.coefficients,
74 filter.preamp,
75 )?;
76 Ok(())
77} 44}
78 45
79pub fn read_filter<T>(file: &mut T) -> Result<Filter, Error> 46pub fn parse_filter(content: &str) -> Result<Filter, Error> {
80where
81 T: io::Read,
82{
83 let mut buffer = String::new();
84
85 info!("Reading filter in GraphicEQ format from the command line");
86 file.read_to_string(&mut buffer)?;
87
88 // TODO: lifetime issue when "throwing" parse error 47 // TODO: lifetime issue when "throwing" parse error
89 let filter = EqualizerApoParser::new() 48 let filter = EqualizerApoParser::new()
90 .parse(&buffer) 49 .parse(&content)
91 .map_err(|e| convert_parse_error(EqualizerConfFormat::EqualizerAPO, e))?; 50 .map_err(|e| convert_parse_error(EqualizerConfFormat::EqualizerAPO, e))?;
92 trace!("Parsed filter: {:?}", filter); 51 trace!("Parsed filter: {:?}", filter);
93 52
@@ -109,13 +68,8 @@ where
109 } 68 }
110} 69}
111 70
112fn get_pulse_dbus_sock() -> Result<String, Error> { 71pub fn decibel_to_ratio(decibel: f64) -> f64 {
113 trace!("Connecting to the D-Bus' session bus"); 72 10f64.powf(decibel / 10f64).sqrt()
114 let conn = Connection::get_private(BusType::Session)?;
115 let conn = conn.with_path("org.PulseAudio1", "/org/pulseaudio/server_lookup1", 2000);
116
117 trace!("Checking PulseAudio's D-Bus socket path");
118 Ok(conn.get_address()?)
119} 73}
120 74
121/* 75/*