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
//!
//!  division.rs
//!
//!  Created by Mitchell Nordine at 02:34AM on November 02, 2014.
//!
//!

use num::{NumCast, FromPrimitive, ToPrimitive};
use std::ops::{Add, Sub};
use super::TimeSig;

pub type NumDiv = i64;

/// An enum with variants used to represent a musical division.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Division {
    Bar,
    Minim,
    Beat,
    Quaver,
    SemiQuaver,
    ThirtySecond,
    SixtyFourth,
    OneHundredTwentyEighth,
    TwoHundredFiftySixth,
    FiveHundredTwelfth,
    OneThousandTwentyFourth,
}

const HIGHEST_ZOOM_STEP: u8 = 10;



impl Division {

    pub fn from_isize<T: ToPrimitive>(num: T) -> Division {
        FromPrimitive::from_isize(num.to_isize().unwrap()).unwrap()
    }

    /// Convert to the equivalent duration as a number of Beats.
    pub fn beats(&self, ts: TimeSig) -> f64 {
        use num::Float;
        match *self {
            Division::Bar => ts.beats_per_bar(),
            _ => 2.0.powi(Division::Beat as i32 - *self as i32),
        }
    }

    /// Convert to the equivalent duration as a number of Bars.
    pub fn bars(&self, ts: TimeSig) -> f64 {
        match *self {
            Division::Bar => 1.0,
            _ => self.beats(ts) / ts.beats_per_bar(),
        }
    }

    /// Zoom into a higher resolution division by the number of steps given.
    pub fn zoom_in(&self, steps: u8) -> Option<Division> {
        let zoom_step = self.to_u8() + steps;
        if zoom_step <= HIGHEST_ZOOM_STEP {
            Some(NumCast::from(zoom_step).unwrap())
        } else {
            None
        }
    }

    /// Zoom into a higher resolution division by the number of steps given.
    pub fn zoom_out(&self, steps: u8) -> Option<Division> {
        let current_zoom_step = self.to_u8();
        if steps <= current_zoom_step {
            let zoom_step = current_zoom_step - steps;
            Some(NumCast::from(zoom_step).unwrap())
        } else {
            None
        }
    }

    /// Convert a Division to its byte equivalent.
    pub fn to_u8(&self) -> u8 {
        ToPrimitive::to_u8(self).unwrap()
    }

    /// Convert a Division to its signed byte equivalent.
    pub fn to_i8(&self) -> i8 {
        ToPrimitive::to_i8(self).unwrap()
    }

}

impl NumCast for Division {
    fn from<T: ToPrimitive>(n: T) -> Option<Division> {
        Some(Division::from_isize(n.to_isize().unwrap()))
    }
}

impl FromPrimitive for Division {
    fn from_i64(n: i64) -> Option<Self> { Self::from_u64(n as u64) }
    fn from_u64(n: u64) -> Option<Self> {
        match n {
            0  => Some(Division::Bar),
            1  => Some(Division::Minim),
            2  => Some(Division::Beat),
            3  => Some(Division::Quaver),
            4  => Some(Division::SemiQuaver),
            5  => Some(Division::ThirtySecond),
            6  => Some(Division::SixtyFourth),
            7  => Some(Division::OneHundredTwentyEighth),
            8  => Some(Division::TwoHundredFiftySixth),
            9  => Some(Division::FiveHundredTwelfth),
            10 => Some(Division::OneThousandTwentyFourth),
            _  => None,
        }
    }
}

impl ToPrimitive for Division {
    fn to_i64(&self) -> Option<i64> { self.to_u64().map(|n| n as i64) }
    fn to_u64(&self) -> Option<u64> {
        Some(match *self {
            Division::Bar => 0,
            Division::Minim => 1,
            Division::Beat => 2,
            Division::Quaver => 3,
            Division::SemiQuaver => 4,
            Division::ThirtySecond => 5,
            Division::SixtyFourth => 6,
            Division::OneHundredTwentyEighth => 7,
            Division::TwoHundredFiftySixth => 8,
            Division::FiveHundredTwelfth => 9,
            Division::OneThousandTwentyFourth => 10,
        })
    }
}

impl Add<isize> for Division {
    type Output = isize;
    fn add(self, rhs: isize) -> isize {
        self.to_isize().unwrap() + rhs
    }
}

impl Sub<isize> for Division {
    type Output = isize;
    fn sub(self, rhs: isize) -> isize {
        self.to_isize().unwrap() - rhs
    }
}

/// The 'Division Type'. Used for handling 'Thirds'.
/// Whole represents a Whole division, while TwoThirds represents two thirds of a division.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum DivType {
    Whole,
    TwoThirds,
}


impl DivType {

    pub fn from_isize<T: NumCast>(num: T) -> DivType {
        FromPrimitive::from_isize(num.to_isize().unwrap()).unwrap()
    }

}

impl NumCast for DivType {
    fn from<T: ToPrimitive>(n: T) -> Option<DivType> {
        Some(DivType::from_isize(n.to_isize().unwrap()))
    }
}

impl ::rand::Rand for DivType {
    fn rand<R: ::rand::Rng>(rng: &mut R) -> DivType {
        let rand: bool = rng.gen();
        if rand { DivType::Whole } else { DivType::TwoThirds }
    }
}

impl FromPrimitive for DivType {
    fn from_i64(n: i64) -> Option<Self> { Self::from_u64(n as u64) }
    fn from_u64(n: u64) -> Option<Self> {
        match n {
            0 => Some(DivType::Whole),
            1 => Some(DivType::TwoThirds),
            _ => None,
        }
    }
}

impl ToPrimitive for DivType {
    fn to_i64(&self) -> Option<i64> { self.to_u64().map(|n| n as i64) }
    fn to_u64(&self) -> Option<u64> {
        Some(match self {
            &DivType::Whole     => 0,
            &DivType::TwoThirds => 1,
        })
    }
}

impl Add<isize> for DivType {
    type Output = isize;
    fn add(self, rhs: isize) -> isize {
        self.to_isize().unwrap() + rhs
    }
}

impl Sub<isize> for DivType {
    type Output = isize;
    fn sub(self, rhs: isize) -> isize {
        self.to_isize().unwrap() - rhs
    }
}