Skip to content

Commit

Permalink
Merge pull request #73 from jpcima/vgi
Browse files Browse the repository at this point in the history
support the VGI file format
  • Loading branch information
Wohlstand authored Mar 28, 2019
2 parents f4f9c73 + 8317e69 commit 46bcc2a
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ set(FILEFORMATS_SOURCES
"src/FileFormats/format_gens_y12.cpp"
"src/FileFormats/format_opm.cpp"
"src/FileFormats/format_mucom88_dat.cpp"
"src/FileFormats/format_vgi.cpp"
"src/FileFormats/format_wohlstand_opn2.cpp")
add_library(FileFormats STATIC ${FILEFORMATS_SOURCES})
target_include_directories(FileFormats PUBLIC "src")
Expand Down
2 changes: 2 additions & 0 deletions FMBankEdit.pro
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ SOURCES += \
src/FileFormats/format_gens_y12.cpp \
src/FileFormats/format_opm.cpp \
src/FileFormats/format_mucom88_dat.cpp \
src/FileFormats/format_vgi.cpp \
src/FileFormats/format_wohlstand_opn2.cpp \
src/formats_sup.cpp \
src/importer.cpp \
Expand Down Expand Up @@ -152,6 +153,7 @@ HEADERS += \
src/FileFormats/format_gens_y12.h \
src/FileFormats/format_opm.h \
src/FileFormats/format_mucom88_dat.h \
src/FileFormats/format_vgi.h \
src/FileFormats/format_wohlstand_opn2.h \
src/formats_sup.h \
src/importer.h \
Expand Down
1 change: 1 addition & 0 deletions src/FileFormats/ffmt_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum InstFormats
FORMAT_INST_DM_DMP = 2,
FORMAT_INST_GEMS_PAT = 3,
FORMAT_INST_Y12 = 4,
FORMAT_INST_VGM_MM = 5,
};

enum class FormatCaps
Expand Down
2 changes: 2 additions & 0 deletions src/FileFormats/ffmt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "format_saxman_ymx.h"
#include "format_tx81z_import.h"
#include "format_gens_y12.h"
#include "format_vgi.h"
#include "format_opm.h"
#include "format_mucom88_dat.h"

Expand Down Expand Up @@ -78,6 +79,7 @@ void FmBankFormatFactory::registerAllFormats()
registerInstFormat(new Gens_Y12());
registerBankFormat(new OPM());
registerBankFormat(new Mucom88_DAT());
registerInstFormat(new VGM_MM());
}


Expand Down
122 changes: 122 additions & 0 deletions src/FileFormats/format_vgi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* OPN2 Bank Editor by Wohlstand, a free tool for music bank editing
* Copyright (c) 2017-2019 Vitaly Novichkov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "format_vgi.h"
#include "../common.h"
#include <QFileInfo>

bool VGM_MM::detectInst(const QString &filePath, char* magic)
{
//By name extension
if(filePath.endsWith(".vgi", Qt::CaseInsensitive))
return true;

//By file size :-P
QFileInfo f(filePath);
if(f.size() == 43)
return true;

return false;
}

FfmtErrCode VGM_MM::loadFileInst(QString filePath, FmBank::Instrument &inst, bool *isDrum)
{
uint8_t idata[43];
QFile file(filePath);

if(!file.open(QIODevice::ReadOnly))
return FfmtErrCode::ERR_NOFILE;

if(file.read(char_p(idata), 43) != 43)
return FfmtErrCode::ERR_BADFORMAT;

inst.algorithm = idata[0] & 7;
inst.feedback = idata[1] & 7;
inst.setRegLfoSens(idata[2]);

for (unsigned op = 0; op < 4; ++op) {
FmBank::Operator &oper = inst.OP[op];
uint8_t *opdata = idata + 3 + op * 10;
oper.fmult = opdata[0] & 15;
oper.detune = opdata[1] & 7;
oper.level = opdata[2] & 127;
oper.ratescale = opdata[3] & 3;
oper.attack = opdata[4] & 31;
oper.decay1 = opdata[5] & 31;
oper.am_enable = opdata[5] >> 7;
oper.decay2 = opdata[6] & 31;
oper.release = opdata[7] & 15;
oper.sustain = opdata[8] & 15;
oper.ssg_eg = opdata[9] & 15;
}

return FfmtErrCode::ERR_OK;
}

FfmtErrCode VGM_MM::saveFileInst(QString filePath, FmBank::Instrument &inst, bool isDrum)
{
uint8_t idata[43];

idata[0] = inst.algorithm;
idata[1] = inst.feedback;
idata[2] = inst.getRegLfoSens();

for (unsigned op = 0; op < 4; ++op) {
uint8_t *opdata = idata + 3 + op * 10;
const FmBank::Operator &oper = inst.OP[op];
opdata[0] = oper.fmult;
opdata[1] = oper.detune;
opdata[2] = oper.level;
opdata[3] = oper.ratescale;
opdata[4] = oper.attack;
opdata[5] = oper.decay1 | (oper.am_enable << 7);
opdata[6] = oper.decay2;
opdata[7] = oper.release;
opdata[8] = oper.sustain;
opdata[9] = oper.ssg_eg;
}

QFile file(filePath);
if(!file.open(QIODevice::WriteOnly))
return FfmtErrCode::ERR_NOFILE;

if(file.write(char_p(idata), 43) != 43 || !file.flush())
return FfmtErrCode::ERR_BADFORMAT;

return FfmtErrCode::ERR_OK;
}

int VGM_MM::formatInstCaps() const
{
return (int)FormatCaps::FORMAT_CAPS_EVERYTHING;
}

QString VGM_MM::formatInstName() const
{
return "VGM Music Maker instrument";
}

QString VGM_MM::formatInstExtensionMask() const
{
return "*.vgi";
}

InstFormats VGM_MM::formatInstId() const
{
return FORMAT_INST_VGM_MM;
}
40 changes: 40 additions & 0 deletions src/FileFormats/format_vgi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* OPN2 Bank Editor by Wohlstand, a free tool for music bank editing
* Copyright (c) 2017-2019 Vitaly Novichkov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef FORMAT_VGI_H
#define FORMAT_VGI_H

#include "ffmt_base.h"

/**
* @brief Reader and Writer of the VGI File Format file
*/
class VGM_MM final : public FmBankFormatBase
{
public:
bool detectInst(const QString &filePath, char* magic) override;

FfmtErrCode loadFileInst(QString filePath, FmBank::Instrument &inst, bool *isDrum = 0) override;
FfmtErrCode saveFileInst(QString filePath, FmBank::Instrument &inst, bool isDrum = false) override;
int formatInstCaps() const override;
QString formatInstName() const override;
QString formatInstExtensionMask() const override;
InstFormats formatInstId() const override;
};

#endif // FORMAT_VGI_H

0 comments on commit 46bcc2a

Please sign in to comment.