Sample* ZInstrument::readSample(const QString& s, MQZipReader* uz) { if (uz) { QList<MQZipReader::FileInfo> fi = uz->fileInfoList(); buf = uz->fileData(s); if (buf.isEmpty()) { printf("Sample::read: cannot read sample data <%s>\n", qPrintable(s)); return 0; } } else { QFile f(s); if (!f.open(QIODevice::ReadOnly)) { printf("Sample::read: open <%s> failed\n", qPrintable(s)); return 0; } buf = f.readAll(); } AudioFile a; if (!a.open(buf)) { printf("open <%s> failed: %s\n", qPrintable(s), a.error()); return 0; } int channel = a.channels(); int frames = a.frames(); int sr = a.samplerate(); short* data = new short[(frames + 3) * channel]; Sample* sa = new Sample(channel, data, frames, sr); sa->setLoopStart(a.loopStart()); sa->setLoopEnd(a.loopEnd()); sa->setLoopMode(a.loopMode()); if (frames != a.read(data + channel, frames)) { qDebug("Sample read failed: %s\n", a.error()); delete sa; sa = 0; } for (int i = 0; i < channel; ++i) { data[i] = data[channel + i]; data[(frames-1) * channel + i] = data[(frames-3) * channel + i]; data[(frames-2) * channel + i] = data[(frames-3) * channel + i]; } return sa; }
bool Sample::decompressOggVorbis(char* src, int size) { AudioFile af; QByteArray ba(src, size); start = 0; end = 0; if (!af.open(ba)) { qDebug("Sample::decompressOggVorbis: open failed: %s", af.error()); return false; } int frames = af.frames(); data = new short[frames * af.channels()]; if (frames != af.readData(data, frames)) { qDebug("Sample read failed: %s", af.error()); delete[] data; data = 0; } end = frames - 1; if (loopend > end ||loopstart >= loopend || loopstart <= start) { /* can pad loop by 8 samples and ensure at least 4 for loop (2*8+4) */ if ((end - start) >= 20) { loopstart = start + 8; loopend = end - 8; } else { // loop is fowled, sample is tiny (can't pad 8 samples) loopstart = start + 1; loopend = end - 1; } } if ((end - start) < 8) { qDebug("invalid sample"); setValid(false); } return true; }