diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index c5aeee0417e..4c1738db532 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -22,7 +22,6 @@ * */ - #include "SampleBuffer.h" @@ -58,6 +57,7 @@ #include "DrumSynth.h" #include "endian_handling.h" #include "Engine.h" +#include "GuiApplication.h" #include "interpolation.h" #include "Mixer.h" #include "templates.h" @@ -173,6 +173,7 @@ void SampleBuffer::update( bool _keep_settings ) MM_FREE( m_data ); } + bool fileLoadError = false; if( m_audioFile.isEmpty() && m_origData != NULL && m_origFrames > 0 ) { // TODO: reverse- and amplification-property is not covered @@ -203,12 +204,27 @@ void SampleBuffer::update( bool _keep_settings ) const QFileInfo fileInfo( file ); if( fileInfo.size() > 100*1024*1024 ) { - qWarning( "refusing to load sample files bigger " - "than 100 MB" ); + fileLoadError = true; } else { + SNDFILE * snd_file; + SF_INFO sf_info; + sf_info.format = 0; + if( ( snd_file = sf_open( f, SFM_READ, &sf_info ) ) != NULL ) + { + f_cnt_t frames = sf_info.frames; + int rate = sf_info.samplerate; + if( frames / rate > 60 * 60 ) // 60 minutes + { + fileLoadError = true; + } + sf_close( snd_file ); + } + } + if( !fileLoadError ) + { #ifdef LMMS_HAVE_OGGVORBIS // workaround for a bug in libsndfile or our libsndfile decoder // causing some OGG files to be distorted -> try with OGG Vorbis @@ -237,22 +253,21 @@ void SampleBuffer::update( bool _keep_settings ) } delete[] f; + } - if ( m_frames == 0 ) // if still no frames, bail - { - // sample couldn't be decoded, create buffer containing - // one sample-frame - m_data = MM_ALLOC( sampleFrame, 1 ); - memset( m_data, 0, sizeof( *m_data ) ); - m_frames = 1; - m_loopStartFrame = m_startFrame = 0; - m_loopEndFrame = m_endFrame = 1; - } - else // otherwise normalize sample rate - { - normalizeSampleRate( samplerate, _keep_settings ); - } - + if ( m_frames == 0 || fileLoadError ) // if still no frames, bail + { + // sample couldn't be decoded, create buffer containing + // one sample-frame + m_data = MM_ALLOC( sampleFrame, 1 ); + memset( m_data, 0, sizeof( *m_data ) ); + m_frames = 1; + m_loopStartFrame = m_startFrame = 0; + m_loopEndFrame = m_endFrame = 1; + } + else // otherwise normalize sample rate + { + normalizeSampleRate( samplerate, _keep_settings ); } } else @@ -273,6 +288,23 @@ void SampleBuffer::update( bool _keep_settings ) } emit sampleUpdated(); + + if( fileLoadError ) + { + QString message = "Audio files are limited to 100 MB " + "in size and 1 hour of playing time"; + if( gui ) + { + QMessageBox::information( NULL, + "Fail to open file", message, + QMessageBox::Ok ); + } + else + { + fprintf( stderr, "%s\n", message.toUtf8().constData() ); + exit( EXIT_FAILURE ); + } + } }