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
#![recursion_limit = "128"]

extern crate proc_macro;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro::TokenStream;

#[proc_macro_derive(AudioScheduledSourceNode)]
pub fn audio_scheduled_source_node(input: TokenStream) -> TokenStream {
    let ast: syn::DeriveInput = syn::parse(input).unwrap();
    let gen = impl_audio_scheduled_source_node(&ast);
    gen.into()
}

fn impl_audio_scheduled_source_node(ast: &syn::DeriveInput) -> proc_macro2::TokenStream {
    let name = &ast.ident;
    quote! {
        impl #name {
            fn should_play_at(&mut self, tick: Tick) -> ShouldPlay {
                let start = if let Some(start) = self.start_at {
                    start
                } else {
                    return ShouldPlay::No;
                };

                let frame_end = tick + Tick::FRAMES_PER_BLOCK;
                if tick < start {
                    if frame_end < start {
                        ShouldPlay::No
                    } else {
                        let delta_start = start - tick;
                        if let Some(stop) = self.stop_at {
                            if stop <= start {
                                self.maybe_trigger_onended_callback();
                                return ShouldPlay::No;
                            }
                            if stop > frame_end {
                                ShouldPlay::Between(delta_start, Tick::FRAMES_PER_BLOCK)
                            } else {
                                self.maybe_trigger_onended_callback();
                                ShouldPlay::Between(delta_start, stop - tick)
                            }
                        } else {
                            ShouldPlay::Between(delta_start, Tick::FRAMES_PER_BLOCK)
                        }
                    }
                } else {
                    let stop = if let Some(stop) = self.stop_at {
                        stop
                    } else {
                        return ShouldPlay::Between(Tick(0), Tick::FRAMES_PER_BLOCK);
                    };
                    if stop > frame_end {
                        ShouldPlay::Between(Tick(0), Tick::FRAMES_PER_BLOCK)
                    } else if stop < tick {
                        self.maybe_trigger_onended_callback();
                        ShouldPlay::No
                    } else {
                        self.maybe_trigger_onended_callback();
                        ShouldPlay::Between(Tick(0), stop - tick)
                    }
                }
            }

            fn start(&mut self, tick: Tick) -> bool {
                // We can only allow a single call to `start` and always before
                // any `stop` calls.
                if self.start_at.is_some() || self.stop_at.is_some() {
                    return false;
                }
                self.start_at = Some(tick);
                true
            }

            fn stop(&mut self, tick: Tick) -> bool {
                // We can only allow calls to `stop` after `start` is called.
                if self.start_at.is_none() {
                    return false;
                }
                // If `stop` is called again after already having been called,
                // the last invocation will be the only one applied.
                self.stop_at = Some(tick);
                true
            }

            fn maybe_trigger_onended_callback(&mut self) {
                // We cannot have an end without a start.
                if self.start_at.is_none() {
                    return;
                }
                if let Some(cb) = self.onended_callback.take() {
                    cb.0()
                }
            }

            fn handle_source_node_message(&mut self, message: AudioScheduledSourceNodeMessage, sample_rate: f32) {
                match message {
                    AudioScheduledSourceNodeMessage::Start(when) => {
                        self.start(Tick::from_time(when, sample_rate));
                    }
                    AudioScheduledSourceNodeMessage::Stop(when) => {
                        self.stop(Tick::from_time(when, sample_rate));
                    }
                    AudioScheduledSourceNodeMessage::RegisterOnEndedCallback(callback) => {
                        self.onended_callback = Some(callback);
                    }
                }
            }
        }
    }
}

#[proc_macro_derive(AudioNodeCommon)]
pub fn channel_info(input: TokenStream) -> TokenStream {
    let ast: syn::DeriveInput = syn::parse(input).unwrap();
    let name = &ast.ident;
    let gen = quote! {
        impl ::node::AudioNodeCommon for #name {
            fn channel_info(&self) -> &::node::ChannelInfo {
                &self.channel_info
            }

            fn channel_info_mut(&mut self) -> &mut ::node::ChannelInfo {
                &mut self.channel_info
            }
        }
    };
    gen.into()
}