Skip to content

Commit

Permalink
a couple of minor fixes (oca_loadwav, audio playback)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonrunov committed Apr 6, 2019
1 parent 712425a commit 4fce188
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
4 changes: 2 additions & 2 deletions scripts/utils/oca_loadwav.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ function oca_loadwav( filename, track=[], mono=false )
end
oca_track_setprop( "channels", ch, id );

t = 0;
t = oca_group_getcontext( {'oca_load_audio_start', 0} );
res = 0;
block_sz = fs*10;
block_sz = fs*1000;

while res < len
x = wavread( filename, res + [1,min(block_sz,len-res)] )';
Expand Down
36 changes: 21 additions & 15 deletions src/OcaAudioController.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2013-2016 Anton Runov
Copyright 2013-2018 Anton Runov
This file is part of Octaudio.
Expand Down Expand Up @@ -92,6 +92,7 @@ OcaAudioController::OcaAudioController()

m_duplexStopRequested( false ),
m_endOfData( false ),
m_startSkipCounter( 0 ),

m_outputDevice( paNoDevice ),
m_inputDevice( paNoDevice ),
Expand Down Expand Up @@ -218,10 +219,6 @@ double OcaAudioController::startPlayback( OcaTrackGroup* group, double t, double
}
}

if( result ) {
result = fillPlaybackBuffer();
}

if( result ) {
result = ( paNoError == Pa_StartStream( stream ) );
}
Expand All @@ -247,6 +244,7 @@ double OcaAudioController::startPlayback( OcaTrackGroup* group, double t, double
m_timer->start( 50 );
}
m_endOfData = false;
m_startSkipCounter = 0;
m_playbackStream = stream;
m_state = e_StatePlaying;
flags = e_FlagStateChanged | e_FlagCursorChanged;
Expand Down Expand Up @@ -849,18 +847,26 @@ bool OcaAudioController::setDevice( QString dev_name, bool recording )

bool OcaAudioController::fillPlaybackBuffer()
{
bool result = false;
if( ( NULL != m_playbackBuffer ) && ( NULL != m_groupPlay ) ) {
bool duplex = ( m_groupPlay == m_groupRecording );
m_playbackCursor = m_groupPlay->readPlaybackData( m_playbackCursor,
m_playbackStopPosition,
m_playbackBuffer,
m_sampleRate,
duplex );
result = ( 0 < m_playbackBuffer->getAvailableLength() );
if( ( NULL == m_playbackBuffer ) || ( NULL == m_groupPlay ) ) {
return false;
}

return result;
// a tricky workaround for dropping initial parts of audio by some systems
// we are waiting a little bit before starting feedding the audio driver
if (4 > m_startSkipCounter) {
if (0 < m_playbackBuffer->getReadCount()) {
++m_startSkipCounter;
}
return true;
}

bool duplex = ( m_groupPlay == m_groupRecording );
m_playbackCursor = m_groupPlay->readPlaybackData( m_playbackCursor,
m_playbackStopPosition,
m_playbackBuffer,
m_sampleRate,
duplex );
return ( 0 < m_playbackBuffer->getAvailableLength() );
}

// -----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/OcaAudioController.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class OcaAudioController : public OcaObject

bool m_duplexStopRequested;
bool m_endOfData;
int m_startSkipCounter;

protected:
int m_outputDevice;
Expand Down
1 change: 1 addition & 0 deletions src/OcaRingBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ int OcaRingBuffer::read( float* data, int length )
if( 0 < remainder ) {
memset( data + length, 0, remainder * sizeof(float) );
}
++m_readCount;
return length;
}

Expand Down
2 changes: 2 additions & 0 deletions src/OcaRingBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class OcaRingBuffer
int write( float* data, int length );
int getAvailableSpace() const { return ring( m_idxRead - m_idxWrite - 1 ); }
int getAvailableLength() const { return ring( m_idxWrite - m_idxRead ); }
unsigned int getReadCount() const {return m_readCount;}

protected:
int ring( int idx ) const;
Expand All @@ -41,6 +42,7 @@ class OcaRingBuffer

volatile int m_idxRead;
volatile int m_idxWrite;
unsigned int m_readCount;
};

#endif // OcaRingBuffer_h

0 comments on commit 4fce188

Please sign in to comment.