Skip to content

Commit

Permalink
Fix sample file loading on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
PhysSong committed Jul 5, 2018
1 parent 9d0aae2 commit ddcae47
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
12 changes: 7 additions & 5 deletions include/DrumSynth.h
Expand Up @@ -30,22 +30,24 @@
#include <stdint.h>
#include "lmms_basics.h"

class QString;

class DrumSynth {
public:
DrumSynth() {};
int GetDSFileSamples(const char *dsfile, int16_t *&wave, int channels, sample_rate_t Fs);
int GetDSFileSamples(QString dsfile, int16_t *&wave, int channels, sample_rate_t Fs);

private:
float LoudestEnv(void);
int LongestEnv(void);
void UpdateEnv(int e, long t);
void GetEnv(int env, const char *sec, const char *key, const char *ini);
void GetEnv(int env, const char *sec, const char *key, QString ini);

float waveform(float ph, int form);

int GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, const char *file);
int GetPrivateProfileInt(const char *sec, const char *key, int def, const char *file);
float GetPrivateProfileFloat(const char *sec, const char *key, float def, const char *file);
int GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, QString file);
int GetPrivateProfileInt(const char *sec, const char *key, int def, QString file);
float GetPrivateProfileFloat(const char *sec, const char *key, float def, QString file);

};

Expand Down
6 changes: 3 additions & 3 deletions include/SampleBuffer.h
Expand Up @@ -270,15 +270,15 @@ public slots:
void convertIntToFloat ( int_sample_t * & _ibuf, f_cnt_t _frames, int _channels);
void directFloatWrite ( sample_t * & _fbuf, f_cnt_t _frames, int _channels);

f_cnt_t decodeSampleSF( const char * _f, sample_t * & _buf,
f_cnt_t decodeSampleSF( QString _f, sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _sample_rate );
#ifdef LMMS_HAVE_OGGVORBIS
f_cnt_t decodeSampleOGGVorbis( const char * _f, int_sample_t * & _buf,
f_cnt_t decodeSampleOGGVorbis( QString _f, int_sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _sample_rate );
#endif
f_cnt_t decodeSampleDS( const char * _f, int_sample_t * & _buf,
f_cnt_t decodeSampleDS( QString _f, int_sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _sample_rate );

Expand Down
24 changes: 15 additions & 9 deletions src/core/DrumSynth.cpp
Expand Up @@ -26,11 +26,13 @@

#include "DrumSynth.h"

#include <fstream>
#include <sstream>
#include <cstring>

#include <math.h> //sin(), exp(), etc.

#include <QFile>

#ifdef LMMS_BUILD_WIN32
#define powf pow
#endif
Expand Down Expand Up @@ -112,7 +114,7 @@ void DrumSynth::UpdateEnv(int e, long t)
}


void DrumSynth::GetEnv(int env, const char *sec, const char *key, const char *ini)
void DrumSynth::GetEnv(int env, const char *sec, const char *key, QString ini)
{
char en[256], s[8];
int i=0, o=0, ep=0;
Expand Down Expand Up @@ -162,17 +164,22 @@ float DrumSynth::waveform(float ph, int form)
}


int DrumSynth::GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, const char *file)
int DrumSynth::GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, QString file)
{
ifstream is;
stringstream is;
bool inSection = false;
char *line;
char *k, *b;
int len = 0;

line = (char*)malloc(200);

is.open (file, ifstream::in);
// Use QFile to handle unicode file name on Windows
// Previously we used ifstream directly
QFile f(file);
f.open(QIODevice::ReadOnly);
QByteArray dat = f.readAll().constData();
is.str(string(dat.constData(), dat.size()));

while (is.good()) {
if (!inSection) {
Expand Down Expand Up @@ -218,13 +225,12 @@ int DrumSynth::GetPrivateProfileString(const char *sec, const char *key, const c
strncpy(buffer, def, size);
}

is.close();
free(line);

return len;
}

int DrumSynth::GetPrivateProfileInt(const char *sec, const char *key, int def, const char *file)
int DrumSynth::GetPrivateProfileInt(const char *sec, const char *key, int def, QString file)
{
char tmp[16];
int i=0;
Expand All @@ -235,7 +241,7 @@ int DrumSynth::GetPrivateProfileInt(const char *sec, const char *key, int def, c
return i;
}

float DrumSynth::GetPrivateProfileFloat(const char *sec, const char *key, float def, const char *file)
float DrumSynth::GetPrivateProfileFloat(const char *sec, const char *key, float def, QString file)
{
char tmp[16];
float f=0.f;
Expand All @@ -252,7 +258,7 @@ float DrumSynth::GetPrivateProfileFloat(const char *sec, const char *key, float
// an associative array or something once we have a datastructure to load in to.
// llama

int DrumSynth::GetDSFileSamples(const char *dsfile, int16_t *&wave, int channels, sample_rate_t Fs)
int DrumSynth::GetDSFileSamples(QString dsfile, int16_t *&wave, int channels, sample_rate_t Fs)
{
//input file
char sec[32];
Expand Down
35 changes: 19 additions & 16 deletions src/core/SampleBuffer.cpp
Expand Up @@ -187,11 +187,6 @@ void SampleBuffer::update( bool _keep_settings )
else if( !m_audioFile.isEmpty() )
{
QString file = tryToMakeAbsolute( m_audioFile );
#ifdef LMMS_BUILD_WIN32
char * f = qstrdup( file.toLocal8Bit().constData() );
#else
char * f = qstrdup( file.toUtf8().constData() );
#endif
int_sample_t * buf = NULL;
sample_t * fbuf = NULL;
ch_cnt_t channels = DEFAULT_CHANNELS;
Expand All @@ -205,10 +200,13 @@ void SampleBuffer::update( bool _keep_settings )
}
else
{
// Use QFile to handle unicode file names on Windows
QFile f(file);
f.open(QIODevice::ReadOnly);
SNDFILE * snd_file;
SF_INFO sf_info;
sf_info.format = 0;
if( ( snd_file = sf_open( f, SFM_READ, &sf_info ) ) != NULL )
if( ( snd_file = sf_open_fd( f.handle(), SFM_READ, &sf_info, false ) ) != NULL )
{
f_cnt_t frames = sf_info.frames;
int rate = sf_info.samplerate;
Expand All @@ -218,6 +216,7 @@ void SampleBuffer::update( bool _keep_settings )
}
sf_close( snd_file );
}
f.close();
}

if( !fileLoadError )
Expand All @@ -228,28 +227,26 @@ void SampleBuffer::update( bool _keep_settings )
// decoder first if filename extension matches "ogg"
if( m_frames == 0 && fileInfo.suffix() == "ogg" )
{
m_frames = decodeSampleOGGVorbis( f, buf, channels, samplerate );
m_frames = decodeSampleOGGVorbis( file, buf, channels, samplerate );
}
#endif
if( m_frames == 0 )
{
m_frames = decodeSampleSF( f, fbuf, channels,
m_frames = decodeSampleSF( file, fbuf, channels,
samplerate );
}
#ifdef LMMS_HAVE_OGGVORBIS
if( m_frames == 0 )
{
m_frames = decodeSampleOGGVorbis( f, buf, channels,
m_frames = decodeSampleOGGVorbis( file, buf, channels,
samplerate );
}
#endif
if( m_frames == 0 )
{
m_frames = decodeSampleDS( f, buf, channels,
m_frames = decodeSampleDS( file, buf, channels,
samplerate );
}

delete[] f;
}

if ( m_frames == 0 || fileLoadError ) // if still no frames, bail
Expand Down Expand Up @@ -405,7 +402,7 @@ void SampleBuffer::normalizeSampleRate( const sample_rate_t _src_sr,



f_cnt_t SampleBuffer::decodeSampleSF( const char * _f,
f_cnt_t SampleBuffer::decodeSampleSF(QString _f,
sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _samplerate )
Expand All @@ -416,7 +413,11 @@ f_cnt_t SampleBuffer::decodeSampleSF( const char * _f,
f_cnt_t frames = 0;
bool sf_rr = false;

if( ( snd_file = sf_open( _f, SFM_READ, &sf_info ) ) != NULL )

// Use QFile to handle unicode file names on Windows
QFile f(_f);
f.open(QIODevice::ReadOnly);
if( ( snd_file = sf_open_fd( f.handle(), SFM_READ, &sf_info, false ) ) != NULL )
{
frames = sf_info.frames;

Expand All @@ -442,6 +443,8 @@ f_cnt_t SampleBuffer::decodeSampleSF( const char * _f,
"sample %s: %s", _f, sf_strerror( NULL ) );
#endif
}
f.close();

//write down either directly or convert i->f depending on file type

if ( frames > 0 && _buf != NULL )
Expand Down Expand Up @@ -507,7 +510,7 @@ long qfileTellCallback( void * _udata )



f_cnt_t SampleBuffer::decodeSampleOGGVorbis( const char * _f,
f_cnt_t SampleBuffer::decodeSampleOGGVorbis( QString _f,
int_sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _samplerate )
Expand Down Expand Up @@ -603,7 +606,7 @@ f_cnt_t SampleBuffer::decodeSampleOGGVorbis( const char * _f,



f_cnt_t SampleBuffer::decodeSampleDS( const char * _f,
f_cnt_t SampleBuffer::decodeSampleDS( QString _f,
int_sample_t * & _buf,
ch_cnt_t & _channels,
sample_rate_t & _samplerate )
Expand Down

0 comments on commit ddcae47

Please sign in to comment.