View Single Post
Old 07-23-2009, 02:24 AM   #9
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 317
Default sample accuracy

another two snippets or examples, related to sample accuracy. you might need to tweak these to make them work in your own code..

if you want somethig to happen every x samples, for timing or sync, you can do something like this:

Code:
desc:sample accurate timing
@block
  size = beats * srate / (tempo/60);
  // transport change
  play_state != prevstate ? (
    play_state&1 ? countdown=0;//speed;
    prevstate = play_state;
  );
  // timing
  offset = 0;
  block = samplesblock;
  while(
    countdown>=block ? (
      countdown-=block;
      block=0;
    ) : (
      offset += countdown;
      block-=countdown;
      // ...do stuff here...
      //  ...or set some flags/variables...
      // ...offset = offset within current block (for midi out, etc)
      countdown=size;
    );
    block>0;
  );
and, one for handling midi events.. only one event per sample/offset here, but if you need more (notes?), you can stuff the _number_ of events per sample into the EVENTS buffer, and have another buffer where the actual events or data is stored, or have a 2d buffer and some indexing (offset*max_events_per_offset):

Code:
desc:sample accurate midi events
slider1: 60 <0,127,1> midi cc num
@init
  EVENTS = $x10000;
  i127 = 1/127;
  i256 = 1/256;
@slider
  midicc = slider1;
@block
  memset(EVENTS,-1,samplesblock); // ???
  while(
    midirecv(ofs,msg1,msg23) ? (
      msg = msg1 & 240;
      msg == (11*16) ? (
        cc = msg23 & 127;
        cc==midicc ? (
          val = (msg23*i256) & 127;
          EVENTS[ofs] = val*i127; // -> 0..1
        );
      );
      //midisend(ofs,msg1,msg23);
    );
  );
  offset = 0;
@sample
  EVENTS[offset]>=0 ? (
    our_value = EVENTS[offset];
  );
  EVENTS[offset] = -1;
  offset += 1;
  // ...use our_value for something
(let's hope my cut'n'paste didn't introduce too many errors or incompabilities)

- ccernn
__________________
later..
cern.th.skei is offline   Reply With Quote