Mercurial > hg > beaglert
comparison scripts/hvresources/render.cpp @ 170:e80340fe527a
merge
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 28 Dec 2015 03:19:59 +0100 |
parents | e6558ce09884 |
children | 3b8a28edae41 |
comparison
equal
deleted
inserted
replaced
169:94751ad27fd6 | 170:e80340fe527a |
---|---|
1 /* | |
2 * render.cpp | |
3 * | |
4 * Template render.cpp file for on-board heavy compiling | |
5 * | |
6 * N.B. this is currently *not* compatible with foleyDesigner source files! | |
7 * | |
8 * Created on: November 5, 2015 | |
9 * | |
10 * Christian Heinrichs | |
11 * | |
12 */ | |
13 | |
14 #include <BeagleRT.h> | |
15 #include <cmath> | |
16 #include "../include/Utilities.h" | |
17 #include "Heavy_bbb.h" | |
18 | |
19 /* | |
20 * HEAVY CONTEXT & BUFFERS | |
21 */ | |
22 | |
23 Hv_bbb *gHeavyContext; | |
24 float *gHvInputBuffers = NULL, *gHvOutputBuffers = NULL; | |
25 int gHvInputChannels = 0, gHvOutputChannels = 0; | |
26 | |
27 float gInverseSampleRate; | |
28 | |
29 /* | |
30 * HEAVY FUNCTIONS | |
31 */ | |
32 | |
33 void printHook(double timestampSecs, const char *printLabel, const char *msgString, void *userData) { | |
34 printf("Message from Heavy patch: [@ %.3f] %s: %s\n", timestampSecs, printLabel, msgString); | |
35 } | |
36 | |
37 static void sendHook( | |
38 double timestamp, // in milliseconds | |
39 const char *receiverName, | |
40 const HvMessage *const m, | |
41 void *userData) { | |
42 | |
43 // only react to messages sent to receivers named "hello" | |
44 if (!strncmp(receiverName, "hello", 5)) { | |
45 } | |
46 | |
47 } | |
48 | |
49 /* | |
50 * SETUP, RENDER LOOP & CLEANUP | |
51 */ | |
52 | |
53 bool setup(BeagleRTContext *context, void *userData) { | |
54 | |
55 /* HEAVY */ | |
56 | |
57 gHeavyContext = hv_bbb_new(context->audioSampleRate); | |
58 | |
59 gHvInputChannels = hv_getNumInputChannels(gHeavyContext); | |
60 gHvOutputChannels = hv_getNumOutputChannels(gHeavyContext); | |
61 | |
62 rt_printf("Starting Heavy context with %d input channels and %d output channels\n", | |
63 gHvInputChannels, gHvOutputChannels); | |
64 | |
65 if(gHvInputChannels != 0) { | |
66 gHvInputBuffers = (float *)calloc(gHvInputChannels * context->audioFrames,sizeof(float)); | |
67 } | |
68 if(gHvOutputChannels != 0) { | |
69 gHvOutputBuffers = (float *)calloc(gHvOutputChannels * context->audioFrames,sizeof(float)); | |
70 } | |
71 | |
72 gInverseSampleRate = 1.0 / context->audioSampleRate; | |
73 | |
74 // Set heavy print hook | |
75 hv_setPrintHook(gHeavyContext, &printHook); | |
76 // Set heavy send hook | |
77 hv_setSendHook(gHeavyContext, sendHook); | |
78 | |
79 return true; | |
80 } | |
81 | |
82 | |
83 void render(BeagleRTContext *context, void *userData) | |
84 { | |
85 | |
86 // De-interleave the data | |
87 if(gHvInputBuffers != NULL) { | |
88 for(int n = 0; n < context->audioFrames; n++) { | |
89 for(int ch = 0; ch < gHvInputChannels; ch++) { | |
90 if(ch >= context->audioChannels+context->analogChannels) { | |
91 // THESE ARE PARAMETER INPUT 'CHANNELS' USED FOR ROUTING | |
92 // 'sensor' outputs from routing channels of dac~ are passed through here | |
93 break; | |
94 } else { | |
95 // If more than 2 ADC inputs are used in the pd patch, route the analog inputs | |
96 // i.e. ADC3->analogIn0 etc. (first two are always audio inputs) | |
97 if(ch >= context->audioChannels) { | |
98 int m = n/2; | |
99 float mIn = context->analogIn[m*context->analogChannels + (ch-context->audioChannels)]; | |
100 gHvInputBuffers[ch * context->audioFrames + n] = mIn; | |
101 } else { | |
102 gHvInputBuffers[ch * context->audioFrames + n] = context->audioIn[n * context->audioChannels + ch]; | |
103 } | |
104 } | |
105 } | |
106 } | |
107 } | |
108 | |
109 // replacement for bang~ object | |
110 //hv_vscheduleMessageForReceiver(gHeavyContext, "bbb_bang", 0.0f, "b"); | |
111 | |
112 hv_bbb_process_inline(gHeavyContext, gHvInputBuffers, gHvOutputBuffers, context->audioFrames); | |
113 | |
114 // Interleave the output data | |
115 if(gHvOutputBuffers != NULL) { | |
116 for(int n = 0; n < context->audioFrames; n++) { | |
117 | |
118 for(int ch = 0; ch < gHvOutputChannels; ch++) { | |
119 if(ch >= context->audioChannels+context->analogChannels) { | |
120 // THESE ARE SENSOR OUTPUT 'CHANNELS' USED FOR ROUTING | |
121 // they are the content of the 'sensor output' dac~ channels | |
122 } else { | |
123 if(ch >= context->audioChannels) { | |
124 int m = n/2; | |
125 context->analogOut[m * context->analogFrames + (ch-context->audioChannels)] = constrain(gHvOutputBuffers[ch*context->audioFrames + n],0.0,1.0); | |
126 } else { | |
127 context->audioOut[n * context->audioChannels + ch] = gHvOutputBuffers[ch * context->audioFrames + n]; | |
128 } | |
129 } | |
130 } | |
131 } | |
132 } | |
133 | |
134 } | |
135 | |
136 | |
137 void cleanup(BeagleRTContext *context, void *userData) | |
138 { | |
139 | |
140 hv_bbb_free(gHeavyContext); | |
141 if(gHvInputBuffers != NULL) | |
142 free(gHvInputBuffers); | |
143 if(gHvOutputBuffers != NULL) | |
144 free(gHvOutputBuffers); | |
145 | |
146 } |