chris@162
|
1 /**
|
chris@162
|
2 * Copyright (c) 2014, 2015, Enzien Audio Ltd.
|
chris@162
|
3 *
|
chris@162
|
4 * Permission to use, copy, modify, and/or distribute this software for any
|
chris@162
|
5 * purpose with or without fee is hereby granted, provided that the above
|
chris@162
|
6 * copyright notice and this permission notice appear in all copies.
|
chris@162
|
7 *
|
chris@162
|
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
chris@162
|
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
chris@162
|
10 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
chris@162
|
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
chris@162
|
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
chris@162
|
13 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
chris@162
|
14 * PERFORMANCE OF THIS SOFTWARE.
|
chris@162
|
15 */
|
chris@162
|
16
|
chris@162
|
17 #include "SignalPhasor.h"
|
chris@162
|
18
|
chris@162
|
19 // input phase is in the range of [0,1]. It is independent of o->phase.
|
chris@162
|
20 #if HV_SIMD_AVX
|
chris@162
|
21 static void sPhasor_updatePhase(SignalPhasor *o, float p) {
|
chris@162
|
22 o->phase = _mm256_set_ps(
|
chris@162
|
23 p+1.0f+7.0f*o->step.f2sc, p+1.0f+6.0f*o->step.f2sc,
|
chris@162
|
24 p+1.0f+5.0f*o->step.f2sc, p+1.0f+4.0f*o->step.f2sc,
|
chris@162
|
25 p+1.0f+3.0f*o->step.f2sc, p+1.0f+2.0f*o->step.f2sc,
|
chris@162
|
26 p+1.0f+o->step.f2sc, p+1.0f);
|
chris@162
|
27
|
chris@162
|
28 // ensure that o->phase is still in range [1,2]
|
chris@162
|
29 o->phase = _mm256_or_ps(_mm256_andnot_ps(
|
chris@162
|
30 _mm256_set1_ps(-INFINITY), o->phase), _mm256_set1_ps(1.0f));
|
chris@162
|
31 #elif HV_SIMD_SSE
|
chris@162
|
32 static void sPhasor_updatePhase(SignalPhasor *o, hv_uint32_t p) {
|
chris@162
|
33 o->phase = _mm_set_epi32(3*o->step.s+p, 2*o->step.s+p, o->step.s+p, p);
|
chris@162
|
34 #elif HV_SIMD_NEON
|
chris@162
|
35 static void sPhasor_updatePhase(SignalPhasor *o, hv_uint32_t p) {
|
chris@162
|
36 o->phase = (uint32x4_t) {p, o->step.s+p, 2*o->step.s+p, 3*o->step.s+p};
|
chris@162
|
37 #else // HV_SIMD_NONE
|
chris@162
|
38 static void sPhasor_updatePhase(SignalPhasor *o, hv_uint32_t p) {
|
chris@162
|
39 o->phase = p;
|
chris@162
|
40 #endif
|
chris@162
|
41 }
|
chris@162
|
42
|
chris@162
|
43 static void sPhasor_updateFrequency(SignalPhasor *o, float f, double r) {
|
chris@162
|
44 #if HV_SIMD_AVX
|
chris@162
|
45 o->step.f2sc = (float) (f/r);
|
chris@162
|
46 o->inc = _mm256_set1_ps((float) (8.0f*f/r));
|
chris@162
|
47 sPhasor_updatePhase(o, o->phase[0]);
|
chris@162
|
48 #elif HV_SIMD_SSE
|
chris@162
|
49 o->step.s = (hv_int32_t) (f*(4294967296.0/r));
|
chris@162
|
50 o->inc = _mm_set1_epi32(4*o->step.s);
|
chris@162
|
51 sPhasor_updatePhase(o, (hv_uint32_t) (o->phase[0] & 0xFFFFFFFFL));
|
chris@162
|
52 #elif HV_SIMD_NEON
|
chris@162
|
53 o->step.s = (hv_int32_t) (f*(4294967296.0/r));
|
chris@162
|
54 o->inc = vdupq_n_s32(4*o->step.s);
|
chris@162
|
55 sPhasor_updatePhase(o, vgetq_lane_u32(o->phase, 0));
|
chris@162
|
56 #else // HV_SIMD_NONE
|
chris@162
|
57 o->step.s = (hv_int32_t) (f*(4294967296.0/r));
|
chris@162
|
58 o->inc = o->step.s;
|
chris@162
|
59 // no need to update phase
|
chris@162
|
60 #endif
|
chris@162
|
61 }
|
chris@162
|
62
|
chris@162
|
63 hv_size_t sPhasor_init(SignalPhasor *o, double samplerate) {
|
chris@162
|
64 #if HV_SIMD_AVX
|
chris@162
|
65 o->phase = _mm256_set1_ps(1.0f);
|
chris@162
|
66 o->inc = _mm256_setzero_ps();
|
chris@162
|
67 o->step.f2sc = (float) (1.0/samplerate);
|
chris@162
|
68 #elif HV_SIMD_SSE
|
chris@162
|
69 o->phase = _mm_setzero_si128();
|
chris@162
|
70 o->inc = _mm_setzero_si128();
|
chris@162
|
71 o->step.f2sc = (float) (4294967296.0/samplerate);
|
chris@162
|
72 #elif HV_SIMD_NEON
|
chris@162
|
73 o->phase = vdupq_n_u32(0);
|
chris@162
|
74 o->inc = vdupq_n_s32(0);
|
chris@162
|
75 o->step.f2sc = (float) (4294967296.0/samplerate);
|
chris@162
|
76 #else // HV_SIMD_NONE
|
chris@162
|
77 o->phase = 0;
|
chris@162
|
78 o->inc = 0;
|
chris@162
|
79 o->step.f2sc = (float) (4294967296.0/samplerate);
|
chris@162
|
80 #endif
|
chris@162
|
81 return 0;
|
chris@162
|
82 }
|
chris@162
|
83
|
chris@162
|
84 void sPhasor_onMessage(HvBase *_c, SignalPhasor *o, int letIn, const HvMessage *m) {
|
chris@162
|
85 if (letIn == 1) {
|
chris@162
|
86 if (msg_isFloat(m,0)) {
|
chris@162
|
87 float phase = msg_getFloat(m,0);
|
chris@162
|
88 while (phase < 0.0f) phase += 1.0f; // wrap phase to [0,1]
|
chris@162
|
89 while (phase > 1.0f) phase -= 1.0f;
|
chris@162
|
90 #if HV_SIMD_AVX
|
chris@162
|
91 sPhasor_updatePhase(o, phase);
|
chris@162
|
92 #else // HV_SIMD_SSE || HV_SIMD_NEON || HV_SIMD_NONE
|
chris@162
|
93 sPhasor_updatePhase(o, (hv_int32_t) (phase * 4294967296.0));
|
chris@162
|
94 #endif
|
chris@162
|
95 }
|
chris@162
|
96 }
|
chris@162
|
97 }
|
chris@162
|
98
|
chris@162
|
99 hv_size_t sPhasor_k_init(SignalPhasor *o, float frequency, double samplerate) {
|
chris@162
|
100 sPhasor_updateFrequency(o, frequency, samplerate);
|
chris@162
|
101 sPhasor_updatePhase(o, 0);
|
chris@162
|
102 return 0;
|
chris@162
|
103 }
|
chris@162
|
104
|
chris@162
|
105 void sPhasor_k_onMessage(HvBase *_c, SignalPhasor *o, int letIn, const HvMessage *m) {
|
chris@162
|
106 if (msg_isFloat(m,0)) {
|
chris@162
|
107 switch (letIn) {
|
chris@162
|
108 case 0: sPhasor_updateFrequency(o, msg_getFloat(m,0), ctx_getSampleRate(_c)); break;
|
chris@162
|
109 case 1: {
|
chris@162
|
110 float phase = msg_getFloat(m,0);
|
chris@162
|
111 while (phase < 0.0f) phase += 1.0f; // wrap phase to [0,1]
|
chris@162
|
112 while (phase > 1.0f) phase -= 1.0f;
|
chris@162
|
113 #if HV_SIMD_AVX
|
chris@162
|
114 sPhasor_updatePhase(o, phase);
|
chris@162
|
115 #else // HV_SIMD_SSE || HV_SIMD_NEON || HV_SIMD_NONE
|
chris@162
|
116 sPhasor_updatePhase(o, (hv_uint32_t) (phase * 4294967296.0));
|
chris@162
|
117 #endif
|
chris@162
|
118 break;
|
chris@162
|
119 }
|
chris@162
|
120 default: break;
|
chris@162
|
121 }
|
chris@162
|
122 }
|
chris@162
|
123 }
|