1 /*
2 * Copyright (c) 2019 Kevin Townsend (KTOWN)
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <math.h>
8 #include <errno.h>
9 #include <zsl/zsl.h>
10 #include <zsl/physics/sound.h>
11
12 int
zsl_phy_sound_press_amp(zsl_real_t b,zsl_real_t k,zsl_real_t a,zsl_real_t * p)13 zsl_phy_sound_press_amp(zsl_real_t b, zsl_real_t k, zsl_real_t a, zsl_real_t *p)
14 {
15 if (b < 0 || k < 0 || a < 0) {
16 *p = NAN;
17 return -EINVAL;
18 }
19
20 *p = b * k * a;
21
22 return 0;
23 }
24
25 int
zsl_phy_sound_level(zsl_real_t i,zsl_real_t i0,zsl_real_t * b)26 zsl_phy_sound_level(zsl_real_t i, zsl_real_t i0, zsl_real_t *b)
27 {
28 if (i < 0 || i0 < 0) {
29 *b = NAN;
30 return -EINVAL;
31 }
32
33 *b = 10 * ZSL_LOG10(i / i0);
34
35 return 0;
36 }
37
38 int
zsl_phy_sound_intensity(zsl_real_t b,zsl_real_t d,zsl_real_t p,zsl_real_t * i)39 zsl_phy_sound_intensity(zsl_real_t b, zsl_real_t d, zsl_real_t p, zsl_real_t *i)
40 {
41 if (b < 0 || d <= 0 || p < 0) {
42 *i = NAN;
43 return -EINVAL;
44 }
45
46 *i = (p * p * ZSL_SQRT(b / d)) / (2 * b);
47
48 return 0;
49 }
50
51 int
zsl_phy_sound_shock_wave_angle(zsl_real_t v,zsl_real_t vs,zsl_real_t * theta)52 zsl_phy_sound_shock_wave_angle(zsl_real_t v, zsl_real_t vs, zsl_real_t *theta)
53 {
54 *theta = ZSL_ASIN(v / vs);
55
56 return 0;
57 }
58
59 int
zsl_phy_sound_dop_effect(zsl_real_t v,zsl_real_t vs,zsl_real_t vl,zsl_real_t fs,zsl_real_t * fl)60 zsl_phy_sound_dop_effect(zsl_real_t v, zsl_real_t vs, zsl_real_t vl,
61 zsl_real_t fs, zsl_real_t *fl)
62 {
63 if (fs < 0 || (v + vs) == 0) {
64 *fl = NAN;
65 return -EINVAL;
66 }
67
68 *fl = (fs * (v + vl)) / (v + vs);
69
70 if (*fl < 0) {
71 *fl = NAN;
72 return -EINVAL;
73 }
74
75 return 0;
76 }
77
78 int
zsl_phy_sound_beat(zsl_real_t fa,zsl_real_t fb,zsl_real_t * f)79 zsl_phy_sound_beat(zsl_real_t fa, zsl_real_t fb, zsl_real_t *f)
80 {
81 if (fa < 0 || fb < 0) {
82 *f = NAN;
83 return -EINVAL;
84 }
85
86 *f = ZSL_ABS(fa - fb);
87
88 return 0;
89 }
90