annotate scripts/hvresources/render.cpp @ 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 e6558ce09884
rev   line source
chris@160 1 /*
chris@160 2 * render.cpp
chris@160 3 *
chris@160 4 * Template render.cpp file for on-board heavy compiling
chris@160 5 *
chris@160 6 * N.B. this is currently *not* compatible with foleyDesigner source files!
chris@160 7 *
chris@160 8 * Created on: November 5, 2015
chris@160 9 *
chris@160 10 * Christian Heinrichs
chris@160 11 *
chris@160 12 */
chris@160 13
chris@160 14 #include <BeagleRT.h>
chris@160 15 #include <cmath>
chris@160 16 #include "../include/Utilities.h"
chris@160 17 #include "Heavy_bbb.h"
chris@160 18
chris@160 19 // #include "I2c_TouchKey.h"
chris@160 20
chris@160 21 // #include "../include/UdpServer.h"
chris@160 22 // #include "../include/UdpClient.h"
chris@160 23 // #include <iostream>
chris@160 24 // #include <fstream>
chris@160 25 // #include "../include/ReceiveAudioThread.h"
chris@160 26
chris@160 27 // #include "../include/render.h"
chris@160 28 // #include <arm_neon.h>
chris@160 29 // #include <time.h>
chris@160 30 // #include <sndfile.h>
chris@160 31
chris@160 32 // #include "../include/RTAudio.h"
chris@160 33 // #include <rtdk.h>
chris@160 34
chris@160 35
chris@160 36 /*
chris@160 37 * HEAVY CONTEXT & BUFFERS
chris@160 38 */
chris@160 39
chris@160 40 Hv_bbb *gHeavyContext;
chris@160 41 float *gHvInputBuffers = NULL, *gHvOutputBuffers = NULL;
chris@160 42 int gHvInputChannels = 0, gHvOutputChannels = 0;
chris@160 43
chris@160 44 float gInverseSampleRate;
chris@160 45
chris@160 46 /*
chris@160 47 * HEAVY FUNCTIONS
chris@160 48 */
chris@160 49
chris@160 50 void printHook(double timestampSecs, const char *printLabel, const char *msgString, void *userData) {
chris@160 51 printf("Message from Heavy patch: [@ %.3f] %s: %s\n", timestampSecs, printLabel, msgString);
chris@160 52 }
chris@160 53
chris@160 54 static void sendHook(
chris@160 55 double timestamp, // in milliseconds
chris@160 56 const char *receiverName,
chris@160 57 const HvMessage *const m,
chris@160 58 void *userData) {
chris@160 59
chris@160 60 // only react to messages sent to receivers named "hello"
chris@160 61 if (!strncmp(receiverName, "hello", 5)) {
chris@160 62 }
chris@160 63
chris@160 64 }
chris@160 65
chris@160 66 /*
chris@160 67 * RENDER INITIALISATION, LOOP & CLEANUP
chris@160 68 */
chris@160 69
chris@160 70
chris@160 71 // bool initialise_render(int numMatrixChannels, int numAudioChannels,
chris@160 72 // int numMatrixFramesPerPeriod,
chris@160 73 // int numAudioFramesPerPeriod,
chris@160 74 // float matrixSampleRate, float audioSampleRate,
chris@160 75 // void *userData)
chris@160 76 // {
chris@160 77 bool setup(BeagleRTContext *context, void *userData) {
chris@160 78
chris@160 79 /* HEAVY */
chris@160 80
chris@160 81 gHeavyContext = hv_bbb_new(context->audioSampleRate);
chris@160 82
chris@160 83 gHvInputChannels = hv_getNumInputChannels(gHeavyContext);
chris@160 84 gHvOutputChannels = hv_getNumOutputChannels(gHeavyContext);
chris@160 85
chris@160 86 // srand ( time(NULL) );
chris@160 87
chris@160 88 rt_printf("Starting Heavy context with %d input channels and %d output channels\n",
chris@160 89 gHvInputChannels, gHvOutputChannels);
chris@160 90
chris@160 91 if(gHvInputChannels != 0) {
chris@160 92 gHvInputBuffers = (float *)calloc(gHvInputChannels * context->audioFrames,sizeof(float));
chris@160 93 //memset(gHvInputBuffers,0,gHvInputChannels * numAudioFramesPerPeriod * sizeof(float));
chris@160 94 }
chris@160 95 if(gHvOutputChannels != 0) {
chris@160 96 gHvOutputBuffers = (float *)calloc(gHvOutputChannels * context->audioFrames,sizeof(float));
chris@160 97 }
chris@160 98
chris@160 99 gInverseSampleRate = 1.0 / context->audioSampleRate;
chris@160 100
chris@160 101 // Set heavy print hook
chris@160 102 hv_setPrintHook(gHeavyContext, &printHook);
chris@160 103 // Set heavy send hook
chris@160 104 hv_setSendHook(gHeavyContext, sendHook);
chris@160 105
chris@160 106 return true;
chris@160 107 }
chris@160 108
chris@160 109
chris@160 110 // void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
chris@160 111 // uint16_t *matrixIn, uint16_t *matrixOut)
chris@160 112 // {
chris@160 113 void render(BeagleRTContext *context, void *userData)
chris@160 114 {
chris@160 115 // use this for thread management
chris@160 116 // if(gCount == 0) {
chris@160 117 // } else {
chris@160 118 // }
chris@160 119 // gCount++;
chris@160 120
chris@160 121 // De-interleave the data
chris@160 122 if(gHvInputBuffers != NULL) {
chris@160 123 for(int n = 0; n < context->audioFrames; n++) {
chris@160 124 for(int ch = 0; ch < gHvInputChannels; ch++) {
chris@160 125 if(ch >= context->audioChannels+context->analogChannels) {
chris@160 126 // THESE ARE PARAMETER INPUT 'CHANNELS' USED FOR ROUTING
chris@160 127 // 'sensor' outputs from routing channels of dac~ are passed through here
chris@160 128 break;
chris@160 129 } else {
chris@160 130 // If more than 2 ADC inputs are used in the pd patch, route the analog inputs
chris@160 131 // i.e. ADC3->analogIn0 etc. (first two are always audio inputs)
chris@160 132 if(ch >= context->audioChannels) {
chris@160 133 int m = n/2;
chris@160 134 float mIn = context->analogIn[m*context->analogChannels + (ch-context->audioChannels)];
chris@160 135 gHvInputBuffers[ch * context->audioFrames + n] = mIn;
chris@160 136 } else {
chris@160 137 gHvInputBuffers[ch * context->audioFrames + n] = context->audioIn[n * context->audioChannels + ch];
chris@160 138 }
chris@160 139 }
chris@160 140 }
chris@160 141 }
chris@160 142 }
chris@160 143
chris@160 144 // replacement for bang~ object
chris@160 145 //hv_vscheduleMessageForReceiver(gHeavyContext, "bbb_bang", 0.0f, "b");
chris@160 146
chris@160 147 hv_bbb_process_inline(gHeavyContext, gHvInputBuffers, gHvOutputBuffers, context->audioFrames);
chris@160 148
chris@160 149 // Interleave the output data
chris@160 150 if(gHvOutputBuffers != NULL) {
chris@160 151 for(int n = 0; n < context->audioFrames; n++) {
chris@160 152
chris@160 153 for(int ch = 0; ch < gHvOutputChannels; ch++) {
chris@160 154 if(ch >= context->audioChannels+context->analogChannels) {
chris@160 155 // THESE ARE SENSOR OUTPUT 'CHANNELS' USED FOR ROUTING
chris@160 156 // they are the content of the 'sensor output' dac~ channels
chris@160 157 } else {
chris@160 158 if(ch >= context->audioChannels) {
chris@160 159 int m = n/2;
chris@160 160 // float mOut = (float)gHvOutputBuffers[ch*numAudioFrames + n];
chris@160 161 // mOut = constrain(mOut,0.0,1.0);
chris@160 162 context->analogOut[m * context->analogFrames + (ch-context->audioChannels)] = constrain(gHvOutputBuffers[ch*context->audioFrames + n],0.0,1.0);
chris@160 163 } else {
chris@160 164 context->audioOut[n * context->audioChannels + ch] = gHvOutputBuffers[ch * context->audioFrames + n];
chris@160 165 }
chris@160 166 }
chris@160 167 }
chris@160 168 }
chris@160 169 }
chris@160 170
chris@160 171 }
chris@160 172
chris@160 173
chris@160 174 void cleanup(BeagleRTContext *context, void *userData)
chris@160 175 {
chris@160 176
chris@160 177 hv_bbb_free(gHeavyContext);
chris@160 178 if(gHvInputBuffers != NULL)
chris@160 179 free(gHvInputBuffers);
chris@160 180 if(gHvOutputBuffers != NULL)
chris@160 181 free(gHvOutputBuffers);
chris@160 182
chris@160 183 }