Skip to content

Commit ddcae47

Browse files
committed
Fix sample file loading on Windows
1 parent 9d0aae2 commit ddcae47

4 files changed

Lines changed: 44 additions & 33 deletions

File tree

include/DrumSynth.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,24 @@
3030
#include <stdint.h>
3131
#include "lmms_basics.h"
3232

33+
class QString;
34+
3335
class DrumSynth {
3436
public:
3537
DrumSynth() {};
36-
int GetDSFileSamples(const char *dsfile, int16_t *&wave, int channels, sample_rate_t Fs);
38+
int GetDSFileSamples(QString dsfile, int16_t *&wave, int channels, sample_rate_t Fs);
3739

3840
private:
3941
float LoudestEnv(void);
4042
int LongestEnv(void);
4143
void UpdateEnv(int e, long t);
42-
void GetEnv(int env, const char *sec, const char *key, const char *ini);
44+
void GetEnv(int env, const char *sec, const char *key, QString ini);
4345

4446
float waveform(float ph, int form);
4547

46-
int GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, const char *file);
47-
int GetPrivateProfileInt(const char *sec, const char *key, int def, const char *file);
48-
float GetPrivateProfileFloat(const char *sec, const char *key, float def, const char *file);
48+
int GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, QString file);
49+
int GetPrivateProfileInt(const char *sec, const char *key, int def, QString file);
50+
float GetPrivateProfileFloat(const char *sec, const char *key, float def, QString file);
4951

5052
};
5153

include/SampleBuffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,15 +270,15 @@ public slots:
270270
void convertIntToFloat ( int_sample_t * & _ibuf, f_cnt_t _frames, int _channels);
271271
void directFloatWrite ( sample_t * & _fbuf, f_cnt_t _frames, int _channels);
272272

273-
f_cnt_t decodeSampleSF( const char * _f, sample_t * & _buf,
273+
f_cnt_t decodeSampleSF( QString _f, sample_t * & _buf,
274274
ch_cnt_t & _channels,
275275
sample_rate_t & _sample_rate );
276276
#ifdef LMMS_HAVE_OGGVORBIS
277-
f_cnt_t decodeSampleOGGVorbis( const char * _f, int_sample_t * & _buf,
277+
f_cnt_t decodeSampleOGGVorbis( QString _f, int_sample_t * & _buf,
278278
ch_cnt_t & _channels,
279279
sample_rate_t & _sample_rate );
280280
#endif
281-
f_cnt_t decodeSampleDS( const char * _f, int_sample_t * & _buf,
281+
f_cnt_t decodeSampleDS( QString _f, int_sample_t * & _buf,
282282
ch_cnt_t & _channels,
283283
sample_rate_t & _sample_rate );
284284

src/core/DrumSynth.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626

2727
#include "DrumSynth.h"
2828

29-
#include <fstream>
29+
#include <sstream>
3030
#include <cstring>
3131

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

34+
#include <QFile>
35+
3436
#ifdef LMMS_BUILD_WIN32
3537
#define powf pow
3638
#endif
@@ -112,7 +114,7 @@ void DrumSynth::UpdateEnv(int e, long t)
112114
}
113115

114116

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

164166

165-
int DrumSynth::GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, const char *file)
167+
int DrumSynth::GetPrivateProfileString(const char *sec, const char *key, const char *def, char *buffer, int size, QString file)
166168
{
167-
ifstream is;
169+
stringstream is;
168170
bool inSection = false;
169171
char *line;
170172
char *k, *b;
171173
int len = 0;
172174

173175
line = (char*)malloc(200);
174176

175-
is.open (file, ifstream::in);
177+
// Use QFile to handle unicode file name on Windows
178+
// Previously we used ifstream directly
179+
QFile f(file);
180+
f.open(QIODevice::ReadOnly);
181+
QByteArray dat = f.readAll().constData();
182+
is.str(string(dat.constData(), dat.size()));
176183

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

221-
is.close();
222228
free(line);
223229

224230
return len;
225231
}
226232

227-
int DrumSynth::GetPrivateProfileInt(const char *sec, const char *key, int def, const char *file)
233+
int DrumSynth::GetPrivateProfileInt(const char *sec, const char *key, int def, QString file)
228234
{
229235
char tmp[16];
230236
int i=0;
@@ -235,7 +241,7 @@ int DrumSynth::GetPrivateProfileInt(const char *sec, const char *key, int def, c
235241
return i;
236242
}
237243

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

255-
int DrumSynth::GetDSFileSamples(const char *dsfile, int16_t *&wave, int channels, sample_rate_t Fs)
261+
int DrumSynth::GetDSFileSamples(QString dsfile, int16_t *&wave, int channels, sample_rate_t Fs)
256262
{
257263
//input file
258264
char sec[32];

src/core/SampleBuffer.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,6 @@ void SampleBuffer::update( bool _keep_settings )
187187
else if( !m_audioFile.isEmpty() )
188188
{
189189
QString file = tryToMakeAbsolute( m_audioFile );
190-
#ifdef LMMS_BUILD_WIN32
191-
char * f = qstrdup( file.toLocal8Bit().constData() );
192-
#else
193-
char * f = qstrdup( file.toUtf8().constData() );
194-
#endif
195190
int_sample_t * buf = NULL;
196191
sample_t * fbuf = NULL;
197192
ch_cnt_t channels = DEFAULT_CHANNELS;
@@ -205,10 +200,13 @@ void SampleBuffer::update( bool _keep_settings )
205200
}
206201
else
207202
{
203+
// Use QFile to handle unicode file names on Windows
204+
QFile f(file);
205+
f.open(QIODevice::ReadOnly);
208206
SNDFILE * snd_file;
209207
SF_INFO sf_info;
210208
sf_info.format = 0;
211-
if( ( snd_file = sf_open( f, SFM_READ, &sf_info ) ) != NULL )
209+
if( ( snd_file = sf_open_fd( f.handle(), SFM_READ, &sf_info, false ) ) != NULL )
212210
{
213211
f_cnt_t frames = sf_info.frames;
214212
int rate = sf_info.samplerate;
@@ -218,6 +216,7 @@ void SampleBuffer::update( bool _keep_settings )
218216
}
219217
sf_close( snd_file );
220218
}
219+
f.close();
221220
}
222221

223222
if( !fileLoadError )
@@ -228,28 +227,26 @@ void SampleBuffer::update( bool _keep_settings )
228227
// decoder first if filename extension matches "ogg"
229228
if( m_frames == 0 && fileInfo.suffix() == "ogg" )
230229
{
231-
m_frames = decodeSampleOGGVorbis( f, buf, channels, samplerate );
230+
m_frames = decodeSampleOGGVorbis( file, buf, channels, samplerate );
232231
}
233232
#endif
234233
if( m_frames == 0 )
235234
{
236-
m_frames = decodeSampleSF( f, fbuf, channels,
235+
m_frames = decodeSampleSF( file, fbuf, channels,
237236
samplerate );
238237
}
239238
#ifdef LMMS_HAVE_OGGVORBIS
240239
if( m_frames == 0 )
241240
{
242-
m_frames = decodeSampleOGGVorbis( f, buf, channels,
241+
m_frames = decodeSampleOGGVorbis( file, buf, channels,
243242
samplerate );
244243
}
245244
#endif
246245
if( m_frames == 0 )
247246
{
248-
m_frames = decodeSampleDS( f, buf, channels,
247+
m_frames = decodeSampleDS( file, buf, channels,
249248
samplerate );
250249
}
251-
252-
delete[] f;
253250
}
254251

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

406403

407404

408-
f_cnt_t SampleBuffer::decodeSampleSF( const char * _f,
405+
f_cnt_t SampleBuffer::decodeSampleSF(QString _f,
409406
sample_t * & _buf,
410407
ch_cnt_t & _channels,
411408
sample_rate_t & _samplerate )
@@ -416,7 +413,11 @@ f_cnt_t SampleBuffer::decodeSampleSF( const char * _f,
416413
f_cnt_t frames = 0;
417414
bool sf_rr = false;
418415

419-
if( ( snd_file = sf_open( _f, SFM_READ, &sf_info ) ) != NULL )
416+
417+
// Use QFile to handle unicode file names on Windows
418+
QFile f(_f);
419+
f.open(QIODevice::ReadOnly);
420+
if( ( snd_file = sf_open_fd( f.handle(), SFM_READ, &sf_info, false ) ) != NULL )
420421
{
421422
frames = sf_info.frames;
422423

@@ -442,6 +443,8 @@ f_cnt_t SampleBuffer::decodeSampleSF( const char * _f,
442443
"sample %s: %s", _f, sf_strerror( NULL ) );
443444
#endif
444445
}
446+
f.close();
447+
445448
//write down either directly or convert i->f depending on file type
446449

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

508511

509512

510-
f_cnt_t SampleBuffer::decodeSampleOGGVorbis( const char * _f,
513+
f_cnt_t SampleBuffer::decodeSampleOGGVorbis( QString _f,
511514
int_sample_t * & _buf,
512515
ch_cnt_t & _channels,
513516
sample_rate_t & _samplerate )
@@ -603,7 +606,7 @@ f_cnt_t SampleBuffer::decodeSampleOGGVorbis( const char * _f,
603606

604607

605608

606-
f_cnt_t SampleBuffer::decodeSampleDS( const char * _f,
609+
f_cnt_t SampleBuffer::decodeSampleDS( QString _f,
607610
int_sample_t * & _buf,
608611
ch_cnt_t & _channels,
609612
sample_rate_t & _samplerate )

0 commit comments

Comments
 (0)