Skip to content

Commit

Permalink
digiIn opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
vlazzarini committed Dec 21, 2017
1 parent 664a638 commit ede6f9f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 5 deletions.
61 changes: 58 additions & 3 deletions Bela/BelaCsound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <Bela.h>
#include <Midi.h>
#include <csound/csound.hpp>
#include <csound/plugin.h>
#include <vector>
#include <sstream>

Expand All @@ -35,7 +36,48 @@ static int ReadMidiData(CSOUND *csound, void *userData, unsigned char *mbuf,
static int OpenMidiOutDevice(CSOUND *csound, void **userData, const char *dev);
static int CloseMidiOutDevice(CSOUND *csound, void *userData);
static int WriteMidiData(CSOUND *csound, void *userData, const unsigned char *mbuf,
int nbytes);
int nbytes);

/** DigiIn opcode
ksig digiInBela ipin
asig digiInBela ipin
*/
struct DigiIn : csnd::Plugin<1, 1> {
int pin;
int fcount;
int frms;
BelaContext *context;

int init() {
pin = (int) inargs[0];
if(pin < 0 ) pin = 0;
if(pin > 15) pin = 15;
context = (BelaContext *) csound->GetHostData();
pinMode(gContext,pin,0);
fcount = 0;
frms = context->digitalFrames;
return OK;
}

int kperf() {
outargs[0] = digitalRead(context,fcount,pin);
if(fcount < frms) fcount+=nsmps;
else fcount = 0;
return OK;
}

int aperf() {
csnd::AudioSig out(this, outargs(0));
int cnt = fcount;
for (auto &s : out) {
s = digitalRead(context,fcount,pin);
if(cnt < frms) cnt++;
else cnt = 0;
}
fcount = cnt;
}
};


struct CsChan {
std::vector<MYFLT> data;
Expand Down Expand Up @@ -74,9 +116,10 @@ bool setup(BelaContext *context, void *Data)
return false;
}

/* setup Csound */
/* set up Csound */
csound = new Csound();
gCsData.csound = csound;
csound->SetHostData((void *) context);
csound->SetHostImplementedAudioIO(1,0);
csound->SetHostImplementedMIDIIO(1);
csound->SetExternalMidiInOpenCallback(OpenMidiInDevice);
Expand All @@ -85,12 +128,24 @@ bool setup(BelaContext *context, void *Data)
csound->SetExternalMidiOutOpenCallback(OpenMidiOutDevice);
csound->SetExternalMidiWriteCallback(WriteMidiData);
csound->SetExternalMidiOutCloseCallback(CloseMidiOutDevice);
/* set up digi opcodes */
if(csnd::plugin<DigiIn>((csnd::Csound *) csound->GetCsound(), "digiInBela" , "k",
"i", csnd::thread::ik) != 0)
printf("Warning: could not add digiInBela k-rate opcode\n");


if(csnd::plugin<DigiIn>((csnd::Csound *) csound->GetCsound(), "digiInBela" , "a",
"i", csnd::thread::ia) != 0)
printf("Warning: could not add digiInBela a-rate opcode\n");


if((gCsData.res = csound->Compile(numArgs, args)) != 0) {
printf("Error: Csound could not compile CSD file.\n");
return false;
}
gCsData.blocksize = csound->GetKsmps()*csound->GetNchnls();
gCsData.count = 0;


/* set up the channels */
for(int i=0; i < ANCHNS; i++) {
Expand Down Expand Up @@ -130,7 +185,7 @@ void render(BelaContext *context, void *Data)
csound->SetChannel(channel[i].name.str().c_str(),
&(channel[i].data[0]));
csound->GetAudioChannel(ochannel[i].name.str().c_str(),
&(ochannel[i].data[0]));
&(ochannel[i].data[0]));
}
/* run csound */
if((res = csound->PerformKsmps()) == 0) count = 0;
Expand Down
3 changes: 2 additions & 1 deletion Top/csound.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,12 @@ static const CSOUND cenviron_ = {
fterror,
csoundGetA4,
csoundAuxAllocAsync,
csoundGetHostData,
{
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL
NULL, NULL, NULL, NULL, NULL, NULL
},
/* ------- private data (not to be used by hosts or externals) ------- */
/* callback function pointers */
Expand Down
3 changes: 2 additions & 1 deletion include/csoundCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1364,11 +1364,12 @@ typedef struct _message_queue_t_ {
MYFLT (*GetA4)(CSOUND *csound);
int (*AuxAllocAsync)(CSOUND *, size_t, AUXCH *,
AUXASYNC *, aux_cb, void *);
void *(*GetHostData)(CSOUND *);
/**@}*/
/** @name Placeholders
To allow the API to grow while maintining backward binary compatibility. */
/**@{ */
SUBR dummyfn_2[38];
SUBR dummyfn_2[37];
/**@}*/
#ifdef __BUILDING_LIBCSOUND
/* ------- private data (not to be used by hosts or externals) ------- */
Expand Down
6 changes: 6 additions & 0 deletions include/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class Csound : CSOUND {
}

public:
/** Host Data
*/
void *host_data() {
return GetHostData(this);
}

/** init-time error message
*/
int init_error(const std::string &s) {
Expand Down

0 comments on commit ede6f9f

Please sign in to comment.