view src/lib/signal/math.rs @ 23:2d5ae243921c

lib: move signal math into a common macro Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Wed, 10 Jun 2020 12:29:27 -0400
parents
children
line wrap: on
line source

/*
 * Copyright (c) 2020 Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

macro_rules! signal_math {
    /* Foo signal */
    ($type:ident) => {
        /* mix two signals */
        impl<T: Signal> Mul<T> for $type {
            type Output = Mixer<$type, T>;

            fn mul(self, rhs: T) -> Self::Output {
                Mixer::new(self, rhs)
            }
        }

        /* add gain to a signal */
        impl Mul<Scalar> for $type {
            type Output = Gain<$type>;

            fn mul(self, rhs: Scalar) -> Self::Output {
                Gain::new(self, rhs)
            }
        }

        impl Mul<$type> for Scalar {
            type Output = Gain<$type>;

            fn mul(self, rhs: $type) -> Self::Output {
                Gain::new(rhs, self)
            }
        }
    };

    /* Foo<T> signal */
    ($type:ident < $lim:ident >) => {
        /* mix two signals */
        impl<T: $lim, U: Signal> Mul<U> for $type<T> {
            type Output = Mixer<$type<T>, U>;

            fn mul(self, rhs: U) -> Self::Output {
                Mixer::new(self, rhs)
            }
        }

        /* add gain to a signal */
        impl<T: $lim> Mul<Scalar> for $type<T> {
            type Output = Gain<$type<T>>;

            fn mul(self, rhs: Scalar) -> Self::Output {
                Gain::new(self, rhs)
            }
        }

        impl<T: $lim> Mul<$type<T>> for Scalar {
            type Output = Gain<$type<T>>;

            fn mul(self, rhs: $type<T>) -> Self::Output {
                Gain::new(rhs, self)
            }
        }
    };

    /* Foo<T, U> signal */
    ($type:ident < $lim1:ident, $lim2:ident >) => {
        /* mix two signals */
        impl<T: $lim1, U: $lim2, V: Signal> Mul<V> for $type<T, U> {
            type Output = Mixer<$type<T, U>, V>;

            fn mul(self, rhs: V) -> Self::Output {
                Mixer::new(self, rhs)
            }
        }

        /* add gain to a signal */
        impl<T: $lim1, U: $lim2> Mul<Scalar> for $type<T, U> {
            type Output = Gain<$type<T, U>>;

            fn mul(self, rhs: Scalar) -> Self::Output {
                Gain::new(self, rhs)
            }
        }

        impl<T: $lim1, U: $lim2> Mul<$type<T, U>> for Scalar {
            type Output = Gain<$type<T, U>>;

            fn mul(self, rhs: $type<T, U>) -> Self::Output {
                Gain::new(rhs, self)
            }
        }
    };
}