void CsoundPerformanceThread::csPerfThread_constructor(CSOUND *csound_) { csound = csound_; firstMessage = (CsoundPerformanceThreadMessage*) 0; lastMessage = (CsoundPerformanceThreadMessage*) 0; queueLock = (void*) 0; pauseLock = (void*) 0; flushLock = (void*) 0; perfThread = (void*) 0; paused = 1; status = CSOUND_MEMORY; cdata = 0; processcallback = 0; running = 0; queueLock = csoundCreateMutex(0); if (!queueLock) return; pauseLock = csoundCreateThreadLock(); if (!pauseLock) return; flushLock = csoundCreateThreadLock(); if (!flushLock) return; try { lastMessage = new CsPerfThreadMsg_Pause(this); } catch (std::bad_alloc&) { return; } firstMessage = lastMessage; perfThread = csoundCreateThread(csoundPerformanceThread_, (void*) this); if (perfThread) status = 0; }
int start(int ) { if (!csound) { ll->printf(1, "skipping %s, csound==NULL\n", __FUNCTION__); return 1; } if (!ThreadID) { PERF_STATUS = CONTINUE; ThreadID = csoundCreateThread(csThread, (void*)this); ll->printf( "INFO(%s:%i) aclient launching performance thread (%p)\n", __FILE__, __LINE__, ThreadID ); return 0; } ll->printf( "INFO(%s:%i) skipping duplicate request to launch a thread\n", __FILE__, __LINE__ ); return 1; }
void CsoundPerformanceThread::csPerfThread_constructor(CSOUND *csound_) { csound = csound_; firstMessage = (CsoundPerformanceThreadMessage*) 0; lastMessage = (CsoundPerformanceThreadMessage*) 0; queueLock = (void*) 0; pauseLock = (void*) 0; flushLock = (void*) 0; recordLock = (void *) 0; perfThread = (void*) 0; paused = 1; status = CSOUND_MEMORY; cdata = 0; processcallback = 0; running = 0; queueLock = csoundCreateMutex(0); if (!queueLock) return; pauseLock = csoundCreateThreadLock(); if (!pauseLock) return; flushLock = csoundCreateThreadLock(); if (!flushLock) return; recordLock = csoundCreateMutex(0); if (!recordLock) return; try { lastMessage = new CsPerfThreadMsg_Pause(this); } catch (std::bad_alloc&) { return; } firstMessage = lastMessage; recordData.cbuf = NULL; recordData.sfile = NULL; recordData.thread = NULL; recordData.running = false; pthread_mutex_init(&recordData.mutex, NULL); pthread_cond_init(&recordData.condvar, NULL); perfThread = csoundCreateThread(csoundPerformanceThread_, (void*) this); if (perfThread) status = 0; }
int main(int arg, char** argv) { void* thread; csoundInitialize(CSOUNDINIT_NO_ATEXIT); CSOUND* csound = csoundCreate(NULL); /* Using SetOption() to configure Csound Note: use only one commandline flag at a time */ csoundSetOption(csound, "-odac"); /* Compile the Csound Orchestra string */ csoundCompileOrc(csound, orc); /* Compile the Csound SCO String */ csoundReadScore(csound, (char*)sco); /* When compiling from strings, this call is necessary * before doing any performing */ csoundStart(csound); /* Create a new thread that will use our performance function and * pass in our CSOUND structure. This call is asynchronous and * will immediately return back here to continue code execution */ thread = csoundCreateThread(&performance_function, (void*)csound); /* Join will wait for the other thread to complete. If we did not * call csoundJoinThread(), after csoundCreateThread() returns we * would immediately move to the next line, csoundStop(). That * would stop Csound without really giving it time to run. */ csoundJoinThread(thread); csoundStop(csound); /* clean up Csound; this is useful if you're going to reuse a Csound * instance */ csoundCleanup(csound); return 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(); }