Example #1
0
/* This version of ldmemfile2 allows you to specify a callback procedure
   to process the file's data after it is loaded.  This method ensures that
   your procedure is only called once even if the file is "loaded" multiple
   times by several opcodes.  callback can be NULL.

   Callback signature:     int myfunc(CSOUND* csound, MEMFIL* mfp)
   Callback return value:  OK (0) or NOTOK (-1)
 */
MEMFIL *ldmemfile2withCB(CSOUND *csound, const char *filnam, int csFileType,
                         int (*callback)(CSOUND*, MEMFIL*))
{                               /* read an entire file into memory and log it */
    MEMFIL  *mfp, *last = NULL; /* share the file with all subsequent requests*/
    char    *allocp;            /* if not fullpath, look in current directory,*/
    int32    len;                /*   then SADIR (if defined).                 */
    char    *pathnam;           /* Used by adsyn, pvoc, and lpread            */

    mfp = csound->memfiles;
    while (mfp != NULL) {                               /* Checking chain */
      if (strcmp(mfp->filename, filnam) == 0)           /*   if match     */
        return mfp;                                     /*   we have it   */
      last = mfp;
      mfp = mfp->next;
    }
    /* Add new file description */
    mfp = (MEMFIL*) csound->Calloc(csound, sizeof(MEMFIL));
    if (last != NULL)
      last->next = mfp;
    else
      csound->memfiles = mfp;
    mfp->next = NULL;
    strncpy(mfp->filename, filnam, 256);

    pathnam = csoundFindInputFile(csound, filnam, "SADIR");
    if (UNLIKELY(pathnam == NULL)) {
      csoundMessage(csound, Str("cannot load %s\n"), filnam);
      delete_memfile(csound, filnam);
      return NULL;
    }
    if (UNLIKELY(Load_File_(csound, pathnam, &allocp, &len, csFileType) != 0)) {
      /* loadfile */
      csoundMessage(csound, Str("cannot load %s, or SADIR undefined\n"),
                            pathnam);
      csound->Free(csound, pathnam);
      delete_memfile(csound, filnam);
      return NULL;
    }
    /* init the struct */
    mfp->beginp = allocp;
    mfp->endp = allocp + len;
    mfp->length = len;
    if (callback != NULL) {
      if (callback(csound, mfp) != OK) {
        csoundMessage(csound, Str("error processing file %s\n"), filnam);
        csound->Free(csound, pathnam);
        delete_memfile(csound, filnam);
        return NULL;
      }
    }
    csoundMessage(csound, Str("file %s (%ld bytes) loaded into memory\n"),
                  pathnam, (long) len);
    csound->Free(csound, pathnam);
    return mfp;                                          /* rtn new slotadr */
}
Example #2
0
static CS_NOINLINE void fdchprint(CSOUND *csound, INSDS *ip)
{
    FDCH *curchp;
    char *name = csound->engineState.instrtxtp[ip->insno]->insname;

    if (name)
      csoundMessage(csound, Str("fdlist for instr %s [%d] (%p):"),
                      name, ip->insno, ip);
    else
      csoundMessage(csound, Str("fdlist for instr %d (%p):"), ip->insno, ip);
    /* chain through fdlocs */
    for (curchp = ip->fdchp; curchp != NULL; curchp = curchp->nxtchp)
      csoundMessage(csound, Str("  fd %p in %p"), curchp->fd, curchp);
    csoundMessage(csound, "\n");
}
Example #3
0
static CS_NOINLINE void auxchprint(CSOUND *csound, INSDS *ip)
{
    AUXCH *curchp;
    char *name = csound->engineState.instrtxtp[ip->insno]->insname;

    if (name)
      csoundMessage(csound, Str("auxlist for instr %s [%d] (%p):\n"),
                      name, ip->insno, ip);
    else
      csoundMessage(csound, Str("auxlist for instr %d (%p):\n"),
                      ip->insno, ip);
    /* chain through auxlocs */
    for (curchp = ip->auxchp; curchp != NULL; curchp = curchp->nxtchp)
      csoundMessage(csound,
                      Str("\tauxch at %p: size %ld, auxp %p, endp %p\n"),
                      curchp, curchp->size, curchp->auxp, curchp->endp);
}
Example #4
0
static void checkOptions(CSOUND *csound)
{
    const char  *csrcname;
    const char  *home_dir;
    FILE        *csrc = NULL;
    void        *fd = NULL;
    char *s;
    /* IV - Feb 17 2005 */
    csrcname = csoundGetEnv(csound, "CSOUND6RC");
    if (csrcname != NULL && csrcname[0] != '\0') {
      fd = csound->FileOpen2(csound, &csrc, CSFILE_STD, csrcname, "r", NULL,
                             CSFTYPE_OPTIONS, 0);
      if (fd == NULL)
        csoundMessage(csound, Str("WARNING: cannot open csound6rc file %s\n"),
                      csrcname);
      else
        csound->Message(csound, Str("Reading options from $CSOUND6RC: %s \n"),
                        csrcname);
    }
    if (fd == NULL && ((home_dir = csoundGetEnv(csound, "HOME")) != NULL &&
                       home_dir[0] != '\0')) {
      s = csoundConcatenatePaths(csound, home_dir, ".csound6rc");
      fd = csound->FileOpen2(csound, &csrc, CSFILE_STD, s, "r", NULL,
                             CSFTYPE_OPTIONS, 0);
      if (fd != NULL)
        csound->Message(csound, Str("Reading options from $HOME/.csound6rc\n"));
      csound->Free(csound, s);
    }
    /* read global .csound6rc file (if exists) */
    if (fd != NULL) {
      readOptions(csound, csrc, 0);
      csound->FileClose(csound, fd);
    }
    /* check for .csound6rc in current directory */
    fd = csound->FileOpen2(csound, &csrc, CSFILE_STD, ".csound6rc", "r", NULL,
                           CSFTYPE_OPTIONS, 0);
    if (fd != NULL) {
      readOptions(csound, csrc, 0);
      csound->Message(csound,
                      Str("Reading options from local directory .csound6rc \n"));
      csound->FileClose(csound, fd);
    }
}
Example #5
0
int CsoundPerformanceThread::Perform()
{
    int retval = 0;
    do {
      while (firstMessage) {
        csoundLockMutex(queueLock);
        do {
          CsoundPerformanceThreadMessage *msg;
          // get oldest message
          msg = (CsoundPerformanceThreadMessage*) firstMessage;
          if (!msg)
            break;
          // unlink from FIFO
          firstMessage = msg->nxt;
          if (!msg->nxt)
            lastMessage = (CsoundPerformanceThreadMessage*) 0;
          // process and destroy message
          retval = msg->run();
          delete msg; // TODO: This should be moved out of the Perform function
        } while (!retval);
        if (paused)
          csoundWaitThreadLock(pauseLock, (size_t) 0);
        // mark queue as empty
        csoundNotifyThreadLock(flushLock);
        csoundUnlockMutex(queueLock);
        // if error or end of score, return now
        if (retval)
          goto endOfPerf;
        // if paused, wait until a new message is received, then loop back
        if (!paused)
          break;
        // VL: if this is paused, then it will double lock.
        csoundWaitThreadLockNoTimeout(pauseLock);
        csoundNotifyThreadLock(pauseLock);
      }
      if(processcallback != NULL)
           processcallback(cdata);
      retval = csoundPerformKsmps(csound);
      if (recordData.running) {
          MYFLT *spout = csoundGetSpout(csound);
          int len = csoundGetKsmps(csound) * csoundGetNchnls(csound);
          if (csoundGet0dBFS(csound) != 1.0) {
              MYFLT zdbfs = csoundGet0dBFS(csound);
              MYFLT *modspout = spout;
              for (int i = 0; i < len; i++) {
                  *modspout /= zdbfs;
                  modspout++;
              }
          }
          int written = csoundWriteCircularBuffer(NULL, recordData.cbuf, spout, len);
          if (written != len) {
              csoundMessage(csound, "perfThread record buffer overrun.");
          }
      }
      pthread_cond_signal(&recordData.condvar); // Needs to be outside the if for the case where stop record was requested
    } while (!retval);
 endOfPerf:
    status = retval;
    csoundCleanup(csound);
    // delete any pending messages
    csoundLockMutex(queueLock);
    {
      CsoundPerformanceThreadMessage *msg;
      msg = (CsoundPerformanceThreadMessage*) firstMessage;
      firstMessage = (CsoundPerformanceThreadMessage*) 0;
      lastMessage = (CsoundPerformanceThreadMessage*) 0;
      while (msg) {
        CsoundPerformanceThreadMessage *nxt = msg->nxt;
        delete msg;
        msg = nxt;
      }
    }
    csoundNotifyThreadLock(flushLock);
    csoundUnlockMutex(queueLock);
    running = 1;
    return retval;
}
Example #6
0
    CsPerfThreadMsg_Record(CsoundPerformanceThread *pt,
                           std::string filename,
                           int samplebits = 16,
                           int numbufs = 4)
    : CsoundPerformanceThreadMessage(pt)
    {
        this->filename = filename;
        CsoundPerformanceThreadMessage::lockRecord();
        recordData_t *recordData = CsoundPerformanceThreadMessage::getRecordData();
        if (recordData->running) {
            CsoundPerformanceThreadMessage::unlockRecord();
            return;
        }
        CSOUND * csound = pt_->GetCsound();
        if (!csound) {
            return;
        }
        int bufsize = csoundGetOutputBufferSize(csound)
                * csoundGetNchnls(csound) * numbufs;
        recordData->cbuf = csoundCreateCircularBuffer(csound,
                                                 bufsize,
                                                 sizeof(MYFLT));

        if (!recordData->cbuf) {
          csoundMessage(csound, "Could create recording buffer.");
          return;
        }

        SF_INFO sf_info;
        sf_info.samplerate = csoundGetSr(csound);
        sf_info.channels = csoundGetNchnls(csound);
        switch (samplebits) {
        case 32:
            sf_info.format = SF_FORMAT_FLOAT;
            break;
        case 24:
            sf_info.format = SF_FORMAT_PCM_24;
            break;
        case 16:
        default:
            sf_info.format = SF_FORMAT_PCM_16;
            break;
        }

        sf_info.format |= SF_FORMAT_WAV;

        recordData->sfile = (void *) sf_open(filename.c_str(),
                                                 SFM_WRITE,
                                                 &sf_info);
        if (!recordData->sfile) {
          csoundMessage(csound, "Could not open file for recording.");
          csoundDestroyCircularBuffer(csound, recordData->cbuf);
          return;
        }
        sf_command((SNDFILE *) recordData->sfile, SFC_SET_CLIPPING,
                   NULL, SF_TRUE);

        recordData->running = true;
        recordData->thread = csoundCreateThread(recordThread_, (void*) recordData);


        CsoundPerformanceThreadMessage::unlockRecord();
    }