summaryrefslogtreecommitdiff
path: root/src/sheet/play.rs
blob: c2a632db55abc67e12f3b85fa0abab0fc47f5b01 (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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#![warn(missing_docs)]
//! This file implements the interface to turn a parsed representation
//! of a sheet into pulses.

use super::*;

#[allow(unused_imports)]
use crate::instruments::{self, Instrument as IInstrument};

#[derive(Debug, Clone, Copy)]
/// The environment when generating waves from a sheet.
struct SheetEnv {
    volume: Volume,
    bpm: BPM,
    instrument: Instrument,
    duration: Beats,
    octave: usize,
    rate: Samples,
}

impl SheetEnv {
    fn set_volume(&mut self, volume: Volume) {
        self.volume = volume;
    }
    fn set_bpm(&mut self, bpm: BPM) {
        self.bpm = bpm;
    }
    fn set_instrument(&mut self, instrument: Instrument) {
        self.instrument = instrument;
    }
    fn set_duration(&mut self, duration: Beats) {
        self.duration = duration;
    }
    fn set_octave(&mut self, octave: usize) {
        self.octave = octave;
    }
}

impl Default for SheetEnv {
    fn default() -> Self {
        let volume = Volume::default();
        let bpm = BPM::default();
        let instrument = Instrument::default();
        let duration = Beats::default();
        let octave = 4usize;
        let rate = Samples::default();

        Self {
            volume,
            bpm,
            instrument,
            duration,
            octave,
            rate,
        }
    }
}

impl From<(&SheetDuration, Beats)> for Beats {
    fn from((duration, env_duration): (&SheetDuration, Beats)) -> Self {
        let duration_base: Beats = if let Some(base) = duration.beats {
            base
        } else {
            env_duration
        };

        let mut final_duration = duration_base;

        for i in (0..duration.dots).map(|n| n + 1) {
            let power = 2usize.pow(i as u32) as f64;

            final_duration = (*final_duration + *duration_base / power).into();
        }

        final_duration
    }
}

impl From<(&SheetUnit, &mut SheetEnv)> for Wave {
    fn from((unit, env): (&SheetUnit, &mut SheetEnv)) -> Self {
        let mut result = Vec::new();

        // Unfortunately our trait is wrongly designed and that is not
        // dynamically dispatchable, so we need separate variables for
        // each instrument.
        let mut piano = instruments::PianoFromC::default();
        let mut violin = instruments::Violin::default();
        let mut sinus = instruments::Sinus::default();
        let mut tangent = instruments::Tangent::default();

        match unit {
            SheetUnit::Tone(chord, duration) => {
                let mut local_waves = Vec::new();

                for tone in chord.0.iter() {
                    let octave = if let Some(abs_or_rel) = tone.octave {
                        match abs_or_rel {
                            SheetOctave::Absolute(absolute_octave) => absolute_octave,
                            SheetOctave::Relative(relative_octave) => {
                                let result: isize = relative_octave + env.octave as isize;

                                assert!(!result.is_negative());

                                result as usize
                            }
                        }
                    } else {
                        env.octave
                    };

                    env.set_octave(octave);

                    let semitone: Semitones = (octave, tone.semitone).into();

                    let final_duration: Beats = (duration, env.duration).into();

                    env.set_duration(final_duration);

                    let wave = match env.instrument {
                        Instrument::Piano => {
                            piano.play(semitone, env.rate, env.bpm, final_duration, env.volume)
                        }
                        Instrument::Violin => {
                            violin.play(semitone, env.rate, env.bpm, final_duration, env.volume)
                        }
                        Instrument::Sinus => {
                            sinus.play(semitone, env.rate, env.bpm, final_duration, env.volume)
                        }
                        Instrument::Tangent => {
                            tangent.play(semitone, env.rate, env.bpm, final_duration, env.volume)
                        }
                    };

                    local_waves.push(wave);
                }

                let local_result = mix_waves_exact(local_waves);

                result.append(&mut local_result.to_vec());
            }
            SheetUnit::Silence(duration) => {
                let duration_per_beat: Seconds = env.bpm.into();

                let final_duration: Beats = (duration, env.duration).into();

                env.set_duration(final_duration);

                let duration: Seconds = (*final_duration * *duration_per_beat).into();
                let nb_samples = (*duration * *env.rate).floor() as usize;

                result.append(&mut vec![0f64.into(); nb_samples]);
            }
            SheetUnit::Instruction(instruction) => match instruction {
                Instruction::VolumeTo(volume) => {
                    env.set_volume(*volume);
                }
                Instruction::BPMTo(bpm) => {
                    env.set_bpm(*bpm);
                }
                Instruction::InstrumentTo(instrument) => {
                    env.set_instrument(*instrument);
                }
            },
        }

        Wave::new(result)
    }
}

impl From<(&mut SheetGroup, &mut SheetEnv)> for Wave {
    fn from((group, env): (&mut SheetGroup, &mut SheetEnv)) -> Self {
        group.do_cram(&mut env.duration);

        // if group.nodes.len() == 19 {
        //     println!("group={group:?}");
        // }

        // After we crammed the expressions, we are sure that all
        // tones have the correct durations, so we can safely ignore
        // all cram expressions afterwards.

        let mut stack = vec![0usize];
        let mut seen = vec![false; group.nodes.len()];
        let mut stack_args: Vec<Wave> = vec![];

        while let Some(top) = stack.pop() {
            match &group.nodes[top] {
                SheetGroupNode::Intermediate(repetition, _) if *repetition != 0 => {
                    if !seen[top] {
                        stack.push(top);

                        stack.append(
                            &mut group.edges[top]
                                .iter()
                                .copied()
                                .rev()
                                .filter(|child| match group.nodes[*child] {
                                    SheetGroupNode::Intermediate(child_repetition, _)
                                        if child_repetition != 0 =>
                                    {
                                        true
                                    }
                                    SheetGroupNode::Unit(_) => true,
                                    _ => false,
                                })
                                .collect(),
                        );
                    } else {
                        match repetition {
                            0 => {
                                unreachable!()
                            }
                            // 1 => {
                            //     for _ in 0..group.edges[top]
                            //         .iter()
                            //         .filter(|child| match group.nodes[**child] {
                            //             SheetGroupNode::Intermediate(child_repetition, _)
                            //                 if child_repetition != 0 =>
                            //             {
                            //                 true
                            //             }
                            //             SheetGroupNode::Unit(_) => true,
                            //             _ => false,
                            //         })
                            //         .count()
                            //     {
                            //         result.append(&mut stack_args.pop().unwrap().to_vec());
                            //     }
                            // }
                            _ => {
                                let mut local_stack_args = vec![];
                                let mut local_results = vec![];
                                let mut final_local_result = vec![];

                                for _ in 0..group.edges[top]
                                    .iter()
                                    .filter(|child| match group.nodes[**child] {
                                        SheetGroupNode::Intermediate(child_repetition, _)
                                            if child_repetition != 0 =>
                                        {
                                            true
                                        }
                                        SheetGroupNode::Unit(_) => true,
                                        _ => false,
                                    })
                                    .count()
                                {
                                    local_stack_args.push(stack_args.pop().unwrap());
                                }

                                for local_wave in local_stack_args.into_iter().rev() {
                                    local_results.append(&mut local_wave.to_vec());
                                }

                                for _ in 0..(repetition - 1) {
                                    final_local_result.append(&mut local_results.clone());
                                }

                                final_local_result.append(&mut local_results);

                                stack_args.push(Wave::new(final_local_result));
                            }
                        }
                    }
                }
                SheetGroupNode::Unit(unit) => {
                    stack_args.push((unit, &mut *env).into());
                }
                _ => {
                    continue;
                }
            }

            seen[top] = true;
        }

        let result = stack_args
            .iter()
            .flat_map(|vec| vec.iter())
            .copied()
            .collect();

        Wave::new(result)
    }
}

// Note: The rest are but ordinary house-keeping codes to glue
// everything together.

impl From<&mut SheetBlock> for Wave {
    fn from(block: &mut SheetBlock) -> Self {
        let mut result = Vec::new();
        let mut env = SheetEnv::default();

        for group in block.groups.iter_mut() {
            let wave: Wave = (group, &mut env).into();

            result.append(&mut wave.to_vec());
        }

        Self::new(result)
    }
}

impl From<&mut Sheet> for Wave {
    fn from(sheet: &mut Sheet) -> Self {
        mix_waves_exact(sheet.blocks.iter_mut().map(Into::<Wave>::into).collect())
    }
}

#[cfg(test)]
mod tests {
    // use super::*;
}