Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crashes and cleanup warnings #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 32 additions & 60 deletions sfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <math.h>
#include <sndfile.h>
#include <vorbis/vorbisenc.h>
#include <vector>

#include "sfont.h"
#include "xml.h"
Expand Down Expand Up @@ -59,11 +60,6 @@ static const bool writeCompressed = true;
// Sample
//---------------------------------------------------------

Sample::Sample()
{
name = 0;
}

Sample::~Sample()
{
free(name);
Expand All @@ -73,11 +69,6 @@ Sample::~Sample()
// Instrument
//---------------------------------------------------------

Instrument::Instrument()
{
name = 0;
}

Instrument::~Instrument()
{
free(name);
Expand Down Expand Up @@ -338,12 +329,12 @@ void SoundFont::readVersion()

char* SoundFont::readString(int n)
{
char data[2500];
if (file->read((char*)data, n) != n)
char *data = (char*)malloc(n+1);
if (file->read(data, n) != n)
throw(QString("unexpected end of file\n"));
if (data[n-1] != 0)
data[n] = 0;
return strdup(data);
return data;
}

//---------------------------------------------------------
Expand Down Expand Up @@ -387,6 +378,9 @@ void SoundFont::readSection(const char* fourcc, int len)
sampleLen = len;
skip(len);
break;
case FOURCC('s','m','2','4'): // 24-bit sample LSBs
skip(len);
break;
case FOURCC('p','h','d','r'): // preset headers
readPhdr(len);
break;
Expand Down Expand Up @@ -896,24 +890,23 @@ void SoundFont::writeSmpl()
}
}
else {
char* buffer = new char[sampleLen];
std::vector<char> buffer(sampleLen);
QFile f(path);
if (!f.open(QIODevice::ReadOnly))
throw(QString("cannot open <%1>").arg(f.fileName()));
foreach(Sample* s, samples) {
f.seek(samplePos + s->start * sizeof(short));

int len = (s->end - s->start) * sizeof(short);
f.read(buffer, len);
write(buffer, len);
f.read(buffer.data(), len);
write(buffer.data(), len);
s->start = sampleLen / sizeof(short);
sampleLen += len;
s->end = sampleLen / sizeof(short);
s->loopstart += s->start;
s->loopend += s->start;
}
f.close();
delete[] buffer;
}
qint64 npos = file->pos();
file->seek(pos);
Expand All @@ -935,8 +928,7 @@ void SoundFont::writePhdr()
writePreset(zoneIdx, p);
zoneIdx += p->zones.size();
}
Preset p;
memset(&p, sizeof(p), 0);
Preset p{};
writePreset(zoneIdx, &p);
}

Expand All @@ -946,8 +938,7 @@ void SoundFont::writePhdr()

void SoundFont::writePreset(int zoneIdx, const Preset* preset)
{
char name[20];
memset(name, 0, 20);
char name[20] = {0};
if (preset->name)
memcpy(name, preset->name, strlen(preset->name));
write(name, 20);
Expand Down Expand Up @@ -1066,8 +1057,7 @@ void SoundFont::writeInst()
writeInstrument(zoneIdx, p);
zoneIdx += p->zones.size();
}
Instrument p;
memset(&p, sizeof(p), 0);
Instrument p{};
writeInstrument(zoneIdx, &p);
}

Expand All @@ -1077,8 +1067,7 @@ void SoundFont::writeInst()

void SoundFont::writeInstrument(int zoneIdx, const Instrument* instrument)
{
char name[20];
memset(name, 0, 20);
char name[20] = {0};
if (instrument->name)
memcpy(name, instrument->name, strlen(instrument->name));
write(name, 20);
Expand All @@ -1095,8 +1084,7 @@ void SoundFont::writeShdr()
writeDword(46 * (samples.size() + 1));
foreach(const Sample* s, samples)
writeSample(s);
Sample s;
memset(&s, 0, sizeof(s));
Sample s{};
writeSample(&s);
}

Expand All @@ -1106,8 +1094,7 @@ void SoundFont::writeShdr()

void SoundFont::writeSample(const Sample* s)
{
char name[20];
memset(name, 0, 20);
char name[20] = {0};
if (s->name)
memcpy(name, s->name, strlen(s->name));
write(name, 20);
Expand Down Expand Up @@ -1137,8 +1124,8 @@ bool SoundFont::writeSampleFile(Sample* s, QString name)
}
f.seek(samplePos + s->start * sizeof(short));
int len = s->end - s->start;
short buffer[len];
f.read((char*)buffer, len * sizeof(short));
std::vector<short> buffer(len);
f.read((char*)buffer.data(), len * sizeof(short));
f.close();

// int format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
Expand All @@ -1157,7 +1144,7 @@ bool SoundFont::writeSampleFile(Sample* s, QString name)
return false;
}

sf_write_short(sf, buffer, len);
sf_write_short(sf, buffer.data(), len);

if (sf_close(sf)) {
fprintf(stderr, "close soundfile failed\n");
Expand All @@ -1179,8 +1166,8 @@ int SoundFont::writeCompressedSample(Sample* s)
}
f.seek(samplePos + s->start * sizeof(short));
int samples = s->end - s->start;
short ibuffer[samples];
f.read((char*)ibuffer, samples * sizeof(short));
std::vector<short> ibuffer(samples);
f.read((char*)ibuffer.data(), samples * sizeof(short));
f.close();

ogg_stream_state os;
Expand Down Expand Up @@ -1212,17 +1199,14 @@ int SoundFont::writeCompressedSample(Sample* s)
ogg_stream_packetin(&os, &header_comm);
ogg_stream_packetin(&os, &header_code);

char obuf[1024 * 1024];
char* p = obuf;
std::vector<char> obuf{};

for (;;) {
int result = ogg_stream_flush(&os, &og);
if (result == 0)
break;
memcpy(p, og.header, og.header_len);
p += og.header_len;
memcpy(p, og.body, og.body_len);
p += og.body_len;
obuf.insert(obuf.end(), og.header, og.header + og.header_len);
obuf.insert(obuf.end(), og.body, og.body + og.body_len);
}

long i;
Expand Down Expand Up @@ -1251,10 +1235,8 @@ int SoundFont::writeCompressedSample(Sample* s)
int result = ogg_stream_pageout(&os, &og);
if (result == 0)
break;
memcpy(p, og.header, og.header_len);
p += og.header_len;
memcpy(p, og.body, og.body_len);
p += og.body_len;
obuf.insert(obuf.end(), og.header, og.header + og.header_len);
obuf.insert(obuf.end(), og.body, og.body + og.body_len);
}
}
}
Expand All @@ -1276,10 +1258,8 @@ int SoundFont::writeCompressedSample(Sample* s)
int result = ogg_stream_pageout(&os, &og);
if (result == 0)
break;
memcpy(p, og.header, og.header_len);
p += og.header_len;
memcpy(p, og.body, og.body_len);
p += og.body_len;
obuf.insert(obuf.end(), og.header, og.header + og.header_len);
obuf.insert(obuf.end(), og.body, og.body + og.body_len);
}
}
}
Expand All @@ -1290,10 +1270,9 @@ int SoundFont::writeCompressedSample(Sample* s)
vorbis_comment_clear(&vc);
vorbis_info_clear(&vi);

int n = p - obuf;
write(obuf, n);
write(obuf.data(), obuf.size());

return n;
return obuf.size();
}

//---------------------------------------------------------
Expand All @@ -1318,8 +1297,8 @@ bool SoundFont::writeCSample(Sample* s, int idx)
}
fi.seek(samplePos + s->start * sizeof(short));
int samples = s->end - s->start;
short ibuffer[samples];
fi.read((char*)ibuffer, samples * sizeof(short));
std::vector<short> ibuffer(samples);
fi.read((char*)ibuffer.data(), samples * sizeof(short));
fi.close();

fprintf(f, "static const short wave%d[] = {\n ", idx);
Expand Down Expand Up @@ -1362,7 +1341,6 @@ static bool checkInstrument(QList<int> pnums, QList<Preset*> presets, int instrI
}

static bool checkInstrument(QList<Preset*> presets, int instrIdx) {
bool result = false;
for(int i = 0; i < presets.size(); i++) {
Preset* p = presets[i];
Zone* z = p->zones[0];
Expand All @@ -1389,7 +1367,6 @@ static bool checkSample(QList<int> pnums, QList<Preset*> presets, QList<Instrume
++idx;
continue;
}
int zones = instrument->zones.size();
foreach(Zone* z, instrument->zones) {
QList<GeneratorList*> gl;
foreach(GeneratorList* g, z->generators) {
Expand Down Expand Up @@ -1417,7 +1394,6 @@ static bool checkSample(QList<Preset*> presets, QList<Instrument*> instruments,
++idx;
continue;
}
int zones = instrument->zones.size();
foreach(Zone* z, instrument->zones) {
QList<GeneratorList*> gl;
foreach(GeneratorList* g, z->generators) {
Expand Down Expand Up @@ -1522,7 +1498,6 @@ bool SoundFont::writeCode()

QList<GeneratorList*> gl;
foreach(GeneratorList* g, z->generators) {
const char* name = generatorNames[g->gen];
if (g->gen == Gen_KeyRange) {
keyLo = g->amount.lo;
keyHi = g->amount.hi;
Expand Down Expand Up @@ -1605,7 +1580,6 @@ bool SoundFont::writeCode()
int instrIdx = -1;

foreach(GeneratorList* g, z->generators) {
const char* name = generatorNames[g->gen];
if (g->gen == Gen_KeyRange) {
keyLo = g->amount.lo;
keyHi = g->amount.hi;
Expand Down Expand Up @@ -1730,7 +1704,6 @@ bool SoundFont::writeCode(QList<int> pnums)
QList<GeneratorList*> gl;

foreach(GeneratorList* g, z->generators) {
const char* name = generatorNames[g->gen];
if (g->gen == Gen_KeyRange) {
keyLo = g->amount.lo;
keyHi = g->amount.hi;
Expand Down Expand Up @@ -1802,7 +1775,6 @@ bool SoundFont::writeCode(QList<int> pnums)
int veloHi = 127;
int instrIdx = -1;
foreach(GeneratorList* g, z->generators) {
const char* name = generatorNames[g->gen];
if (g->gen == Gen_KeyRange) {
keyLo = g->amount.lo;
keyHi = g->amount.hi;
Expand Down
26 changes: 12 additions & 14 deletions sfont.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,10 @@ struct Preset {
//---------------------------------------------------------

struct Instrument {
char* name;
int index; // used only for read
QList<Zone*> zones;
char* name {0};
int index {0}; // used only for read
QList<Zone*> zones {};

Instrument();
~Instrument();
};

Expand All @@ -133,18 +132,17 @@ struct Instrument {
//---------------------------------------------------------

struct Sample {
char* name;
uint start;
uint end;
uint loopstart;
uint loopend;
uint samplerate;
char* name {0};
uint start {0};
uint end {0};
uint loopstart {0};
uint loopend {0};
uint samplerate {0};

int origpitch;
int pitchadj;
int sampletype;
int origpitch {0};
int pitchadj {0};
int sampletype {0};

Sample();
~Sample();
};

Expand Down