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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#![no_std]

extern crate smallvec;
use smallvec::SmallVec;

pub struct SyncDevice {
    /// sync tracks (the vertical columns in the editor)
    pub tracks: SmallVec<[SyncTrack; 64]>,
    /// rows per beat
    pub rpb: u8,
    /// beats per minute
    pub bpm: f64,
    /// rows per second
    pub rps: f64,
    pub is_paused: bool,
    /// current row
    pub row: u32,
    /// current time in milliseconds
    pub time: u32,
}

impl SyncDevice {
    pub fn new(bpm: f64, rpb: u8) -> SyncDevice {
        SyncDevice {
            tracks: SmallVec::new(),
            rpb: rpb,
            bpm: bpm,
            rps: rps(bpm, rpb),
            is_paused: true,
            row: 0,
            time: 0,
        }
    }

    pub fn set_row_from_time(&mut self) {
        let r: f64 = (self.time as f64 / 1000.0) * self.rps + 0.5;
        self.row = r as u32;
    }

    pub fn get_track_value(&self, track_id: usize) -> Result<f64, SyncError> {
        if self.tracks.len() > track_id {
            return Ok(self.tracks[track_id].value_at(self.row));
        } else {
            return Err(SyncError::TrackDoesntExist);
        }
    }
}

pub struct SyncTrack {
    /// key frames, rows where values change
    pub keys: SmallVec<[TrackKey; 64]>,
}

pub enum SyncError {
    TrackDoesntExist
}

pub struct TrackKey {
    pub row: u32,
    pub value: f32,
    /// interpolation type
    pub key_type: KeyType,
}

pub enum KeyType {
    Step, // constant until value changes
    Linear, // linear interpolation
    Smooth, // smooth curve
    Ramp, // exponential ramp
    NOOP,
}

pub enum ActiveKeyIdx {
    /// key is on this row
    ExactRow(usize),
    /// key is on a previous row
    PrevRow(usize),
    /// the row is before the first key
    BeforeFirstRow,
    /// row moved past the last row
    AfterLastRow,
}

impl SyncTrack {
    pub fn new() -> SyncTrack {
        SyncTrack {
            keys: SmallVec::new(),
        }
    }

    /// Adds a key to the track, inserting sorted by row, replacing if one already exists on that row
    pub fn add_key(&mut self, track_key: TrackKey) {

        let res = self.find_active_key_idx_for_row(track_key.row);

        if let Some(idx) = res {
            // Some kind of active key
            use self::ActiveKeyIdx::*;
            match idx {
                // replace key
                ExactRow(n) => self.keys[n] = track_key,

                // add new key
                PrevRow(n) => self.keys.insert(n+1, track_key),
                BeforeFirstRow => self.keys.insert(0, track_key),
                AfterLastRow => self.keys.push(track_key),
            }
        } else {
            // No keys, first key
            self.keys.push(track_key);
        }
    }

    /// Deletes the key found on the given row
    pub fn delete_key(&mut self, row: u32) {
        if let Some(idx) = self.find_key_idx_by_row(row) {
            self.keys.remove(idx);
        }
    }

    /// Returns index of the key with the given row, or `None`
    pub fn find_key_idx_by_row(&self, row: u32) -> Option<usize> {
        for (idx, key) in self.keys.iter().enumerate() {
            if key.row == row {
                return Some(idx);
            }
        }

        None
    }

    pub fn value_at(&self, row: u32) -> f64 {

        let hit_idx: usize;

        if let Some(hit) = self.find_active_key_idx_for_row(row) {
            use self::ActiveKeyIdx::*;
            match hit {
                ExactRow(n) => return self.keys[n].value as f64,

                PrevRow(n) => hit_idx = n,

                // hit is beyond the last key
                AfterLastRow => return self.keys[self.keys.len() - 1].value as f64,

                BeforeFirstRow => return self.keys[0].value as f64,
            }
        } else {
            return 0.0;
        }

        // return interpolated value
        let cur_key = &self.keys[hit_idx];
        let next_key = &self.keys[hit_idx + 1];

	      let t: f64 = ((row - cur_key.row) as f64) / ((next_key.row - cur_key.row) as f64);
        let a: f64 = cur_key.value as f64;
        let b: f64 = (next_key.value - cur_key.value) as f64;

        use self::KeyType::*;
        match cur_key.key_type {
            Step => return a,

            Linear => return a + b * t,

            Smooth => return a + b * (t*t * (3.0 - 2.0 * t)),

            Ramp => return a + b * t*t,

            NOOP => return 0.0,
        }

    }

    /// Find the active key idx for a row
    pub fn find_active_key_idx_for_row(&self, row: u32) -> Option<ActiveKeyIdx> {
        if self.keys.len() == 0 {
            return None;
        }

        // Linear search. Keys are sorted by row.

        let mut hit_idx: usize = 0;
        let mut ret: Option<ActiveKeyIdx> = None;

        for (idx, key) in self.keys.iter().enumerate() {
            if key.row == row {
                return Some(ActiveKeyIdx::ExactRow(idx));
            } else if key.row < row {
                hit_idx = idx;
                ret = Some(ActiveKeyIdx::PrevRow(hit_idx));
            }
        }

        if hit_idx == self.keys.len() - 1 {
            return Some(ActiveKeyIdx::AfterLastRow);
        }

        if hit_idx == 0 && ret.is_none() {
            return Some(ActiveKeyIdx::BeforeFirstRow);
        }

        ret
    }
}

impl TrackKey {
    pub fn new() -> TrackKey {
        TrackKey {
            row: 0,
            value: 0.0,
            key_type: KeyType::Step,
        }
    }
}

/// Calculate rows per second
pub fn rps(bpm: f64, rpb: u8) -> f64 {
    (bpm / 60.0) * (rpb as f64)
}

pub fn key_to_code(key: &KeyType) -> u8 {
    use self::KeyType::*;
    match *key {
        Step      => 0,
        Linear    => 1,
        Smooth    => 2,
        Ramp      => 3,
        NOOP      => 255,
    }
}

pub fn code_to_key(code: u8) -> KeyType {
    use self::KeyType::*;
    match code {
        0 => Step,
        1 => Linear,
        2 => Smooth,
        3 => Ramp,
        _ => NOOP,
    }
}