summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 171fe3cd8e0f72bbd53e692a7a4e8fac02e948cf (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
extern crate dbus;

#[macro_use]
extern crate log;
#[macro_use]
extern crate failure;

#[macro_use]
extern crate clap;
extern crate clap_log_flag;
extern crate clap_verbosity_flag;
extern crate structopt;

mod dbus_api;
mod parsing;
mod utils;

use utils::*;

use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer;
use failure::Error;
use structopt::StructOpt;

use std::fs::File;
use std::io;

#[derive(StructOpt, Debug)]
#[structopt(raw(setting = "structopt::clap::AppSettings::ColoredHelp"))]
/// Hello World! How are you doing?
struct Cli {
    #[structopt(flatten)]
    verbose: clap_verbosity_flag::Verbosity,
    #[structopt(flatten)]
    log: clap_log_flag::Log,
    #[structopt(short = "s")]
    /// Use the given sink.
    ///
    /// By default it will use the last equalized sink it finds
    sink: Option<String>,
    #[structopt(subcommand)]
    cmd: Command,
}

#[derive(StructOpt, Debug)]
enum Command {
    #[structopt(name = "load",)]
    /// Load and switch to a given equalizer configuration
    Load(LoadCli),
    #[structopt(name = "reset")]
    /// Switch to a neutral equalizer
    Reset(ResetCli),
}

#[derive(StructOpt, Debug)]
struct LoadCli {
    #[structopt(default_value = "-")]
    /// The file from which to load the equalizer configuration
    ///
    /// If "-" is given, read the configuration from the command-line.
    file: String,
    #[structopt(
        short = "f",
        raw(
            possible_values = "&EqualizerConfFormat::variants()",
            case_insensitive = "true"
        ),
        default_value = "EqualizerAPO"
    )]
    /// The file format of the equalizer configuration
    format: EqualizerConfFormat,
}

arg_enum! {
    #[derive(Debug)]
    enum EqualizerConfFormat {
        EqualizerAPO
    }
}

#[derive(StructOpt, Debug)]
struct ResetCli {}

#[derive(Fail, Debug)]
#[fail(display = "No equalized sink found")]
struct NoEqualizedSink;

#[derive(Debug)]
pub struct Filter {
    preamp: f64,
    frequencies: Vec<u32>,
    coefficients: Vec<f64>,
}

impl Filter {
    fn pad(self, filter_rate: u32) -> Self {
        Filter {
            preamp: self.preamp,
            frequencies: vec![0u32]
                .into_iter()
                .chain(self.frequencies.into_iter())
                .chain(vec![filter_rate / 2u32])
                .collect(),
            coefficients: vec![1f64]
                .into_iter()
                .chain(self.coefficients.into_iter())
                .chain(vec![1f64])
                .collect(),
        }
    }
}

fn main() -> Result<(), Error> {
    let args = Cli::from_args();
    args.log.log_all(args.verbose.log_level())?;

    use Command::*;

    match args.cmd {
        Load(args) => load(args),
        Reset(args) => reset(args),
    }
}

fn reset(args: ResetCli) -> Result<(), Error> {
    let conn = connect()?;
    let conn_sink = get_equalized_sink(&conn)?;
    let filter_rate = conn_sink.get_filter_sample_rate()?;
    let filter = Filter {
        preamp: 1f64,
        frequencies: vec![],
        coefficients: vec![],
    }.pad(filter_rate);

    send_filter(&conn_sink, filter)?;

    Ok(())
}

fn load(args: LoadCli) -> Result<(), Error> {
    let conn = connect()?;
    let conn_sink = get_equalized_sink(&conn)?;

    let filter = if args.file == "-" {
        let stdin = io::stdin();
        let mut lock = stdin.lock();
        read_filter(&mut lock)?
    } else {
        let mut file = File::open(args.file)?;
        read_filter(&mut file)?
    };

    let filter_rate = conn_sink.get_filter_sample_rate()?;
    send_filter(&conn_sink, filter.pad(filter_rate))?;

    Ok(())
}