Skip to content
Snippets Groups Projects
Commit 0797b5b3 authored by Florent Berthaut's avatar Florent Berthaut
Browse files

Fix audio uniforms

parent e77d86d0
No related branches found
No related tags found
No related merge requests found
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
void* audioThreadFunction(void* pvParam) { void* audioThreadFunction(void* pvParam) {
AudioManager *pThis=(AudioManager*)pvParam; AudioManager *pThis=(AudioManager*)pvParam;
pThis->startStream(); pThis->startStream();
return 0; return 0;
} }
...@@ -48,116 +48,116 @@ int AudioManager::getMixSize(){ ...@@ -48,116 +48,116 @@ int AudioManager::getMixSize(){
} }
int AudioManager::rtaudio_callback(void *outbuf, void *inbuf, unsigned int nFrames, int AudioManager::rtaudio_callback(void *outbuf, void *inbuf, unsigned int nFrames,
double streamtime, RtAudioStreamStatus status, void *userdata) { double streamtime, RtAudioStreamStatus status, void *userdata) {
(void)inbuf; (void)inbuf;
float *buf = (float*)outbuf; float *buf = (float*)outbuf;
AudioManager::CallbackData *data = (AudioManager::CallbackData*) userdata; AudioManager::CallbackData *data = (AudioManager::CallbackData*) userdata;
//if we've reached the first buffer again, retrieve values from the ring //if we've reached the first buffer again, retrieve values from the ring
if(data->bufCounter==0) { if(data->bufCounter==0) {
if(ringbuffer_read_space(data->ringBuffer) >= data->bufSize) { if(ringbuffer_read_space(data->ringBuffer) >= data->bufSize) {
//read all the audio values //read all the audio values
ringbuffer_read(data->ringBuffer, data->buffer, data->bufSize); ringbuffer_read(data->ringBuffer, data->buffer, data->bufSize);
//mix the first buffer with the last of the previous //mix the first buffer with the last of the previous
float step = 1.0/float(nFrames); float step = 1.0/float(nFrames);
float cnt = 0.0; float cnt = 0.0;
for(int i=0; i < nFrames; ++i) { for(int i=0; i < nFrames; ++i) {
buf[i*2] = data->buffer[i]*cnt + data->mixBuffer[i]*(1.0-cnt); buf[i*2] = data->buffer[i]*cnt + data->mixBuffer[i]*(1.0-cnt);
buf[i*2+1] = buf[i*2]; buf[i*2+1] = buf[i*2];
cnt+=step; cnt+=step;
} }
//save the last buffer for crossfading //save the last buffer for crossfading
memcpy(&data->mixBuffer[0], memcpy(&data->mixBuffer[0],
data->buffer+(data->bufSize-data->mixSize), data->buffer+(data->bufSize-data->mixSize),
data->mixSize*sizeof(float)); data->mixSize*sizeof(float));
} }
else { else {
memset(buf, 0, nFrames*data->nChannel*sizeof(float)); memset(buf, 0, nFrames*data->nChannel*sizeof(float));
memset(data->buffer, 0, data->bufSize*sizeof(float)); memset(data->buffer, 0, data->bufSize*sizeof(float));
memset(data->mixBuffer, 0, nFrames*sizeof(float)); memset(data->mixBuffer, 0, nFrames*sizeof(float));
//cout<<"AudioManager : Could not read buffer"<<endl; //cout<<"AudioManager : Could not read buffer"<<endl;
} }
} }
else { else {
//read the next buffer //read the next buffer
for(int i=0; i<nFrames; ++i) { for(int i=0; i<nFrames; ++i) {
buf[i*2] = data->buffer[data->bufCounter*nFrames+i]; buf[i*2] = data->buffer[data->bufCounter*nFrames+i];
buf[i*2+1] = buf[i*2]; buf[i*2+1] = buf[i*2];
} }
} }
//move to the next buffer //move to the next buffer
data->bufCounter=(data->bufCounter+1)%(data->nbBufs-1); data->bufCounter=(data->bufCounter+1)%(data->nbBufs-1);
return 0; return 0;
} }
AudioManager::AudioManager() { AudioManager::AudioManager() {
m_init=false; m_init=false;
} }
void AudioManager::init() { void AudioManager::init() {
//m_rtAudio = new RtAudio(RtAudio::LINUX_PULSE); //m_rtAudio = new RtAudio(RtAudio::LINUX_PULSE);
m_rtAudio = new RtAudio(RtAudio::UNIX_JACK); m_rtAudio = new RtAudio(RtAudio::UNIX_JACK);
param = new RtAudio::StreamParameters(); param = new RtAudio::StreamParameters();
param->deviceId = m_rtAudio->getDefaultOutputDevice(); param->deviceId = m_rtAudio->getDefaultOutputDevice();
param->nChannels = 2; param->nChannels = 2;
options = new RtAudio::StreamOptions(); options = new RtAudio::StreamOptions();
options->streamName = "Rivill"; options->streamName = "Rivill";
data.nRate = m_rtAudio->getDeviceInfo(param->deviceId).preferredSampleRate; data.nRate = m_rtAudio->getDeviceInfo(param->deviceId).preferredSampleRate;
m_rtBufSize = 1024; m_rtBufSize = 1024;
bool init=true; bool init=true;
try{ try{
m_rtAudio->openStream(param, NULL, RTAUDIO_FLOAT32, data.nRate, m_rtAudio->openStream(param, NULL, RTAUDIO_FLOAT32, data.nRate,
&m_rtBufSize, &m_rtBufSize,
AudioManager::rtaudio_callback, &data, options); AudioManager::rtaudio_callback, &data, options);
} }
catch(const exception& e) { catch(const exception& e) {
init=false; init=false;
} }
if(init) { if(init) {
int nbBufs = 5; int nbBufs = 6-m_rtBufSize/256;
m_bufSize = m_rtBufSize*nbBufs; m_bufSize = m_rtBufSize*nbBufs;
data.ringBuffer = ringbuffer_create(m_bufSize*3); data.ringBuffer = ringbuffer_create(m_bufSize*3);
data.nChannel = param->nChannels; data.nChannel = param->nChannels;
data.bufSize = m_bufSize; data.bufSize = m_bufSize;
data.buffer = new float[m_bufSize]; data.buffer = new float[m_bufSize];
data.nbBufs = nbBufs; data.nbBufs = nbBufs;
data.bufCounter = 0; data.bufCounter = 0;
memset(&data.buffer[0], 0, data.bufSize*sizeof(float)); memset(&data.buffer[0], 0, data.bufSize*sizeof(float));
data.mixSize = m_rtBufSize; data.mixSize = m_rtBufSize;
data.mixBuffer = new float[data.mixSize]; data.mixBuffer = new float[data.mixSize];
memset(&data.mixBuffer[0], 0, data.mixSize*sizeof(float)); memset(&data.mixBuffer[0], 0, data.mixSize*sizeof(float));
m_thread = new std::thread(audioThreadFunction, this); m_thread = new std::thread(audioThreadFunction, this);
m_init = true; m_init = true;
} }
} }
bool AudioManager::changeBuf(float* outputBuf, float maxSinValue) { bool AudioManager::changeBuf(float* outputBuf, float maxSinValue) {
if(m_init) { if(m_init) {
//check if there is space to write the new values //check if there is space to write the new values
size_t write_space = ringbuffer_write_space(data.ringBuffer); size_t write_space = ringbuffer_write_space(data.ringBuffer);
if(write_space >= m_bufSize) { if(write_space >= m_bufSize) {
//write all data to the ringbuffer //write all data to the ringbuffer
ringbuffer_write(data.ringBuffer, outputBuf, data.bufSize); ringbuffer_write(data.ringBuffer, outputBuf, data.bufSize);
return true; return true;
} }
else { else {
//cout<<"AudioManager : Could not write buffer"<<endl; //cout<<"AudioManager : Could not write buffer"<<endl;
} }
} }
return false; return false;
} }
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment