comparison projects/heavy/samphold/SignalPhasor.c @ 160:5bcf04234f80 heavy-updated

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