zynaddsubfx

ZynAddSubFX open source synthesizer
Log | Files | Refs | Submodules | LICENSE

commit 5392de0a2979968c217d79c0eaec84e0a690803c
parent cbc1092e05786a20ba296ef87441c6fc9520064a
Author: fundamental <[email protected]>
Date:   Wed, 16 Dec 2009 10:57:14 -0500

Nio: Enhanced OutMgr's requests and cleaned OssEng

Diffstat:
Msrc/Nio/AudioOut.cpp | 8+++++---
Msrc/Nio/NulEngine.cpp | 3+--
Msrc/Nio/OssEngine.cpp | 86++++++++++++++++++++-----------------------------------------------------------
Msrc/Nio/OssEngine.h | 10+++-------
Msrc/Nio/OutMgr.cpp | 5++---
Msrc/Nio/OutMgr.h | 3++-
6 files changed, 34 insertions(+), 81 deletions(-)

diff --git a/src/Nio/AudioOut.cpp b/src/Nio/AudioOut.cpp @@ -43,14 +43,15 @@ void AudioOut::out(const Stereo<Sample> smps) const Stereo<Sample> AudioOut::getNext() { + const int BUFF_SIZE = 4; Stereo<Sample> ans; pthread_mutex_lock(&outBuf_mutex); bool isEmpty = outBuf.empty(); pthread_mutex_unlock(&outBuf_mutex); if(isEmpty)//fetch samples if possible { - if(manager->getRunning()<4) - manager->requestSamples(); + if(manager->getRunning()<BUFF_SIZE) + manager->requestSamples(BUFF_SIZE-manager->getRunning()); if(true) cout << "-----------------Starvation------------------"<< endl; return current; @@ -61,7 +62,8 @@ const Stereo<Sample> AudioOut::getNext() ans = outBuf.front(); outBuf.pop(); if(outBuf.size()+manager->getRunning()<4) - manager->requestSamples(); + manager->requestSamples(BUFF_SIZE - (outBuf.size() + + manager->getRunning())); if(false) cout << "AudioOut "<< outBuf.size()<< '+' << manager->getRunning() << endl; pthread_mutex_unlock(&outBuf_mutex); diff --git a/src/Nio/NulEngine.cpp b/src/Nio/NulEngine.cpp @@ -44,7 +44,6 @@ void *NulEngine::_AudioThread(void *arg) void *NulEngine::AudioThread() { - //set_realtime(); while (!threadStop()) { const Stereo<Sample> smps = getNext(); @@ -69,7 +68,7 @@ void NulEngine::dummyOut() //This will add latency... usleep(remaining - 10000); if(remaining < 0) - ;//cerr << "WARNING - too late" << endl; + cerr << "WARNING - too late" << endl; } playing_until.tv_usec += SOUND_BUFFER_SIZE * 1000000 / SAMPLE_RATE; if(remaining < 0) diff --git a/src/Nio/OssEngine.cpp b/src/Nio/OssEngine.cpp @@ -38,58 +38,52 @@ using namespace std; OssEngine::OssEngine(OutMgr *out) :AudioOut(out) { - current = Stereo<Sample>(Sample(SOUND_BUFFER_SIZE), - Sample(SOUND_BUFFER_SIZE)); - int i; - int snd_bitsize = 16; snd_fragment = 0x00080009; //fragment size (?) snd_stereo = 1; //stereo snd_format = AFMT_S16_LE; snd_samplerate = SAMPLE_RATE; - playing_until.tv_sec = 0; - playing_until.tv_usec = 0; - smps = new short int[SOUND_BUFFER_SIZE * 2]; - for(i = 0; i < SOUND_BUFFER_SIZE * 2; i++) - smps[i] = 0; + smps = new short[SOUND_BUFFER_SIZE * 2]; + memset(smps, 0, sizeof(short) * SOUND_BUFFER_SIZE * 2); + + cerr << "hello?" << endl; +} + +OssEngine::~OssEngine() +{ + close(snd_handle); + delete [] smps; +} +bool OssEngine::openAudio() +{ + int snd_bitsize = 16; snd_handle = open(config.cfg.LinuxOSSWaveOutDev, O_WRONLY, 0); cerr << config.cfg.LinuxOSSWaveOutDev << endl; if(snd_handle == -1) { - cerr << "ERROR - I can't open the "; - cerr << config.cfg.LinuxOSSWaveOutDev << '.' << endl; - return; + cerr << "ERROR - I can't open the " + << config.cfg.LinuxOSSWaveOutDev << '.' << endl; + return false; } ioctl(snd_handle, SNDCTL_DSP_RESET, NULL); - ioctl(snd_handle, SNDCTL_DSP_SETFMT, &snd_format); ioctl(snd_handle, SNDCTL_DSP_STEREO, &snd_stereo); ioctl(snd_handle, SNDCTL_DSP_SPEED, &snd_samplerate); ioctl(snd_handle, SNDCTL_DSP_SAMPLESIZE, &snd_bitsize); ioctl(snd_handle, SNDCTL_DSP_SETFRAGMENT, &snd_fragment); - cerr << "hello?" << endl; -} - -OssEngine::~OssEngine() -{ - close(snd_handle); - delete [] smps; + return true; } -//bool OssEngine::openAudio() -//{ -// return true; -//} - bool OssEngine::Start() { - int chk; + if(!openAudio()) + return false; pthread_attr_t attr; threadStop = false; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_create(&pThread, &attr, _AudioThread, this); - cerr << "Starting?"; + cout << "Starting Oss"; enabled = true; return true; @@ -101,11 +95,6 @@ void OssEngine::Stop() enabled = false; } -//void OssEngine::Close() -//{ -// Stop(); -//} - void *OssEngine::_AudioThread(void *arg) { return (static_cast<OssEngine*>(arg))->AudioThread(); @@ -121,9 +110,7 @@ void *OssEngine::AudioThread() set_realtime(); while (!threadStop()) { - //cout << "OssEngine THREAD" << endl; const Stereo<Sample> smps = getNext(); - //cout << smps.l()[3] << endl; OSSout(smps.l().c_buf(),smps.r().c_buf()); } return NULL; @@ -135,37 +122,6 @@ void *OssEngine::AudioThread() */ void OssEngine::OSSout(const REALTYPE *smp_left, const REALTYPE *smp_right) { - if(snd_handle < 0) { //output could not be opened - struct timeval now; - int remaining = 0; - gettimeofday(&now, NULL); - if((playing_until.tv_usec == 0) && (playing_until.tv_sec == 0)) { - playing_until.tv_usec = now.tv_usec; - playing_until.tv_sec = now.tv_sec; - } - else { - remaining = (playing_until.tv_usec - now.tv_usec) - + (playing_until.tv_sec - now.tv_sec) * 1000000; - if(remaining > 10000) //Don't sleep() less than 10ms. - //This will add latency... - usleep(remaining - 10000); - if(remaining < 0) - cerr << "WARNING - too late" << endl; - } - playing_until.tv_usec += SOUND_BUFFER_SIZE * 1000000 / SAMPLE_RATE; - if(remaining < 0) - playing_until.tv_usec -= remaining; - playing_until.tv_sec += playing_until.tv_usec / 1000000; - playing_until.tv_usec %= 1000000; - return; - } - outOut(smp_left,smp_right); - return; -} - -void OssEngine::outOut(const REALTYPE *smp_left, const REALTYPE *smp_right) -{ - //cout << "Oi" << smp_left[2] << smp_right[2] << endl; int i; REALTYPE l, r; for(i = 0; i < SOUND_BUFFER_SIZE; i++) { diff --git a/src/Nio/OssEngine.h b/src/Nio/OssEngine.h @@ -33,9 +33,6 @@ class OssEngine: public AudioOut OssEngine(OutMgr *out); ~OssEngine(); - //the out is [-1.0 .. 1.0] - /* smp_left[] and smp_right[] has the size of SOUND_BUFFER_SIZE */ - bool Start(); void Stop(); @@ -44,19 +41,18 @@ class OssEngine: public AudioOut static void *_AudioThread(void *arg); private: + /**Open the audio device + * @return true for success*/ + bool openAudio(); void OSSout(const REALTYPE *smp_left, const REALTYPE *smp_right); - void outOut(const REALTYPE *smp_left, const REALTYPE *smp_right); int snd_handle; int snd_fragment; int snd_stereo; int snd_format; int snd_samplerate; - struct timeval playing_until; short int *smps; //Samples to be sent to soundcard - - }; #endif diff --git a/src/Nio/OutMgr.cpp b/src/Nio/OutMgr.cpp @@ -74,7 +74,6 @@ void *OutMgr::outputThread() running=true; init=true; bool doWait=false; - int lRequests; while(running) { --numRequests; @@ -175,9 +174,9 @@ int OutMgr::getRunning() return numRequests(); } -int OutMgr::requestSamples() +int OutMgr::requestSamples(unsigned int n) { - ++numRequests; + numRequests = numRequests() + n; pthread_mutex_lock(&processing); pthread_cond_signal(&needsProcess); diff --git a/src/Nio/OutMgr.h b/src/Nio/OutMgr.h @@ -27,8 +27,9 @@ class OutMgr void remove(AudioOut *out); /**Request a new set of samples + * @param n number of requested samples (defaults to 1) * @return -1 for locking issues 0 for valid request*/ - int requestSamples(); + int requestSamples(unsigned int n=1); /**Return the number of building samples*/ int getRunning();