2013-03-26 19:59:51 +01:00
|
|
|
//=============================================================================
|
|
|
|
// Zerberus
|
|
|
|
// Zample player
|
|
|
|
//
|
|
|
|
// Copyright (C) 2013 Werner Schweer
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2
|
|
|
|
// as published by the Free Software Foundation and appearing in
|
|
|
|
// the file LICENCE.GPL
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QStringList>
|
|
|
|
|
|
|
|
#include "libmscore/xml.h"
|
2013-06-03 12:54:11 +02:00
|
|
|
#include "audiofile/audiofile.h"
|
2014-05-07 09:28:19 +02:00
|
|
|
#include "thirdparty/qzip/qzipreader_p.h"
|
2013-03-26 19:59:51 +01:00
|
|
|
|
|
|
|
#include "instrument.h"
|
|
|
|
#include "zone.h"
|
|
|
|
#include "sample.h"
|
|
|
|
|
2013-03-27 10:07:14 +01:00
|
|
|
QByteArray ZInstrument::buf;
|
|
|
|
int ZInstrument::idx;
|
2013-03-26 19:59:51 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// Sample
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
Sample::~Sample()
|
|
|
|
{
|
|
|
|
delete _data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// readSample
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-07-26 15:39:46 +02:00
|
|
|
Sample* ZInstrument::readSample(const QString& s, MQZipReader* uz)
|
2013-03-26 19:59:51 +01:00
|
|
|
{
|
|
|
|
if (uz) {
|
2013-07-26 15:39:46 +02:00
|
|
|
QList<MQZipReader::FileInfo> fi = uz->fileInfoList();
|
2013-03-26 19:59:51 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2013-06-03 12:54:11 +02:00
|
|
|
|
|
|
|
AudioFile a;
|
|
|
|
if (!a.open(buf)) {
|
|
|
|
printf("open <%s> failed: %s\n", qPrintable(s), a.error());
|
2013-03-26 19:59:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2013-06-03 12:54:11 +02:00
|
|
|
|
|
|
|
int channel = a.channels();
|
|
|
|
int frames = a.frames();
|
|
|
|
int sr = a.samplerate();
|
|
|
|
|
|
|
|
short* data = new short[(frames + 3) * channel];
|
2013-03-26 19:59:51 +01:00
|
|
|
Sample* sa = new Sample(channel, data, frames, sr);
|
|
|
|
|
2013-06-03 12:54:11 +02:00
|
|
|
if (frames != a.read(data + channel, frames)) {
|
|
|
|
printf("Sample read failed: %s\n", a.error());
|
2013-03-26 19:59:51 +01:00
|
|
|
delete[] data;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
2013-03-27 10:07:14 +01:00
|
|
|
// ZInstrument
|
2013-03-26 19:59:51 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-07-18 09:23:45 +02:00
|
|
|
ZInstrument::ZInstrument(Zerberus* z)
|
2013-03-26 19:59:51 +01:00
|
|
|
{
|
2013-07-18 09:23:45 +02:00
|
|
|
zerberus = z;
|
2013-04-18 14:05:34 +02:00
|
|
|
_program = -1;
|
|
|
|
_refCount = 0;
|
2013-03-26 19:59:51 +01:00
|
|
|
}
|
|
|
|
|
2013-04-04 18:09:00 +02:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// ZInstrument
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
|
|
|
ZInstrument::~ZInstrument()
|
|
|
|
{
|
|
|
|
for (Zone* z : _zones)
|
|
|
|
delete z;
|
|
|
|
}
|
|
|
|
|
2013-03-26 19:59:51 +01:00
|
|
|
//---------------------------------------------------------
|
|
|
|
// load
|
|
|
|
// return true on success
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-03-27 10:07:14 +01:00
|
|
|
bool ZInstrument::load(const QString& path)
|
2013-03-26 19:59:51 +01:00
|
|
|
{
|
|
|
|
instrumentPath = path;
|
|
|
|
QFileInfo fi(path);
|
|
|
|
_name = fi.baseName();
|
|
|
|
if (fi.isFile())
|
|
|
|
return loadFromFile(path);
|
|
|
|
if (fi.isDir())
|
|
|
|
return loadFromDir(path);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// loadFromDir
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-03-27 10:07:14 +01:00
|
|
|
bool ZInstrument::loadFromDir(const QString& s)
|
2013-03-26 19:59:51 +01:00
|
|
|
{
|
|
|
|
QFile f(s + "/orchestra.xml");
|
|
|
|
if (!f.open(QIODevice::ReadOnly)) {
|
|
|
|
printf("cannot load orchestra.xml in <%s>\n", qPrintable(s));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray buf = f.readAll();
|
|
|
|
if (buf.isEmpty()) {
|
|
|
|
printf("Instrument::loadFromFile: orchestra.xml is empty\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return read(buf, 0, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// loadFromFile
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-03-27 10:07:14 +01:00
|
|
|
bool ZInstrument::loadFromFile(const QString& path)
|
2013-03-26 19:59:51 +01:00
|
|
|
{
|
|
|
|
if (path.endsWith(".sfz"))
|
|
|
|
return loadSfz(path);
|
|
|
|
if (!path.endsWith(".msoz")) {
|
|
|
|
printf("<%s> not a orchestra file\n", qPrintable(path));
|
|
|
|
return false;
|
|
|
|
}
|
2013-07-26 15:39:46 +02:00
|
|
|
MQZipReader uz(path);
|
2013-03-26 19:59:51 +01:00
|
|
|
if (!uz.exists()) {
|
|
|
|
printf("Instrument::load: %s not found\n", qPrintable(path));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QByteArray buf = uz.fileData("orchestra.xml");
|
|
|
|
if (buf.isEmpty()) {
|
|
|
|
printf("Instrument::loadFromFile: orchestra.xml not found\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return read(buf, &uz, QString());
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------
|
|
|
|
// read
|
|
|
|
// read orchestra
|
|
|
|
//---------------------------------------------------------
|
|
|
|
|
2013-07-26 15:39:46 +02:00
|
|
|
bool ZInstrument::read(const QByteArray& buf, MQZipReader* /*uz*/, const QString& /*path*/)
|
2013-03-26 19:59:51 +01:00
|
|
|
{
|
2013-05-13 18:49:17 +02:00
|
|
|
Ms::XmlReader e(buf);
|
2013-03-26 19:59:51 +01:00
|
|
|
while (e.readNextStartElement()) {
|
|
|
|
if (e.name() == "MuseSynth") {
|
|
|
|
while (e.readNextStartElement()) {
|
|
|
|
if (e.name() == "Instrument") {
|
|
|
|
// if (!read(e, uz, path))
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
e.unknown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
e.unknown();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|