63 __INFOLOG(
"DiskWriterDriver thread start" );
70 soundInfo.channels = 2;
72 int sfformat = 0x010000;
97 soundInfo.format = sfformat|bits;
103 soundInfo.format = SF_FORMAT_OGG | SF_FORMAT_VORBIS;
139 if ( !sf_format_check( &soundInfo ) ) {
141 pthread_exit(
nullptr );
146 SNDFILE* m_file = sf_open( pDriver->
m_sFilename.toLocal8Bit(), SFM_WRITE, &soundInfo );
147 if ( m_file ==
nullptr ) {
148 __ERRORLOG( QString(
"Unable to open file [%1] using libsndfile: %2" )
150 .arg( sf_strerror(
nullptr ) ) );
151 pthread_exit(
nullptr );
163 auto pSong = pHydrogen->
getSong();
166 std::vector<PatternList*> *pPatternColumns = pSong->getPatternGroupVector();
167 int nColumns = pPatternColumns->size();
169 int nPatternSize, nBufferWriteLength;
172 int nMaxNumberOfSilentFrames = 200;
173 for (
int patternPosition = 0; patternPosition < nColumns; ++patternPosition ) {
175 PatternList *pColumn = ( *pPatternColumns )[ patternPosition ];
176 if ( pColumn->
size() != 0 ) {
184 pSong->getResolution() );
187 int nPatternLengthInFrames = fTicksize * nPatternSize;
188 int nFrameNumber = 0;
190 int nSuccessiveZeros = 0;
191 while ( ( patternPosition < nColumns - 1 &&
194 nFrameNumber < nPatternLengthInFrames ) ||
195 ( patternPosition == nColumns - 1 &&
198 ( nFrameNumber < nPatternLengthInFrames ||
199 pSampler->isRenderingNotes() ) ) ) {
208 if( patternPosition < nColumns - 1 &&
209 nPatternLengthInFrames - nFrameNumber < pDriver->m_nBufferSize ){
210 nLastRun = nPatternLengthInFrames - nFrameNumber;
211 nUsedBuffer = nLastRun;
221 if ( patternPosition == nColumns - 1 &&
222 nPatternLengthInFrames - nFrameNumber < nUsedBuffer ) {
237 nBufferWriteLength = 0;
239 int nSilentFrames = 0;
240 for (
int ii = 0; ii < nUsedBuffer; ++ii ) {
241 ++nBufferWriteLength;
243 if ( std::abs( pData_L[ii] ) == 0 &&
244 std::abs( pData_R[ii] ) == 0 ) {
248 if ( nSuccessiveZeros == nMaxNumberOfSilentFrames ) {
253 nBufferWriteLength = nUsedBuffer;
256 nFrameNumber += nBufferWriteLength;
258 for (
unsigned ii = 0; ii < nBufferWriteLength; ii++ ) {
259 if( pData_L[ ii ] > 1 ) {
261 }
else if( pData_L[ ii ] < -1 ) {
262 pData[ ii * 2 ] = -1;
264 pData[ ii * 2 ] = pData_L[ ii ];
267 if( pData_R[ ii ] > 1 ){
268 pData[ ii * 2 + 1 ] = 1;
269 }
else if ( pData_R[ ii ] < -1 ) {
270 pData[ ii * 2 + 1 ] = -1;
272 pData[ ii * 2 + 1 ] = pData_R[ ii ];
276 const int res = sf_writef_float( m_file, pData, nBufferWriteLength );
277 if ( res != (
int )nBufferWriteLength ) {
278 __ERRORLOG( QString(
"Error during sf_write_float. Floats written: [%1], target: [%2]. %3" )
280 .arg( nBufferWriteLength )
281 .arg( sf_strerror(
nullptr ) ) );
287 if ( nSuccessiveZeros == nMaxNumberOfSilentFrames ) {
293 int nPercent =
static_cast<int>( ( float )(patternPosition +1) /
294 ( float )nColumns * 100.0 );
295 if ( nPercent < 100 ) {
308 __INFOLOG(
"DiskWriterDriver thread end" );
310 pthread_exit(
nullptr );