Skip to content

Commit

Permalink
analogue channels
Browse files Browse the repository at this point in the history
  • Loading branch information
vlazzarini committed Dec 19, 2017
1 parent 3b9a65b commit 643e3c2
Showing 1 changed file with 54 additions and 17 deletions.
71 changes: 54 additions & 17 deletions Bela/BelaCsound.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,49 @@
/*******************************************************************************/
/* Bela Csound Rendering functions */
/* */
/* */
/*******************************************************************************/

#include <Bela.h>
#include <csound/csound.hpp>
#include <vector>
#include <sstream>

struct csChan {
std::vector<MYFLT> data;
std::stringstream name;
};

struct csData{
Csound *csound;
int blocksize;
int res;
int count;
csChan channel[8];
};

csData* gCsData;
csData* gCsData;

bool setup(BelaContext *context, void *userData)
{
Csound *csound = new Csound();
const char *csdfile = "my.csd";
const char *csdfile = "my.csd"; /* CSD name */
int numArgs = 6;
char *args[] = { "csound", csdfile, "-iadc", "-odac","-+rtaudio=null", "--realtime",
"--daemon"};
char *args[] = { "csound", csdfile, "-iadc", "-odac","-+rtaudio=null",
"--realtime", "--daemon"};

gCsData = new csData;
csound->SetHostImplementedAudioIO(1,32);
gCsData->res = csound->Compile(numArgs, args);
gCsData->csound = csound;
gCsData->blocksize = csound->GetOutputBufferSize();
gCsData->count = 0;

/* set up the channels */
for(int i; i < context->analogInChannels; i++) {
gCsData.channel[i].data.resize(context->analogFrames);
gCsData.channel[i].name << "analogue" << i;
}

if(gCsData->res != 0) return false;
else return true;
}
Expand All @@ -37,17 +52,22 @@ void render(BelaContext *context, void *Data)
{
csData *userData = gCsData;
if(gCsData->res == 0) {
int n,i,frame,blocksize, res;
int n,i,k,count, frmcount,blocksize,res;
MYFLT scal = userData->csound->Get0dBFS();
MYFLT* audioIn = userData->csound->GetInputBuffer();
MYFLT* audioOut = userData->csound->GetOutputBuffer();
int nchnls = userData->csound->GetNchnls();
int chns = nchnls;
frame = userData->count;
int an_chns = context->analogInChannels;
CsChan *channel = userData->channel;
float frm = 0, incr = ((float) context->analogFrames)/context->audioFrames;
int an_chans = context->analogInChannels;
count = userData->count;
blocksize = userData->blocksize;


/* this is called when Csound is not running */
if(frame < 0) {
if(count < 0) {
for(n = 0; n < context->audioFrames; n++){
for(i = 0; i < context->audioOutChannels; i++){
audioWrite(context,n,i,0);
Expand All @@ -58,23 +78,40 @@ void render(BelaContext *context, void *Data)

if(chns > context->audioOutChannels)
chns = context->audioOutChannels;

/* this is where Csound is called */
for(n = 0; n < context->audioFrames; n++){
if(frame == blocksize) {
if((res = userData->csound->PerformBuffer()) == 0) frame = 0;
for(n = 0; n < context->audioFrames; n++, frm+=incr, count+=nchnls){
if(count == blocksize) {
/* set the channels */
for(i = 0; i < an_chns; i++) {
csound->SetChannel(channel[i].name.str().c_str(),
&(channel[i].data[0]));
}
if((res = userData->csound->PerformBuffer()) == 0) count = 0;
else {
frame = -1;
count = -1;
break;
}
}
/* read/write audio data */
for(i = 0; i < chns; i++){
audioIn[frame+i] = audioRead(context,n,i);
audioWrite(context,n,i,audioOut[frame+i]/scal);
audioIn[count+i] = audioRead(context,n,i);
audioWrite(context,n,i,audioOut[count+i]/scal);
}

/* read analogue data
analogue frame pos gets incremented according to the
ratio analogFrames/audioFrames.
*/
frmcount = count/nchnls;
for(i = 0; i < an_chns; i++) {
k = (int) frm;
channel[i].data[frmcount] = analogRead(context,k,i);
}
frame += nchnls;

}
gCsData->res = res;
userData->count = frame;
userData->count = count;
}
}

Expand Down

0 comments on commit 643e3c2

Please sign in to comment.