summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: ef4b93f83d37b18999db64e4ca8533bfabd7e7fd (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use rumu::{self, output::Output};
use std::env;
use std::path::PathBuf;

#[derive(Debug)]
struct MainError {
    mes: &'static str,
}

impl std::fmt::Display for MainError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "main error: {}", self.mes)
    }
}

impl std::error::Error for MainError {}

impl From<&'static str> for MainError {
    fn from(mes: &'static str) -> Self {
        Self { mes }
    }
}

fn get_root() -> std::io::Result<PathBuf> {
    let path = env::current_dir()?;
    let path_ancestors = path.as_path().ancestors();

    for p in path_ancestors {
        let has_cargo_p = std::fs::read_dir(p)?
            .into_iter()
            .any(|p| p.unwrap().file_name() == std::ffi::OsString::from("Cargo.toml"));

        if has_cargo_p {
            return Ok(PathBuf::from(p));
        }
    }

    Err(std::io::Error::new(
        std::io::ErrorKind::NotFound,
        "Cannot find project root",
    ))
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args: Vec<_> = env::args().skip(1).collect();

    if args.iter().any(|string| string as &str == "-h") {
        println!(
            "Usage: cargo run [--features ffmpeg] -- [-h] [sheet filename] [output filename] [encoder]"
        );

        println!();

        let prefix = " ".repeat(7);

        println!("{prefix}-h: just print this usage and exit");

        println!();

        println!("{prefix}sheet filename \t the file to read the sheet from");
        println!("{prefix}output filename \t the name of the output audio file");
        println!("{prefix}encoder \t\t the encoder used to encode the output");

        println!("{}", "-".repeat(89));

        println!("{prefix}To skip a paramter, use \"-\" in its place");

        return Ok(());
    }

    let mut sheet_name = "Ievan Polkka".to_owned();

    let mut output_name = "Ievan Polkka.opus".to_owned();

    let mut encoder = if cfg!(feature = "ffmpeg") {
        "opus"
    } else {
        "plain"
    };

    match args.len() {
        0 => {}
        1 => {
            if args[0] != "-" {
                sheet_name = args[0].to_owned();
                output_name = format!("{sheet_name}.opus");
            }
        }
        2 => {
            if args[0] != "-" {
                sheet_name = args[0].to_owned();
            }

            if args[1] != "-" {
                output_name = args[1].to_owned();
            } else {
                output_name = format!("{sheet_name}.opus");
            }
        }
        3 => {
            if args[0] != "-" {
                sheet_name = args[0].to_owned();
            }

            let mut output_not_specified = false;

            if args[1] != "-" {
                output_name = args[1].to_owned();
            } else {
                output_not_specified = true;
                output_name = format!("{sheet_name}.{encoder}");
            }

            if args[2] != "-" {
                encoder = &args[2];

                if output_not_specified {
                    output_name = format!("{sheet_name}.{encoder}");
                }
            }
        }
        _ => {
            println!(
                "Usage: cargo run [--features ffmpeg] -- \
                 [-h] [sheet filename] [output filename] [encoder]"
            );

            println!();

            let prefix = " ".repeat(7);

            println!("{prefix}-h: just print this usage and exit");

            println!();

            println!("{prefix}sheet filename \t the file to read the sheet from");
            println!("{prefix}output filename \t the name of the output audio file");
            println!("{prefix}encoder \t\t the encoder used to encode the output");

            println!("{}", "-".repeat(89));

            println!("{prefix}To skip a paramter, use \"-\" in its place");

            std::process::exit(1);
        }
    }

    let project_root = get_root()?.to_str().unwrap().to_owned();

    output_name = format!("{project_root}/audio files/{output_name}");

    let source = std::fs::read_to_string(format!("{project_root}/songs/{sheet_name}.rumu"))?;

    let mut sheet: rumu::sheet::Sheet = source.parse()?;

    let wave: rumu::Wave = (&mut sheet).into();

    let rate: rumu::Samples = 44100f64.into();

    match encoder {
        "opus" => {
            if !cfg!(feature = "ffmpeg") {
                return Err(
                    "To use the opus encoder one has to enable the \"ffmpeg\" feature.".into(),
                );
            }

            #[cfg(feature = "ffmpeg")]
            {
                let output = rumu::output::ffmpeg_output::OpusOutput::default();

                output.save(wave, rate, &output_name)?;
            }
        }
        "mp3" => {
            if !cfg!(feature = "ffmpeg") {
                return Err(
                    "To use the mp3 encoder one has to enable the \"ffmpeg\" feature.".into(),
                );
            }

            #[cfg(feature = "ffmpeg")]
            {
                let output = rumu::output::ffmpeg_output::MP3Output::default();

                output.save(wave, rate, &output_name)?;
            }
        }
        "aac" => {
            if !cfg!(feature = "ffmpeg") {
                return Err(
                    "To use the aac encoder one has to enable the \"ffmpeg\" feature.".into(),
                );
            }

            #[cfg(feature = "ffmpeg")]
            {
                let output = rumu::output::ffmpeg_output::AACOutput::default();

                output.save(wave, rate, &output_name)?;
            }
        }
        "plain" => {
            let output = rumu::output::PlainOutput::default();

            output.save(wave, rate, &output_name)?;
        }
        _ => {
            return Err("Unrecognized encoder: {encoder}".into());
        }
    }

    Ok(())
}