From 1e104cfea61ad78bf1f32a529d61b96b2b382b5c Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Fri, 26 Apr 2019 21:27:33 +0200 Subject: [PATCH 1/5] sfont: Skip 24-bit sample extensions --- sfont.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/sfont.cpp b/sfont.cpp index 5061101..e48198f 100644 --- a/sfont.cpp +++ b/sfont.cpp @@ -387,6 +387,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; From 8e4ee522f2c172f65854fb441e4c97d4a769c43e Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Fri, 26 Apr 2019 21:28:29 +0200 Subject: [PATCH 2/5] sfont: Read into malloc'd space instead of using strdup Avoids having an arbitrarily sized, big array on the stack. Also avoids an extra copy. --- sfont.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sfont.cpp b/sfont.cpp index e48198f..dad76da 100644 --- a/sfont.cpp +++ b/sfont.cpp @@ -338,12 +338,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; } //--------------------------------------------------------- From a09134b2d0cad4bce9f1a7c24422bbd06c9a7ad2 Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Fri, 26 Apr 2019 21:29:55 +0200 Subject: [PATCH 3/5] sfont: Replace arrays with vectors Avoids stack overflows. The compressed sample data is appended to a vector, avoiding constraining the size arbitrarily. --- sfont.cpp | 48 ++++++++++++++++++++---------------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/sfont.cpp b/sfont.cpp index dad76da..2d91589 100644 --- a/sfont.cpp +++ b/sfont.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include "sfont.h" #include "xml.h" @@ -899,7 +900,7 @@ void SoundFont::writeSmpl() } } else { - char* buffer = new char[sampleLen]; + std::vector buffer(sampleLen); QFile f(path); if (!f.open(QIODevice::ReadOnly)) throw(QString("cannot open <%1>").arg(f.fileName())); @@ -907,8 +908,8 @@ void SoundFont::writeSmpl() 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); @@ -916,7 +917,6 @@ void SoundFont::writeSmpl() s->loopend += s->start; } f.close(); - delete[] buffer; } qint64 npos = file->pos(); file->seek(pos); @@ -1140,8 +1140,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 buffer(len); + f.read((char*)buffer.data(), len * sizeof(short)); f.close(); // int format = SF_FORMAT_WAV | SF_FORMAT_PCM_16; @@ -1160,7 +1160,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"); @@ -1182,8 +1182,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 ibuffer(samples); + f.read((char*)ibuffer.data(), samples * sizeof(short)); f.close(); ogg_stream_state os; @@ -1215,17 +1215,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 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; @@ -1254,10 +1251,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); } } } @@ -1279,10 +1274,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); } } } @@ -1293,10 +1286,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(); } //--------------------------------------------------------- @@ -1321,8 +1313,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 ibuffer(samples); + fi.read((char*)ibuffer.data(), samples * sizeof(short)); fi.close(); fprintf(f, "static const short wave%d[] = {\n ", idx); From 7a330d83495bd1861ca12e598852f2cedd2bad91 Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Fri, 26 Apr 2019 21:24:52 +0200 Subject: [PATCH 4/5] sfont: Avoid warnings from -Wclass-memaccess --- sfont.cpp | 28 ++++++---------------------- sfont.h | 26 ++++++++++++-------------- 2 files changed, 18 insertions(+), 36 deletions(-) diff --git a/sfont.cpp b/sfont.cpp index 2d91589..bda8acf 100644 --- a/sfont.cpp +++ b/sfont.cpp @@ -60,11 +60,6 @@ static const bool writeCompressed = true; // Sample //--------------------------------------------------------- -Sample::Sample() - { - name = 0; - } - Sample::~Sample() { free(name); @@ -74,11 +69,6 @@ Sample::~Sample() // Instrument //--------------------------------------------------------- -Instrument::Instrument() - { - name = 0; - } - Instrument::~Instrument() { free(name); @@ -938,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); } @@ -949,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); @@ -1069,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); } @@ -1080,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); @@ -1098,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); } @@ -1109,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); diff --git a/sfont.h b/sfont.h index ed204ef..d445a7b 100644 --- a/sfont.h +++ b/sfont.h @@ -120,11 +120,10 @@ struct Preset { //--------------------------------------------------------- struct Instrument { - char* name; - int index; // used only for read - QList zones; + char* name {0}; + int index {0}; // used only for read + QList zones {}; - Instrument(); ~Instrument(); }; @@ -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(); }; From 563b2a2339ae33bafe2125c3c959f11130001f22 Mon Sep 17 00:00:00 2001 From: "Jan Alexander Steffens (heftig)" Date: Fri, 26 Apr 2019 21:27:19 +0200 Subject: [PATCH 5/5] sfont: Remove unused variables --- sfont.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/sfont.cpp b/sfont.cpp index bda8acf..ec3e4e3 100644 --- a/sfont.cpp +++ b/sfont.cpp @@ -1341,7 +1341,6 @@ static bool checkInstrument(QList pnums, QList presets, int instrI } static bool checkInstrument(QList presets, int instrIdx) { - bool result = false; for(int i = 0; i < presets.size(); i++) { Preset* p = presets[i]; Zone* z = p->zones[0]; @@ -1368,7 +1367,6 @@ static bool checkSample(QList pnums, QList presets, QListzones.size(); foreach(Zone* z, instrument->zones) { QList gl; foreach(GeneratorList* g, z->generators) { @@ -1396,7 +1394,6 @@ static bool checkSample(QList presets, QList instruments, ++idx; continue; } - int zones = instrument->zones.size(); foreach(Zone* z, instrument->zones) { QList gl; foreach(GeneratorList* g, z->generators) { @@ -1501,7 +1498,6 @@ bool SoundFont::writeCode() QList 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; @@ -1584,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; @@ -1709,7 +1704,6 @@ bool SoundFont::writeCode(QList pnums) QList 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; @@ -1781,7 +1775,6 @@ bool SoundFont::writeCode(QList 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;