aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: e60ee45d862c1bf3d1009bf41e8b7398fb99e1d9 (plain) (blame)
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
// trriggy - a trigger/drum replacer in Rust
// Copyright (C) 2018  Harald Eilertsen <haraldei@anduin.net>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use clap::{self, crate_name, crate_authors, crate_version, crate_description};
use hound;
use std::io::Read;
use std::iter::FromIterator;

#[derive(Default)]
struct Trigger {
    trigged: bool,
    playing: bool,
    acc: u32,
    sample_pos: usize,
    sample: Vec<i16>,
}

impl Trigger {
    pub fn new<T: Read>(samples: hound::WavReader<T>) -> Trigger {
        Trigger::default().read_samples(samples)
    }

    fn read_samples<T: Read>(mut self, mut reader: hound::WavReader<T>) -> Trigger {
        self.sample = Vec::from_iter(reader.samples().map(|s| s.unwrap()));
        self
    }

    pub fn process(&mut self, sample: i16) -> i16 {
        let mut res = 0i16;

        let abs = sample.abs() as u32;
        let div = if (abs > self.acc) {
            abs - self.acc
        }
        else {
            0
        };

        self.acc = (self.acc + abs) / 2;
        if !self.trigged && div > 5000 {
            self.trigged = true;
            self.playing = true;
            self.sample_pos = 0;
        }
        else if self.trigged && self.acc < 250 {
            self.trigged = false;
        }

        if self.playing {
            res = self.sample[self.sample_pos];
            self.sample_pos += 1;
        }

        if self.sample_pos == self.sample.len() {
            self.playing = false;
        }

        res
    }
}

fn print_wavspec<R: Read>(spec: &hound::WavSpec, r: &hound::WavReader<R>) {
    println!("  channels: {}", spec.channels);
    println!("  samplerate: {}", spec.sample_rate);
    println!("  bits/sample: {}", spec.bits_per_sample);
    println!("  sample format: {}", match spec.sample_format {
        hound::SampleFormat::Int => "integer",
        hound::SampleFormat::Float => "floating point",
    });
    println!("  duration: {} samples/{} sec/{} min",
        r.duration(),
        r.duration()/spec.sample_rate,
        r.duration()/(spec.sample_rate * 60));
}

fn main() {
    let m = clap::app_from_crate!()
        .arg(clap::Arg::with_name("sample")
            .short("s")
            .long("sample")
            .value_name("SAMPLE_FILE")
            .help("The sample to replace when trigged.")
            .takes_value(true)
            .required(true))
        .arg(clap::Arg::with_name("output")
            .short("o")
            .long("output")
            .value_name("OUTPUT_FILE")
            .help("Where to store the result.")
            .takes_value(true))
        .arg(clap::Arg::with_name("INPUT")
            .help("The input file")
            .required(true))
        .get_matches();

    let filename = m.value_of("INPUT").unwrap();
    let mut reader = hound::WavReader::open(&filename).unwrap();

    println!("Reading file {}:", &filename);

    let spec = reader.spec();
    print_wavspec(&spec, &reader);

    let sample_filename = m.value_of("sample").unwrap();
    println!("Using sample file: {}", sample_filename);
    let mut sample_file = hound::WavReader::open(sample_filename).unwrap();

    print_wavspec(&sample_file.spec(), &sample_file);

    let output_filename = m.value_of("output").unwrap_or("output.wav");
    println!("Writing to output: {}", output_filename);
    let mut output = hound::WavWriter::create(output_filename, spec).unwrap();

    let samples = reader.samples::<i16>();
    let mut triggey = Trigger::new(sample_file);

    samples
        .map(|s| s.unwrap())
        .for_each(|s| {
            output.write_sample(triggey.process(s)).unwrap();
        });

    output.finalize().unwrap();
}