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